Context set-up, Subsubcommands, COMPOSE->{_FILE}
continuous-integration/drone/push Build is passing Details

This commit is contained in:
3wc 2020-09-22 00:08:13 +02:00
parent efd43752d6
commit f4798bf3bd
1 changed files with 86 additions and 20 deletions

106
abra
View File

@ -3,8 +3,8 @@
PROGRAM_NAME=$(basename "$0") PROGRAM_NAME=$(basename "$0")
ABRA_CONFIG=abra.yml ABRA_CONFIG=abra.yml
if [ -z "$COMPOSE" ]; then if [ -z "$COMPOSE_FILE" ]; then
COMPOSE="compose.yml" COMPOSE_FILE="compose.yml"
fi fi
yml_pattern_exists() { yml_pattern_exists() {
@ -66,7 +66,14 @@ sub_help() {
echo "" echo ""
echo "Make sure \$STACK_NAME is set using direnv or -a" echo "Make sure \$STACK_NAME is set using direnv or -a"
echo "" echo ""
echo "Runs compose.yml by default, set e.g. COMPOSE=\"compose.yml compose2.yml\" to override" echo "Runs compose.yml by default, set e.g. COMPOSE_FILE=\"compose.yml:compose2.yml\" to override"
}
sub_secret_help() {
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] secret <subcommand> [options]"
echo ""
echo "Subcommands:"
echo " generate [PW] generate & store secret"
} }
sub_secret_generate(){ sub_secret_generate(){
@ -84,6 +91,13 @@ sub_secret_generate(){
>(pass insert "hosts/autonomic-swarm/${STACK_NAME}/${SECRET}" -m) >(pass insert "hosts/autonomic-swarm/${STACK_NAME}/${SECRET}" -m)
} }
sub_secret() {
SUBCOMMAND=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND" "secret" $@
}
sub_run_args(){ sub_run_args(){
SERVICE=$1 SERVICE=$1
DOCKER_ARGS=$2 DOCKER_ARGS=$2
@ -119,7 +133,7 @@ sub_run(){
sub_deploy (){ sub_deploy (){
echo "About to deploy:" echo "About to deploy:"
echo " Compose: $(tput setaf 3)${PWD}/${COMPOSE}$(tput sgr0)" echo " Compose: $(tput setaf 3)${PWD}/${COMPOSE_FILE}$(tput sgr0)"
if [ -n "$DOMAIN" ]; then if [ -n "$DOMAIN" ]; then
echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)" echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
fi fi
@ -134,7 +148,7 @@ sub_deploy (){
esac esac
# shellcheck disable=SC2086 # shellcheck disable=SC2086
if docker stack deploy -c ${COMPOSE/ / -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)" echo "$(tput setaf 2)Yay! App should be available at https://${DOMAIN}$(tput sgr0)"
else else
@ -194,18 +208,70 @@ sub_cp() {
docker cp $CP_ARGS docker cp $CP_ARGS
} }
subcommand=$1 sub_context_help() {
case $subcommand in echo "Usage: $PROGRAM_NAME [-a STACK_NAME] context <subcommand> [options]"
"" | "-h" | "--help") echo ""
sub_help echo "Subcommands:"
;; echo " init HOST [USER] [PORT] set up remote Docker context"
*) echo " use activate remote Docker context"
shift }
"sub_${subcommand}" "$@"
if [ $? = 127 ]; then sub_context_init() {
echo "Error: '$subcommand' is not a known subcommand." >&2 HOST="$1"
echo " Run '$PROGRAM_NAME --help' for a list of known subcommands." >&2 USERNAME="$2"
exit 1 PORT="$3"
fi
;; if [ -n "$PORT" ]; then
esac PORT=":$PORT"
fi
if [ -n "$USERNAME" ]; then
USERNAME="$USERNAME@"
fi
docker context create "$HOST" \
--docker "host=ssh://$USERNAME$HOST$PORT"
}
sub_context_use() {
docker context use "$1"
}
sub_context() {
SUBCOMMAND2=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND2" "context" $@
}
parse_subcommand() {
SUBCOMMAND="$1"
PREFIX=$2
if [ -n "$PREFIX" ]; then
PPREFIX="_$2"
SPREFIX="$2 "
SSPREFIX=" $2"
fi
case $SUBCOMMAND in
"" | "-h" | "--help")
"sub${PPREFIX}_help"
;;
*)
shift 2
"sub${PPREFIX}_${SUBCOMMAND}" "$@"
if [ $? = 127 ]; then
echo "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand." >&2
echo " Run '$PROGRAM_NAME$SSPREFIX --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac
}
SUBCOMMAND=$1
shift
# shellcheck disable=SC2086,SC2068
parse_subcommand $SUBCOMMAND "" $@