Goodbye, parse_subcommand! 👋
continuous-integration/drone/push Build is passing Details

Ref #18
This commit is contained in:
3wc 2020-10-26 16:06:40 +02:00
parent b49b510c43
commit c50b1d8760
1 changed files with 336 additions and 312 deletions

648
abra
View File

@ -5,8 +5,21 @@
DOC="
The cooperative cloud utility belt 🎩🐇
Usage: abra [-c|--config=<path>] [-e|--env=<path>] [-h|--help]
[-s|--stack=<path>] [-v|--version] <command> [<args>...]
Usage:
abra [options] deploy
abra [options] upgrade
abra [options] logs [<app>]
abra [options] multilogs [<app>]
abra [options] cp (<app>:<path> <path>|<path> <app>:<path>)
abra [options] run [--user=<user>] <app> <args>...
abra [options] run <app> <args>...
abra [options] secret generate <secret> <version> [<cmd>]
abra [options] secret insert <secret> <version> <data>
abra [options] server add <host> [<user>] [<port>]
abra [options] server rm <host>
abra [options] server use <host>
abra [options] server init <host>
abra [options] server ls
Options:
-c, --config=<path> Stack configuration to use
@ -15,26 +28,6 @@ Options:
-s, --stack=<path> Name of the target stack
-v, --version Show program version
These are common Abra commands used in various situations:
Manage an application lifecycle:
deploy let 'em rip
upgrade upgrade to the latest version
View logs from running applications:
logs tail logs from a deployed service
multilogs tail logs from a whole stack
Interact with running containers:
cp <src> <dest> copy files to a container
run <service> <cmd> run <cmd> in <service>'s container
Manage servers:
server add <host> [user] [port] add docker context
server rm <host> remove docker context
server use <host> activate docker context
server init <host> activate docker swarm mode
Manage secrets:
secret generate <secret> <version> [pwgen] generate & store secret
secret insert <secret> <version> <password> save password in docker & pass
@ -120,8 +113,14 @@ left=("${initial_left[@]}"); return 1; }; optional() { local node_idx
for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() {
local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; switch() { local i
for i in "${!left[@]}"; do local l=${left[$i]}
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; _command() { local i
local name=${2:-$1}; for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = 'a' ]]; then
if [[ ${parsed_values[$l]} != "$name" ]]; then return 1; fi
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
eval "((var_$1++)) || true"; else eval "var_$1=true"; fi; return 0; fi; done
return 1; }; switch() { local i; for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
@ -135,37 +134,88 @@ eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1353}
usage=${DOC:40:131}; digest=0d574; shorts=(-c -e -h -s -v)
longs=(--config --env --help --stack --version); argcounts=(1 1 0 1 0)
node_0(){ value __config 0 true; }; node_1(){ value __env 1 true; }; node_2(){
switch __help 2; }; node_3(){ value __stack 3 true; }; node_4(){
switch __version 4; }; node_5(){ value _command_ a; }; node_6(){
value _args_ a true; }; node_7(){ optional 0 0; }; node_8(){ optional 1 1; }
node_9(){ either 2 2; }; node_10(){ optional 9; }; node_11(){ optional 3 3; }
node_12(){ either 4 4; }; node_13(){ optional 12; }; node_14(){ oneormore 6; }
node_15(){ optional 14; }; node_16(){ required 7 8 10 11 13 5 15; }; node_17(){
required 16; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
printf "%s\n" "${DOC:40:131}" >&2; exit 1; }'; unset var___config var___env \
var___help var___stack var___version var__command_ var__args_; parse 17 "$@"
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__config" "${prefix}__env" \
"${prefix}__help" "${prefix}__stack" "${prefix}__version" "${prefix}_command_" \
"${prefix}_args_"; if declare -p var___config >/dev/null 2>&1; then
eval "${prefix}"'__config=("${var___config[@]}")'; else
eval "${prefix}"'__config=()'; fi; if declare -p var___env >/dev/null 2>&1; then
eval "${prefix}"'__env=("${var___env[@]}")'; else eval "${prefix}"'__env=()'; fi
eval "${prefix}"'__help=${var___help:-false}'
if declare -p var___stack >/dev/null 2>&1; then
eval "${prefix}"'__stack=("${var___stack[@]}")'; else
eval "${prefix}"'__stack=()'; fi
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1102}
usage=${DOC:40:574}; digest=fab9b; shorts=(-h -v -e -c -s '')
longs=(--help --version --env --config --stack --user); argcounts=(0 0 1 1 1 1)
node_0(){ switch __help 0; }; node_1(){ switch __version 1; }; node_2(){
value __env 2; }; node_3(){ value __config 3; }; node_4(){ value __stack 4; }
node_5(){ value __user 5; }; node_6(){ value _app_ a; }; node_7(){
value _app___path_ a; }; node_8(){ value _path_ a; }; node_9(){
value _args_ a true; }; node_10(){ value _secret_ a; }; node_11(){
value _version_ a; }; node_12(){ value _cmd_ a; }; node_13(){ value _data_ a; }
node_14(){ value _host_ a; }; node_15(){ value _user_ a; }; node_16(){
value _port_ a; }; node_17(){ _command deploy; }; node_18(){ _command upgrade; }
node_19(){ _command logs; }; node_20(){ _command multilogs; }; node_21(){
_command cp; }; node_22(){ _command run; }; node_23(){ _command secret; }
node_24(){ _command generate; }; node_25(){ _command insert; }; node_26(){
_command server; }; node_27(){ _command add; }; node_28(){ _command rm; }
node_29(){ _command use; }; node_30(){ _command init; }; node_31(){ _command ls
}; node_32(){ optional 0 1 2 3 4; }; node_33(){ optional 32; }; node_34(){
required 33 17; }; node_35(){ required 33 18; }; node_36(){ optional 6; }
node_37(){ required 33 19 36; }; node_38(){ required 33 20 36; }; node_39(){
required 7 8; }; node_40(){ required 8 7; }; node_41(){ either 39 40; }
node_42(){ required 41; }; node_43(){ required 33 21 42; }; node_44(){
optional 5; }; node_45(){ oneormore 9; }; node_46(){ required 33 22 44 6 45; }
node_47(){ required 33 22 6 45; }; node_48(){ optional 12; }; node_49(){
required 33 23 24 10 11 48; }; node_50(){ required 33 23 25 10 11 13; }
node_51(){ optional 15; }; node_52(){ optional 16; }; node_53(){
required 33 26 27 14 51 52; }; node_54(){ required 33 26 28 14; }; node_55(){
required 33 26 29 14; }; node_56(){ required 33 26 30 14; }; node_57(){
required 33 26 31; }; node_58(){
either 34 35 37 38 43 46 47 49 50 53 54 55 56 57; }; node_59(){ required 58; }
cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
printf "%s\n" "${DOC:40:574}" >&2; exit 1; }'; unset var___help var___version \
var___env var___config var___stack var___user var__app_ var__app___path_ \
var__path_ var__args_ var__secret_ var__version_ var__cmd_ var__data_ \
var__host_ var__user_ var__port_ var_deploy var_upgrade var_logs var_multilogs \
var_cp var_run var_secret var_generate var_insert var_server var_add var_rm \
var_use var_init var_ls; parse 59 "$@"; local prefix=${DOCOPT_PREFIX:-''}
unset "${prefix}__help" "${prefix}__version" "${prefix}__env" \
"${prefix}__config" "${prefix}__stack" "${prefix}__user" "${prefix}_app_" \
"${prefix}_app___path_" "${prefix}_path_" "${prefix}_args_" \
"${prefix}_secret_" "${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" \
"${prefix}_host_" "${prefix}_user_" "${prefix}_port_" "${prefix}deploy" \
"${prefix}upgrade" "${prefix}logs" "${prefix}multilogs" "${prefix}cp" \
"${prefix}run" "${prefix}secret" "${prefix}generate" "${prefix}insert" \
"${prefix}server" "${prefix}add" "${prefix}rm" "${prefix}use" "${prefix}init" \
"${prefix}ls"; eval "${prefix}"'__help=${var___help:-false}'
eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'_command_=${var__command_:-}'
eval "${prefix}"'__env=${var___env:-}'
eval "${prefix}"'__config=${var___config:-}'
eval "${prefix}"'__stack=${var___stack:-}'
eval "${prefix}"'__user=${var___user:-}'; eval "${prefix}"'_app_=${var__app_:-}'
eval "${prefix}"'_app___path_=${var__app___path_:-}'
eval "${prefix}"'_path_=${var__path_:-}'
if declare -p var__args_ >/dev/null 2>&1; then
eval "${prefix}"'_args_=("${var__args_[@]}")'; else eval "${prefix}"'_args_=()'
fi; local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__config" \
"${prefix}__env" "${prefix}__help" "${prefix}__stack" "${prefix}__version" \
"${prefix}_command_" "${prefix}_args_"; done; }
fi; eval "${prefix}"'_secret_=${var__secret_:-}'
eval "${prefix}"'_version_=${var__version_:-}'
eval "${prefix}"'_cmd_=${var__cmd_:-}'; eval "${prefix}"'_data_=${var__data_:-}'
eval "${prefix}"'_host_=${var__host_:-}'
eval "${prefix}"'_user_=${var__user_:-}'
eval "${prefix}"'_port_=${var__port_:-}'
eval "${prefix}"'deploy=${var_deploy:-false}'
eval "${prefix}"'upgrade=${var_upgrade:-false}'
eval "${prefix}"'logs=${var_logs:-false}'
eval "${prefix}"'multilogs=${var_multilogs:-false}'
eval "${prefix}"'cp=${var_cp:-false}'; eval "${prefix}"'run=${var_run:-false}'
eval "${prefix}"'secret=${var_secret:-false}'
eval "${prefix}"'generate=${var_generate:-false}'
eval "${prefix}"'insert=${var_insert:-false}'
eval "${prefix}"'server=${var_server:-false}'
eval "${prefix}"'add=${var_add:-false}'; eval "${prefix}"'rm=${var_rm:-false}'
eval "${prefix}"'use=${var_use:-false}'
eval "${prefix}"'init=${var_init:-false}'; eval "${prefix}"'ls=${var_ls:-false}'
local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__help" \
"${prefix}__version" "${prefix}__env" "${prefix}__config" "${prefix}__stack" \
"${prefix}__user" "${prefix}_app_" "${prefix}_app___path_" "${prefix}_path_" \
"${prefix}_args_" "${prefix}_secret_" "${prefix}_version_" "${prefix}_cmd_" \
"${prefix}_data_" "${prefix}_host_" "${prefix}_user_" "${prefix}_port_" \
"${prefix}deploy" "${prefix}upgrade" "${prefix}logs" "${prefix}multilogs" \
"${prefix}cp" "${prefix}run" "${prefix}secret" "${prefix}generate" \
"${prefix}insert" "${prefix}server" "${prefix}add" "${prefix}rm" \
"${prefix}use" "${prefix}init" "${prefix}ls"; done; }
# docopt parser above, complete command for generating this parser is `docopt.sh abra`
PROGRAM_NAME=$(basename "$0")
@ -173,423 +223,397 @@ PROGRAM_NAME=$(basename "$0")
###### Utility functions
yml_pattern_exists() {
PATTERN=$1
PATTERN=$1
if [ -f "$ABRA_CONFIG" ]; then
RESULT=$(yq read "$ABRA_CONFIG" "$PATTERN")
if [ -f "$ABRA_CONFIG" ]; then
RESULT=$(yq read "$ABRA_CONFIG" "$PATTERN")
if [ "$RESULT" != 0 ]; then
return 0
fi
fi
if [ "$RESULT" != 0 ]; then
return 0
fi
fi
return 1
}
parse_subcommand() {
SUBCOMMAND="$1"
PREFIX=$2
if [ -n "$PREFIX" ]; then
PPREFIX="_$2"
SPREFIX="$2 "
fi
case $SUBCOMMAND in
"" | "-h" | "--help")
"sub${PPREFIX}_help"
;;
*)
shift 2
"sub${PPREFIX}_${SUBCOMMAND}" "$@"
if [ $? = 127 ]; then
error "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand."
fi
;;
esac
return 1
}
error() {
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
exit 1
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
exit 1
}
warning() {
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
}
success() {
echo "$(tput setaf 2)$*$(tput sgr0)"
echo "$(tput setaf 2)$*$(tput sgr0)"
}
###### Default settings
if [ -z "$COMPOSE_FILE" ]; then
COMPOSE_FILE="compose.yml"
COMPOSE_FILE="compose.yml"
fi
if [ -z "$ABRA_STACK_DIR" ]; then
ABRA_STACK_DIR="stacks/$SERVICE"
ABRA_STACK_DIR="stacks/$SERVICE"
fi
###### Safety checks
require_yq() {
if ! type yq > /dev/null 2>&1; then
error "yq program is not installed"
fi
if ! type yq > /dev/null 2>&1; then
error "yq program is not installed"
fi
}
require_multitail() {
if ! type multitail > /dev/null 2>&1; then
error "multitail program is not installed"
fi
if ! type multitail > /dev/null 2>&1; then
error "multitail program is not installed"
fi
}
require_stack() {
if [ -z "$STACK_NAME" ]; then
error "no stack_name, export \$STACK_NAME=my_cool_app or add it to abra.yml"
fi
if [ -z "$STACK_NAME" ]; then
error "no stack_name, export \$STACK_NAME=my_cool_app or add it to abra.yml"
fi
}
require_stack_dir() {
if [ -z "$ABRA_STACK_DIR" ] || [ ! -d "$ABRA_STACK_DIR" ]; then
error "can't find \$ABRA_STACK_DIR '$ABRA_STACK_DIR'"
fi
if [ -z "$ABRA_STACK_DIR" ] || [ ! -d "$ABRA_STACK_DIR" ]; then
error "can't find \$ABRA_STACK_DIR '$ABRA_STACK_DIR'"
fi
}
if [ -z "$ABRA_ENV" ] && [ -f .envrc ] && type direnv > /dev/null 2>&1 && ! direnv status | grep -q 'Found RC allowed true'; then
error "direnv is blocked, run direnv allow"
error "direnv is blocked, run direnv allow"
fi
###### Run-time loading
load_custom_commands() {
if [ -f abra-commands.sh ]; then
# shellcheck disable=SC1091
source abra-commands.sh
fi
if [ -f abra-commands.sh ]; then
# shellcheck disable=SC1091
source abra-commands.sh
fi
if [ -f "$ABRA_STACK_DIR/abra-commands.sh" ]; then
# shellcheck disable=SC1090
source "$ABRA_STACK_DIR/abra-commands.sh"
fi
if [ -f "$ABRA_STACK_DIR/abra-commands.sh" ]; then
# shellcheck disable=SC1090
source "$ABRA_STACK_DIR/abra-commands.sh"
fi
}
load_context() {
# Load current context from env or Docker
if [ -z "$DOCKER_CONTEXT" ]; then
warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
# shellcheck disable=SC2063
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
# make sure grep doesn't parse this, we want a literal '*'
fi
# Load current context from env or Docker
if [ -z "$DOCKER_CONTEXT" ]; then
warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
# shellcheck disable=SC2063
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
# make sure grep doesn't parse this, we want a literal '*'
fi
}
###### Subcommand `secret`
sub_secret_insert() {
require_stack
load_context
require_stack
load_context
SECRET=$1
VERSION=$2
PW=$3
SECRET="$abra__secret_"
VERSION="$abra__version_"
PW="$abra__data_"
if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
error "Required arguments missing"
echo
echo "Usage: $PROGRAM_NAME secret insert SECRET VERSION PW"
exit
fi
if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
error "Required arguments missing"
fi
echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
if type pass > /dev/null 2>&1; then
echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
success "pass: hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}"
fi
}
sub_secret_generate(){
SECRET=$1
VERSION=$2
PWGEN=${3:-pwqgen}
SECRET="$abra__secret_"
VERSION="$abra__version_"
PWGEN=${abra__cmd_:-pwqgen}
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
error "Required arguments missing"
echo
echo "Usage: $PROGRAM_NAME secret generate SECRET VERSION [PW]"
exit
fi
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
error "Required arguments missing"
echo
echo "Usage: $PROGRAM_NAME secret generate SECRET VERSION [PW]"
exit
fi
PW=$($PWGEN)
PW=$($PWGEN)
success "Password: $PW"
success "Password: $PW"
sub_secret_insert "$SECRET" "$VERSION" "$PW"
}
# FIXME 3wc: this is a little janky, might be better to make a
# util_secret_insert function which this and sub_secret_insert can call
abra__data_="$PW"
sub_secret() {
SUBCOMMAND=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND" "secret" $@
sub_secret_insert
}
###### Subcommand `run`
sub_run(){
require_stack
require_stack
if [ "$1" == "-u" ]; then
RUN_USER="-u $2"
shift 2
fi
if [ -n "$abra___user" ]; then
RUN_USER="-u $abra___user"
fi
SERVICE=$1
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
| grep "${STACK_NAME}_${abra__app_}" | head -n1 | cut -d',' -f1)
shift
if [ -z "$CONTAINER" ]; then
error "Can't find a container for ${STACK_NAME}_${abra__app_}"
exit
fi
if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME run [-u USER] SERVICE [CMD]"
exit
fi
# shellcheck disable=SC2086
docker exec $RUN_USER -it "$CONTAINER" "$@"
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
| grep "${STACK_NAME}_${SERVICE}" | head -n1 | cut -d',' -f1)
if [ -z "$CONTAINER" ]; then
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
exit
fi
# shellcheck disable=SC2086
docker exec $RUN_USER -it "$CONTAINER" "$@"
return
return
}
###### Subcommand `deploy`
sub_deploy (){
require_stack
require_stack_dir
load_context
require_stack
require_stack_dir
load_context
echo "About to deploy:"
echo " Context: $(tput setaf 4)${DOCKER_CONTEXT}$(tput sgr0)"
if [ "${COMPOSE_FILE/:/}" == "${COMPOSE_FILE}" ]; then
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/${COMPOSE_FILE}$(tput sgr0)"
else
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/"
IFS=':' read -ra COMPOSE_FILES <<< "$COMPOSE_FILE"
for COMPOSE in "${COMPOSE_FILES[@]}"; do
echo " - ${COMPOSE}"
done
tput sgr0
fi
if [ -n "$DOMAIN" ]; then
echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
fi
echo " Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)"
echo "About to deploy:"
echo " Context: $(tput setaf 4)${DOCKER_CONTEXT}$(tput sgr0)"
if [ "${COMPOSE_FILE/:/}" == "${COMPOSE_FILE}" ]; then
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/${COMPOSE_FILE}$(tput sgr0)"
else
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/"
IFS=':' read -ra COMPOSE_FILES <<< "$COMPOSE_FILE"
for COMPOSE in "${COMPOSE_FILES[@]}"; do
echo " - ${COMPOSE}"
done
tput sgr0
fi
if [ -n "$DOMAIN" ]; then
echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
fi
echo " Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)"
read -rp "Continue? (y/[n])? " choice
read -rp "Continue? (y/[n])? " choice
case "$choice" in
y|Y ) ;;
n|N ) return;;
* ) return;;
esac
case "$choice" in
y|Y ) ;;
n|N ) return;;
* ) return;;
esac
(
cd "$ABRA_STACK_DIR" || error "\$ABRA_STACK_DIR '$ABRA_STACK_DIR' not found"
# shellcheck disable=SC2086
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
if [ -n "$DOMAIN" ]; then
success "Yay! App should be available at https://${DOMAIN}"
else
success "Yay! That worked. No \$DOMAIN defined, check logs."
fi
else
error "Oh no! Something went wrong 😕 Check errors above"
fi
)
(
cd "$ABRA_STACK_DIR" || error "\$ABRA_STACK_DIR '$ABRA_STACK_DIR' not found"
# shellcheck disable=SC2086
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
if [ -n "$DOMAIN" ]; then
success "Yay! App should be available at https://${DOMAIN}"
else
success "Yay! That worked. No \$DOMAIN defined, check logs."
fi
else
error "Oh no! Something went wrong 😕 Check errors above"
fi
)
}
###### Subcommand `logs`
# Inspired by https://github.com/moby/moby/issues/31458#issuecomment-475411564
sub_multilogs() {
require_stack
require_multitail
require_stack
require_multitail
# Get a list of the service names
SERVICES=$(docker stack services --format "{{.Name}}" "${STACK_NAME}")
# Sort the service names
SERVICES=$(echo "${SERVICES}" | sort)
# Create the command to run
COMMAND='multitail --mergeall'
for SERVICE in ${SERVICES}; do
COMMAND="${COMMAND} -L 'docker service logs --tail 20 -f ${SERVICE}'"
done
# Run the command
bash -c "${COMMAND}"
# Get a list of the service names
SERVICES=$(docker stack services --format "{{.Name}}" "${STACK_NAME}")
# Sort the service names
SERVICES=$(echo "${SERVICES}" | sort)
# Create the command to run
COMMAND='multitail --mergeall'
for SERVICE in ${SERVICES}; do
COMMAND="${COMMAND} -L 'docker service logs --tail 20 -f ${SERVICE}'"
done
# Run the command
bash -c "${COMMAND}"
}
sub_logs (){
require_stack
require_stack
SERVICE=$1
SERVICE=$1
if [ -z "$SERVICE" ]; then
warning "No \$SERVICE provided, running multilogs"
sub_multilogs
fi
if [ -z "$SERVICE" ]; then
warning "No \$SERVICE provided, running multilogs"
sub_multilogs
fi
shift
shift
if [ $# -eq 0 ]; then
LOGS_ARGS="\
--follow \
--tail 20 \
--no-trunc \
--details \
--timestamps"
else
# shellcheck disable=SC2124
LOGS_ARGS=$@
fi
if [ $# -eq 0 ]; then
LOGS_ARGS="\
--follow \
--tail 20 \
--no-trunc \
--details \
--timestamps"
else
# shellcheck disable=SC2124
LOGS_ARGS=$@
fi
# shellcheck disable=SC2086
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
# shellcheck disable=SC2086
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
}
###### Subcommand `cp`
sub_cp() {
require_stack
require_stack
SOURCE=$1
DEST=$2
SOURCE=$1
DEST=$2
SERVICE=$(echo "$SOURCE" | grep -o '^[^:]\+:' || echo "$DEST" | grep -o '^[^:]\+:')
SERVICE=$(echo "$SERVICE" | tr -d ':')
SERVICE=$(echo "$SOURCE" | grep -o '^[^:]\+:' || echo "$DEST" | grep -o '^[^:]\+:')
SERVICE=$(echo "$SERVICE" | tr -d ':')
if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
echo ""
error "Can't find SERVICE in either SRC or DEST"
fi
if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
echo ""
error "Can't find SERVICE in either SRC or DEST"
fi
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
if [ -z "$CONTAINER" ]; then
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
exit
fi
if [ -z "$CONTAINER" ]; then
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
exit
fi
CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE:/$CONTAINER:/")
CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE:/$CONTAINER:/")
# shellcheck disable=SC2086
docker cp ${CP_ARGS}
# shellcheck disable=SC2086
docker cp ${CP_ARGS}
}
###### Subcommand `context`
sub_server_init() {
export DOCKER_CONTEXT="$1"
export DOCKER_CONTEXT="$1"
if [ -n "$DOCKER_CONTEXT" ]; then
load_context
fi
if [ -n "$DOCKER_CONTEXT" ]; then
load_context
fi
docker swarm init || true
docker network create --driver=overlay proxy --scope swarm || true
docker swarm init || true
docker network create --driver=overlay proxy --scope swarm || true
}
sub_server_add() {
HOST="$1"
USERNAME="$2"
PORT="$3"
HOST="$abra__host_"
USERNAME="$abra__user_"
PORT="$abra__port_"
if [ -n "$PORT" ]; then
PORT=":$PORT"
fi
if [ -n "$PORT" ]; then
PORT=":$PORT"
fi
if [ -n "$USERNAME" ]; then
USERNAME="$USERNAME@"
fi
if [ -n "$USERNAME" ]; then
USERNAME="$USERNAME@"
fi
docker context create "$HOST" \
--docker "host=ssh://$USERNAME$HOST$PORT"
docker context create "$HOST" \
--docker "host=ssh://$USERNAME$HOST$PORT"
}
sub_server_rm() {
HOST="$1"
docker context rm "$HOST"
docker context rm "$abra__host_"
}
sub_server_use() {
docker context use "$1"
docker context use "$abra__host_"
}
sub_server_ls() {
docker context ls
docker context ls
}
sub_server() {
SUBCOMMAND2=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND2" "server" $@
}
###### Subcommand `upgrade`
sub_upgrade() {
curl -fsSL https://install.abra.autonomic.zone | bash
curl -fsSL https://install.abra.autonomic.zone | bash
}
###### Docker alias subcommands
sub_stack() {
# shellcheck disable=SC2068
docker stack $@
# shellcheck disable=SC2068
docker stack $@
}
sub_volume() {
# shellcheck disable=SC2068
docker volume $@
# shellcheck disable=SC2068
docker volume $@
}
###### Main
abra() {
declare abra___stack abra___env abra___config abra__command_ abra__args_
# TODO (3wc): we either need to do this, or add s'hellcheck disable' all over
# the place to handle the dynamically-defined vars
declare abra___stack abra___env abra___config abra__command_ abra__args_ \
abra__secret_ abra__version_ abra__data_ abra___user abra__host_ \
abra__app_ abra__port_ abra__user_
DOCOPT_PREFIX=abra_
eval "$(docopt "$@")"
STACK_NAME=$abra___stack
STACK_NAME=$abra___stack
if [ -n "$abra___env" ]; then
# shellcheck disable=SC1090
source "$abra___env" || error "Unable to load env from '$abra___env'"
fi
if [ -n "$abra___env" ]; then
# shellcheck disable=SC1090
source "$abra___env" || error "Unable to load env from '$abra___env'"
fi
if [ -n "$abra___config" ]; then
require_yq
if [ -n "$abra___config" ]; then
require_yq
if yml_pattern_exists stack_name; then
STACK_NAME=$(yq read "$ABRA_CONFIG" stack_name)
fi
# FIXME load other variables somehow
fi
if yml_pattern_exists stack_name; then
STACK_NAME=$(yq read "$ABRA_CONFIG" stack_name)
fi
# FIXME load other variables somehow
fi
load_custom_commands
load_custom_commands
# shellcheck disable=SC2086
"sub_${abra__command_}" ${abra__args_[*]}
# Search for sub_* functions, and check if any of them matches enabled
# arguments (i.e. is a command and is specified)
for SUBCOMMAND in $(declare -Ff | grep 'sub_' | cut -d' ' -f3 | sort -r); do
IFS='_' read -r -a PARTS <<< "$SUBCOMMAND"
for PART in "${PARTS[@]:1}"; do
VAR=$(eval "echo \$abra_$PART")
if [ ! "$VAR" == "true" ]; then
continue 2
fi
done
abra__command_=$(IFS="_"; echo "${PARTS[*]:1}")
break
done
# Fallback for non-docopt-defined options
# shellcheck disable=SC2086
"sub_${abra__command_}" ${abra__args_[*]}
}
abra "$@"