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