@@ -62,154 +62,159 @@ CONDA_ENV_NAME=$APP_NAME
62
62
GIT_REMOTE_URL=" https://github.com/codeperfectplus/SystemDashboard" # Set this if you want to add a remote
63
63
ENV_FILE=" /home/$( whoami) /.bashrc"
64
64
65
- # Function to fetch the value of an environment variable from a file
66
- fetch_env_variable () {
67
- var_name=$1 # The name of the environment variable to fetch
68
- # Check if the environment file exists
69
- if [ ! -f " $ENV_FILE " ]; then
70
- echo " Error: Environment file '$ENV_FILE ' not found."
71
- return 1
72
- fi
73
-
74
- # Fetch the value of the environment variable
75
- var_value=$( grep -E " ^${var_name} =" " $ENV_FILE " | sed -E " s/^${var_name} =(.*)/\1/" )
76
-
77
- # Check if the variable was found and has a value
78
- if [ -z " $var_value " ]; then
79
- echo " Error: Variable '$var_name ' not found in '$ENV_FILE '."
80
- return 1
81
- fi
82
-
83
- # Print the value of the environment variable
84
- echo " $var_value "
85
- }
86
-
87
- auto_update=$( fetch_env_variable " sg_auto_update" )
88
- # Fetch from bashrc for auto-update
89
- echo " Auto update for $APP_NAME is set to $auto_update "
90
-
91
- # Ensure log directory exists
92
- LOG_DIR=" $( dirname " $LOG_FILE " ) "
93
- mkdir -p " $LOG_DIR "
94
-
95
- # Check for Miniconda3 and Anaconda3
96
- CONDA_PATHS=(" /home/$USERNAME /miniconda3" " /home/$USERNAME /anaconda3" )
97
- CONDA_FOUND=false
98
-
99
- for CONDA_PATH in " ${CONDA_PATHS[@]} " ; do
100
- if [ -d " $CONDA_PATH " ]; then
101
- CONDA_FOUND=true
102
- CONDA_EXECUTABLE=" $CONDA_PATH /bin/conda"
103
- CONDA_SETUP_SCRIPT=" $CONDA_PATH /etc/profile.d/conda.sh"
104
- break
105
- fi
106
- done
107
-
108
- if [ " $CONDA_FOUND " = false ]; then
109
- log_message " Neither Miniconda3 nor Anaconda3 found. Ensure Conda is installed."
110
- exit 1
111
- fi
112
-
113
- # Check if Conda setup script exists
114
- if [ ! -f " $CONDA_SETUP_SCRIPT " ]; then
115
- log_message " Conda setup script not found at $CONDA_SETUP_SCRIPT ."
116
- exit 1
117
- fi
118
-
119
- # Initialize Conda
120
- source " $CONDA_SETUP_SCRIPT "
121
-
122
- # Check if the Conda environment exists and create it if not
123
- if ! conda info --envs | awk ' {print $1}' | grep -q " ^$CONDA_ENV_NAME $" ; then
124
- log_message " Conda environment '$CONDA_ENV_NAME ' not found. Creating it..."
125
- conda create -n " $CONDA_ENV_NAME " python=3.10 -y
126
-
127
- log_message " Activating Conda environment '$CONDA_ENV_NAME ' and installing requirements."
128
- conda run -n " $CONDA_ENV_NAME " pip install -r " $REQUIREMENTS_FILE "
129
- else
130
- log_message " Activating existing Conda environment '$CONDA_ENV_NAME '."
131
- fi
132
-
133
65
# Export Flask environment variables
134
66
export FLASK_APP=" $FLASK_APP_PATH "
135
67
export FLASK_ENV=production
136
-
137
- fetch_latest_changes () {
138
- local project_dir=" $1 "
139
- local git_remote_url=" ${2-} " # Optional remote URL if needed
68
+ export FLASK_RUN_PORT=" $FLASK_PORT "
69
+ export FLASK_RUN_HOST=$( hostname -I | cut -d' ' -f1)
70
+
71
+ echo " Flask app path: $FLASK_APP "
72
+ echo " Flask environment: $FLASK_ENV "
73
+ echo " Flask run host: $FLASK_RUN_HOST "
74
+ echo " Flask run port: $FLASK_RUN_PORT "
75
+
76
+ # # Function to fetch the value of an environment variable from a file
77
+ # fetch_env_variable() {
78
+ # var_name=$1 # The name of the environment variable to fetch
79
+ # # Check if the environment file exists
80
+ # if [ ! -f "$ENV_FILE" ]; then
81
+ # echo "Error: Environment file '$ENV_FILE' not found."
82
+ # return 1
83
+ # fi
84
+
85
+ # # Fetch the value of the environment variable
86
+ # var_value=$(grep -E "^${var_name}=" "$ENV_FILE" | sed -E "s/^${var_name}=(.*)/\1/")
87
+
88
+ # # Check if the variable was found and has a value
89
+ # if [ -z "$var_value" ]; then
90
+ # echo "Error: Variable '$var_name' not found in '$ENV_FILE'."
91
+ # return 1
92
+ # fi
93
+
94
+ # # Print the value of the environment variable
95
+ # echo "$var_value"
96
+ # }
97
+
98
+ # auto_update=$(fetch_env_variable "sg_auto_update")
99
+ # # Fetch from bashrc for auto-update
100
+ # echo "Auto update for $APP_NAME is set to $auto_update"
101
+
102
+ # # Ensure log directory exists
103
+ # LOG_DIR="$(dirname "$LOG_FILE")"
104
+ # mkdir -p "$LOG_DIR"
105
+
106
+ # # Check for Miniconda3 and Anaconda3
107
+ # CONDA_PATHS=("/home/$USERNAME/miniconda3" "/home/$USERNAME/anaconda3")
108
+ # CONDA_FOUND=false
109
+
110
+ # for CONDA_PATH in "${CONDA_PATHS[@]}"; do
111
+ # if [ -d "$CONDA_PATH" ]; then
112
+ # CONDA_FOUND=true
113
+ # CONDA_EXECUTABLE="$CONDA_PATH/bin/conda"
114
+ # CONDA_SETUP_SCRIPT="$CONDA_PATH/etc/profile.d/conda.sh"
115
+ # source "$CONDA_SETUP_SCRIPT" &> /dev/null
116
+ # break
117
+ # fi
118
+ # done
119
+
120
+ # if [ "$CONDA_FOUND" = false ]; then
121
+ # log_message "Neither Miniconda3 nor Anaconda3 found. Ensure Conda is installed."
122
+ # exit 1
123
+ # fi
124
+
125
+ # # Check if Conda setup script exists
126
+ # if [ ! -f "$CONDA_SETUP_SCRIPT" ]; then
127
+ # log_message "Conda setup script not found at $CONDA_SETUP_SCRIPT."
128
+ # exit 1
129
+ # fi
130
+
131
+ # # Check if the Conda environment exists and create it if not
132
+ # if ! conda info --envs | awk '{print $1}' | grep -q "^$CONDA_ENV_NAME$"; then
133
+ # log_message "Conda environment '$CONDA_ENV_NAME' not found. Creating it..."
134
+ # conda create -n "$CONDA_ENV_NAME" python=3.10 -y
135
+
136
+ # log_message "Activating Conda environment '$CONDA_ENV_NAME' and installing requirements."
137
+ # conda run -n "$CONDA_ENV_NAME" pip install -r "$REQUIREMENTS_FILE"
138
+ # else
139
+ # log_message "Activating existing Conda environment '$CONDA_ENV_NAME'."
140
+ # fi
141
+
142
+ # fetch_latest_changes() {
143
+ # local project_dir="$1"
144
+ # local git_remote_url="${2-}" # Optional remote URL if needed
140
145
141
- # Check if the project directory is set and exists
142
- if [[ -z " $project_dir " || ! -d " $project_dir " ]]; then
143
- log_message " ERROR" " Invalid project directory specified."
144
- return 1
145
- fi
146
+ # # Check if the project directory is set and exists
147
+ # if [[ -z "$project_dir" || ! -d "$project_dir" ]]; then
148
+ # log_message "ERROR" "Invalid project directory specified."
149
+ # return 1
150
+ # fi
146
151
147
- # Check if Git is installed
148
- if ! command -v git & > /dev/null; then
149
- log_message " ERROR" " Git is not installed. Please install Git and try again."
150
- return 1
151
- fi
152
-
153
- # Check if the directory is a Git repository
154
- if [ -d " $project_dir /.git" ]; then
155
- log_message " INFO" " Repository found at $project_dir . Checking the current branch and for latest changes..."
152
+ # # Check if Git is installed
153
+ # if ! command -v git &> /dev/null; then
154
+ # log_message "ERROR" "Git is not installed. Please install Git and try again."
155
+ # return 1
156
+ # fi
157
+
158
+ # # Check if the directory is a Git repository
159
+ # if [ -d "$project_dir/.git" ]; then
160
+ # log_message "INFO" "Repository found at $project_dir. Checking the current branch and for latest changes..."
156
161
157
- # Navigate to the project directory
158
- pushd " $project_dir " > /dev/null
162
+ # # Navigate to the project directory
163
+ # pushd "$project_dir" > /dev/null
159
164
160
- # Get the current branch name
161
- branch=$( git symbolic-ref --short HEAD 2> /dev/null)
162
- log_message " INFO" " Current branch is '$branch '."
165
+ # # Get the current branch name
166
+ # branch=$(git symbolic-ref --short HEAD 2>/dev/null)
167
+ # log_message "INFO" "Current branch is '$branch'."
163
168
164
- # Check if there are untracked files
165
- if git status --porcelain | grep ' ^[?]' ; then
166
- # Check if the repository has any commits
167
- if [ $( git rev-list --count HEAD 2> /dev/null) -gt 0 ]; then
168
- # Repository has commits, proceed with stashing
169
- log_message " INFO" " Stashing untracked files..."
170
- if git stash -u; then
171
- log_message " INFO" " Untracked files stashed successfully."
172
- stash_applied=true
173
- else
174
- log_message " ERROR" " Failed to stash untracked files."
175
- popd > /dev/null
176
- return 1
177
- fi
178
- else
179
- log_message " ERROR" " Repository does not have any commits. Cannot stash untracked files."
180
- log_message " INFO" " Manual intervention required to handle untracked files."
181
- popd > /dev/null
182
- return 1
183
- fi
184
- fi
185
-
186
- if git pull origin " $branch " ; then
187
- log_message " INFO" " Successfully pulled the latest changes from branch '$branch '."
188
- else
189
- log_message " ERROR" " Failed to pull the latest changes. Check your network connection or repository settings."
190
- popd > /dev/null
191
- return 1
192
- fi
193
-
194
- # Apply stashed changes if any
195
- if [ " $stash_applied " = true ]; then
196
- log_message " INFO" " Applying stashed changes..."
197
- git stash pop
198
- fi
199
-
200
- # Return to the original directory
201
- popd > /dev/null
202
- fi
203
- }
204
-
205
- # Check if Flask app is running
206
- if ! pgrep -f " flask run --host=0.0.0.0 --port=$FLASK_PORT " > /dev/null; then
207
- log_message " Flask app is not running. Checking repository and starting it..."
208
- [ " $auto_update " = true ] &&
209
- fetch_latest_changes $PROJECT_DIR $GIT_REMOTE_URL
210
- log_message " Starting Flask app..."
211
- # Ensure environment activation and `flask` command
212
- bash -c " source $CONDA_SETUP_SCRIPT && conda activate $CONDA_ENV_NAME && flask run --host=0.0.0.0 --port=$FLASK_PORT " & >> " $LOG_FILE " &
213
- else
214
- log_message " Flask app is already running."
215
- fi
169
+ # # Check if there are untracked files
170
+ # if git status --porcelain | grep '^[?]'; then
171
+ # # Check if the repository has any commits
172
+ # if [ $(git rev-list --count HEAD 2>/dev/null) -gt 0 ]; then
173
+ # # Repository has commits, proceed with stashing
174
+ # log_message "INFO" "Stashing untracked files..."
175
+ # if git stash -u; then
176
+ # log_message "INFO" "Untracked files stashed successfully."
177
+ # stash_applied=true
178
+ # else
179
+ # log_message "ERROR" "Failed to stash untracked files."
180
+ # popd > /dev/null
181
+ # return 1
182
+ # fi
183
+ # else
184
+ # log_message "ERROR" "Repository does not have any commits. Cannot stash untracked files."
185
+ # log_message "INFO" "Manual intervention required to handle untracked files."
186
+ # popd > /dev/null
187
+ # return 1
188
+ # fi
189
+ # fi
190
+
191
+ # if git pull origin "$branch"; then
192
+ # log_message "INFO" "Successfully pulled the latest changes from branch '$branch'."
193
+ # else
194
+ # log_message "ERROR" "Failed to pull the latest changes. Check your network connection or repository settings."
195
+ # popd > /dev/null
196
+ # return 1
197
+ # fi
198
+
199
+ # # Apply stashed changes if any
200
+ # if [ "$stash_applied" = true ]; then
201
+ # log_message "INFO" "Applying stashed changes..."
202
+ # git stash pop
203
+ # fi
204
+
205
+ # # Return to the original directory
206
+ # popd > /dev/null
207
+ # fi
208
+ # }
209
+
210
+ # # Check if Flask app is running
211
+ # if ! pgrep -f "flask run --host=0.0.0.0 --port=$FLASK_PORT" > /dev/null; then
212
+ # log_message "Flask app is not running. Checking repository and starting it..."
213
+ # [ "$auto_update" = true ] &&
214
+ # fetch_latest_changes $PROJECT_DIR $GIT_REMOTE_URL
215
+ # log_message "Starting Flask app..."
216
+ # # Ensure environment activation and `flask` command
217
+ # bash -c "source $CONDA_SETUP_SCRIPT && conda activate $CONDA_ENV_NAME && flask run --host=0.0.0.0 --port=$FLASK_PORT" &>> "$LOG_FILE" &
218
+ # else
219
+ # log_message "Flask app is already running."
220
+ # fi
0 commit comments