Skip to content

Instantly share code, notes, and snippets.

@marquicus
Created September 14, 2019 13:17
Show Gist options
  • Save marquicus/f1fc66dcad9194ff527ee4da9fa9b71f to your computer and use it in GitHub Desktop.
Save marquicus/f1fc66dcad9194ff527ee4da9fa9b71f to your computer and use it in GitHub Desktop.
Herramienta de gestión de proyectos para python django en cumplimiento con12factor.net
#!/bin/bash
#
##
## Vendor: Zentek ST
##
DIRNAME=$(dirname $0)
APPMAIL="[email protected]"
APPVERSION="2.0"
CLR='\033[0;31m'
NCLR='\033[0m'
PROJECT="ngateway"
SLUG=ngw
## Built in functions ##
usage() {
cat <<EOF
Utilidad para habilitar entornos de ejecucion, v$APPVERSION"
Usage:
source ${0##*/} [help|dev|qa|uat]
Parameters:
Muestra el entorno activado y este menu de ayuda
help Muestra este menu de ayuda
dev Activa entorno desarrollo
qa Activa entorno testing
uat Activa entorno produccion
migrations Ejecuta migrations (genera schema desde 0)
fixtures Carga fixtures (carga datos al schema)
taskqueue Levanta componentes para tareas asincronas en background
deploy Corre las tareas migrations, fixtures y taskqueue
build Construye imagenes docker
Reportar errores a $APPMAIL
EOF
}
dev() {
if [ "$0" != "$BASH_SOURCE" ]; then
export DJANGO_SETTINGS_MODULE=${PROJECT}.settings.development
echo -e "\n\tAmbiente [${CLR}${DJANGO_SETTINGS_MODULE}${NCLR}] activado...\n\n"
echo "Levante el servicio con el siguiente comando:"
echo -e "\n\t./manage.py runserver"
else
echo "NO se ha activado el ambiente, ejecute:"
echo -e "\tsource env.sh dev"
fi
}
qa() {
if [ "$0" != "$BASH_SOURCE" ]; then
export DJANGO_SETTINGS_MODULE=${PROJECT}.settings.testing
echo -e "\n\tAmbiente [${CLR}${DJANGO_SETTINGS_MODULE}${NCLR}] activado...\n"
echo -e "\nLevantar componentes adicionales de modo NO PERSISTENTE [y|n]"
read -p "postgres:5432? " yn
case $yn in
[Yy]* )
[[ ! $(docker ps -a | grep "${SLUG}-postgres") ]] && docker run -d --rm --name ${SLUG}-postgres -p 5432:5432 -e POSTGRES_DB=${SLUG} -e POSTGRES_USER=${POSTGRES_USER} -e POSTGRES_PASSWORD=${POSTGRES_PASSWORD} postgres:9.6-alpine || echo "[${SLUG}-postgres] Ya existe un contenedor postgres"
;;
* )
;;
esac
read -p "redis:6379? " yn
case $yn in
[Yy]* )
[[ ! $(docker ps -a | grep "${SLUG}-redis") ]] && docker run -d --rm --name ${SLUG}-redis -p 6379:6379 redis:5-alpine || echo "[${SLUG}-redis] Ya existe un contenedor redis"
;;
* )
;;
esac
echo "Habilite el task queue con los siguientes comandos:"
echo -e "\n\tcelery -A ${PROJECT} worker -l info\n\tcelery -A ${PROJECT} beat -l info -S django\n"
echo "Levante el servicio con el siguiente comando:"
echo -e "\n\t./manage.py runserver"
else
echo "NO se ha activado el ambiente, ejecute:"
echo -e "\tsource env.sh qa"
fi
}
uat() {
if [ "$0" != "$BASH_SOURCE" ]; then
export DJANGO_SETTINGS_MODULE=${PROJECT}.settings.production
echo -e "\n\tAmbiente [${CLR}${DJANGO_SETTINGS_MODULE}${NCLR}] activado...\n"
echo -e "\nLevantar componentes adicionales de modo PERSISTENTE [y|n]"
read -p "postgres:5432? " yn
case $yn in
[Yy]* )
[[ ! $(docker ps -a | grep "${SLUG}-postgres") ]] && docker run -d --name ${SLUG}-postgres -p 5432:5432 -e POSTGRES_DB=${SLUG} -e POSTGRES_USER=${POSTGRES_USER} -e POSTGRES_PASSWORD=${POSTGRES_PASSWORD} postgres:9.6-alpine || echo "[${SLUG}-postgres] Ya existe un contenedor postgres"
;;
* )
;;
esac
read -p "redis:6379? " yn
case $yn in
[Yy]* )
[[ ! $(docker ps -a | grep "${SLUG}-redis") ]] && docker run -d --name ${SLUG}-redis -p 6379:6379 redis:5-alpine || echo "[${SLUG}-redis] Ya existe un contenedor redis"
;;
* )
;;
esac
echo "Habilite el taskqueue con los siguientes comandos:"
echo -e "\n\tcelery -A ${PROJECT} worker -l info\n\tcelery -A ${PROJECT} beat -l info -S django\n"
echo "Levante el servicio con el siguiente comando:"
echo -e "\n\tgunicorn --log-level debug ${PROJECT}.wsgi:application"
else
echo "NO se ha activado el ambiente, ejecute:"
echo -e "\tsource env.sh uat"
fi
}
migrations() {
if [ "$0" == "$BASH_SOURCE" ]; then
echo -e "\n\tReinicializando migrations con perfil [${CLR}${DJANGO_SETTINGS_MODULE}${NCLR}] ...\n"
find ${PROJECT} -path "*/migrations/*.py" -not -name "__init__.py" -delete
find ${PROJECT} -path "*/migrations/*.pyc" -delete
find ${PROJECT} -name __pycache__ -exec rm -rf {} \; 2> /dev/null
./manage.py makemigrations
if [[ "$DJANGO_SETTINGS_MODULE" = "${PROJECT}.settings.production" ]]; then
./manage.py migrate_schemas --shared
./manage.py initialize_tenants
else
./manage.py migrate
fi
else
echo "Ejecute:"
echo -e "\t./env.sh migrations"
fi
}
fixtures() {
if [ "$0" == "$BASH_SOURCE" ]; then
echo -e "\n\tCargando fixtures con perfil [${CLR}${DJANGO_SETTINGS_MODULE}${NCLR}] ...\n"
./manage.py loaddata fixtures/*.yaml
if [[ "$DJANGO_SETTINGS_MODULE" = "${PROJECT}.settings.testing" || "$DJANGO_SETTINGS_MODULE" = "${PROJECT}.settings.production" ]]; then
./manage.py collectmedia --noinput
./manage.py collectstatic -c --noinput
fi
else
echo "Ejecute:"
echo -e "\t./env.sh fixtures"
fi
}
taskqueue() {
if [[ "$0" == "$BASH_SOURCE" && -n "${DJANGO_SETTINGS_MODULE}" ]]; then
echo -e "\n\tLevantando taskqueue del proyecto con perfil [${CLR}${DJANGO_SETTINGS_MODULE}${NCLR}] ...\n"
if ! screen -ls | grep -q "worker"; then
screen -S worker -dm celery -A ${PROJECT} worker -l info
else
echo "Ya existe una sesion: worker"
fi
if ! screen -ls | grep -q "beat"; then
screen -S beat -dm celery -A ${PROJECT} beat -l info -S django
else
echo "Ya existe una sesion: beat"
fi
screen -ls
echo -e "\nPara acceder ejecute:\n\tscreen -x <screen>"
else
echo "Asegurese de tener activado un ambiente y posteriormente ejecute:"
echo -e "\t./env.sh taskqueue"
fi
}
build() {
if [ "$0" == "$BASH_SOURCE" ]; then
echo -e "\n\tGenerando contenedores\n"
docker login registry.gitlab.com
docker build -t registry.gitlab.com/zentekmx/ztst/${PROJECT} .
docker push registry.gitlab.com/zentekmx/ztst/${PROJECT}
else
echo "Ejecute:"
echo -e "\t./env.sh build"
fi
}
export SENTRY_DSN="$(grep SENTRY_DSN .env | cut -d= -f2)"
export SENTRY_ENVIRONMENT="uat"
export REDIS_HOST="localhost"
export DB_HOST="localhost"
export POSTGRES_USER="$(grep POSTGRES_USER .env | cut -d= -f2)"
export POSTGRES_PASSWORD="$(grep POSTGRES_PASSWORD .env | cut -d= -f2)"
export POSTGRES_DB="$(grep POSTGRES_DB .env | cut -d= -f2)"
export SENDGRID_API_KEY="$(grep SENDGRID_API_KEY .env | cut -d= -f2)"
export SECRET_KEY="$(grep SECRET_KEY .env | cut -d= -f2)"
if [ "$#" -ne "0" ]; then
case "${1}" in
help)
usage
;;
dev)
dev
;;
qa)
qa
;;
uat)
uat
;;
migrations)
migrations
;;
fixtures)
fixtures
;;
taskqueue)
taskqueue
;;
build)
build
;;
deploy)
migrations
fixtures
taskqueue
;;
*)
usage
;;
esac
else
[ -z "${DJANGO_SETTINGS_MODULE}" ] && DJANGO_SETTINGS_MODULE=${PROJECT}.settings.development
printf "AMBIENTE ACTIVO: ${CLR}${DJANGO_SETTINGS_MODULE#*=}${NCLR}\n\n"
usage
fi
unset dev
unset qa
unset uat
unset usage
unset CLR
unset NCLR
# End of file
# vim: set ts=2 sw=2 noet:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment