Skip to content

Commit f85e7d9

Browse files
chore: Refactor installation script and improve setup.sh
1 parent 2e554bd commit f85e7d9

File tree

2 files changed

+82
-46
lines changed

2 files changed

+82
-46
lines changed

setup.sh

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ create_and_own_dir() {
197197

198198
create_and_own_dir "$LOG_DIR"
199199
create_and_own_dir "$BACKUP_DIR"
200-
create_and_own_dir "$EXTRACT_DIR"
200+
201201

202202
# Check if running with sudo
203203
if [ "$EUID" -eq 0 ]; then
@@ -220,7 +220,27 @@ change_ownership() {
220220
fi
221221
}
222222

223+
# check if conda is installed or not
224+
check_conda() {
225+
CONDA_PATHS=("/home/$USER_NAME/miniconda3" "/home/$USER_NAME/anaconda3")
226+
CONDA_FOUND=false
227+
228+
# Find Conda installation
229+
for CONDA_PATH in "${CONDA_PATHS[@]}"; do
230+
if [ -d "$CONDA_PATH" ]; then
231+
CONDA_EXECUTABLE="$CONDA_PATH/bin/conda"
232+
CONDA_SETUP_SCRIPT="$CONDA_PATH/etc/profile.d/conda.sh"
233+
CONDA_FOUND=true
234+
break
235+
fi
236+
done
223237

238+
if [ "$CONDA_FOUND" = false ]; then
239+
log "ERROR" "Conda not found. Please install Conda or check your Conda paths."
240+
exit 1
241+
fi
242+
echo "Conda executable: $CONDA_EXECUTABLE"
243+
}
224244

225245
# Function to add a cron job with error handling
226246
add_cron_job() {
@@ -376,22 +396,29 @@ install_executable() {
376396
fi
377397
}
378398

379-
# remove previous installation of cron jobs and SystemGuard
380-
remove_previous_installation() {
399+
# remove extract directory, break below functions
400+
# if the directory is not present
401+
remove_extract_dir() {
381402
if [ -d "$EXTRACT_DIR" ]; then
382403
rm -rf "$EXTRACT_DIR"
383404
log "Old installation removed."
384405
else
385406
log "No previous installation found."
386407
fi
408+
}
387409

410+
remove_cronjob () {
388411
if $crontab_cmd -l | grep -q "$CRON_PATTERN"; then
389412
$crontab_cmd -l | grep -v "$CRON_PATTERN" | $crontab_cmd -
390413
log "Old cron jobs removed."
391414
else
392415
log "No previous cron jobs found."
393416
fi
394-
417+
}
418+
# remove previous installation of cron jobs and SystemGuard
419+
remove_previous_installation() {
420+
remove_extract_dir
421+
remove_cronjob
395422
}
396423

397424
# Function to fetch the latest version of SystemGuard from GitHub releases
@@ -477,6 +504,7 @@ install_from_git() {
477504
set_auto_update
478505

479506
log "Cloning the $APP_NAME repository from GitHub..."
507+
create_and_own_dir "$GIT_INSTALL_DIR"
480508
if ! git clone $FULL_GIT_URL "$GIT_INSTALL_DIR"; then
481509
log "ERROR" "Failed to clone the repository. Please check your internet connection and the branch name, and try again."
482510
exit 1
@@ -545,6 +573,9 @@ display_credentials() {
545573
# Install function
546574
install() {
547575
log "Starting installation of $APP_NAME..."
576+
check_conda # check if conda is installed
577+
install_conda_env # if conda is installed then install the conda environment
578+
create_and_own_dir "$EXTRACT_DIR"
548579
echo ""
549580
echo "Do you want to install from a Git repository or a specific release?"
550581
echo "|----------------------------------------------------|"
@@ -575,7 +606,6 @@ install() {
575606
uninstall() {
576607
log "Uninstalling $APP_NAME..."
577608
remove_previous_installation
578-
579609
stop_server
580610
generate_ascii_art "SystemGuard Uninstalled" "red"
581611
}
@@ -695,31 +725,11 @@ show_installer_logs() {
695725
fi
696726
}
697727

698-
699728
update_dependencies() {
700-
# Define possible Conda installation paths
701-
CONDA_PATHS=("/home/$USER_NAME/miniconda3" "/home/$USER_NAME/anaconda3")
702-
CONDA_FOUND=false
703-
704-
# Find Conda installation
705-
for CONDA_PATH in "${CONDA_PATHS[@]}"; do
706-
if [ -d "$CONDA_PATH" ]; then
707-
CONDA_EXECUTABLE="$CONDA_PATH/bin/conda"
708-
CONDA_SETUP_SCRIPT="$CONDA_PATH/etc/profile.d/conda.sh"
709-
CONDA_FOUND=true
710-
break
711-
fi
712-
done
713-
714-
if [ "$CONDA_FOUND" = false ]; then
715-
log "ERROR" "Conda not found. Please install Conda or check your Conda paths."
716-
exit 1
717-
fi
718-
719729
# Activate Conda environment
720730
source "$CONDA_SETUP_SCRIPT"
721731
conda activate "$CONDA_ENV_NAME" || { log "ERROR" "Failed to activate Conda environment $CONDA_ENV_NAME"; exit 1; }
722-
732+
723733
# Check for missing dependencies
724734
log "INFO" "Checking for missing dependencies..."
725735
cd $EXTRACT_DIR/$APP_NAME-*/ || { log "ERROR" "Failed to change directory to $EXTRACT_DIR/$APP_NAME-*/"; exit 1; }
@@ -731,10 +741,6 @@ update_dependencies() {
731741
fi
732742

733743
log "INFO" "Installing dependencies from $REQUIREMENTS_FILE..."
734-
echo "--------------------------------------------------------"
735-
cat "$REQUIREMENTS_FILE"
736-
echo ""
737-
echo "--------------------------------------------------------"
738744

739745
# Install dependencies silently
740746
sudo -u "$SUDO_USER" bash -c "source $CONDA_SETUP_SCRIPT && conda activate $CONDA_ENV_NAME && pip install -r $EXTRACT_DIR/$APP_NAME-*/$REQUIREMENTS_FILE" > /dev/null 2>&1 || {
@@ -745,6 +751,20 @@ update_dependencies() {
745751
log "INFO" "Dependencies installation complete."
746752
}
747753

754+
# Function to install Conda environment and dependencies
755+
install_conda_env() {
756+
log "Checking conda environment $CONDA_ENV_NAME..."
757+
if ! "$CONDA_EXECUTABLE" env list | grep -q "$CONDA_ENV_NAME"; then
758+
log "Creating Conda environment $CONDA_ENV_NAME..."
759+
"$CONDA_EXECUTABLE" create -n "$CONDA_ENV_NAME" python=3.8 -y || { log "ERROR" "Failed to create Conda environment $CONDA_ENV_NAME"; exit 1; }
760+
# install the dependencies
761+
update_dependencies
762+
else
763+
log "Conda environment $CONDA_ENV_NAME already exists."
764+
update_dependencies
765+
fi
766+
}
767+
748768

749769
# stop flask server
750770
stop_server_helper() {
@@ -768,6 +788,20 @@ fix() {
768788
stop_server
769789
}
770790

791+
# update the code to the latest version
792+
install_latest() {
793+
cd $EXTRACT_DIR/$APP_NAME-*/
794+
# check if the .git directory exists
795+
if [ -d ".git" ]; then
796+
log "Updating the code to the latest version..."
797+
git pull
798+
log "Code updated successfully."
799+
else
800+
log "Probably you have installed the code from the release, so you can't update the code."
801+
log "Please install the code from the git repository to update the code."
802+
fi
803+
}
804+
771805
# Display help
772806
show_help() {
773807
echo "$APP_NAME Installer"
@@ -810,9 +844,15 @@ show_help() {
810844
echo " --server-stop Stop the $APP_NAME server."
811845
echo " This will stop the running server instance."
812846
echo ""
813-
echo " --update-dependencies"
814-
echo " Update the dependencies of the $APP_NAME."
815-
echo " This will update the dependencies of the application."
847+
echo " --install-latest Update the code to the latest version."
848+
echo " This will pull the latest code from the Git repository."
849+
echo ""
850+
echo ""
851+
echo " --check-conda Check if Conda is installed and available."
852+
echo " This will verify the presence of Conda and display the installation path."
853+
echo ""
854+
echo " --update-dependencies Update the dependencies for $APP_NAME."
855+
echo " This will install any missing dependencies required for the application."
816856
echo ""
817857
echo " --help Display this help message."
818858
echo " Shows information about all available options and how to use them."
@@ -834,6 +874,8 @@ for arg in "$@"; do
834874
--server-stop) stop_server; exit 0 ;;
835875
--fix) fix; exit 0 ;;
836876
--update-dependencies) update_dependencies; exit 0 ;;
877+
--check-conda) check_conda; exit 0 ;;
878+
--install-latest) ACTION="install_latest" ;;
837879
--help) show_help; exit 0 ;;
838880
*) echo "Unknown option: $arg"; show_help; exit 1 ;;
839881
esac
@@ -845,14 +887,19 @@ case $ACTION in
845887
uninstall) uninstall ;;
846888
restore) restore ;;
847889
load_test) load_test ;;
848-
install_latest) install_latest ;;
849890
check_status) check_status ;;
850891
health_check) health_check ;;
851892
cleanup_backups) cleanup_backups ;;
852893
stop_server) stop_server ;;
853894
logs) show_server_logs ;;
854895
installation-logs) show_installer_logs ;;
855896
fix) fix ;;
897+
check-conda) check_conda ;;
898+
install_latest) install_latest ;;
856899
update_dependencies) update_dependencies ;;
857900
*) echo "No action specified. Use --help for usage information." ;;
858901
esac
902+
903+
904+
# check conda is there or not in fix
905+
# argument to update manually

src/scripts/dashboard.sh

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,9 @@ fi
9595
# Initialize Conda
9696
source "$CONDA_SETUP_SCRIPT"
9797

98-
# Check if the Conda environment exists and create it if not
99-
if ! conda info --envs | awk '{print $1}' | grep -q "^$CONDA_ENV_NAME$"; then
100-
log_message "Conda environment '$CONDA_ENV_NAME' not found. Creating it..."
101-
conda create -n "$CONDA_ENV_NAME" python=3.10 -y
102-
103-
log_message "Activating Conda environment '$CONDA_ENV_NAME' and installing requirements."
104-
conda run -n "$CONDA_ENV_NAME" pip install -r "$REQUIREMENTS_FILE"
105-
else
106-
log_message "Activating existing Conda environment '$CONDA_ENV_NAME'."
107-
fi
108-
10998
# Export Flask environment variables
11099
export FLASK_APP="$FLASK_APP_PATH"
111-
export FLASK_ENV=development # or production
100+
export FLASK_ENV=production
112101

113102
fetch_latest_changes() {
114103
local project_dir="$1"

0 commit comments

Comments
 (0)