Add --status option to app ls

Closes #35
This commit is contained in:
3wc 2020-11-06 22:32:53 +02:00
parent dd570e224b
commit e307286db7

197
abra
View File

@ -12,7 +12,7 @@ The cooperative cloud utility belt 🎩🐇
Usage: Usage:
abra [options] app new [--server=<server>] [--domain=<domain>] [--pass] [--auto] <app> abra [options] app new [--server=<server>] [--domain=<domain>] [--pass] [--auto] <app>
abra [options] app (list|ls) abra [options] app (list|ls) [--status]
abra [options] app <domain> deploy abra [options] app <domain> deploy
abra [options] app <domain> undeploy abra [options] app <domain> undeploy
abra [options] app <domain> config abra [options] app <domain> config
@ -141,77 +141,78 @@ 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() { return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() { printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() { [[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1360} printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1371}
usage=${DOC:40:1052}; digest=62c18; shorts=(-e -v -h -s '' '' '' '' '' '' '') usage=${DOC:40:1063}; digest=d0254; shorts=(-v -s -e -h '' '' '' '' '' '' '' '')
longs=(--env --version --help --stack --server --domain --pass --auto --force --user --all) longs=(--version --stack --env --help --server --domain --pass --auto --status --force --user --all)
argcounts=(1 0 0 1 1 1 0 0 0 1 0); node_0(){ value __env 0; }; node_1(){ argcounts=(0 1 1 0 1 1 0 0 0 0 1 0); node_0(){ switch __version 0; }; node_1(){
switch __version 1; }; node_2(){ switch __help 2; }; node_3(){ value __stack 3 value __stack 1; }; node_2(){ value __env 2; }; node_3(){ switch __help 3; }
}; node_4(){ value __server 4; }; node_5(){ value __domain 5; }; node_6(){ node_4(){ value __server 4; }; node_5(){ value __domain 5; }; node_6(){
switch __pass 6; }; node_7(){ switch __auto 7; }; node_8(){ switch __force 8; } switch __pass 6; }; node_7(){ switch __auto 7; }; node_8(){ switch __status 8; }
node_9(){ value __user 9; }; node_10(){ switch __all 10; }; node_11(){ node_9(){ switch __force 9; }; node_10(){ value __user 10; }; node_11(){
value _app_ a; }; node_12(){ value _domain_ a; }; node_13(){ value _service_ a switch __all 11; }; node_12(){ value _app_ a; }; node_13(){ value _domain_ a; }
}; node_14(){ value _src_ a; }; node_15(){ value _dst_ a; }; node_16(){ node_14(){ value _service_ a; }; node_15(){ value _src_ a; }; node_16(){
value _args_ a true; }; node_17(){ value _secret_ a; }; node_18(){ value _dst_ a; }; node_17(){ value _args_ a true; }; node_18(){ value _secret_ a
value _version_ a; }; node_19(){ value _cmd_ a; }; node_20(){ value _data_ a; } }; node_19(){ value _version_ a; }; node_20(){ value _cmd_ a; }; node_21(){
node_21(){ value _host_ a; }; node_22(){ value _user_ a; }; node_23(){ value _data_ a; }; node_22(){ value _host_ a; }; node_23(){ value _user_ a; }
value _port_ a; }; node_24(){ _command app; }; node_25(){ _command new; } node_24(){ value _port_ a; }; node_25(){ _command app; }; node_26(){
node_26(){ _command list; }; node_27(){ _command ls; }; node_28(){ _command new; }; node_27(){ _command list; }; node_28(){ _command ls; }
_command deploy; }; node_29(){ _command undeploy; }; node_30(){ _command config node_29(){ _command deploy; }; node_30(){ _command undeploy; }; node_31(){
}; node_31(){ _command delete; }; node_32(){ _command rm; }; node_33(){ _command config; }; node_32(){ _command delete; }; node_33(){ _command rm; }
_command logs; }; node_34(){ _command multilogs; }; node_35(){ _command cp; } node_34(){ _command logs; }; node_35(){ _command multilogs; }; node_36(){
node_36(){ _command run; }; node_37(){ _command secret; }; node_38(){ _command cp; }; node_37(){ _command run; }; node_38(){ _command secret; }
_command auto; }; node_39(){ _command generate; }; node_40(){ _command insert; } node_39(){ _command auto; }; node_40(){ _command generate; }; node_41(){
node_41(){ _command server; }; node_42(){ _command add; }; node_43(){ _command insert; }; node_42(){ _command server; }; node_43(){ _command add; }
_command use; }; node_44(){ _command init; }; node_45(){ _command upgrade; } node_44(){ _command use; }; node_45(){ _command init; }; node_46(){
node_46(){ optional 0 1 2 3; }; node_47(){ optional 46; }; node_48(){ optional 4 _command upgrade; }; node_47(){ optional 0 1 2 3; }; node_48(){ optional 47; }
}; node_49(){ optional 5; }; node_50(){ optional 6; }; node_51(){ optional 7; } node_49(){ optional 4; }; node_50(){ optional 5; }; node_51(){ optional 6; }
node_52(){ required 47 24 25 48 49 50 51 11; }; node_53(){ either 26 27; } node_52(){ optional 7; }; node_53(){ required 48 25 26 49 50 51 52 12; }
node_54(){ required 53; }; node_55(){ required 47 24 54; }; node_56(){ node_54(){ either 27 28; }; node_55(){ required 54; }; node_56(){ optional 8; }
required 47 24 12 28; }; node_57(){ required 47 24 12 29; }; node_58(){ node_57(){ required 48 25 55 56; }; node_58(){ required 48 25 13 29; }
required 47 24 12 30; }; node_59(){ either 31 32; }; node_60(){ required 59; } node_59(){ required 48 25 13 30; }; node_60(){ required 48 25 13 31; }
node_61(){ optional 8; }; node_62(){ required 47 24 12 60 61; }; node_63(){ node_61(){ either 32 33; }; node_62(){ required 61; }; node_63(){ optional 9; }
optional 13; }; node_64(){ required 47 24 12 33 63; }; node_65(){ node_64(){ required 48 25 13 62 63; }; node_65(){ optional 14; }; node_66(){
required 47 24 12 34; }; node_66(){ required 47 24 12 35 14 15; }; node_67(){ required 48 25 13 34 65; }; node_67(){ required 48 25 13 35; }; node_68(){
optional 9; }; node_68(){ oneormore 16; }; node_69(){ required 48 25 13 36 15 16; }; node_69(){ optional 10; }; node_70(){
required 47 24 12 36 67 13 68; }; node_70(){ required 47 24 12 36 13 68; } oneormore 17; }; node_71(){ required 48 25 13 37 69 14 70; }; node_72(){
node_71(){ required 47 24 12 37 38; }; node_72(){ optional 19; }; node_73(){ required 48 25 13 37 14 70; }; node_73(){ required 48 25 13 38 39; }; node_74(){
required 47 24 12 37 39 17 18 72 50; }; node_74(){ optional 20; }; node_75(){ required 48 25 13 38 40 18 19 74 51; }; node_76(){
required 47 24 12 37 40 17 18 20 50; }; node_75(){ either 17 10; }; node_76(){ required 48 25 13 38 41 18 19 21 51; }; node_77(){ either 18 11; }; node_78(){
required 75; }; node_77(){ required 47 24 12 37 60 76 50 61; }; node_78(){ required 77; }; node_79(){ required 48 25 13 38 62 78 51 63; }; node_80(){
optional 22; }; node_79(){ optional 23; }; node_80(){ required 47 41 42 21 78 79 optional 23; }; node_81(){ optional 24; }; node_82(){ required 48 42 43 22 80 81
}; node_81(){ required 47 41 54; }; node_82(){ required 47 41 32 21; } }; node_83(){ required 48 42 55; }; node_84(){ required 48 42 33 22; }
node_83(){ required 47 41 43 21; }; node_84(){ required 47 41 44 21; } node_85(){ required 48 42 44 22; }; node_86(){ required 48 42 45 22; }
node_85(){ required 47 45; }; node_86(){ node_87(){ required 48 46; }; node_88(){
either 52 55 56 57 58 62 64 65 66 69 70 71 73 74 77 80 81 82 83 84 85; } either 53 57 58 59 60 64 66 67 68 71 72 73 75 76 79 82 83 84 85 86 87; }
node_87(){ required 86; }; cat <<<' docopt_exit() { node_89(){ required 88; }; cat <<<' docopt_exit() {
[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:40:1052}" >&2 [[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:40:1063}" >&2
exit 1; }'; unset var___env var___version var___help var___stack var___server \ exit 1; }'; unset var___version var___stack var___env var___help var___server \
var___domain var___pass var___auto var___force var___user var___all var__app_ \ var___domain var___pass var___auto var___status var___force var___user \
var__domain_ var__service_ var__src_ var__dst_ var__args_ var__secret_ \ var___all var__app_ var__domain_ var__service_ var__src_ var__dst_ var__args_ \
var__version_ var__cmd_ var__data_ var__host_ var__user_ var__port_ var_app \ var__secret_ var__version_ var__cmd_ var__data_ var__host_ var__user_ \
var_new var_list var_ls var_deploy var_undeploy var_config var_delete var_rm \ var__port_ var_app var_new var_list var_ls var_deploy var_undeploy var_config \
var_logs var_multilogs var_cp var_run var_secret var_auto var_generate \ var_delete var_rm var_logs var_multilogs var_cp var_run var_secret var_auto \
var_insert var_server var_add var_use var_init var_upgrade; parse 87 "$@" var_generate var_insert var_server var_add var_use var_init var_upgrade
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__env" "${prefix}__version" \ parse 89 "$@"; local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__version" \
"${prefix}__help" "${prefix}__stack" "${prefix}__server" "${prefix}__domain" \ "${prefix}__stack" "${prefix}__env" "${prefix}__help" "${prefix}__server" \
"${prefix}__pass" "${prefix}__auto" "${prefix}__force" "${prefix}__user" \ "${prefix}__domain" "${prefix}__pass" "${prefix}__auto" "${prefix}__status" \
"${prefix}__all" "${prefix}_app_" "${prefix}_domain_" "${prefix}_service_" \ "${prefix}__force" "${prefix}__user" "${prefix}__all" "${prefix}_app_" \
"${prefix}_src_" "${prefix}_dst_" "${prefix}_args_" "${prefix}_secret_" \ "${prefix}_domain_" "${prefix}_service_" "${prefix}_src_" "${prefix}_dst_" \
"${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" "${prefix}_host_" \ "${prefix}_args_" "${prefix}_secret_" "${prefix}_version_" "${prefix}_cmd_" \
"${prefix}_user_" "${prefix}_port_" "${prefix}app" "${prefix}new" \ "${prefix}_data_" "${prefix}_host_" "${prefix}_user_" "${prefix}_port_" \
"${prefix}list" "${prefix}ls" "${prefix}deploy" "${prefix}undeploy" \ "${prefix}app" "${prefix}new" "${prefix}list" "${prefix}ls" "${prefix}deploy" \
"${prefix}config" "${prefix}delete" "${prefix}rm" "${prefix}logs" \ "${prefix}undeploy" "${prefix}config" "${prefix}delete" "${prefix}rm" \
"${prefix}multilogs" "${prefix}cp" "${prefix}run" "${prefix}secret" \ "${prefix}logs" "${prefix}multilogs" "${prefix}cp" "${prefix}run" \
"${prefix}auto" "${prefix}generate" "${prefix}insert" "${prefix}server" \ "${prefix}secret" "${prefix}auto" "${prefix}generate" "${prefix}insert" \
"${prefix}add" "${prefix}use" "${prefix}init" "${prefix}upgrade" "${prefix}server" "${prefix}add" "${prefix}use" "${prefix}init" \
eval "${prefix}"'__env=${var___env:-}' "${prefix}upgrade"; eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'__help=${var___help:-false}'
eval "${prefix}"'__stack=${var___stack:-}' eval "${prefix}"'__stack=${var___stack:-}'
eval "${prefix}"'__env=${var___env:-}'
eval "${prefix}"'__help=${var___help:-false}'
eval "${prefix}"'__server=${var___server:-}' eval "${prefix}"'__server=${var___server:-}'
eval "${prefix}"'__domain=${var___domain:-}' eval "${prefix}"'__domain=${var___domain:-}'
eval "${prefix}"'__pass=${var___pass:-false}' eval "${prefix}"'__pass=${var___pass:-false}'
eval "${prefix}"'__auto=${var___auto:-false}' eval "${prefix}"'__auto=${var___auto:-false}'
eval "${prefix}"'__status=${var___status:-false}'
eval "${prefix}"'__force=${var___force:-false}' eval "${prefix}"'__force=${var___force:-false}'
eval "${prefix}"'__user=${var___user:-}' eval "${prefix}"'__user=${var___user:-}'
eval "${prefix}"'__all=${var___all:-false}' eval "${prefix}"'__all=${var___all:-false}'
@ -245,18 +246,18 @@ eval "${prefix}"'add=${var_add:-false}'; eval "${prefix}"'use=${var_use:-false}'
eval "${prefix}"'init=${var_init:-false}' eval "${prefix}"'init=${var_init:-false}'
eval "${prefix}"'upgrade=${var_upgrade:-false}'; local docopt_i=1 eval "${prefix}"'upgrade=${var_upgrade:-false}'; local docopt_i=1
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do
declare -p "${prefix}__env" "${prefix}__version" "${prefix}__help" \ declare -p "${prefix}__version" "${prefix}__stack" "${prefix}__env" \
"${prefix}__stack" "${prefix}__server" "${prefix}__domain" "${prefix}__pass" \ "${prefix}__help" "${prefix}__server" "${prefix}__domain" "${prefix}__pass" \
"${prefix}__auto" "${prefix}__force" "${prefix}__user" "${prefix}__all" \ "${prefix}__auto" "${prefix}__status" "${prefix}__force" "${prefix}__user" \
"${prefix}_app_" "${prefix}_domain_" "${prefix}_service_" "${prefix}_src_" \ "${prefix}__all" "${prefix}_app_" "${prefix}_domain_" "${prefix}_service_" \
"${prefix}_dst_" "${prefix}_args_" "${prefix}_secret_" "${prefix}_version_" \ "${prefix}_src_" "${prefix}_dst_" "${prefix}_args_" "${prefix}_secret_" \
"${prefix}_cmd_" "${prefix}_data_" "${prefix}_host_" "${prefix}_user_" \ "${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" "${prefix}_host_" \
"${prefix}_port_" "${prefix}app" "${prefix}new" "${prefix}list" "${prefix}ls" \ "${prefix}_user_" "${prefix}_port_" "${prefix}app" "${prefix}new" \
"${prefix}deploy" "${prefix}undeploy" "${prefix}config" "${prefix}delete" \ "${prefix}list" "${prefix}ls" "${prefix}deploy" "${prefix}undeploy" \
"${prefix}rm" "${prefix}logs" "${prefix}multilogs" "${prefix}cp" \ "${prefix}config" "${prefix}delete" "${prefix}rm" "${prefix}logs" \
"${prefix}run" "${prefix}secret" "${prefix}auto" "${prefix}generate" \ "${prefix}multilogs" "${prefix}cp" "${prefix}run" "${prefix}secret" \
"${prefix}insert" "${prefix}server" "${prefix}add" "${prefix}use" \ "${prefix}auto" "${prefix}generate" "${prefix}insert" "${prefix}server" \
"${prefix}init" "${prefix}upgrade"; done; } "${prefix}add" "${prefix}use" "${prefix}init" "${prefix}upgrade"; done; }
# docopt parser above, complete command for generating this parser is `docopt.sh abra` # docopt parser above, complete command for generating this parser is `docopt.sh abra`
PROGRAM_NAME=$(basename "$0") PROGRAM_NAME=$(basename "$0")
@ -440,15 +441,33 @@ sub_app_list (){
ENV_FILES=(~/.abra/servers/*/*.env) ENV_FILES=(~/.abra/servers/*/*.env)
shopt -u nullglob dotglob shopt -u nullglob dotglob
STATUS="${abra___status:+STATUS}"
if [ -n "$STATUS" ]; then
get_servers
local -a DEPLOYED_APPS # array
warning "Loading status from ${#SERVERS[@]} servers, patience advised.."
for SERVER in "${SERVERS[@]}"; do
SERVER="${SERVER##*/}" # basename
mapfile -t SERVER_APPS < <(DOCKER_CONTEXT="$SERVER" docker stack ls --format '{{ .Name }}')
# add $SERVER~ to the start of each DEPLOYED_APPS
DEPLOYED_APPS+=("${SERVER_APPS[@]/#/$SERVER~}")
done
fi
printf "%s lovely apps:\n\n" "${#ENV_FILES[@]}" printf "%s lovely apps:\n\n" "${#ENV_FILES[@]}"
for i in "${!ENV_FILES[@]}"; do for i in "${!ENV_FILES[@]}"; do
# Do this inside the loop to include in pipe to `column` # Output header inside the loop, so it's included in the pipe to `column`
if [ "$i" == 0 ]; then if [ "$i" == 0 ]; then
printf " DOMAIN\tAPP\tSERVER\n --\t--\t--\n" printf " DOMAIN\tAPP\tSERVER%s%s\n" "${STATUS:+ }" "${STATUS}"
printf " --\t--\t--%s\n" "${STATUS:+ --}"
fi fi
local ENV_FILE="${ENV_FILES[$i]}" local ENV_FILE="${ENV_FILES[$i]}" APP_STACK_NAME
IFS='/' read -r -a PARTS <<< "$ENV_FILE" IFS='/' read -r -a PARTS <<< "$ENV_FILE"
@ -457,7 +476,15 @@ sub_app_list (){
DOMAIN="${FILE%.env}" DOMAIN="${FILE%.env}"
# shellcheck disable=SC1090 # shellcheck disable=SC1090
APP="$(source "$ENV_FILE" && echo "$APP")" APP="$(source "$ENV_FILE" && echo "$APP")"
printf " %s\t%s\t%s\n" "$DOMAIN" "$APP" "$SERVER" # shellcheck disable=SC1090
APP_STACK_NAME="$(source "$ENV_FILE" && echo "$STACK_NAME")"
if [ -z "$STACK_NAME" ]; then
APP_STACK_NAME="${DOMAIN//./_}"
fi
if [ -n "$STATUS" ]; then
APP_STATUS=$(printf '%s\n' "${DEPLOYED_APPS[@]}" | grep -P "^${SERVER}~${APP_STACK_NAME}$" && echo "true")
fi
printf " %s\t%s\t%s%s\n" "$DOMAIN" "$APP" "$SERVER" "${STATUS:+ }${APP_STATUS:+deployed}"
done | column -s' ' -t done | column -s' ' -t
# Align table `-t` based on tab characters -s`^V<Tab>` # Align table `-t` based on tab characters -s`^V<Tab>`
} }
@ -594,7 +621,7 @@ sub_app_delete (){
load_instance_env load_instance_env
if [ "$abra___force" != "true" ]; then if [ "$abra___force" != "true" ]; then
warning "Delete $ENV_FILE?" warning "About to delete $ENV_FILE"
prompt_confirm prompt_confirm
fi fi
@ -903,7 +930,7 @@ abra() {
abra__secret_ abra__version_ abra__data_ abra___user abra__host_ \ abra__secret_ abra__version_ abra__data_ abra___user abra__host_ \
abra__app_ abra__port_ abra__user_ abra__service_ abra__src_ abra__dst_ \ abra__app_ abra__port_ abra__user_ abra__service_ abra__src_ abra__dst_ \
abra__domain_ abra___server abra___domain abra___force abra___pass \ abra__domain_ abra___server abra___domain abra___force abra___pass \
abra___auto abra___auto abra___status
if ! type tput > /dev/null 2>&1; then if ! type tput > /dev/null 2>&1; then
tput() { tput() {