Skip to content

Commit

Permalink
Shell linting (#374)
Browse files Browse the repository at this point in the history
* shellcheck lint

* update usage
  • Loading branch information
aahunter authored Feb 2, 2017
1 parent 26afd76 commit 60b9116
Showing 1 changed file with 123 additions and 86 deletions.
209 changes: 123 additions & 86 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,105 @@
# $1 host
# $2 port
function dockerwait {
while ! exec 6<>/dev/tcp/$1/$2; do
echo "$(date) - waiting to connect $1 $2"
while ! exec 6<>/dev/tcp/"$1"/"$2"; do
warn "$(date) - waiting to connect $1 $2"
sleep 5
done
echo "$(date) - connected to $1 $2"
success "$(date) - connected to $1 $2"

exec 6>&-
exec 6<&-
}


function info () {
printf "\r [\033[00;34mINFO\033[0m] %s\n" "$1"
}


function warn () {
printf "\r [\033[00;33mWARN\033[0m] %s\n" "$1"
}


function success () {
printf "\r\033[2K [\033[00;32m OK \033[0m] %s\n" "$1"
}


function fail () {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] %s\n" "$1"
echo ''
exit 1
}


# wait for services to become available
# this prevents race conditions using fig
function wait_for_services {
if [[ "$WAIT_FOR_DB" ]] ; then
dockerwait $DBSERVER $DBPORT
dockerwait "$DBSERVER" "$DBPORT"
fi
if [[ "$WAIT_FOR_CLINICAL_DB" ]] ; then
dockerwait $CLINICAL_DBSERVER $CLINICAL_DBPORT
dockerwait "$CLINICAL_DBSERVER" "$CLINICAL_DBPORT"
fi
if [[ "$WAIT_FOR_CACHE" ]] ; then
dockerwait $CACHESERVER $CACHEPORT
dockerwait "$CACHESERVER" "$CACHEPORT"
fi
if [[ "$WAIT_FOR_RUNSERVER" ]] ; then
dockerwait $RUNSERVER $RUNSERVERPORT
dockerwait "$RUNSERVER" "$RUNSERVERPORT"
fi
if [[ "$WAIT_FOR_MONGO" ]] ; then
dockerwait $MONGOSERVER $MONGOPORT
dockerwait "$MONGOSERVER" "$MONGOPORT"
fi
if [[ "$WAIT_FOR_HOST_PORT" ]]; then
dockerwait $DOCKER_ROUTE $WAIT_FOR_HOST_PORT
dockerwait "$DOCKER_ROUTE" "$WAIT_FOR_HOST_PORT"
fi
if [[ "$WAIT_FOR_UWSGI" ]] ; then
dockerwait $UWSGISERVER $UWSGIPORT
dockerwait "$UWSGISERVER" "$UWSGIPORT"
fi
}


function defaults {
: ${DBSERVER:="db"}
: ${DBPORT:="5432"}
: ${DBUSER:="webapp"}
: ${DBNAME:="${DBUSER}"}
: ${DBPASS:="${DBUSER}"}

: ${CLINICAL_DBSERVER:="clinicaldb"}
: ${CLINICAL_DBPORT:="5432"}
: ${CLINICAL_DBUSER:="webapp"}
: ${CLINICAL_DBNAME:="${CLINICAL_DBUSER}"}
: ${CLINICAL_DBPASS:="${CLINICAL_DBUSER}"}

: ${DOCKER_ROUTE:=$(/sbin/ip route|awk '/default/ { print $3 }')}

: ${UWSGISERVER:="uwsgi"}
: ${UWSGIPORT:="9000"}
: ${RUNSERVER:="runserver"}
: ${RUNSERVERPORT:="8000"}
: ${CACHESERVER:="cache"}
: ${CACHEPORT:="11211"}
: ${MEMCACHE:="${CACHESERVER}:${CACHEPORT}"}
: ${MONGOSERVER:="mongo"}
: ${MONGOPORT:="27017"}
: "${DBSERVER:=db}"
: "${DBPORT:=5432}"
: "${DBUSER:=webapp}"
: "${DBNAME:=${DBUSER}}"
: "${DBPASS:=${DBUSER}}"

: "${CLINICAL_DBSERVER:=clinicaldb}"
: "${CLINICAL_DBPORT:=5432}"
: "${CLINICAL_DBUSER:=webapp}"
: "${CLINICAL_DBNAME:=${CLINICAL_DBUSER}}"
: "${CLINICAL_DBPASS:=${CLINICAL_DBUSER}}"

: "${DOCKER_ROUTE:=$(/sbin/ip route|awk '/default/ { print $3 }')}"

: "${UWSGISERVER:=uwsgi}"
: "${UWSGIPORT:=9000}"
: "${UWSGI_OPTS:=/app/uwsgi/docker.ini}"
: "${RUNSERVER:=runserver}"
: "${RUNSERVERPORT:=8000}"
: "${RUNSERVER_CMD:=runserver}"
: "${CACHESERVER:=cache}"
: "${CACHEPORT:=11211}"
: "${MEMCACHE:=${CACHESERVER}:${CACHEPORT}}"
: "${MONGOSERVER:=mongo}"
: "${MONGOPORT:=27017}"

# variables to control where tests will look for the app (aloe via selenium hub)
: ${TEST_APP_SCHEME:="http"}
: ${TEST_APP_HOST:="runservertest"}
: ${TEST_APP_PORT:="8000"}
: ${TEST_APP_PATH:="/"}
: ${TEST_APP_URL:="${TEST_APP_SCHEME}://${TEST_APP_HOST}:${TEST_APP_PORT}${TEST_APP_PATH}"}
#: ${TEST_BROWSER:="chrome"}
: ${TEST_BROWSER:="firefox"}
: ${TEST_WAIT:="30"}
: ${TEST_SELENIUM_HUB:="http://hub:4444/wd/hub"}

: ${DJANGO_FIXTURES="none"}
: "${TEST_APP_SCHEME:=http}"
: "${TEST_APP_HOST:=runservertest}"
: "${TEST_APP_PORT:=8000}"
: "${TEST_APP_PATH:=/}"
: "${TEST_APP_URL:=${TEST_APP_SCHEME}://${TEST_APP_HOST}:${TEST_APP_PORT}${TEST_APP_PATH}}"
#: "${TEST_BROWSER:=chrome}"
: "${TEST_BROWSER:=firefox}"
: "${TEST_WAIT:=30}"
: "${TEST_SELENIUM_HUB:=http://hub:4444/wd/hub}"

: "${DJANGO_FIXTURES:=none}"

export DBSERVER DBPORT DBUSER DBNAME DBPASS MONGOSERVER MONGOPORT MEMCACHE DOCKER_ROUTE
export CLINICAL_DBSERVER CLINICAL_DBPORT CLINICAL_DBUSER CLINICAL_DBNAME CLINICAL_DBPASS
Expand All @@ -90,41 +114,57 @@ function defaults {


function _django_check_deploy {
echo "running check --deploy"
django-admin.py check --deploy --settings=${DJANGO_SETTINGS_MODULE} 2>&1 | tee ${LOG_DIRECTORY}/uwsgi-check.log
info "running check --deploy"
set -x
django-admin.py check --deploy --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-check.log
set +x
}


function _django_migrate {
echo "running migrate"
django-admin.py migrate --noinput --settings=${DJANGO_SETTINGS_MODULE} 2>&1 | tee ${LOG_DIRECTORY}/uwsgi-migrate.log
django-admin.py migrate --database=clinical --noinput --settings=${DJANGO_SETTINGS_MODULE} 2>&1 | tee ${LOG_DIRECTORY}/uwsgi-migrate-clinical.log
django-admin.py update_permissions --settings=${DJANGO_SETTINGS_MODULE} 2>&1 | tee ${LOG_DIRECTORY}/uwsgi-permissions.log
info "running migrate"
set -x
django-admin.py migrate --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-migrate.log
django-admin.py migrate --database=clinical --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-migrate-clinical.log
django-admin.py update_permissions --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-permissions.log
set +x
}


function _django_collectstatic {
echo "running collectstatic"
django-admin.py collectstatic --noinput --settings=${DJANGO_SETTINGS_MODULE} 2>&1 | tee ${LOG_DIRECTORY}/uwsgi-collectstatic.log
info "running collectstatic"
set -x
django-admin.py collectstatic --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-collectstatic.log
set +x
}


function _django_test_fixtures {
echo "loading test (iprestrict permissive) fixture"
info 'loading test (iprestrict permissive) fixture'
set -x
django-admin.py init iprestrict_permissive
django-admin.py reload_rules
set +x
}


function _django_dev_fixtures {
echo "loading DEV fixture"
info "loading DEV fixture"
set -x
django-admin.py init DEV
django-admin.py reload_rules
set +x
}


function _rdrf_import_grdr {
echo "importing grdr registry"
info "importing grdr registry"
set -x
django-admin.py import_registry --file=/app/grdr.yaml
set +x
}


function _django_fixtures {
if [ "${DJANGO_FIXTURES}" = 'test' ]; then
_django_test_fixtures
Expand All @@ -137,21 +177,24 @@ function _django_fixtures {


function _runserver() {
echo "RUNSERVER_OPTS is ${RUNSERVER_OPTS}"
: "${RUNSERVER_OPTS=${RUNSERVER_CMD} 0.0.0.0:${RUNSERVERPORT} --settings=${DJANGO_SETTINGS_MODULE}}"

_django_collectstatic
_django_migrate
_django_fixtures

echo "running runserver ..."
info "RUNSERVER_OPTS is ${RUNSERVER_OPTS}"
set -x
# shellcheck disable=SC2086
exec django-admin.py ${RUNSERVER_OPTS}
}


function _aloe() {
export DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}_test
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE}"_test
shift
exec django-admin.py harvest --with-xunit --xunit-file=${WRITABLE_DIRECTORY}/tests.xml --verbosity=3 "$@"
set -x
exec django-admin.py harvest --with-xunit --xunit-file="${WRITABLE_DIRECTORY}"/tests.xml --verbosity=3 "$@"
}


Expand All @@ -162,80 +205,74 @@ wait_for_services

# prod uwsgi entrypoint
if [ "$1" = 'uwsgi' ]; then
echo "[Run] Starting prod uwsgi"

: ${UWSGI_OPTS="/app/uwsgi/docker.ini"}
echo "UWSGI_OPTS is ${UWSGI_OPTS}"
info "[Run] Starting prod uwsgi"

_django_collectstatic
_django_migrate
_django_check_deploy

exec uwsgi --die-on-term --ini ${UWSGI_OPTS}
set -x
exec uwsgi --die-on-term --ini "${UWSGI_OPTS}"
fi

# local and test uwsgi entrypoint
if [ "$1" = 'uwsgi_local' ]; then
echo "[Run] Starting local uwsgi"

: ${UWSGI_OPTS="/app/uwsgi/docker.ini"}
echo "UWSGI_OPTS is ${UWSGI_OPTS}"
info "[Run] Starting local uwsgi"

_django_collectstatic
_django_migrate
_django_fixtures
_django_check_deploy

exec uwsgi --die-on-term --ini ${UWSGI_OPTS}
set -x
exec uwsgi --die-on-term --ini "${UWSGI_OPTS}"
fi

# runserver entrypoint
if [ "$1" = 'runserver' ]; then
echo "[Run] Starting runserver"

: ${RUNSERVER_OPTS="runserver 0.0.0.0:${RUNSERVERPORT} --settings=${DJANGO_SETTINGS_MODULE}"}
info "[Run] Starting runserver"
_runserver
fi

# runserver_plus entrypoint
if [ "$1" = 'runserver_plus' ]; then
echo "[Run] Starting runserver_plus"

: ${RUNSERVER_OPTS="runserver_plus 0.0.0.0:${RUNSERVERPORT} --settings=${DJANGO_SETTINGS_MODULE}"}
info "[Run] Starting runserver_plus"
RUNSERVER_CMD=runserver_plus
_runserver
fi

# grdr entrypoint
if [ "$1" = 'grdr' ]; then
echo "[Run] Starting runserver with GRDR data elements"

: ${RUNSERVER_OPTS="runserver_plus 0.0.0.0:${RUNSERVERPORT} --settings=${DJANGO_SETTINGS_MODULE}"}
echo "RUNSERVER_OPTS is ${RUNSERVER_OPTS}"
info "[Run] Starting runserver_plus with GRDR data elements"

_django_collectstatic
_django_migrate
_django_fixtures
_rdrf_import_grdr

echo "running runserver ..."
exec django-admin.py ${RUNSERVER_OPTS}
RUNSERVER_CMD=runserver_plus
_runserver
fi

# runtests entrypoint
if [ "$1" = 'runtests' ]; then
echo "[Run] Starting tests"
export DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}_test
info "[Run] Starting tests"
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE}"_test

set -x
exec django-admin.py test --noinput -v 3 rdrf
fi

# aloe entrypoint
if [ "$1" = 'aloe' ]; then
echo "[Run] Starting aloe"
info "[Run] Starting aloe"
cd /app/rdrf || exit
_aloe "$@"
fi

echo "[RUN]: Builtin command not provided [tarball|aloe|runtests|runserver|uwsgi|uwsgi_fixtures]"
echo "[RUN]: $@"
warn "[RUN]: Builtin command not provided [tarball|aloe|runtests|runserver|runserver_plus|uwsgi|uwsgi_local]"
info "[RUN]: $*"

set -x
# shellcheck disable=SC2086 disable=SC2048
exec "$@"

0 comments on commit 60b9116

Please sign in to comment.