-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AAP-9942] Add ability to create super user with no interaction (#103)
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
1 parent
1003986
commit d86fa0b
Showing
8 changed files
with
144 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ check_vars() { | |
exit 1 | ||
fi | ||
done | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters