Skip to content

Commit c36d885

Browse files
Refactor setup.sh and cronjob.sh scripts
1 parent cbb9ab3 commit c36d885

4 files changed

Lines changed: 51 additions & 96 deletions

File tree

setup.sh

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ get_user_home() {
2323
USER_HOME=$(get_user_home)
2424
USER_NAME=$(echo $USER_HOME | awk -F'/' '{print $3}')
2525
DOWNLOAD_DIR="/tmp"
26-
EXTRACT_DIR="$USER_HOME/.systemguard/"
26+
EXTRACT_DIR="$USER_HOME/.systemguard"
2727
LOG_DIR="$HOME/logs"
2828
LOG_FILE="$LOG_DIR/systemguard-installer.log"
2929
BACKUP_DIR="$USER_HOME/.systemguard_backup"
3030
EXECUTABLE="/usr/local/bin/systemguard-installer"
3131
LOCUST_FILE="$EXTRACT_DIR/SystemGuard-*/src/scripts/locustfile.py"
3232
HOST_URL="http://localhost:5050"
3333
INSTALLER_SCRIPT='setup.sh'
34+
ISSUE_URL="https://github.com/codeperfectplus/SystemGuard/issues"
3435

3536
# Create necessary directories
3637
mkdir -p "$LOG_DIR"
@@ -42,6 +43,48 @@ log() {
4243
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE"
4344
}
4445

46+
# Function to add a cron job with error handling
47+
add_cron_job() {
48+
# Define log directory and cron job command
49+
local USER_NAME=$(whoami)
50+
local log_dir="/home/$USER_NAME/logs"
51+
local cron_job="* * * * * /bin/bash $(find $EXTRACT_DIR -name dashboard.sh) >> $log_dir/systemguard_cron.log 2>&1"
52+
53+
# Create log directory with error handling
54+
mkdir -p "$log_dir"
55+
if [ $? -ne 0 ]; then
56+
echo "Error: Failed to create log directory: $log_dir"
57+
exit 1
58+
fi
59+
60+
# Verify user retrieval
61+
if [ -z "$USER_NAME" ]; then
62+
echo "Error: Unable to retrieve current username."
63+
exit 1
64+
fi
65+
66+
# Add cron job to crontab with error handling
67+
# Temporarily store current crontab to avoid overwriting on error
68+
temp_cron=$(mktemp)
69+
crontab -l 2>/dev/null > "$temp_cron"
70+
if [ $? -ne 0 ] && [ ! -s "$temp_cron" ]; then
71+
echo "Error: Unable to list current crontab or it's empty."
72+
rm "$temp_cron"
73+
exit 1
74+
fi
75+
76+
echo "$cron_job" >> "$temp_cron"
77+
crontab "$temp_cron"
78+
if [ $? -ne 0 ]; then
79+
echo "Error: Failed to add the cron job to the crontab."
80+
rm "$temp_cron"
81+
exit 1
82+
fi
83+
84+
rm "$temp_cron"
85+
echo "Cron job added successfully: $cron_job"
86+
}
87+
4588
# Backup function for existing configurations
4689
backup_configs() {
4790
log "Backing up existing configurations..."
@@ -157,7 +200,7 @@ install() {
157200

158201
# Clean up previous cron jobs related to SystemGuard
159202
log "Cleaning up previous cron jobs related to SystemGuard..."
160-
CRON_PATTERN=".systemguard/SystemGuard-.*/dashboard.sh"
203+
CRON_PATTERN=".systemguard/SystemGuard-.*/src/scripts/dashboard.sh"
161204
if crontab -l | grep -q "$CRON_PATTERN"; then
162205
crontab -l | grep -v "$CRON_PATTERN" | crontab -
163206
log "Old cron jobs removed."
@@ -175,20 +218,15 @@ install() {
175218
rm $DOWNLOAD_DIR/systemguard.zip
176219
log "Extraction completed."
177220

178-
# Navigate to the setup directory and execute setup script
179-
log "Navigating to the SystemGuard setup directory..."
180-
cd $EXTRACT_DIR/SystemGuard-*/src/scripts
181-
if [ ! -f "cronjob.sh" ]; then
182-
log "Error: cronjob.sh not found in the extracted directory. Please verify the contents."
221+
log "Preparing cronjob script..."
222+
add_cron_job
223+
# check if the cronjob added successfully or not
224+
if ! crontab -l | grep -q "$CRON_PATTERN"; then
225+
log "Error: Failed to add the cron job to the crontab."
226+
log "Installation failed... Report this issue to the SystemGuard maintainers at $ISSUE_URL."
183227
exit 1
184228
fi
185229

186-
# Make cronjob.sh executable and run it
187-
log "Preparing cronjob script..."
188-
chmod +x cronjob.sh
189-
log "Executing the cronjob setup..."
190-
./cronjob.sh
191-
192230
# Install the executable
193231
install_executable
194232
log "SystemGuard version $VERSION installed successfully!"

src/scripts/cronjob.sh

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/scripts/dashboard.sh

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,6 @@ export FLASK_ENV=development # or production
7272
if ! pgrep -f "flask run --host=0.0.0.0 --port=$FLASK_PORT" > /dev/null; then
7373
log_message "Flask app is not running. Checking repository and starting it..."
7474

75-
# # Navigate to project directory
76-
# current_dir=$(pwd)
77-
# cd "$PROJECT_DIR" || exit
78-
79-
# # Initialize git and pull updates
80-
# if [ ! -d ".git" ]; then
81-
# git init
82-
# fi
83-
# git remote remove origin 2>/dev/null
84-
# git remote add origin https://github.com/codeperfectplus/SystemGuard.git
85-
86-
# # Apply stashed changes if any
87-
# if git stash list | grep -q 'stash@{0}'; then
88-
# log_message "Applying stashed changes..."
89-
# git stash pop
90-
# fi
91-
# # Fetch and pull the latest changes from the main branch
92-
# if ! git pull origin main; then
93-
# log_message "Failed to pull updates from Git repository."
94-
# cd "$current_dir" || exit
95-
# fi
96-
97-
# cd "$current_dir" || exit
98-
9975
log_message "Starting Flask app..."
10076
# Ensure environment activation and `flask` command
10177
bash -c "source $CONDA_SETUP_SCRIPT && conda activate $CONDA_ENV_NAME && flask run --host=0.0.0.0 --port=$FLASK_PORT" &>> "$LOG_FILE" &

test.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)