Skip to content

Commit 9a44275

Browse files
chore: Refactor installation script and improve setup.sh
1 parent 8e7221e commit 9a44275

File tree

1 file changed

+104
-81
lines changed

1 file changed

+104
-81
lines changed

setup.sh

Lines changed: 104 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,45 @@
33
# SystemGuard Installer Script
44
# ----------------------------
55
# This script installs, uninstalls, backs up, restores SystemGuard, and includes load testing using Locust.
6-
# Determine the correct user's home directory
6+
7+
# Retrieve the home directory of the current user
8+
USER_HOME=$(get_user_home)
9+
USER_NAME=$(basename "$USER_HOME")
10+
11+
# Define directories and file paths
12+
DOWNLOAD_DIR="/tmp"
13+
APP_NAME="SystemGuard"
14+
EXTRACT_DIR="$USER_HOME/.systemguard"
15+
GIT_INSTALL_DIR="$EXTRACT_DIR/${APP_NAME}-git"
16+
LOG_DIR="$USER_HOME/logs"
17+
LOG_FILE="$LOG_DIR/systemguard-installer.log"
18+
BACKUP_DIR="$USER_HOME/.systemguard_backup"
19+
EXECUTABLE="/usr/local/bin/systemguard-installer"
20+
21+
# Application-related file paths
22+
HOST_URL="http://localhost:5050"
23+
INSTALLER_SCRIPT="setup.sh"
24+
FLASK_LOG_FILE="$LOG_DIR/systemguard_flask.log"
25+
26+
# Backup settings
27+
NUM_BACKUPS=5
28+
29+
# Cron job pattern for SystemGuard
30+
CRON_PATTERN=".systemguard/${APP_NAME}-.*/src/scripts/dashboard.sh"
31+
32+
# GitHub repository details
33+
GITHUB_USER="codeperfectplus"
34+
GITHUB_REPO="$APP_NAME"
35+
GITHUB_URL="https://github.com/$GITHUB_USER/$GITHUB_REPO"
36+
ISSUE_TRACKER_URL="$GITHUB_URL/issues"
37+
38+
# Environment variables
39+
CONDA_ENV_NAME="systemguard"
40+
ENV_FILE="$USER_HOME/.bashrc" # Default environment file
41+
42+
# SystemGuard authentication
43+
SYSTEMGUARD_USER="admin"
44+
SYSTEMGUARD_PASS="admin"
745

846
set -e
947
trap 'echo "An error occurred. Exiting..."; exit 1;' ERR
@@ -129,37 +167,6 @@ check_dependencies() {
129167
fi
130168
}
131169

132-
133-
134-
# Function to set systemguard_auto_update variable permanently
135-
set_auto_update() {
136-
local env_file="$USER_HOME/.bashrc" # Default file for setting environment variables
137-
138-
# Prompt user for input
139-
echo "Do you want to enable systemguard_auto_update? (true/false)"
140-
echo "This will enable automatic updates for SystemGuard."
141-
read -p "Enter your choice: " auto_update
142-
143-
# Validate input
144-
if [[ "$auto_update" != "true" && "$auto_update" != "false" ]]; then
145-
echo "Invalid input. Please enter 'true' or 'false'."
146-
return 1
147-
fi
148-
149-
# Check if the variable is already set
150-
if grep -q '^export systemguard_auto_update=' "$env_file"; then
151-
# Update existing entry
152-
sed -i "s/^export systemguard_auto_update=.*/export systemguard_auto_update=$auto_update/" "$env_file"
153-
else
154-
# Add new entry
155-
echo "export systemguard_auto_update=$auto_update" >> "$env_file"
156-
fi
157-
158-
# Notify user and reload the environment file
159-
echo "systemguard_auto_update set to $auto_update in $env_file."
160-
source "$env_file"
161-
}
162-
163170
get_user_home() {
164171
if [ -n "$SUDO_USER" ]; then
165172
# When using sudo, SUDO_USER gives the original user who invoked sudo
@@ -174,57 +181,26 @@ get_user_home() {
174181
echo "$USER_HOME"
175182
}
176183

177-
# Retrieve the home directory of the current user
178-
USER_HOME=$(get_user_home)
179-
USER_NAME=$(basename "$USER_HOME")
180-
181-
# Define directories and file paths
182-
DOWNLOAD_DIR="/tmp"
183-
APP_NAME="SystemGuard"
184-
EXTRACT_DIR="$USER_HOME/.systemguard"
185-
GIT_INSTALL_DIR="$EXTRACT_DIR/${APP_NAME}-git"
186-
LOG_DIR="$USER_HOME/logs"
187-
LOG_FILE="$LOG_DIR/systemguard-installer.log"
188-
BACKUP_DIR="$USER_HOME/.systemguard_backup"
189-
EXECUTABLE_APP_NAME="systemguard-installer"
190-
EXECUTABLE="/usr/local/bin/$EXECUTABLE_APP_NAME"
191-
192-
# File paths related to the application
193-
HOST_URL="http://localhost:5050"
194-
INSTALLER_SCRIPT='setup.sh'
195-
FLASK_LOG_FILE="$USER_HOME/logs/systemguard_flask.log"
196-
197-
# Number of backups to keep
198-
NUM_OF_BACKUP=5
199-
200-
# Pattern for identifying cron jobs related to SystemGuard
201-
CRON_PATTERN=".systemguard/${APP_NAME}-.*/src/scripts/dashboard.sh"
202-
203-
# GitHub repository details
204-
GIT_USER="codeperfectplus"
205-
GIT_REPO="$APP_NAME"
206-
GIT_URL="https://github.com/$GIT_USER/$GIT_REPO"
207-
ISSUE_URL="$GIT_URL/issues"
208-
209-
# ENVIRONMENT VARIABLES
210-
CONDA_ENV_NAME="systemguard"
211-
212-
# systemguard authentication
213-
SYSTEMGUARD_USERNAME="admin"
214-
SYSTEMGUARD_PASSWORD="admin"
215184

216185
# Function to create a directory if it does not exist
217186
create_and_own_dir() {
218187
local dir="$1"
219188
if [ ! -d "$dir" ]; then
220-
mkdir -p "$dir" || { log "ERROR" "Failed to create directory: $dir"; exit 1; }
221-
chown "$USER_NAME:$USER_NAME" "$dir" || { log "ERROR" "Failed to change ownership of directory: $dir"; exit 1; }
189+
mkdir -p "$dir" || { log "ERROR" "Failed to create directory: $dir"; exit 10; }
190+
chown "$USER_NAME:$USER_NAME" "$dir" || { log "ERROR" "Failed to change ownership of directory: $dir"; exit 11; }
222191
fi
223192
}
224193

225194
create_and_own_dir "$LOG_DIR"
226195
create_and_own_dir "$BACKUP_DIR"
227196

197+
# Function to handle errors
198+
handle_error() {
199+
local exit_code="$1"
200+
local message="$2"
201+
log "ERROR" "$message"
202+
exit "$exit_code"
203+
}
228204

229205
# Check if running with sudo
230206
if [ "$EUID" -eq 0 ]; then
@@ -233,6 +209,53 @@ else
233209
crontab_cmd="crontab"
234210
fi
235211

212+
# Function to validate the user input
213+
validate_choice() {
214+
local choice="$1"
215+
if [[ "$choice" != "true" && "$choice" != "false" ]]; then
216+
echo "Invalid input. Please enter 'true' or 'false'."
217+
return 1
218+
fi
219+
return 0
220+
}
221+
222+
update_env_variable() {
223+
local var_name="$1"
224+
local var_value="$2"
225+
226+
# Check if the variable already exists in the environment file
227+
if grep -q "^export $var_name=" "$ENV_FILE"; then
228+
# Update existing entry
229+
sed -i "s/^export $var_name=.*/export $var_name=$var_value/" "$ENV_FILE"
230+
else
231+
# Add new entry
232+
echo "export $var_name=$var_value" >> "$ENV_FILE"
233+
fi
234+
235+
# Notify user of change
236+
echo "$var_name set to $var_value in $ENV_FILE."
237+
}
238+
239+
prompt_user() {
240+
echo "Do you want to enable $var_name? (true/false) This will enable automatic updates for SystemGuard."
241+
read -p "Enter your choice (true/false): " user_choice
242+
echo "$user_choice"
243+
}
244+
245+
# set the auto update variable
246+
set_auto_update() {
247+
var_name=$1
248+
prompt_user # Prompt user for input
249+
# Validate input
250+
if ! validate_choice "$user_choice"; then
251+
return 1
252+
fi
253+
# Update environment variable
254+
update_env_variable "$var_name" "$user_choice"
255+
# Reload environment file
256+
source "$ENV_FILE"
257+
}
258+
236259
# this function will change the ownership of the directory
237260
# from root to the user, as the script is run as root
238261
# and the installation directory should be owned by the user
@@ -496,11 +519,11 @@ install_from_git() {
496519

497520
echo ""
498521
echo "Select the version of $APP_NAME to install:"
499-
echo "|-----------------------------------------------------------------------------|"
500-
echo "| 1. Production (stable) - Recommended for most users |"
501-
echo "| 2. Development (dev) - Latest features, may be unstable |"
502-
echo "| 3. Specify a branch - Enter the branch/tag name when prompted |"
503-
echo "|-----------------------------------------------------------------------------|"
522+
echo "|------------------------------------------------------------------------------|"
523+
echo "| 1. Production (stable) -> Recommended for most users |"
524+
echo "| 2. Development (dev) -> Latest features, may be unstable |"
525+
echo "| 3. Specify a branch -> Enter the branch/tag name when prompted |"
526+
echo "|------------------------------------------------------------------------------|"
504527
echo "Enter the number of your choice:"
505528
read -r VERSION
506529

@@ -520,23 +543,23 @@ install_from_git() {
520543
log "Selected branch: $BRANCH."
521544
;;
522545
*) # Invalid input handling
523-
log "WARNING" "Invalid selection. Please enter '1' for Stable, '2' for Development, or '3' to specify a branch."
524-
exit 1
546+
BRANCH="production"
547+
log "WARNING" "Invalid branch selected. Defaulting to 'production'."
525548
;;
526549
esac
527550

528551
# Construct the full Git URL with branch
529552
FULL_GIT_URL="https://github.com/codeperfectplus/SystemGuard.git -b $BRANCH"
530553

531-
set_auto_update
554+
set_auto_update "systemguard_auto_update"
532555

533556
log "Cloning the $APP_NAME repository from GitHub..."
534557
create_and_own_dir "$GIT_INSTALL_DIR"
535558
check_conda # check if conda is installed
536559

537560
if ! git clone $FULL_GIT_URL "$GIT_INSTALL_DIR"; then
538561
log "ERROR" "Failed to clone the repository. Please check your internet connection and the branch name, and try again."
539-
exit 1
562+
exit 30
540563
fi
541564

542565
log "Repository cloned successfully."

0 commit comments

Comments
 (0)