Skip to content

Commit 5a50623

Browse files
chore: bash script improved
1 parent 643fa42 commit 5a50623

File tree

1 file changed

+111
-52
lines changed

1 file changed

+111
-52
lines changed

setup.sh

Lines changed: 111 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,45 @@ get_user_home() {
2525
echo "$USER_HOME"
2626
}
2727

28-
# Set paths relative to the correct user's home directory
28+
# Retrieve the home directory of the current user
2929
USER_HOME=$(get_user_home)
30-
USER_NAME=$(echo $USER_HOME | awk -F'/' '{print $3}')
30+
USER_NAME=$(basename "$USER_HOME")
31+
32+
# Define directories and file paths
3133
DOWNLOAD_DIR="/tmp"
34+
APP_NAME="SystemGuard"
3235
EXTRACT_DIR="$USER_HOME/.systemguard"
33-
GIT_INSTALL_DIR="$EXTRACT_DIR/SystemGuard-dev"
34-
LOG_DIR="$HOME/logs"
36+
GIT_INSTALL_DIR="$EXTRACT_DIR/${APP_NAME}-git"
37+
LOG_DIR="$USER_HOME/logs"
3538
LOG_FILE="$LOG_DIR/systemguard-installer.log"
3639
BACKUP_DIR="$USER_HOME/.systemguard_backup"
3740
EXECUTABLE="/usr/local/bin/systemguard-installer"
38-
LOCUST_FILE="$EXTRACT_DIR/SystemGuard-*/src/scripts/locustfile.py"
41+
42+
# File paths related to the application
43+
LOCUST_FILE="$EXTRACT_DIR/${APP_NAME}-*/src/scripts/locustfile.py"
3944
HOST_URL="http://localhost:5050"
4045
INSTALLER_SCRIPT='setup.sh'
41-
ISSUE_URL="https://github.com/codeperfectplus/SystemGuard/issues"
42-
CRON_PATTERN=".systemguard/SystemGuard-.*/src/scripts/dashboard.sh"
4346

44-
# Create necessary directories
45-
mkdir -p "$LOG_DIR"
46-
mkdir -p "$BACKUP_DIR"
47+
# Pattern for identifying cron jobs related to SystemGuard
48+
CRON_PATTERN=".systemguard/${APP_NAME}-.*/src/scripts/dashboard.sh"
49+
50+
# GitHub repository details
51+
GIT_USER="codeperfectplus"
52+
GIT_REPO="$APP_NAME"
53+
GIT_URL="https://github.com/$GIT_USER/$GIT_REPO.git"
54+
ISSUE_URL="$GIT_URL/issues"
55+
56+
57+
# Function to create a directory if it does not exist
58+
create_dir_if_not_exists() {
59+
local dir="$1"
60+
if [ ! -d "$dir" ]; then
61+
mkdir -p "$dir" || { log "Error: Failed to create directory: $dir"; exit 1; }
62+
fi
63+
}
64+
65+
create_dir_if_not_exists "$LOG_DIR"
66+
create_dir_if_not_exists "$BACKUP_DIR"
4767

4868
# Logging function with timestamp
4969
log() {
@@ -72,13 +92,7 @@ change_ownership() {
7292
fi
7393
}
7494

75-
# Function to create a directory if it does not exist
76-
create_dir_if_not_exists() {
77-
local dir="$1"
78-
if [ ! -d "$dir" ]; then
79-
mkdir -p "$dir" || { log "Error: Failed to create directory: $dir"; exit 1; }
80-
fi
81-
}
95+
8296

8397
# Function to add a cron job with error handling
8498
add_cron_job() {
@@ -185,7 +199,7 @@ restore() {
185199
install_executable() {
186200
# Use $0 to get the full path of the currently running script
187201
# CURRENT_SCRIPT=$(realpath "$0")
188-
cd $EXTRACT_DIR/SystemGuard-*/
202+
cd $EXTRACT_DIR/$APP_NAME-*/
189203
CURRENT_SCRIPT=$(pwd)/$INSTALLER_SCRIPT
190204
# Verify that the script exists before attempting to copy
191205
if [ -f "$CURRENT_SCRIPT" ]; then
@@ -199,13 +213,13 @@ install_executable() {
199213

200214
# remove previous installation of cron jobs and SystemGuard
201215
remove_previous_installation() {
202-
log "Removing previous installation of SystemGuard, if any..."
216+
log "Removing previous installation of $APP_NAME, if any..."
203217
if [ -d "$EXTRACT_DIR" ]; then
204218
rm -rf "$EXTRACT_DIR"
205219
log "Old installation removed."
206220
fi
207221

208-
log "Cleaning up previous cron jobs related to SystemGuard..."
222+
log "Cleaning up previous cron jobs related to $APP_NAME..."
209223
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
210224
$crontab_cmd -l | grep -v "$CRON_PATTERN" | $crontab_cmd -
211225
log "Old cron jobs removed."
@@ -216,8 +230,8 @@ remove_previous_installation() {
216230

217231
# Function to fetch the latest version of SystemGuard from GitHub releases
218232
fetch_latest_version() {
219-
log "Fetching the latest version of SystemGuard from GitHub..."
220-
VERSION=$(curl -s https://api.github.com/repos/codeperfectplus/SystemGuard/releases/latest | grep -Po '"tag_name": "\K.*?(?=")')
233+
log "Fetching the latest version of $APP_NAME from GitHub..."
234+
VERSION=$(curl -s https://api.github.com/repos/$GIT_USER/$GIT_REPO/releases/latest | grep -Po '"tag_name": "\K.*?(?=")')
221235
if [ -z "$VERSION" ]; then
222236
log "Error: Unable to fetch the latest version. Please try again or specify a version manually."
223237
exit 1
@@ -229,9 +243,9 @@ fetch_latest_version() {
229243
download_release() {
230244
local url=$1
231245
local output=$2
232-
log "Downloading SystemGuard from $url..."
246+
log "Downloading $APP_NAME from $url..."
233247
if ! wget -q "$url" -O "$output"; then
234-
log "Error: Failed to download SystemGuard. Please check the URL and try again."
248+
log "Error: Failed to download $APP_NAME. Please check the URL and try again."
235249
exit 1
236250
fi
237251
log "Download completed successfully."
@@ -249,41 +263,86 @@ setup_cron_job() {
249263
fi
250264
}
251265

252-
# Function to install the latest version of SystemGuard
266+
# Function to install SystemGuard from Git repository
253267
install_from_git() {
254-
log "Installing SystemGuard from Git repository..."
268+
log "Starting installation of $APP_NAME from Git repository..."
255269

270+
# Backup existing configurations
256271
backup_configs
272+
273+
# Remove any previous installations
257274
remove_previous_installation
258275

259-
log "Cloning the SystemGuard repository from GitHub..."
260-
if ! git clone https://github.com/codeperfectplus/SystemGuard.git "$GIT_INSTALL_DIR"; then
261-
log "Error: Failed to clone the repository. Please check your internet connection and try again."
276+
log "Select the version of $APP_NAME to install:"
277+
echo "1. Stable (default)"
278+
echo "2. Development (unstable)"
279+
echo "3. Enter a specific branch name"
280+
read -r VERSION
281+
282+
# Set Git URL based on user choice
283+
case "$VERSION" in
284+
1|"") # Stable is the default option if nothing is entered
285+
BRANCH="main"
286+
log "Selected Stable version (main branch)."
287+
;;
288+
2) # Development version
289+
BRANCH="dev"
290+
log "Selected Development (dev branch)."
291+
;;
292+
3) # Specific branch
293+
echo "Enter the branch name to install:"
294+
read -r BRANCH
295+
log "Selected branch: $BRANCH."
296+
;;
297+
*) # Invalid input handling
298+
log "Invalid selection. Please enter '1' for Stable, '2' for Development, or '3' to specify a branch."
299+
exit 1
300+
;;
301+
esac
302+
303+
# Construct the full Git URL with branch
304+
FULL_GIT_URL="https://github.com/codeperfectplus/SystemGuard.git -b $BRANCH"
305+
306+
log "Cloning the $APP_NAME repository from GitHub..."
307+
if ! git clone $FULL_GIT_URL "$GIT_INSTALL_DIR"; then
308+
log "Error: Failed to clone the repository. Please check your internet connection and the branch name, and try again."
262309
exit 1
263310
fi
264311

265312
log "Repository cloned successfully."
266-
cd "$GIT_INSTALL_DIR" || { log "Error: Failed to navigate to the installation directory."; exit 1; }
267-
268-
log "Setting up SystemGuard from Git repository..."
313+
314+
# Change to the installation directory
315+
cd "$GIT_INSTALL_DIR" || {
316+
log "Error: Failed to navigate to the installation directory.";
317+
exit 1;
318+
}
319+
320+
log "Setting up $APP_NAME from Git repository..."
321+
322+
# Install the executable
269323
install_executable
270324

271-
log "SystemGuard installed successfully from Git!"
325+
log "$APP_NAME installed successfully from Git!"
326+
327+
# Set up the cron job if necessary
272328
setup_cron_job
273329

330+
# Change ownership of the installation directory
274331
change_ownership "$EXTRACT_DIR"
332+
333+
log "Installation complete. $APP_NAME is ready to use."
275334
exit 0
276335
}
277336

278337
# install the latest version of SystemGuard from the release
279338
install_from_release() {
280-
echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):"
339+
echo "Enter the version of $APP_NAME to install (e.g., v1.0.0 or 'latest' for the latest version):"
281340
read -r VERSION
282341

283342
[ "$VERSION" == "latest" ] && fetch_latest_version
284343

285-
ZIP_URL="https://github.com/codeperfectplus/SystemGuard/archive/refs/tags/$VERSION.zip"
286-
log "Installing SystemGuard version $VERSION..."
344+
ZIP_URL="$GIT_URL/archive/refs/tags/$VERSION.zip"
345+
log "Installing $APP_NAME version $VERSION..."
287346

288347
download_release "$ZIP_URL" "$DOWNLOAD_DIR/systemguard.zip"
289348

@@ -293,7 +352,7 @@ install_from_release() {
293352
log "Setting up installation directory..."
294353
mkdir -p "$EXTRACT_DIR"
295354

296-
log "Extracting SystemGuard package..."
355+
log "Extracting $APP_NAME package..."
297356
unzip -q "$DOWNLOAD_DIR/systemguard.zip" -d "$EXTRACT_DIR"
298357
rm "$DOWNLOAD_DIR/systemguard.zip"
299358
log "Extraction completed."
@@ -302,13 +361,13 @@ install_from_release() {
302361
setup_cron_job
303362

304363
change_ownership "$EXTRACT_DIR"
305-
log "SystemGuard version $VERSION installed successfully!"
364+
log "$APP_NAME version $VERSION installed successfully!"
306365
log "Server may take a few minutes to start. If you face any issues, try restarting the server."
307366
}
308367

309368
# Install function
310369
install() {
311-
log "Starting installation of SystemGuard..."
370+
log "Starting installation of $APP_NAME..."
312371
echo "Do you want to install from a Git repository or a specific release?"
313372
echo "1. Git repository"
314373
echo "2. Release"
@@ -329,7 +388,7 @@ install() {
329388
}
330389
# Uninstall function
331390
uninstall() {
332-
log "Uninstalling SystemGuard..."
391+
log "Uninstalling $APP_NAME..."
333392

334393
# Remove cron jobs related to SystemGuard
335394
# cronjob
@@ -364,26 +423,26 @@ load_test() {
364423

365424
# Check if SystemGuard is installed
366425
check_status() {
367-
log "Checking SystemGuard status..."
426+
log "Checking $APP_NAME status..."
368427

369428
if [ -d "$EXTRACT_DIR" ]; then
370-
log "SystemGuard is installed at $EXTRACT_DIR."
429+
log "$APP_NAME is installed at $EXTRACT_DIR."
371430
else
372-
log "SystemGuard is not installed."
431+
log "$APP_NAME is not installed."
373432
fi
374433

375434

376435
if $crontab_cmd '-l' | grep -q "$CRON_PATTERN"; then
377-
log "Cron job for SystemGuard is set."
436+
log "Cron job for $APP_NAME is set."
378437
else
379-
log "No cron job found for SystemGuard."
438+
log "No cron job found for $APP_NAME."
380439
fi
381440

382441
log "Performing health check on $HOST_URL..."
383442
if curl -s --head $HOST_URL | grep "200 OK" > /dev/null; then
384-
log "SystemGuard services are running."
443+
log "$APP_NAME services are running."
385444
else
386-
log "SystemGuard services are not running."
445+
log "$APP_NAME services are not running."
387446
fi
388447
}
389448

@@ -400,14 +459,14 @@ health_check() {
400459

401460
# Display help
402461
show_help() {
403-
echo "SystemGuard Installer"
462+
echo "$APP_NAME Installer"
404463
echo "Usage: ./installer.sh [options]"
405464
echo "Options:"
406-
echo " --install Install SystemGuard"
407-
echo " --uninstall Uninstall SystemGuard"
408-
echo " --restore Restore SystemGuard from a backup"
465+
echo " --install Install $APP_NAME"
466+
echo " --uninstall Uninstall $APP_NAME"
467+
echo " --restore Restore $APP_NAME from a backup"
409468
echo " --load-test Start Locust load testing"
410-
echo " --status Check the status of SystemGuard installation"
469+
echo " --status Check the status of $APP_NAME installation"
411470
echo " --health-check Perform a health check on localhost:5005"
412471
echo " --help Display this help message"
413472
}

0 commit comments

Comments
 (0)