Add success() method & `secret insert` subcommand

This commit is contained in:
3wc 2020-09-24 21:17:08 +02:00
parent 5627e67bf7
commit aa1ffd5d8a
1 changed files with 30 additions and 10 deletions

40
abra
View File

@ -57,6 +57,10 @@ warning() {
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)" echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
} }
success() {
echo "$(tput setaf 2)$*$(tput sgr0)"
}
###### Top-level arguments ###### Top-level arguments
ABRA_CONFIG=abra.yml ABRA_CONFIG=abra.yml
@ -102,7 +106,9 @@ fi
load_context() { load_context() {
if [ -z "$DOCKER_CONTEXT" ]; then if [ -z "$DOCKER_CONTEXT" ]; then
warning "\$DOCKER_CONTEXT not set, (slowly) looking it up" warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
# shellcheck disable=SC2063
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1) DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
# FIXME 3wc: make sure grep doesn't parse this, we're want a literal '*'
fi fi
} }
@ -159,24 +165,38 @@ sub_secret_help() {
echo "" echo ""
echo "Subcommands:" echo "Subcommands:"
echo " generate SECRET VERSION [PW] generate & store secret" echo " generate SECRET VERSION [PW] generate & store secret"
echo " insert SECRET VERSION PW save PW in docker and pass"
} }
sub_secret_generate(){ sub_secret_insert() {
require_stack require_stack
load_context load_context
SECRET=$1 SECRET=$1
VERSION=$2 VERSION=$2
PW=${3:-pwqgen} PW=$3
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
echo "Usage: $PROGRAM_NAME secret_generate SECRET VERSION" echo "Usage: $PROGRAM_NAME secret insert SECRET VERSION PW"
exit exit
fi fi
$PW | tee \ echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
>(docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" -) \ echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
>(pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m) }
sub_secret_generate(){
SECRET=$1
VERSION=$2
PWGEN=${3:-pwqgen}
PW=$($PWGEN)
success "Password: $PW"
echo "sub_secret_insert \"$SECRET\" \"$VERSION\" \"$PW\""
exit
sub_secret_insert "$SECRET" "$VERSION" "$PW"
} }
sub_secret() { sub_secret() {
@ -197,7 +217,7 @@ sub_run_args(){
shift 2 shift 2
if [ -z "$SERVICE" ]; then if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME run SERVICE [CMD]" echo "Usage: $PROGRAM_NAME run SERVICE DOCKER_ARGS [CMD]"
exit exit
fi fi
@ -251,9 +271,9 @@ sub_deploy (){
# shellcheck disable=SC2086 # shellcheck disable=SC2086
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
if [ -n "$DOMAIN" ]; then if [ -n "$DOMAIN" ]; then
echo "$(tput setaf 2)Yay! App should be available at https://${DOMAIN}$(tput sgr0)" success "Yay! App should be available at https://${DOMAIN}"
else else
echo "$(tput setaf 2)Yay! That worked. No \$DOMAIN defined, check logs.(tput sgr0)" success "Yay! That worked. No \$DOMAIN defined, check logs."
fi fi
else else
error "Oh no! Something went wrong 😕 Check errors above" error "Oh no! Something went wrong 😕 Check errors above"