From efc3065b55e18b0bd3976e5494c555e5348a8cb4 Mon Sep 17 00:00:00 2001 From: guezoloic Date: Mon, 8 Sep 2025 18:11:51 +0000 Subject: [PATCH] feat: add info_print flag --- install.sh | 22 +++++++++++----------- libs/common.sh | 22 +++++++++++++++------- libs/notifications.sh | 2 +- scripts/aws-backup.sh | 8 ++++---- scripts/disk-monitor.sh | 7 ++++++- scripts/docker-compose.sh | 2 +- scripts/sshd-login.sh | 6 +++--- 7 files changed, 41 insertions(+), 28 deletions(-) diff --git a/install.sh b/install.sh index 99aaf0e..90efb89 100644 --- a/install.sh +++ b/install.sh @@ -16,7 +16,7 @@ fi touch "$LOG" || ISERROR=true if $ISERROR; then - info_print "Failed to Create $LOG" 3 false; exit 1 + info_print "Failed to Create $LOG" $ERROR_FLAG false; exit 1 fi chmod 644 "$LOG" @@ -31,7 +31,7 @@ info_print "Date : Installation $(date '+%Y-%m-%d %H:%M:%S')" if $ISERROR; then - info_print "Failed to move some scripts to $SCRIPT_FILE, See log $LOG" 3 false; exit 1 + info_print "Failed to move some scripts to $SCRIPT_FILE, See log $LOG" $ERROR_FLAG false; exit 1 fi info_print "\n\ @@ -45,12 +45,12 @@ for config in config/*; do if [ -d "$config" ]; then cp -r "$config" "$ETC_DIR/" \ - && { info_print "$ETC_DIR/$filename installed (directory)." 6; } \ - || { info_print "$ETC_DIR/$filename failed (directory)." 3; ISERROR=true; } + && { info_print "$ETC_DIR/$filename installed (directory)." $SUCCESS_FLAG; } \ + || { info_print "$ETC_DIR/$filename failed (directory)." $ERROR_FLAG; ISERROR=true; } else install -Dm755 "$config" "$ETC_DIR/$filename" \ - && { info_print "$ETC_DIR/$filename installed." 6; } \ - || { info_print "$ETC_DIR/$filename failed." 3; ISERROR=true; } + && { info_print "$ETC_DIR/$filename installed." $SUCCESS_FLAG; } \ + || { info_print "$ETC_DIR/$filename failed." $ERROR_FLAG; ISERROR=true; } fi done @@ -65,15 +65,15 @@ info_print "\n\ for dep in ${INSTALLED_DEP[@]}; do if command -v "$dep" &>/dev/null; then - info_print "$dep is installed." 6 + info_print "$dep is installed." $SUCCESS_FLAG else - info_print "$dep is not installed." 3 + info_print "$dep is not installed." $ERROR_FLAG ISERROR=true fi done if $ISERROR; then - info_print "Some Dependencies are missing. Please check requirements.txt." 3 false; exit 1 + info_print "Some Dependencies are missing. Please check requirements.txt." $ERROR_FLAG false; exit 1 fi info_print "\n\ @@ -86,8 +86,8 @@ for scripts in libs/*.sh scripts/*.sh; do output="$SCRIPT_FILE/$scripts" install $argument "$scripts" $output -Dm755 \ - && { info_print "$output installed." 6; } \ - || { info_print "$output failed." 3; ISERROR=true; } + && { info_print "$output installed." $SUCCESS_FLAG; } \ + || { info_print "$output failed." $ERROR_FLAG; ISERROR=true; } done touch $ENV_FILE diff --git a/libs/common.sh b/libs/common.sh index ce759c0..b34a59c 100644 --- a/libs/common.sh +++ b/libs/common.sh @@ -14,6 +14,14 @@ ERROR="\e[31mERROR\e[0m" DEBUG="\e[35mDEBUG\e[0m" ACTION="\e[36mACTION\e[0m" +INFO_FLAG=1 +WARN_FLAG=2 +ERROR_FLAG=3 +DEBUG_FLAG=4 +ACTION_FLAG=5 +SUCCESS_FLAG=6 + + DATETIME_FORMAT="%d-%m-%Y %H:%M:%S" info_print() { @@ -45,10 +53,10 @@ create_env_variable() { if [[ -z "$value" ]]; then if grep -q "^$key=*" "$file" 2>/dev/null; then - info_print "$key not updated." 2 + info_print "$key not updated." $WARN_FLAG return else - info_print "$key not set (empty input)." 2 + info_print "$key not set (empty input)." $WARN_FLAG return fi fi @@ -59,15 +67,15 @@ create_env_variable() { case "$yn" in [Yy]*) sed -i "s/^$key=.*/$key=$value/" "$file" - info_print "$key updated." 6 + info_print "$key updated." $SUCCESS_FLAG ;; *) - info_print "$key not changed." 2 + info_print "$key not changed." $WARN_FLAG ;; esac else echo "$key=$value" >> "$file" - info_print "$key set." 6 + info_print "$key set." $SUCCESS_FLAG fi } @@ -76,9 +84,9 @@ create_raw_line_variable() { local file="$2" if grep -Fxq "$line" "$file" 2>/dev/null; then - info_print "'$line' already defined as raw line." 2 + info_print "'$line' already defined as raw line." $WARN_FLAG else echo "$line" >> "$file" - info_print "'$line' added as raw line." 6 + info_print "'$line' added as raw line." $SUCCESS_FLAG fi } \ No newline at end of file diff --git a/libs/notifications.sh b/libs/notifications.sh index f56b51a..3eaf275 100644 --- a/libs/notifications.sh +++ b/libs/notifications.sh @@ -9,7 +9,7 @@ if [[ "--install" == $INSTALLED ]]; then notifications Installation\n\ --------------------------------------------------" - ENV_LIST=("TELEGRAM_CHAT_ID" "TELEGRAM_TOKEN") + ENV_LIST=("TELEGRAM_TOKEN" "TELEGRAM_CHAT_ID") for env in "${ENV_LIST[@]}"; do read -p "Enter value for $env: " value diff --git a/scripts/aws-backup.sh b/scripts/aws-backup.sh index 080b4e5..86ce467 100644 --- a/scripts/aws-backup.sh +++ b/scripts/aws-backup.sh @@ -39,7 +39,7 @@ if [[ "--install" == $INSTALLED ]]; then crontab -l | grep -F "$CRON_JOB" > /dev/null 2>&1 if ! crontab -l | grep -Fq "$CRON_JOB"; then (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab - - info_print "Cron job added." 6 + info_print "Cron job added." $SUCCESS_FLAG fi exit 0 @@ -76,16 +76,16 @@ while IFS= read -r SOURCE_PATH || [ -n "$SOURCE_PATH" ]; do "${aws_cmd[@]}" if [ $? -ne 0 ]; then - info_print "Error while syncing $SOURCE_PATH to the AWS server." 3 + info_print "Error while syncing $SOURCE_PATH to the AWS server." $ERROR_FLAG BACKUPSUCCESSED=false exit 1 else - info_print "Successfully synced $SOURCE_PATH" 6 + info_print "Successfully synced $SOURCE_PATH" $SUCCESS_FLAG BACKUPSUCCESSED=true fi else - info_print "$SOURCE_PATH not found or inaccessible." 3 + info_print "$SOURCE_PATH not found or inaccessible." $ERROR_FLAG BACKUPSUCCESSED=false exit 1 fi diff --git a/scripts/disk-monitor.sh b/scripts/disk-monitor.sh index b0b6337..727f177 100644 --- a/scripts/disk-monitor.sh +++ b/scripts/disk-monitor.sh @@ -9,11 +9,16 @@ if [[ "--install" == $INSTALLED ]]; then ==================================================\n\ disk-monitor Installation\n\ --------------------------------------------------" + if ! command -v crontab >/dev/null 2>&1; then + info_print "Cron job added." $ERROR_FLAG + exit 1 + fi + CRON_JOB="0 3 * * 1 $SCRIPT_FILE/scripts/disk-monitor.sh" crontab -l | grep -F "$CRON_JOB" > /dev/null 2>&1 if ! crontab -l | grep -Fq "$CRON_JOB"; then (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab - - info_print "Cron job added." 6 + info_print "Cron job added." $SUCCESS_FLAG fi exit 0; fi diff --git a/scripts/docker-compose.sh b/scripts/docker-compose.sh index 9f71d90..14e09c5 100644 --- a/scripts/docker-compose.sh +++ b/scripts/docker-compose.sh @@ -19,7 +19,7 @@ if [[ "--install" == $INSTALLED ]]; then source /etc/serverconfig/.env if [[ -f "$ETC_DIR/docker-compose.yml" ]]; then - docker compose -f "$ETC_DIR/docker-compose.yml" up -d --force-recreate && \ + docker compose -f "$ETC_DIR/docker-compose.yml" up -d --force-recreate --remove-orphans && \ info_print "$ETC_DIR/docker-compose.yml is running." 6; else info_print "no docker-compose.yml found at $ETC_DIR" 3; diff --git a/scripts/sshd-login.sh b/scripts/sshd-login.sh index a2aa9e1..0395466 100644 --- a/scripts/sshd-login.sh +++ b/scripts/sshd-login.sh @@ -14,15 +14,15 @@ if [[ "--install" == $INSTALLED ]]; then file='/etc/pam.d/common-session' if [[ ! -f "$file" ]]; then - info_print "$file doesn't found." 3 + info_print "$file doesn't found." $ERROR_FLAG exit 1 fi if ! grep -Fxq "$login" "$file"; then echo "$login" >> "$file" - info_print "login command added to $file." 6 + info_print "login command added to $file." $SUCCESS_FLAG else - info_print "login command already added to $file." 2 + info_print "login command already added to $file." $WARN_FLAG fi exit 0; fi