Added new script to handle loading files into the db and updating them. Python handled it better than bash.
This commit is contained in:
parent
c528a68703
commit
84133c9315
51
Scripts/update-database.py
Executable file
51
Scripts/update-database.py
Executable file
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
dirs = (
|
||||||
|
'/etc/cron.d/',
|
||||||
|
'/etc/cron.hourly',
|
||||||
|
'/etc/cron.daily',
|
||||||
|
'/etc/cron.monthly',
|
||||||
|
'/var/spool/cron/crontabs'
|
||||||
|
)
|
||||||
|
|
||||||
|
fs_files = dict()
|
||||||
|
db_files = dict()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Get crontabs from each crontab directory (if it exists)
|
||||||
|
for dir in [ Path(dir) for dir in dirs ]:
|
||||||
|
if dir.exists():
|
||||||
|
for fs_file in dir.iterdir():
|
||||||
|
if fs_file.is_file():
|
||||||
|
with fs_file.open('r') as file:
|
||||||
|
fs_files.update({str(fs_file.absolute()):file.read()})
|
||||||
|
|
||||||
|
|
||||||
|
conn = sqlite3.connect('../webcron.db')
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
# Get the path and data of each of the crontabs in the db
|
||||||
|
cur.execute("SELECT crontab_path, crontab_data FROM crontabs")
|
||||||
|
for path, data in cur.fetchall():
|
||||||
|
db_files.update({path:data})
|
||||||
|
|
||||||
|
|
||||||
|
# Put files into db if they do not exist there already
|
||||||
|
for file_path, file_data in fs_files.items():
|
||||||
|
if file_path not in db_files:
|
||||||
|
cur.execute("INSERT INTO crontabs (crontab_path, crontab_data) VALUES (?,?)",(file_path, file_data,))
|
||||||
|
else:
|
||||||
|
if file_data != db_files[file_path]:
|
||||||
|
cur.execute("UPDATE crontabs SET crontab_data = ? WHERE crontab_path = ?",(file_data, file_path,))
|
||||||
|
|
||||||
|
|
||||||
|
# Commit changes from memory into db-file
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -21,6 +21,13 @@ fi
|
|||||||
# (-y) just means assume yes (for package download and updates)
|
# (-y) just means assume yes (for package download and updates)
|
||||||
apt update -y && apt upgrade -y
|
apt update -y && apt upgrade -y
|
||||||
|
|
||||||
|
declare -a python_packages=(
|
||||||
|
# To enumerate the files from the FS
|
||||||
|
pathlib
|
||||||
|
|
||||||
|
#sqlite3 # this is already preinstalled
|
||||||
|
)
|
||||||
|
|
||||||
# These are the packages needed for setting up the server for my PHP application
|
# These are the packages needed for setting up the server for my PHP application
|
||||||
declare -a needed_packages=(
|
declare -a needed_packages=(
|
||||||
# Webserver packages
|
# Webserver packages
|
||||||
@ -35,6 +42,8 @@ declare -a needed_packages=(
|
|||||||
acl
|
acl
|
||||||
|
|
||||||
# Necessary Project Packages
|
# Necessary Project Packages
|
||||||
|
python3
|
||||||
|
python3-pip
|
||||||
sqlite3
|
sqlite3
|
||||||
|
|
||||||
# Check packages
|
# Check packages
|
||||||
@ -52,12 +61,16 @@ for package in ${needed_packages[@]}; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
unset needed_packages
|
unset needed_packages
|
||||||
|
|
||||||
# Installing the packages that aren't already installed
|
# Installing the packages that aren't already installed
|
||||||
apt install -y ${packages_to_install}
|
apt install -y ${packages_to_install[@]}
|
||||||
|
|
||||||
# Start: Filesystem Setup
|
# Start: Filesystem Setup
|
||||||
|
|
||||||
|
# Install the necessary python packages
|
||||||
|
python3 -m pip install --user ${python_packages[@]}
|
||||||
|
unset python_packages
|
||||||
|
|
||||||
|
|
||||||
# This will give the webcron user the ability to read, write and execute crontab in all of the regular places
|
# This will give the webcron user the ability to read, write and execute crontab in all of the regular places
|
||||||
delcare -a crontab_dirs=(
|
delcare -a crontab_dirs=(
|
||||||
# All of the normal system crontabs are located here
|
# All of the normal system crontabs are located here
|
||||||
@ -67,12 +80,14 @@ delcare -a crontab_dirs=(
|
|||||||
/etc/cron.weekly
|
/etc/cron.weekly
|
||||||
/etc/cron.monthly
|
/etc/cron.monthly
|
||||||
|
|
||||||
# These are the crontabs created by users of the system
|
# Depending on the system, user crontabs are stored in this directory and the crontabs sub directory doesn't exist
|
||||||
/var/spool/cron/crontabs
|
/var/spool/cron
|
||||||
|
# These are the crontabs created by users of the system (commented out becuse the above acl would include this directory and files
|
||||||
|
#/var/spool/cron/crontabs
|
||||||
)
|
)
|
||||||
|
|
||||||
for dir in ${crontab_dirs[@]}; do
|
for dir in ${crontab_dirs[@]}; do
|
||||||
# This (while not changing the permissions via chmod)
|
# This (while not changing the permissions via chmod) what will allow the webcron user to access all of the crontab directories & files
|
||||||
setfacl -m u:webcron:rwx -R $dir
|
setfacl -m u:webcron:rwx -R $dir
|
||||||
done
|
done
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user