Created
July 13, 2021 17:24
-
-
Save bhujelaayushgc/0008be612003da8bab8bfc8b4ded6af6 to your computer and use it in GitHub Desktop.
Scripting Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: | |
# $ bin/restart | |
# Remember to be on the root of the project. | |
# Script does not take any arguments. | |
# This script makes most of its decision based on the ENVIRONMENT value in .env. | |
# This is important for deploying correct branch as well as starting correct celery worker. | |
main() { | |
echo "Welcome!" | |
echo "This script will kill and re-run Flask and Celery Workers." | |
echo | |
echo "-----------------------------------------------------" | |
# Checking if .env exists | |
if [ ! -f .env ]; then | |
echo "dotenv (.env) file not found. EXITING!" | |
exit 1 | |
fi | |
environment="$(awk -F= -v key="ENVIRONMENT" '$1==key {print $2}' .env)" | |
if [ -z "$environment" ]; then | |
echo "ENVIRONMENT not set in .env" | |
exit 1 | |
fi | |
if [[ "$environment" != "staging" && "$environment" != "production" ]]; then | |
echo "Invalid ENVIRONMENT setup." | |
exit 1 | |
fi | |
confirm_prerequisites "$environment" | |
read -p "Do you want to proceed with deployment process? [y/N] " -n 1 -r | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
echo | |
echo "You chose not to proceed! Exiting!" | |
exit 1 | |
fi | |
update_codes | |
flask_string="flask run --host 0.0.0.0" | |
celery_string="app.celery_app:app" | |
echo "-----------------------------------------------------" | |
echo "!!! Kill process initiated !!!" | |
# Killing existing processes... | |
kill_processes "$flask_string" # Flask | |
kill_processes "$celery_string" # Celery Worker | |
restart_server "$environment" | |
echo | |
echo "Script Execution Complete! Bye! Bye!" | |
} | |
update_codes() { | |
echo; echo; | |
echo "-----------------------------------------------------" | |
branch=$(git branch --show-current) | |
echo "Do you want to pull the code ('git pull origin ${branch}')" | |
PS3='Please enter your choice: ' | |
options=("Pull Codes" "Already Pulled (Continue)" "Manual Pull (Quit)") | |
select opt in "${options[@]}" | |
do | |
case $opt in | |
"Pull Codes") | |
echo "Pulling codes!" | |
git pull origin "$branch" | |
break | |
;; | |
"Already Pulled (Continue)") | |
echo "Assuming you pulled codes manually." | |
break | |
;; | |
"Manual Pull (Quit)") | |
echo "You chose to exit!" | |
exit 0 | |
break | |
;; | |
*) echo "Invalid option $REPLY";; | |
esac | |
done | |
echo | |
} | |
confirm_prerequisites() { | |
echo "IMPORTANT: Make sure of following things: " | |
echo "1. Codes are updated." | |
echo "2. You are in correct branch." | |
echo "3. You have activated correct virtualenv." | |
echo "4. You are inside the Project's root directory." | |
echo "-----------------------------------------------------" | |
echo | |
git_branch=$(git branch --show-current) | |
invalid_branch=false | |
virtualenv_inactive=false | |
if [[ $1 == "staging" && $git_branch != "master" || $1 == "production" && $git_branch != "production" ]]; then | |
invalid_branch=true | |
fi | |
if [ -z "$VIRTUAL_ENV" ]; then | |
virtualenv_inactive=true | |
fi | |
echo "Current Environment:" | |
echo "Environment :${1}" | |
echo "Git branch :${git_branch}" | |
echo "Python Virtual ENV :$VIRTUAL_ENV" | |
echo "-----------------------------------------------------" | |
errors="ERRORS!!!" | |
if [ $invalid_branch = true ]; then | |
errors="${errors} | |
⚠ Seems like the environment does not match with current git branch! | |
Correct combination is: [staging & master] | [production & production]." | |
fi | |
if [ $virtualenv_inactive = true ]; then | |
errors="${errors} | |
⚠ Seems like the Virtual Environment has not been activated. | |
Activate the respective python virtual environment." | |
fi | |
if [[ $invalid_branch = true || $virtualenv_inactive = true ]]; then | |
echo | |
echo "⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠" | |
echo "$errors" | |
echo "⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠" | |
echo | |
echo "Some prerequisites did not meet! EXITING!" | |
exit 1 | |
fi | |
} | |
kill_processes() { | |
search_string=$1 | |
echo "Killing: ${search_string}" | |
process_ids=($(ps -ef | grep "${search_string}" | grep -v "grep" | awk '{print $2}')) | |
echo $process_ids | |
for pid in "${process_ids[@]}" | |
do | |
kill -9 "$pid" | |
echo "Process (${pid}) Killed!" | |
done | |
echo "Killing Complete!" | |
echo "-----------------------------------------------------" | |
} | |
restart_server() { | |
echo | |
echo "Restart the Server!" | |
read -p "Do you want to restart the server and celery? [y/N] " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "Starting: Server" | |
echo "nohup flask run --host 0.0.0.0 &" | |
nohup flask run --host 0.0.0.0 & | |
echo "---------------------------------" | |
echo "Starting: Celery Worker" | |
echo "nohup celery -A app.celery_app:app worker -n $USER-$1-worker --queues=default --pool=gevent --loglevel=INFO --concurrency=1 --max-tasks-per-child=1 > default.out 2> default.err &" | |
nohup celery -A app.celery_app:app worker -n "$USER-$1-worker" --queues=default --pool=gevent --loglevel=INFO --concurrency=1 --max-tasks-per-child=1 > default.out 2> default.err & | |
echo "---------------------------------" | |
fi | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment