Skip to content

Commit

Permalink
Add ability to create super user with no input
Browse files Browse the repository at this point in the history
  • Loading branch information
ddonahue007 committed Mar 10, 2023
1 parent f09117b commit affdc31
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Taskfile.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vars:
DOCKER_COMPOSE_ARGS: '{{ default "-p eda -f tools/docker/docker-compose-dev.yaml" .DOCKER_COMPOSE_ARGS }}'
PYTEST_CMD: "poetry run python -m pytest"
MINIKUBE_CMD: "scripts/eda_kube.sh"
SUSER_CMD: "scripts/create_superuser.sh"

tasks:
default:
Expand Down Expand Up @@ -188,6 +189,22 @@ tasks:
vars:
CLI_ARGS: exec -it worker aap-eda-manage shell

create:superuser:
desc: "create a superuser to use with EDA API."
summary: |
Run create_superuser.sh with specified CLI arguments. If no arguments are
given the following defaults are used.
Defaults:
user: admin
password: testpass
email: [email protected]
Examples:
$ task create:superuser
$ task create:superuser -- -u test_user -p none2tuff -e [email protected]
cmds:
- "{{.SUSER_CMD}} {{.CLI_ARGS}}"

minikube:
desc: "Run eda_kube.sh with specified CLI arguments."
summary: |
Expand Down
4 changes: 2 additions & 2 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ Run `createsuperuser` management command to create a user:
Locally:

```shell
task manage -- createsuperuser
task createsuperuser
```

With docker compose:

```shell
task docker -- run --rm api aap-eda-manage createsuperuser
task create:superuser
```

### Starting API server
Expand Down
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"
}
84 changes: 84 additions & 0 deletions scripts/create_superuser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset

SCRIPTS_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PROJECT_DIR="${SCRIPTS_DIR}/.."

export DEBUG=${DEBUG:-false}

# import common & logging
source "${SCRIPTS_DIR}"/common/logging.sh
source "${SCRIPTS_DIR}"/common/utils.sh

trap handle_errors ERR

handle_errors() {
log-err "An error occurred on or around line ${BASH_LINENO[0]}. Unable to continue."
exit 1
}

usage() {
log-info "Usage: "
log-info "$(basename "$0") -u <username>(default:admin) -p <password>(default:testpass) -e <email>(default: [email protected]"
log-info "$(basename "$0") -h (returns command usage)"
exit 0
}

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}})"

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}")" == "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"

log-debug "task manage -- createsuperuser --noinput"
if task manage -- createsuperuser --noinput &> /dev/null; then
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!"
fi
}

#
# args check
#
export DJANGO_SUPERUSER_USERNAME="admin"
export DJANGO_SUPERUSER_PASSWORD="testpass"
export DJANGO_SUPERUSER_EMAIL="[email protected]"

while getopts p:u:e:h opt; do
case $opt in
p) export DJANGO_SUPERUSER_PASSWORD=$OPTARG ;;
u) export DJANGO_SUPERUSER_USERNAME=$OPTARG ;;
e) export DJANGO_SUPERUSER_EMAIL=$OPTARG ;;
h) usage ;;
*) log-err "Invalid flag supplied"; exit 2
esac
done

shift "$(( OPTIND - 1 ))"

#
# execute
#
create_user

0 comments on commit affdc31

Please sign in to comment.