Added new script to handle loading files into the db and updating them. Python handled it better than bash.

This commit is contained in:
Tristan Ancelet 2023-05-21 13:38:12 -05:00
parent c528a68703
commit 84133c9315
2 changed files with 71 additions and 5 deletions

51
Scripts/update-database.py Executable file
View 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()

View File

@ -21,6 +21,13 @@ fi
# (-y) just means assume yes (for package download and updates)
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
declare -a needed_packages=(
# Webserver packages
@ -35,6 +42,8 @@ declare -a needed_packages=(
acl
# Necessary Project Packages
python3
python3-pip
sqlite3
# Check packages
@ -52,12 +61,16 @@ for package in ${needed_packages[@]}; do
done
unset needed_packages
# Installing the packages that aren't already installed
apt install -y ${packages_to_install}
apt install -y ${packages_to_install[@]}
# 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
delcare -a crontab_dirs=(
# All of the normal system crontabs are located here
@ -67,12 +80,14 @@ delcare -a crontab_dirs=(
/etc/cron.weekly
/etc/cron.monthly
# These are the crontabs created by users of the system
/var/spool/cron/crontabs
# Depending on the system, user crontabs are stored in this directory and the crontabs sub directory doesn't exist
/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
# 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
done