dev/finalFixes #81

Merged
tom_trgr merged 14 commits from dev/finalFixes into main 2026-03-01 10:11:45 +01:00
3 changed files with 313 additions and 177 deletions
Showing only changes of commit b12a467ef9 - Show all commits

3
.gitignore vendored
View File

@@ -30,7 +30,8 @@ DB/pg_data
.env.* .env.*
.env .env
!.env.example
Docker/config
.idea .idea
.vscode .vscode

View File

@@ -1,3 +1,4 @@
# cron-timing: 01 00 01 01 *
# Calls endpoint to write all public Holidays for the current year inside a database. # Calls endpoint to write all public Holidays for the current year inside a database.
port=__PORT__ port=__PORT__
curl localhost:$port/auto/feiertage curl localhost:$port/auto/feiertage

View File

@@ -1,15 +1,21 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#©Tom Tröger 2026
set -e set -e
envFile=Docker/.env envFile=Docker/.env
envBkp=Docker/.env.old
envExample=Docker/env.example envExample=Docker/env.example
autoBackupScript=Cron/autoBackup.sh cronFilePath=Cron
autoHolidaysScript=Cron/autoHolidays.sh customCronFilePath=Docker/config/cron
autoLogoutScript=Cron/autoLogout.sh
echo "Checking Docker installation..." autoBackupScript=autoBackup.sh
if ! command -v docker >/dev/null 2>&1; then autoHolidaysScript=autoHolidays.sh
autoLogoutScript=autoLogout.sh
function checkDocker() {
echo "Checking Docker installation..."
if ! command -v docker >/dev/null 2>&1; then
echo "Docker not found. Install Docker? [y/N]" echo "Docker not found. Install Docker? [y/N]"
read -r install_docker read -r install_docker
if [[ "$install_docker" =~ ^[Yy]$ ]]; then if [[ "$install_docker" =~ ^[Yy]$ ]]; then
@@ -18,42 +24,68 @@ if ! command -v docker >/dev/null 2>&1; then
echo "Docker is required. Exiting." echo "Docker is required. Exiting."
exit 1 exit 1
fi fi
else else
echo "Docker is already installed." echo "Docker is already installed."
fi fi
########################################################################### ###########################################################################
echo "Checking Docker Compose..." echo "Checking Docker Compose..."
if ! docker compose version >/dev/null 2>&1; then if ! docker compose version >/dev/null 2>&1; then
echo "Docker Compose plugin missing. You may need to update Docker." echo "Docker Compose plugin missing. You may need to update Docker."
exit 1 exit 1
fi fi
}
########################################################################### ###########################################################################
echo "Preparing .env file..." function setupConfig() {
if [ ! -f $envFile ]; then local reconfig=false
if [ $# -gt 0 ]; then
if ask_reconfig $1 "Reconfigure .env File?"
then
reconfig=true
else
return 0
fi
fi
echo -e "\r\n==================================================\r\n"
echo "Preparing .env file..."
if [ ! -f $envFile ] || [ $reconfig == true ]; then
if [ -f $envExample ]; then if [ -f $envExample ]; then
if [ $reconfig == true ]; then
echo "Reconfiguring env file. Backup stored at $envBkp"
echo "All previous values will be used as defaults!"
cp $envFile $envBkp
else
echo ".env not found. Creating interactively from .env.example." echo ".env not found. Creating interactively from .env.example."
fi
> $envFile > $envFile
while IFS= read -r line; do while IFS= read -r line; do
#ignore empty lines and comments #ignore empty lines and comments
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
key=$(printf "%s" "$line" | cut -d '=' -f 1) local key=$(printf "%s" "$line" | cut -d '=' -f 1)
rest=$(printf "%s" "$line" | cut -d '=' -f 2-) local rest=$(printf "%s" "$line" | cut -d '=' -f 2-)
# extract inline comment portion # extract inline comment portion
comment=$(printf "%s" "$rest" | sed -n 's/.*# \(.*\)$/\1/p') local comment=$(printf "%s" "$rest" | sed -n 's/.*# \(.*\)$/\1/p')
raw_val=$(printf "%s" "$rest" | sed 's/ *#.*//') local raw_val=$(printf "%s" "$rest" | sed 's/ *#.*//')
default_value=$(printf "%s" "$raw_val" | sed 's/"//g')
local default_value=$(printf "%s" "$raw_val" | sed 's/"//g')
if [ $reconfig == true ]; then
local previous_value=$(grep -E "^$key=" $envBkp | cut -d= -f2)
if [ -n "$previous_value" ]; then
default_value=$previous_value
fi
fi
# Replace __ROOT__ with script pwd # Replace __ROOT__ with script pwd
default_value="${default_value/__ROOT__/$(pwd)}" local default_value="${default_value/__ROOT__/$(pwd)}"
regex="" regex=""
if [[ "$comment" =~ regex:(.*)$ ]]; then if [[ "$comment" =~ regex:(.*)$ ]]; then
@@ -103,26 +135,74 @@ if [ ! -f $envFile ]; then
echo "Creating an empty .env file for manual editing." echo "Creating an empty .env file for manual editing."
touch $envFile touch $envFile
fi fi
else else
echo "Using existing .env. (found at $envFile)" echo "Using existing .env. (found at $envFile)"
fi fi
}
########################################################################### ###########################################################################
LOG_PATH=$(grep -E '^LOG_PATH=' $envFile | cut -d= -f2) function setupFolders(){
if [ -z "$LOG_PATH" ]; then if [ $# -gt 0 ]; then
if ! ask_reconfig $1 "Recreate Folders?"
then
return 0
fi
fi
LOG_PATH=$(grep -E '^LOG_PATH=' $envFile | cut -d= -f2)
if [ -z "$LOG_PATH" ]; then
echo "LOG_PATH not found in .env using default $(pwd)/logs" echo "LOG_PATH not found in .env using default $(pwd)/logs"
LOG_PATH=$(pwd)/logs LOG_PATH=$(pwd)/logs
fi fi
mkdir -p $LOG_PATH if [ ! -d "$LOG_PATH" ]; then
echo "Created logs folder at $LOG_PATH" mkdir -p $LOG_PATH
echo "Created logs folder at $LOG_PATH"
fi
POSTGRES_PATH=$(grep -E '^POSTGRES_PATH=' $envFile | cut -d= -f2)
if [ -z "$POSTGRES_PATH" ]; then
echo "POSTGRES_PATH not found in .env using default $(pwd)/DB"
POSTGRES_PATH=$(pwd)/DB
fi
if [ ! -d "$POSTGRES_PATH" ]; then
mkdir -p $POSTGRES_PATH
echo "Created DB folder at $POSTGRES_PATH"
fi
BACKUP_FOLDER=$(grep -E '^BACKUP_FOLDER=' $envFile | cut -d= -f2)
if [ -z "$BACKUP_FOLDER" ]; then
echo "BACKUP_FOLDER not found in .env using default $(pwd)/backup"
BACKUP_FOLDER=$(pwd)/backup
fi
if [ ! -d "$BACKUP_FOLDER" ]; then
mkdir -p $BACKUP_FOLDER
echo "Created backup folder at $BACKUP_FOLDER"
fi
}
########################################################################### ###########################################################################
echo -e "\n\n" function setupCron(){
echo "Setup Crontab for automatic logout, backup and holiday creation? [y/N]" echo -e "\r\n==================================================\r\n"
read -r setup_cron echo "Setup Crontab for automatic logout, backup and holiday creation? [y/N]"
if [[ "$setup_cron" =~ ^[Yy]$ ]]; then read -r setup_cron
if [[ "$setup_cron" =~ ^[Yy]$ ]]; then
echo "Copying custom cron files to $customCronFilePath"
mkdir -p "$customCronFilePath"
if [ ! -s "$customCronFilePath/$autoBackupScript" ];then
cp "$cronFilePath/$autoBackupScript" "$customCronFilePath/$autoBackupScript"
echo "Copied $autoBackupScript"
fi
if [ ! -s "$customCronFilePath/$autoLogoutScript" ];then
cp "$cronFilePath/$autoLogoutScript" "$customCronFilePath/$autoLogoutScript"
echo "Copied $autoLogoutScript"
fi
if [ ! -s "$customCronFilePath/$autoHolidaysScript" ];then
cp "$cronFilePath/$autoHolidaysScript" "$customCronFilePath/$autoHolidaysScript"
echo "Copied $autoHolidaysScript"
fi
WEB_PORT=$(grep -E '^WEB_PORT=' $envFile | cut -d= -f2) WEB_PORT=$(grep -E '^WEB_PORT=' $envFile | cut -d= -f2)
if [ -z "$WEB_PORT" ]; then if [ -z "$WEB_PORT" ]; then
echo "WEB_PORT not found in .env using default 8000" echo "WEB_PORT not found in .env using default 8000"
@@ -141,19 +221,20 @@ if [[ "$setup_cron" =~ ^[Yy]$ ]]; then
BACKUP_FOLDER="$(pwd)/backup" BACKUP_FOLDER="$(pwd)/backup"
fi fi
sed -i "s/__PORT__/$WEB_PORT/" $autoHolidaysScript sed -i "s|__PORT__|$WEB_PORT|" $customCronFilePath/$autoHolidaysScript && \
sed -i "s/__PORT__/$WEB_PORT/" $autoLogoutScript sed -i "s|__PORT__|$WEB_PORT|" $customCronFilePath/$autoLogoutScript && \
sed -i "s/__DATABASE__/$POSTGRES_DB/" $autoBackupScript sed -i "s|__DATABASE__|$POSTGRES_DB|" $customCronFilePath/$autoBackupScript && \
sed -i "s/__BACKUP_FOLDER__/$BACKUP_FOLDER" $autoBackupScript sed -i "s|__BACKUP_FOLDER__|$BACKUP_FOLDER|" $customCronFilePath/$autoBackupScript
chmod +x $autoBackupScript $autoHolidaysScript $autoLogoutScript chmod +x "$customCronFilePath/$autoBackupScript" "$customCronFilePath/$autoHolidaysScript" "$customCronFilePath/$autoLogoutScript"
# echo "Scripts build with PORT=$WEB_PORT and DATABSE=$POSTGRES_DB!" # echo "Scripts build with PORT=$WEB_PORT and DATABSE=$POSTGRES_DB!"
echo "Adding rules to crontab." echo "Adding rules to crontab."
cron_commands=$(mktemp /tmp/arbeitszeitmessung-cron.XXX) cron_commands=$(mktemp /tmp/arbeitszeitmessung-cron.XXX)
pwd
for file in Cron/*; do for file in $customCronFilePath/*; do
cron_timing=$(grep -E '^# cron-timing:' "$file" | sed 's/^# cron-timing:[[:space:]]*//') cron_timing=$(grep -E '^# cron-timing:' "$file" | sed 's/^# cron-timing:[[:space:]]*//')
if [ -z "$cron_timing" ]; then if [ -z "$cron_timing" ]; then
@@ -163,7 +244,6 @@ if [[ "$setup_cron" =~ ^[Yy]$ ]]; then
( crontab -l ; echo "$cron_timing $(pwd)/$file" )| awk '!x[$0]++' | crontab - ( crontab -l ; echo "$cron_timing $(pwd)/$file" )| awk '!x[$0]++' | crontab -
echo "Added entry to crontab: $cron_timing $(pwd)/$file." echo "Added entry to crontab: $cron_timing $(pwd)/$file."
sleep 2
done done
if systemctl is-active --quiet cron.service ; then if systemctl is-active --quiet cron.service ; then
@@ -173,21 +253,75 @@ if [[ "$setup_cron" =~ ^[Yy]$ ]]; then
echo "For how to start a service, see: https://wiki.ubuntuusers.de/systemd/systemctl UNITNAME will be cron.service" echo "For how to start a service, see: https://wiki.ubuntuusers.de/systemd/systemctl UNITNAME will be cron.service"
fi fi
else else
echo "Please setup cron manually by executing crontab -e and adding all files from inside the Cron directory!" echo "Please setup cron manually by executing crontab -e and adding all files from inside the Cron directory!"
fi fi
}
########################################################################### ###########################################################################
echo -e "\n\n" function startContainer(){
echo "Start containers with docker compose up -d? [y/N]" echo -e "\r\n==================================================\r\n"
read -r start_containers echo "Start containers with docker compose up -d? [y/N]"
if [[ "$start_containers" =~ ^[Yy]$ ]]; then read -r start_containers
if [[ "$start_containers" =~ ^[Yy]$ ]]; then
cd Docker cd Docker
docker compose up -d docker compose up -d
echo "Containers started." echo "Containers started."
else else
echo "You can start them manually with: docker compose up -d" echo "You can start them manually with: docker compose up -d"
fi fi
}
###########################################################################
echo "Installation finished, you can re-run the script any time!" function help(){
echo "Installer Script für Arbeitszeitmessung Software"
echo -e "\r\n==================================================\r\n"
echo "Nutzung: ./install.sh [options]"
echo -e "\r\n==================================================\r\n"
echo "Optionen:"
echo " -h zeigt diese Übersicht"
echo " -c .env Datei bearbeiten/aktualisieren && cron neu configurieren"
echo -e "\r\n=================================================="
}
###########################################################################
function main(){
echo -e "================Arbeitszeitmessung================\r\n"
if [ $# -gt 0 ];then
if [ $1 == reconfig ]; then
echo -e "================Reconfiguring================\r\n"
setupConfig $1
setupFolders $1
setupCron $1
fi
else
checkDocker
setupConfig
setupFolders
setupCron
startContainer
fi
echo "Installation finished, you can re-run the script any time!"
}
###########################################################################
function ask_reconfig(){
echo -e "\r\n==================================================\r\n"
echo "$2 [y/N]"
read -r do_reconfig
[[ "$do_reconfig" =~ ^[Yy]$ ]] && return # true
echo "Skipping..."
return 1
}
###########################################################################
while getopts ":hc" opt; do
case $opt in
h) help; exit 0 ;;
c) main reconfig; exit 0 ;;
*) echo "Ungültiges Argument"; exit 1 ;;
esac
done
main