Skip to content

Commit 2c3f8a3

Browse files
chore: Improve setup script
1 parent 1c2bce9 commit 2c3f8a3

1 file changed

Lines changed: 30 additions & 28 deletions

File tree

setup.sh

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
# SystemGuard Installer Script
44
# ----------------------------
55
# This script installs, uninstalls, backs up, restores SystemGuard, and includes load testing using Locust.
6-
76
# Determine the correct user's home directory
7+
8+
# run this script with sudo
9+
if [ "$EUID" -ne 0 ]; then
10+
echo "Please run this program with sudo."
11+
exit 1
12+
fi
13+
814
get_user_home() {
915
if [ -n "$SUDO_USER" ]; then
1016
# When using sudo, SUDO_USER gives the original user who invoked sudo
@@ -24,6 +30,7 @@ USER_HOME=$(get_user_home)
2430
USER_NAME=$(echo $USER_HOME | awk -F'/' '{print $3}')
2531
DOWNLOAD_DIR="/tmp"
2632
EXTRACT_DIR="$USER_HOME/.systemguard"
33+
GIT_INSTALL_DIR="$EXTRACT_DIR/SystemGuard-dev"
2734
LOG_DIR="$HOME/logs"
2835
LOG_FILE="$LOG_DIR/systemguard-installer.log"
2936
BACKUP_DIR="$USER_HOME/.systemguard_backup"
@@ -51,6 +58,17 @@ else
5158
crontab_cmd="crontab"
5259
fi
5360

61+
change_ownership() {
62+
local directory="$1"
63+
if [ -d "$directory" ]; then
64+
# if permission is set to root then change it to the user
65+
if [ "$(stat -c %U "$directory")" == "root" ]; then
66+
chown -R "$USER_NAME:$USER_NAME" "$directory"
67+
log "Ownership changed from root to $USER_NAME for $directory"
68+
fi
69+
fi
70+
}
71+
5472

5573
# Function to add a cron job with error handling
5674
add_cron_job() {
@@ -98,7 +116,8 @@ add_cron_job() {
98116
fi
99117

100118
rm "$temp_cron"
101-
log "Cron job added successfully: $cron_job"
119+
log "Cron job added successfully"
120+
log $cron_job
102121
}
103122

104123
# Backup function for existing configurations
@@ -171,10 +190,12 @@ install_executable() {
171190
# Install function
172191
install() {
173192
log "Starting installation of SystemGuard..."
174-
echo "Do you want to install from a Git repository or a specific release? (git/release):"
193+
echo "Do you want to install from a Git repository or a specific release?"
194+
echo "1. Git repository"
195+
echo "2. Release"
175196
read INSTALL_METHOD
176197

177-
if [ "$INSTALL_METHOD" == "git" ]; then
198+
if [ "$INSTALL_METHOD" == "1" ]; then
178199
log "Installing SystemGuard from Git repository..."
179200

180201

@@ -194,13 +215,13 @@ install() {
194215
fi
195216

196217
log "Cloning the SystemGuard repository from GitHub..."
197-
if ! git clone https://github.com/codeperfectplus/SystemGuard.git "$EXTRACT_DIR/SystemGuard-dev"; then
218+
if ! git clone https://github.com/codeperfectplus/SystemGuard.git $GIT_INSTALL_DIR; then
198219
log "Error: Failed to clone the repository. Please check your internet connection and try again."
199220
exit 1
200221
fi
201222
log "Repository cloned successfully."
202223

203-
cd "$EXTRACT_DIR/SystemGuard-dev"
224+
cd $GIT_INSTALL_DIR
204225
log "Setting up SystemGuard from Git repository..."
205226

206227
# Install the executable
@@ -217,9 +238,10 @@ install() {
217238
log "Error: Failed to add the cron job."
218239
exit 1
219240
fi
241+
change_ownership "$EXTRACT_DIR"
220242

221243
exit 0
222-
elif [ "$INSTALL_METHOD" == "release" ]; then
244+
elif [ "$INSTALL_METHOD" == "2" ]; then
223245
echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):"
224246
read VERSION
225247
echo "Warning: This script will remove any existing installation of SystemGuard. Continue? (y/n)"
@@ -294,6 +316,7 @@ install() {
294316

295317
# Install the executable
296318
install_executable
319+
change_ownership "$EXTRACT_DIR"
297320
log "SystemGuard version $VERSION installed successfully!"
298321
log "Server may take a few minutes to start, if you face any try to restart the server."
299322

@@ -434,25 +457,4 @@ case $ACTION in
434457
health_check) health_check ;;
435458
*) echo "No action specified. Use --help for usage information." ;;
436459
esac
437-
438-
# this script ran with sudo command so all the files have root permission
439-
# remove the root permission from the files to the user
440-
# Function to change ownership of a directory to the user
441-
change_ownership() {
442-
local directory="$1"
443-
if [ -d "$directory" ]; then
444-
# if permission is set to root then change it to the user
445-
if [ "$(stat -c %U "$directory")" == "root" ]; then
446-
chown -R "$USER_NAME:$USER_NAME" "$directory"
447-
log "Ownership changed to $USER_NAME for directory: $directory"
448-
fi
449-
fi
450-
}
451-
452-
453-
# Call the change_ownership function
454-
change_ownership "$EXTRACT_DIR"
455-
456-
# log "For any issues or feedback, please report at: $ISSUE_URL"
457-
# log "For more information, check the log file: $LOG_FILE"
458460
# # End of script

0 commit comments

Comments
 (0)