2
2
3
3
# SystemGuard Installer Script
4
4
# ----------------------------
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 .
6
6
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)
8
24
DOWNLOAD_DIR=" /tmp"
9
- EXTRACT_DIR=" /home/ $USER /.systemguard"
25
+ EXTRACT_DIR=" $USER_HOME /.systemguard"
10
26
LOG_DIR=" $HOME /logs"
11
27
LOG_FILE=" $LOG_DIR /systemguard-installer.log"
12
- BACKUP_DIR=" /home/ $USER /.systemguard_backup"
28
+ BACKUP_DIR=" $USER_HOME /.systemguard_backup"
13
29
EXECUTABLE=" /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'
14
33
34
+ echo " User: $( whoami) "
15
35
# Create necessary directories
16
36
mkdir -p " $LOG_DIR "
17
37
mkdir -p " $BACKUP_DIR "
@@ -73,6 +93,23 @@ restore() {
73
93
fi
74
94
}
75
95
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
+
76
113
# Install function
77
114
install () {
78
115
log " Starting installation of SystemGuard..."
@@ -155,7 +192,7 @@ install() {
155
192
156
193
# Install the executable
157
194
log " Installing executable to /usr/local/bin/systemguard-installer..."
158
- # cp "$(basename "$0")" "$EXECUTABLE"
195
+ install_executable
159
196
log " SystemGuard version $VERSION installed successfully!"
160
197
}
161
198
@@ -190,6 +227,24 @@ uninstall() {
190
227
fi
191
228
}
192
229
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
+
193
248
# Display help
194
249
show_help () {
195
250
echo " SystemGuard Installer"
@@ -198,6 +253,7 @@ show_help() {
198
253
echo " --install Install SystemGuard"
199
254
echo " --uninstall Uninstall SystemGuard"
200
255
echo " --restore Restore SystemGuard from a backup"
256
+ echo " --load-test Start Locust load testing"
201
257
echo " --help Display this help message"
202
258
}
203
259
@@ -207,6 +263,7 @@ for arg in "$@"; do
207
263
--install) ACTION=" install" ;;
208
264
--uninstall) ACTION=" uninstall" ;;
209
265
--restore) ACTION=" restore" ;;
266
+ --load-test) ACTION=" load_test" ;;
210
267
--help) show_help; exit 0 ;;
211
268
* ) echo " Unknown option: $arg " ; show_help; exit 1 ;;
212
269
esac
@@ -217,5 +274,6 @@ case $ACTION in
217
274
install) install ;;
218
275
uninstall) uninstall ;;
219
276
restore) restore ;;
277
+ load_test) load_test ;;
220
278
* ) echo " No action specified. Use --help for usage information." ;;
221
279
esac
0 commit comments