Skip to content

Commit

Permalink
refactor to move container check to common
Browse files Browse the repository at this point in the history
  • Loading branch information
ddonahue007 committed Mar 10, 2023
1 parent 0ea7349 commit 969a418
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
29 changes: 29 additions & 0 deletions scripts/common/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,32 @@ check_vars() {
fi
done
}

wait-for-container() {
# Wait for specific time(sec) for a container become healthy.
#
# Args:
# ($1): _container_name - Name of the docker container to health check
# ($2): _timeout - Number of seconds before timeout (default: 15 seconds)
#
local _container_name="${1}"
local _heath_check=".State.Health.Status"
local _cnt=0
local _timeout=15

log-info "Creating super user account..."

log-debug "Checking for ${_container_name} container"
log-debug "docker inspect -f {{${_heath_check}}} ${_container_name} == healthy"
until [ "$(docker inspect -f {{${_heath_check}}} "${_container_name}" 2> /dev/null)" == "healthy" ] || [ "${_cnt}" -eq "${_timeout}" ]; do
log-debug "Healthcheck waiting...[$((++_cnt))s]"
sleep 1;

if [ "${_cnt}" -eq "${_timeout}" ]; then
log-err "timeout waiting for ${_container_name} service!"
exit 1
fi
done;

log-debug "${_container_name} is healthy"
}
25 changes: 4 additions & 21 deletions scripts/create_superuser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,18 @@ usage() {
}

create_user() {
local _heath_check=".State.Health.Status"
local _cnt=0
local _timeout=15
local _container_name="$( docker container ls -f name=^eda-postgres --format {{.Names}} 2> /dev/null)"
local _container_name="$(docker container ls -f name=^eda-postgres --format {{.Names}} 2> /dev/null)"

log-info "Creating super user account..."

log-debug "Checking for ${_container_name} container"
log-debug "docker inspect -f {{${_heath_check}}} ${_container_name} == healthy"
until [ "$(docker inspect -f {{${_heath_check}}} "${_container_name}" 2> /dev/null)" == "healthy" ] || [ "${_cnt}" -eq "${_timeout}" ]; do
log-debug "Healthcheck waiting...[$((++_cnt))s]"
sleep 1;

if [ "${_cnt}" -eq "${_timeout}" ]; then
log-err "timeout waiting for postgres service!"
exit 1
fi
done;

log-debug "${_container_name} is healthy"
wait-for-container "${_container_name}"

log-debug "task manage -- createsuperuser --noinput"
if task manage -- createsuperuser --noinput &> /dev/null; then
log-info "Superuser created:"
log-info "Superuser created"
log-debug "\t User: ${DJANGO_SUPERUSER_USERNAME}"
log-debug "\t Password: ${DJANGO_SUPERUSER_PASSWORD}"
log-debug "\t Email: ${DJANGO_SUPERUSER_EMAIL}"
else
log-debug "Superuser Already Exists!"
log-info "Superuser \'${DJANGO_SUPERUSER_USERNAME}\' Already Exists!"
fi
}

Expand Down

0 comments on commit 969a418

Please sign in to comment.