Skip to content

Commit 643fa42

Browse files
chore: systemguard installation script improved
1 parent 2c3f8a3 commit 643fa42

1 file changed

Lines changed: 125 additions & 145 deletions

File tree

setup.sh

Lines changed: 125 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ else
5858
crontab_cmd="crontab"
5959
fi
6060

61+
# this function will change the ownership of the directory
62+
# from root to the user, as the script is run as root
63+
# and the installation directory should be owned by the user
6164
change_ownership() {
6265
local directory="$1"
6366
if [ -d "$directory" ]; then
@@ -69,6 +72,13 @@ change_ownership() {
6972
fi
7073
}
7174

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+
}
7282

7383
# Function to add a cron job with error handling
7484
add_cron_job() {
@@ -187,174 +197,144 @@ install_executable() {
187197
fi
188198
}
189199

190-
# Install function
191-
install() {
192-
log "Starting installation of SystemGuard..."
193-
echo "Do you want to install from a Git repository or a specific release?"
194-
echo "1. Git repository"
195-
echo "2. Release"
196-
read INSTALL_METHOD
200+
# remove previous installation of cron jobs and SystemGuard
201+
remove_previous_installation() {
202+
log "Removing previous installation of SystemGuard, if any..."
203+
if [ -d "$EXTRACT_DIR" ]; then
204+
rm -rf "$EXTRACT_DIR"
205+
log "Old installation removed."
206+
fi
197207

198-
if [ "$INSTALL_METHOD" == "1" ]; then
199-
log "Installing SystemGuard from Git repository..."
208+
log "Cleaning up previous cron jobs related to SystemGuard..."
209+
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
210+
$crontab_cmd -l | grep -v "$CRON_PATTERN" | $crontab_cmd -
211+
log "Old cron jobs removed."
212+
else
213+
log "No previous cron jobs found."
214+
fi
215+
}
200216

217+
# Function to fetch the latest version of SystemGuard from GitHub releases
218+
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.*?(?=")')
221+
if [ -z "$VERSION" ]; then
222+
log "Error: Unable to fetch the latest version. Please try again or specify a version manually."
223+
exit 1
224+
fi
225+
log "Latest version found: $VERSION"
226+
}
201227

202-
log "Removing previous installation of SystemGuard, if any..."
203-
if [ -d "$EXTRACT_DIR" ]; then
204-
rm -rf "$EXTRACT_DIR"
205-
log "Old installation removed."
206-
fi
228+
# Function to download a release from a given URL
229+
download_release() {
230+
local url=$1
231+
local output=$2
232+
log "Downloading SystemGuard from $url..."
233+
if ! wget -q "$url" -O "$output"; then
234+
log "Error: Failed to download SystemGuard. Please check the URL and try again."
235+
exit 1
236+
fi
237+
log "Download completed successfully."
238+
}
207239

208-
# Clean up previous cron jobs related to SystemGuard
209-
log "Cleaning up previous cron jobs related to SystemGuard..."
210-
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
211-
$crontab_cmd -l | grep -v "$CRON_PATTERN" | $crontab_cmd -
212-
log "Old cron jobs removed."
213-
else
214-
log "No previous cron jobs found."
215-
fi
216-
217-
log "Cloning the SystemGuard repository from GitHub..."
218-
if ! git clone https://github.com/codeperfectplus/SystemGuard.git $GIT_INSTALL_DIR; then
219-
log "Error: Failed to clone the repository. Please check your internet connection and try again."
220-
exit 1
221-
fi
222-
log "Repository cloned successfully."
240+
# Function to setup the cron job for SystemGuard
241+
setup_cron_job() {
242+
log "Preparing cron job script..."
243+
add_cron_job
244+
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
245+
log "Cron job added successfully."
246+
else
247+
log "Error: Failed to add the cron job."
248+
exit 1
249+
fi
250+
}
223251

224-
cd $GIT_INSTALL_DIR
225-
log "Setting up SystemGuard from Git repository..."
226-
227-
# Install the executable
228-
install_executable
229-
log "SystemGuard installed successfully from Git!"
230-
231-
# Set up cron job if needed
232-
log "Preparing cron job script..."
233-
add_cron_job
234-
235-
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
236-
log "Cron job added successfully."
237-
else
238-
log "Error: Failed to add the cron job."
239-
exit 1
240-
fi
241-
change_ownership "$EXTRACT_DIR"
252+
# Function to install the latest version of SystemGuard
253+
install_from_git() {
254+
log "Installing SystemGuard from Git repository..."
242255

243-
exit 0
244-
elif [ "$INSTALL_METHOD" == "2" ]; then
245-
echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):"
246-
read VERSION
247-
echo "Warning: This script will remove any existing installation of SystemGuard. Continue? (y/n)"
248-
read CONFIRM
256+
backup_configs
257+
remove_previous_installation
249258

250-
if [ "$CONFIRM" != "y" ]; then
251-
log "Installation aborted by user."
252-
exit 0
253-
fi
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."
262+
exit 1
263+
fi
254264

255-
# Fetch latest version if specified
256-
if [ "$VERSION" == "latest" ]; then
257-
log "Fetching the latest version of SystemGuard from GitHub..."
258-
VERSION=$(curl -s https://api.github.com/repos/codeperfectplus/SystemGuard/releases/latest | grep -Po '"tag_name": "\K.*?(?=")')
259-
if [ -z "$VERSION" ]; then
260-
log "Error: Unable to fetch the latest version. Please try again or specify a version manually."
261-
exit 1
262-
fi
263-
log "Latest version found: $VERSION"
264-
fi
265+
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..."
269+
install_executable
265270

266-
# Define URL after determining the version
267-
ZIP_URL="https://github.com/codeperfectplus/SystemGuard/archive/refs/tags/$VERSION.zip"
268-
log "Installing SystemGuard version $VERSION..."
269-
270-
# Download the SystemGuard zip file
271-
log "Downloading SystemGuard version $VERSION from $ZIP_URL..."
272-
if ! wget -q "$ZIP_URL" -O "$DOWNLOAD_DIR/systemguard.zip"; then
273-
log "Error: Failed to download SystemGuard version $VERSION. Please check the version number and try again."
274-
exit 1
275-
fi
276-
log "Download completed successfully."
271+
log "SystemGuard installed successfully from Git!"
272+
setup_cron_job
277273

278-
# Backup existing configurations
279-
backup_configs
274+
change_ownership "$EXTRACT_DIR"
275+
exit 0
276+
}
280277

281-
# Remove any existing installation of SystemGuard
282-
log "Removing previous installation of SystemGuard, if any..."
283-
if [ -d "$EXTRACT_DIR" ]; then
284-
rm -rf "$EXTRACT_DIR"
285-
log "Old installation removed."
286-
fi
278+
# install the latest version of SystemGuard from the release
279+
install_from_release() {
280+
echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):"
281+
read -r VERSION
287282

288-
# Clean up previous cron jobs related to SystemGuard
289-
log "Cleaning up previous cron jobs related to SystemGuard..."
290-
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
291-
$crontab_cmd -l | grep -v "$CRON_PATTERN" | $crontab_cmd -
292-
log "Old cron jobs removed."
293-
else
294-
log "No previous cron jobs found."
295-
fi
283+
[ "$VERSION" == "latest" ] && fetch_latest_version
296284

297-
# Create the extraction directory
298-
log "Setting up installation directory..."
299-
mkdir -p $EXTRACT_DIR
300-
301-
# Extract the downloaded zip file
302-
log "Extracting SystemGuard package..."
303-
unzip -q $DOWNLOAD_DIR/systemguard.zip -d $EXTRACT_DIR
304-
rm $DOWNLOAD_DIR/systemguard.zip
305-
log "Extraction completed."
306-
log "Preparing cron job script..."
307-
add_cron_job
308-
309-
# Check if the cron job is added successfully
310-
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
311-
log "Cron job added successfully."
312-
else
313-
log "Error: Failed to add the cron job."
314-
exit 1
315-
fi
285+
ZIP_URL="https://github.com/codeperfectplus/SystemGuard/archive/refs/tags/$VERSION.zip"
286+
log "Installing SystemGuard version $VERSION..."
316287

317-
# Install the executable
318-
install_executable
319-
change_ownership "$EXTRACT_DIR"
320-
log "SystemGuard version $VERSION installed successfully!"
321-
log "Server may take a few minutes to start, if you face any try to restart the server."
288+
download_release "$ZIP_URL" "$DOWNLOAD_DIR/systemguard.zip"
322289

323-
else
324-
log "Invalid installation method. Please choose 'git' or 'release'."
325-
exit 1
326-
fi
327-
}
290+
backup_configs
291+
remove_previous_installation
292+
293+
log "Setting up installation directory..."
294+
mkdir -p "$EXTRACT_DIR"
328295

296+
log "Extracting SystemGuard package..."
297+
unzip -q "$DOWNLOAD_DIR/systemguard.zip" -d "$EXTRACT_DIR"
298+
rm "$DOWNLOAD_DIR/systemguard.zip"
299+
log "Extraction completed."
329300

301+
install_executable
302+
setup_cron_job
303+
304+
change_ownership "$EXTRACT_DIR"
305+
log "SystemGuard version $VERSION installed successfully!"
306+
log "Server may take a few minutes to start. If you face any issues, try restarting the server."
307+
}
308+
309+
# Install function
310+
install() {
311+
log "Starting installation of SystemGuard..."
312+
echo "Do you want to install from a Git repository or a specific release?"
313+
echo "1. Git repository"
314+
echo "2. Release"
315+
read -r INSTALL_METHOD
316+
317+
case $INSTALL_METHOD in
318+
1)
319+
install_from_git
320+
;;
321+
2)
322+
install_from_release
323+
;;
324+
*)
325+
log "Invalid installation method. Please choose '1' for Git repository or '2' for Release."
326+
exit 1
327+
;;
328+
esac
329+
}
330330
# Uninstall function
331331
uninstall() {
332332
log "Uninstalling SystemGuard..."
333333

334334
# Remove cron jobs related to SystemGuard
335335
# cronjob
336-
337-
if $crontab_cmd -l 2>/dev/null | grep -E "$CRON_PATTERN" > /dev/null; then
338-
echo "Are you sure you want to remove all cron jobs related to SystemGuard? (y/n)"
339-
read CONFIRM
340-
if [ "$CONFIRM" != "y" ]; then
341-
log "Uninstallation aborted by user."
342-
exit 0
343-
fi
344-
$crontab_cmd -l | grep -v "$CRON_PATTERN" | $crontab_cmd -
345-
log "Cron jobs removed."
346-
else
347-
log "No cron jobs found."
348-
fi
349-
350-
# Remove the SystemGuard installation directory
351-
if [ -d "$EXTRACT_DIR" ]; then
352-
rm -rf "$EXTRACT_DIR"
353-
log "SystemGuard has been removed from your system."
354-
else
355-
log "SystemGuard is not installed on this system."
356-
fi
357-
336+
remove_previous_installation
337+
358338
# Remove the executable
359339
if [ -f "$EXECUTABLE" ]; then
360340
rm "$EXECUTABLE"

0 commit comments

Comments
 (0)