Skip to content

Commit

Permalink
[AAP-9942] Add ability to create super user with no interaction (#103)
Browse files Browse the repository at this point in the history
Closes: [AAP-9942](https://issues.redhat.com/browse/AAP-9942) - Add a
script to create a default admin user

Usage task:
```
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 
```

Usage script
```
Usage: 
create_superuser.sh -u <username>(default:admin) -p <password>(default:testpass) -e <email>(default: [email protected]
create_superuser.sh -h (returns command usage)
```
  • Loading branch information
ddonahue007 authored Mar 15, 2023
1 parent 1003986 commit d86fa0b
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 34 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
12 changes: 1 addition & 11 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,8 @@ task docker:migrate

### Create superuser

Run `createsuperuser` management command to create a user:

Locally:

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

With docker compose:

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

### Starting API server
Expand Down
67 changes: 48 additions & 19 deletions scripts/common/logging.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,69 @@
#!/usr/bin/env bash

# colors
ERR=$(tput setaf 1)
INFO=$(tput setaf 178)
WARN=$(tput setaf 165)
TRACE=$(tput setaf 27)
TS=$(tput setaf 2)
TAG=$(tput setaf 10)
RESET=$(tput sgr0)

# timestamp
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")

log(){
if which tput &> /dev/null; then
# colors
ERR=$(tput setaf 1)
INFO=$(tput setaf 178)
WARN=$(tput setaf 165)
TRACE=$(tput setaf 27)
TS=$(tput setaf 2)
TAG=$(tput setaf 10)
RESET=$(tput sgr0)

log(){
local _tag_name=${1}
local _msg=${@:2}

printf "${TS}${TIMESTAMP} ${TAG}[${_tag_name}\t] ${_msg}\n"
printf ${RESET}
}
}

log-info() {
log-info() {
log "INFO" "${INFO} $@"
}
}

log-warn() {
log-warn() {
log "WARNING" "${WARN} $@"
}
}

log-err() {
log-err() {
log "ERROR" "${ERR} $@"
}
}

log-debug() {
log-debug() {
local _debug=$(tr '[:upper:]' '[:lower:]' <<<"$DEBUG")
if [[ ! -z "${DEBUG}" && ${_debug} == true ]];then
log "DEBUG" "${TRACE} $@"
fi
}
}

else
log(){
local _tag_name=${1}
local _msg=${@:2}

printf "${TIMESTAMP} [${_tag_name}\t] ${_msg}\n"
}

log-info() {
log "INFO" "$@"
}

log-warn() {
log "WARNING" "$@"
}

log-debug() {
local _debug=$(tr '[:upper:]' '[:lower:]' <<<"$DEBUG")
if [[ ! -z "${DEBUG}" && ${_debug} == true ]];then
log "DEBUG" "$@"
fi
}

log-err() {
log "ERROR" "$@"
}
fi
2 changes: 1 addition & 1 deletion scripts/common/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ check_vars() {
exit 1
fi
done
}
}
70 changes: 70 additions & 0 deletions scripts/create_superuser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/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() {
log-debug "poetry run /usr/bin/env src/aap_eda/manage.py createsuperuser --noinput"
local _result=$(poetry run /usr/bin/env src/aap_eda/manage.py createsuperuser --noinput 2>&1)

if [[ "${_result}" =~ "username is already taken" ]]; then
log-warn "username ${DJANGO_SUPERUSER_USERNAME} is already taken"
elif [ -n "${_result}" ]; then
log-err "${_result}"
exit 1
else
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}"
fi
}

#
# args check
#
export DJANGO_SUPERUSER_USERNAME="admin"
export DJANGO_SUPERUSER_PASSWORD="testpass"
export DJANGO_SUPERUSER_EMAIL="[email protected]"
export EDA_DB_HOST=${EDA_DB_HOST:-localhost}
export EDA_DB_PASSWORD=${EDA_DB_PASSWORD:-secret}

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
6 changes: 5 additions & 1 deletion tools/deploy/eda-api/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spec:
- args:
- /bin/bash
- -c
- aap-eda-manage migrate && aap-eda-manage runserver 0.0.0.0:8000
- aap-eda-manage migrate && scripts/create_superuser.sh && aap-eda-manage runserver 0.0.0.0:8000
env:
- name: EDA_DATABASE_URL
value: postgresql+asyncpg://postgres:secret@postgres/eda
Expand All @@ -40,4 +40,8 @@ spec:
- containerPort: 8000
resources: {}
restartPolicy: Always
initContainers:
- name: wait-for-postgres
image: docker.io/alpine:latest
command: [ 'sh', '-c', "until nslookup ${EDA_DB_HOST}.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for ${EDA_DB_HOST}; sleep 2; done" ]
status: {}
2 changes: 1 addition & 1 deletion tools/docker/docker-compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
command:
- /bin/bash
- -c
- aap-eda-manage migrate && aap-eda-manage runserver 0.0.0.0:8000
- aap-eda-manage migrate && scripts/create_superuser.sh && aap-eda-manage runserver 0.0.0.0:8000
ports:
- '8000:8000'
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion tools/docker/docker-compose-stage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
command:
- /bin/bash
- -c
- aap-eda-manage migrate && aap-eda-manage runserver 0.0.0.0:8000
- aap-eda-manage migrate && scripts/create_superuser.sh && aap-eda-manage runserver 0.0.0.0:8000
ports:
- '8000:8000'
depends_on:
Expand Down

0 comments on commit d86fa0b

Please sign in to comment.