22
33# SystemGuard Installer Script
44# ----------------------------
5- # This script installs, uninstalls, backs up, and restores SystemGuard by managing its installation, cleanup, and configuration .
5+ # This script installs, uninstalls, backs up, restores SystemGuard, and includes load testing using Locust .
66
7- # Variables
7+ # Determine the correct user's home directory
8+ get_user_home () {
9+ if [ -n " $SUDO_USER " ]; then
10+ # When using sudo, SUDO_USER gives the original user who invoked sudo
11+ TARGET_USER=" $SUDO_USER "
12+ else
13+ # If not using sudo, use LOGNAME to find the current user
14+ TARGET_USER=" $LOGNAME "
15+ fi
16+
17+ # Get the home directory of the target user
18+ USER_HOME=$( eval echo ~ $TARGET_USER )
19+ echo " $USER_HOME "
20+ }
21+
22+ # Set paths relative to the correct user's home directory
23+ USER_HOME=$( get_user_home)
824DOWNLOAD_DIR=" /tmp"
9- EXTRACT_DIR=" /home/ $USER /.systemguard"
25+ EXTRACT_DIR=" $USER_HOME /.systemguard"
1026LOG_DIR=" $HOME /logs"
1127LOG_FILE=" $LOG_DIR /systemguard-installer.log"
12- BACKUP_DIR=" /home/ $USER /.systemguard_backup"
28+ BACKUP_DIR=" $USER_HOME /.systemguard_backup"
1329EXECUTABLE=" /usr/local/bin/systemguard-installer"
30+ LOCUST_FILE=" $EXTRACT_DIR /SystemGuard-*/src/scripts/locustfile.py"
31+ HOST_URL=" http://localhost:5050"
32+ INSTALLER_SCRIPT=' setup.sh'
1433
34+ echo " User: $( whoami) "
1535# Create necessary directories
1636mkdir -p " $LOG_DIR "
1737mkdir -p " $BACKUP_DIR "
@@ -73,6 +93,23 @@ restore() {
7393 fi
7494}
7595
96+ # Function to install the script as an executable
97+ install_executable () {
98+ # Use $0 to get the full path of the currently running script
99+ # CURRENT_SCRIPT=$(realpath "$0")
100+ cd $EXTRACT_DIR /SystemGuard-* /
101+ CURRENT_SCRIPT=$( pwd) /$INSTALLER_SCRIPT
102+ echo " Current script: $CURRENT_SCRIPT "
103+ # Verify that the script exists before attempting to copy
104+ if [ -f " $CURRENT_SCRIPT " ]; then
105+ log " Installing executable to /usr/local/bin/systemguard-installer..."
106+ cp " $CURRENT_SCRIPT " " $EXECUTABLE "
107+ log " Executable installed successfully."
108+ else
109+ log " Error: Script file not found. Cannot copy to /usr/local/bin."
110+ fi
111+ }
112+
76113# Install function
77114install () {
78115 log " Starting installation of SystemGuard..."
@@ -155,7 +192,7 @@ install() {
155192
156193 # Install the executable
157194 log " Installing executable to /usr/local/bin/systemguard-installer..."
158- # cp "$(basename "$0")" "$EXECUTABLE"
195+ install_executable
159196 log " SystemGuard version $VERSION installed successfully!"
160197}
161198
@@ -190,6 +227,24 @@ uninstall() {
190227 fi
191228}
192229
230+ # Load test function to start Locust server
231+ load_test () {
232+ log " Starting Locust server for load testing..."
233+
234+ # Check if Locust is installed
235+ if ! command -v locust & > /dev/null
236+ then
237+ log " Locust is not installed. Please install it first."
238+ exit 1
239+ fi
240+
241+ # Start Locust server
242+ log " Starting Locust server..."
243+ locust -f " $LOCUST_FILE " --host=" $HOST_URL "
244+ # Optionally, you can pass additional Locust flags here if needed
245+ # locust -f "$LOCUST_FILE" --host="$HOST_URL" --headless -u 10 -r 1 --run-time 1m
246+ }
247+
193248# Display help
194249show_help () {
195250 echo " SystemGuard Installer"
@@ -198,6 +253,7 @@ show_help() {
198253 echo " --install Install SystemGuard"
199254 echo " --uninstall Uninstall SystemGuard"
200255 echo " --restore Restore SystemGuard from a backup"
256+ echo " --load-test Start Locust load testing"
201257 echo " --help Display this help message"
202258}
203259
@@ -207,6 +263,7 @@ for arg in "$@"; do
207263 --install) ACTION=" install" ;;
208264 --uninstall) ACTION=" uninstall" ;;
209265 --restore) ACTION=" restore" ;;
266+ --load-test) ACTION=" load_test" ;;
210267 --help) show_help; exit 0 ;;
211268 * ) echo " Unknown option: $arg " ; show_help; exit 1 ;;
212269 esac
@@ -217,5 +274,6 @@ case $ACTION in
217274 install) install ;;
218275 uninstall) uninstall ;;
219276 restore) restore ;;
277+ load_test) load_test ;;
220278 * ) echo " No action specified. Use --help for usage information." ;;
221279esac
0 commit comments