Last active
September 18, 2024 23:25
-
-
Save stevensdotb/359658b015c4bcc67962cd47a36ba895 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
########################################### | |
# Author: Stevens Brito | |
# Github: stevensdotb | |
########################################### | |
# Set environment variables | |
export FLASK_CONFIG="development" | |
export FLASK_DEBUG="True" | |
export FLASK_APP="run.py" | |
# Show usage help | |
function usage() { | |
echo "Usage: $(basename $0) [option [argument]] [run]" >&2 | |
echo | |
echo "Flag options:" | |
echo " -e <venv-path> Activate the virtualenv" | |
echo " -r Apply 'pip install' packages installation from requirement.txt file" | |
echo " -u Make a git pull to update your project with latest changes" | |
echo " -i Init alembic migration" | |
echo " -m Migrate and upgrade changes to the database" | |
echo " -d <database-service> Start the database service" | |
echo | |
echo "Positional Argument:" | |
echo " run Run the server" | |
echo | |
echo "Examples:" | |
echo " Activate the virtualenv ./flask_local.sh -e venv/path" | |
echo " Start database service ./flask_local.sh -d mysql " | |
echo " Install all packages from requirements.txt ./flask_local.sh -r" | |
echo " flask db init: ./flask_local.sh -i" | |
echo " flask db migrate and upgrade: ./flask_local.sh -m" | |
echo " flask db init, migrate and upgrade: ./flask_local.sh -im" | |
echo " Git pull to update the project: ./flask_local.sh -e venv/path -u" | |
echo " Git pull, flask db init, migrate and upgrade: ./flask_local.sh -e venv/path -uim" | |
echo | |
echo " All together: ./flask_local.sh -e venv/path -uim -d mysql run" | |
echo | |
echo "Notes:" | |
echo " 1. To run the server after use flag options use the positional argument 'run' at the end" | |
echo " e.g: ./flask_local.sh -e venv/path -d mysql run" | |
echo | |
echo " Keep in mind that you can not run the server if you do not have flask installed on your server" | |
echo " or on your virtualenv (user -e option to activate it)" | |
echo | |
echo " 2. -d flag argument can be use for any database service, such as mysql, postgres, mongodb, etc." | |
echo | |
echo " 3. To install requirements packages over the virtualenv, the -e option has to be specified." | |
echo " As the packages installation is executed when -u is used the -e option has to be specified as well" | |
echo " e.g: ./flask_local.sh -e ../venv/ -r" | |
echo | |
echo " 4. When you apply the -u option, the program asks for new packages installation, so, it is not recommended to" | |
echo " apply the -r option, since the program might ask for packages installation twice" | |
echo " e.g: " | |
echo " - ./flask_local.sh -e ../venv/ -u [ Yes ]" | |
echo " - ./flask_local.sh -e ../venv/ -ur [ No ]" | |
echo | |
exit 1 | |
} | |
# Start the db service | |
function start_db_service() { | |
if [ $1 != "" ]; | |
then | |
if [[ "$(service $1 status|grep running|wc -l)" -eq "0" ]] | |
then | |
echo " * Starting $1 service"; | |
service mysql start; | |
else | |
echo " * Service $1 already running"; | |
fi; | |
fi; | |
} | |
# Activate the virtualenv | |
function activate_venv() { | |
if [[ $1 =~ /$ ]]; | |
then | |
VPATH="$1bin/activate" | |
else | |
VPATH="$1/bin/activate" | |
fi; | |
echo " * Activate virtual environment $VPATH"; | |
source $VPATH | |
} | |
function update_project() { | |
git pull | |
echo | |
echo "[Install new packages]" | |
echo "You have applied an update for your project and maybe, there are new packages into" | |
echo "your requirements.txt file. If so, you can apply a 'pip install' to install them;" | |
echo "otherwise, you can skip this step." | |
install_new_packages | |
} | |
function install_new_packages() { | |
echo | |
read -p "Apply 'pip install -r requirements.txt' to install new packages? [y/n]: " -n 1 answer | |
if [[ "$answer" =~ [Yy] ]]; | |
then | |
echo " [Installing]..." | |
pip install -r ../requirements.txt | |
elif [[ "$answer" =~ [Nn] ]]; | |
then | |
echo " [Skiped]" | |
else | |
echo " [Invalid option]" | |
install_new_packages | |
fi; | |
echo | |
} | |
# Parameters | |
OPTIND=1; | |
while getopts "e:d:hurim" opt; | |
do | |
case $opt in | |
h) | |
usage | |
;; | |
e) # Path to activate the virtualenv | |
activate_venv $OPTARG | |
;; | |
r) # Path to activate the virtualenv | |
install_new_packages | |
;; | |
d) # Start the database service | |
start_db_service $OPTARG | |
;; | |
u) # Make a git pull to update your project with latest changes | |
update_project | |
;; | |
i) # Init alembic migrations | |
flask db init; | |
;; | |
m) # Migrate and upgrade changes to the database | |
flask db migrate; | |
flask db upgrade; | |
;; | |
:) | |
echo -e "\033[31mError\033[0m: option ${OPTARG} requires an argument" 1>&2 | |
usage | |
;; | |
\?) | |
echo | |
echo -e "\033[31mError\033[0m: Invalid option" 1>&2 | |
usage | |
;; | |
*) usage ;; | |
esac | |
done | |
# Run the app | |
if [[ "${@:$OPTIND}" == "run" ]]; | |
then | |
flask run | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment