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