Skip to content

Instantly share code, notes, and snippets.

@stevensdotb
Last active June 25, 2019 00:37
Show Gist options
  • Save stevensdotb/de2f84b3d7ab968e488caae7c7278901 to your computer and use it in GitHub Desktop.
Save stevensdotb/de2f84b3d7ab968e488caae7c7278901 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
###########################################
# Author: Stevens Brito
# Github: stevensdotb
# Email: stevensbrito.tech@gmail.com
###########################################
# Show usage help
function usage() {
echo "Usage: $(basename $0) [option [argument]] [runserver|runsslserver]" >&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 " -m Make migrations of model changes to the database"
echo " -d <database-service> Start the database service"
echo
echo "Positional Arguments:"
echo " runserver Run the Server"
echo " runsslserver Run the SSL Server"
echo
echo "Examples:"
echo " Activate the virtualenv ./django_local.sh -e venv/path"
echo " Start database service ./django_local.sh -d mysql "
echo " Install all packages from requirements.txt ./django_local.sh -r"
echo " Apply makemigrations and migrate: ./django_local.sh -m"
echo " Git pull to update the project: ./django_local.sh -e venv/path -u"
echo " Git pull, makemigrations and migrate ./django_local.sh -e venv/path -um"
echo
echo " All together: ./django_local.sh -e venv/path -um -d mysql run"
echo
echo "Notes:"
echo " 1. To run the server after use flag options use the positional argument 'runserver' or 'runsslserver at the end"
echo " e.g: ./django_local.sh -e venv/path -d mysql runserver"
echo
echo " Keep in mind that you can not run the server if you do not have Django 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: ./django_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 " - ./django_local.sh -e ../venv/ -u [ Yes ]"
echo " - ./django_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
echo
}
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() {
# install packages from requirements.txt by default
local pkg=${1:--r requirements.txt}
echo
read -p "Apply 'pip install $pkg'? [y/n]: " -n 1 answer
if [[ "$answer" =~ [Yy] ]];
then
echo " [Installing]..."
pip install ${pkg}
if [[ $1 != "" ]];
then # If new packages update the requirements.txt file
echo "Updating the requirements.txt file"
pip freeze > requirements.txt
fi;
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) # Install requirements.txt packages
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
;;
m) # makemigrations and migrate changes to the database
./manage.py makemigrations
./manage.py migrate
;;
:)
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}" == "runserver" ]];
then
./manage.py runserver
fi;
if [[ "${@:$OPTIND}" == "runsslserver" ]];
then
if [[ "$(cat requirements.txt | grep django-sslserver | wc -l)" -eq "0" ]];
then
echo "Django SSL Server is not in the requirements.txt file. Make sure that it is installed"
echo "on your server or virtualenv. In case it is not installed, proceed to apply a pip installation."
install_new_packages django-sslserver
fi;
./manage.py runsslserver
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment