diff --git a/abra b/abra index ecf268b..e05ae5f 100755 --- a/abra +++ b/abra @@ -43,8 +43,10 @@ Options: -e, --env= Environment variables to load -h, --help Show this message and exit -s, --stack= Name of the target stack - -C, --skip-check Don't verify app variables - -U, --skip-update Don't pull latest app definitions + -C, --skip-check Don't verify app variables + -U, --skip-update Don't pull latest app definitions + -v, --verbose Show INFO messages + -d, --debug Show DEBUG messages See 'abra help ...' to read about a specific subcommand. " @@ -148,79 +150,80 @@ 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:1696} -usage=${DOC:40:1320}; digest=563d5 -shorts=(-h -s -e -U -C '' '' '' '' '' '' '' '' '' '') -longs=(--help --stack --env --skip-update --skip-check --status --server --domain --app-name --pass --secrets --all --force --no-tty --user) -argcounts=(0 1 1 0 0 0 1 1 1 0 0 0 0 0 1); node_0(){ switch __help 0; } -node_1(){ value __stack 1; }; node_2(){ value __env 2; }; node_3(){ -switch __skip_update 3; }; node_4(){ switch __skip_check 4; }; node_5(){ -switch __status 5; }; node_6(){ value __server 6; }; node_7(){ value __domain 7 -}; node_8(){ value __app_name 8; }; node_9(){ switch __pass 9; }; node_10(){ -switch __secrets 10; }; node_11(){ switch __all 11; }; node_12(){ -switch __force 12; }; node_13(){ switch __no_tty 13; }; node_14(){ -value __user 14; }; node_15(){ value _type_ a; }; node_16(){ value _app_ a; } -node_17(){ value _service_ a; }; node_18(){ value _src_ a; }; node_19(){ -value _dst_ a; }; node_20(){ value _backup_file_ a; }; node_21(){ -value _args_ a true; }; node_22(){ value _secret_ a; }; node_23(){ -value _version_ a; }; node_24(){ value _cmd_ a; }; node_25(){ value _data_ a; } -node_26(){ value _command_ a; }; node_27(){ value _host_ a; }; node_28(){ -value _user_ a; }; node_29(){ value _port_ a; }; node_30(){ -value _subcommands_ a true; }; node_31(){ _command app; }; node_32(){ -_command list; }; node_33(){ _command ls; }; node_34(){ _command new; } -node_35(){ _command backup; }; node_36(){ _command deploy; }; node_37(){ -_command check; }; node_38(){ _command config; }; node_39(){ _command cp; } -node_40(){ _command logs; }; node_41(){ _command ps; }; node_42(){ -_command restore; }; node_43(){ _command rm; }; node_44(){ _command delete; } -node_45(){ _command run; }; node_46(){ _command secret; }; node_47(){ -_command generate; }; node_48(){ _command insert; }; node_49(){ -_command undeploy; }; node_50(){ _command server; }; node_51(){ _command add; } -node_52(){ _command init; }; node_53(){ _command apps; }; node_54(){ -_command upgrade; }; node_55(){ _command version; }; node_56(){ _command help; } -node_57(){ optional 0 1 2 3 4; }; node_58(){ optional 57; }; node_59(){ -either 32 33; }; node_60(){ required 59; }; node_61(){ optional 5; }; node_62(){ -optional 6; }; node_63(){ required 58 31 60 61 62; }; node_64(){ optional 7; } -node_65(){ optional 8; }; node_66(){ optional 9; }; node_67(){ optional 10; } -node_68(){ required 58 31 34 62 64 65 66 67 15; }; node_69(){ either 17 11; } -node_70(){ required 69; }; node_71(){ required 58 31 16 35 70; }; node_72(){ -required 58 31 16 36; }; node_73(){ required 58 31 16 37; }; node_74(){ -required 58 31 16 38; }; node_75(){ required 58 31 16 39 18 19; }; node_76(){ -optional 17; }; node_77(){ required 58 31 16 40 76; }; node_78(){ -required 58 31 16 41; }; node_79(){ required 58 31 16 42 70; }; node_80(){ -either 43 44; }; node_81(){ required 80; }; node_82(){ optional 12; } -node_83(){ required 58 31 16 81 82; }; node_84(){ optional 20; }; node_85(){ -required 58 31 16 42 17 84; }; node_86(){ optional 13; }; node_87(){ optional 14 -}; node_88(){ oneormore 21; }; node_89(){ required 58 31 16 45 86 87 17 88; } -node_90(){ required 22 23; }; node_91(){ either 90 11; }; node_92(){ required 91 -}; node_93(){ optional 24; }; node_94(){ required 58 31 16 46 47 92 93 66; } -node_95(){ required 58 31 16 46 48 22 23 25 66; }; node_96(){ either 22 11; } -node_97(){ required 96; }; node_98(){ required 58 31 16 46 81 97 66 82; } -node_99(){ required 58 31 16 49; }; node_100(){ optional 88; }; node_101(){ -required 58 31 16 26 100; }; node_102(){ optional 28; }; node_103(){ optional 29 -}; node_104(){ required 58 50 51 27 102 103; }; node_105(){ required 58 50 60; } -node_106(){ required 58 50 27 43; }; node_107(){ required 58 50 27 52; } -node_108(){ required 58 50 27 53 61; }; node_109(){ required 58 54; } -node_110(){ required 58 55; }; node_111(){ oneormore 30; }; node_112(){ -optional 111; }; node_113(){ required 58 56 112; }; node_114(){ -either 63 68 71 72 73 74 75 77 78 79 83 85 89 94 95 98 99 101 104 105 106 107 108 109 110 113 -}; node_115(){ required 114; }; cat <<<' docopt_exit() { +printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1783} +usage=${DOC:40:1320}; digest=616a5 +shorts=(-C -v -d -s -e -h -U '' '' '' '' '' '' '' '' '' '') +longs=(--skip-check --verbose --debug --stack --env --help --skip-update --status --server --domain --app-name --pass --secrets --all --force --no-tty --user) +argcounts=(0 0 0 1 1 0 0 0 1 1 1 0 0 0 0 0 1); node_0(){ switch __skip_check 0 +}; node_1(){ switch __verbose 1; }; node_2(){ switch __debug 2; }; node_3(){ +value __stack 3; }; node_4(){ value __env 4; }; node_5(){ switch __help 5; } +node_6(){ switch __skip_update 6; }; node_7(){ switch __status 7; }; node_8(){ +value __server 8; }; node_9(){ value __domain 9; }; node_10(){ +value __app_name 10; }; node_11(){ switch __pass 11; }; node_12(){ +switch __secrets 12; }; node_13(){ switch __all 13; }; node_14(){ +switch __force 14; }; node_15(){ switch __no_tty 15; }; node_16(){ +value __user 16; }; node_17(){ value _type_ a; }; node_18(){ value _app_ a; } +node_19(){ value _service_ a; }; node_20(){ value _src_ a; }; node_21(){ +value _dst_ a; }; node_22(){ value _backup_file_ a; }; node_23(){ +value _args_ a true; }; node_24(){ value _secret_ a; }; node_25(){ +value _version_ a; }; node_26(){ value _cmd_ a; }; node_27(){ value _data_ a; } +node_28(){ value _command_ a; }; node_29(){ value _host_ a; }; node_30(){ +value _user_ a; }; node_31(){ value _port_ a; }; node_32(){ +value _subcommands_ a true; }; node_33(){ _command app; }; node_34(){ +_command list; }; node_35(){ _command ls; }; node_36(){ _command new; } +node_37(){ _command backup; }; node_38(){ _command deploy; }; node_39(){ +_command check; }; node_40(){ _command config; }; node_41(){ _command cp; } +node_42(){ _command logs; }; node_43(){ _command ps; }; node_44(){ +_command restore; }; node_45(){ _command rm; }; node_46(){ _command delete; } +node_47(){ _command run; }; node_48(){ _command secret; }; node_49(){ +_command generate; }; node_50(){ _command insert; }; node_51(){ +_command undeploy; }; node_52(){ _command server; }; node_53(){ _command add; } +node_54(){ _command init; }; node_55(){ _command apps; }; node_56(){ +_command upgrade; }; node_57(){ _command version; }; node_58(){ _command help; } +node_59(){ optional 0 1 2 3 4 5 6; }; node_60(){ optional 59; }; node_61(){ +either 34 35; }; node_62(){ required 61; }; node_63(){ optional 7; }; node_64(){ +optional 8; }; node_65(){ required 60 33 62 63 64; }; node_66(){ optional 9; } +node_67(){ optional 10; }; node_68(){ optional 11; }; node_69(){ optional 12; } +node_70(){ required 60 33 36 64 66 67 68 69 17; }; node_71(){ either 19 13; } +node_72(){ required 71; }; node_73(){ required 60 33 18 37 72; }; node_74(){ +required 60 33 18 38; }; node_75(){ required 60 33 18 39; }; node_76(){ +required 60 33 18 40; }; node_77(){ required 60 33 18 41 20 21; }; node_78(){ +optional 19; }; node_79(){ required 60 33 18 42 78; }; node_80(){ +required 60 33 18 43; }; node_81(){ required 60 33 18 44 72; }; node_82(){ +either 45 46; }; node_83(){ required 82; }; node_84(){ optional 14; } +node_85(){ required 60 33 18 83 84; }; node_86(){ optional 22; }; node_87(){ +required 60 33 18 44 19 86; }; node_88(){ optional 15; }; node_89(){ optional 16 +}; node_90(){ oneormore 23; }; node_91(){ required 60 33 18 47 88 89 19 90; } +node_92(){ required 24 25; }; node_93(){ either 92 13; }; node_94(){ required 93 +}; node_95(){ optional 26; }; node_96(){ required 60 33 18 48 49 94 95 68; } +node_97(){ required 60 33 18 48 50 24 25 27 68; }; node_98(){ either 24 13; } +node_99(){ required 98; }; node_100(){ required 60 33 18 48 83 99 68 84; } +node_101(){ required 60 33 18 51; }; node_102(){ optional 90; }; node_103(){ +required 60 33 18 28 102; }; node_104(){ optional 30; }; node_105(){ optional 31 +}; node_106(){ required 60 52 53 29 104 105; }; node_107(){ required 60 52 62; } +node_108(){ required 60 52 29 45; }; node_109(){ required 60 52 29 54; } +node_110(){ required 60 52 29 55 63; }; node_111(){ required 60 56; } +node_112(){ required 60 57; }; node_113(){ oneormore 32; }; node_114(){ +optional 113; }; node_115(){ required 60 58 114; }; node_116(){ +either 65 70 73 74 75 76 77 79 80 81 85 87 91 96 97 100 101 103 106 107 108 109 110 111 112 115 +}; node_117(){ required 116; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:40:1320}" >&2 -exit 1; }'; unset var___help var___stack var___env var___skip_update \ -var___skip_check var___status var___server var___domain var___app_name \ -var___pass var___secrets var___all var___force var___no_tty var___user \ -var__type_ var__app_ var__service_ var__src_ var__dst_ var__backup_file_ \ -var__args_ var__secret_ var__version_ var__cmd_ var__data_ var__command_ \ -var__host_ var__user_ var__port_ var__subcommands_ var_app var_list var_ls \ -var_new var_backup var_deploy var_check var_config var_cp var_logs var_ps \ -var_restore var_rm var_delete var_run var_secret var_generate var_insert \ -var_undeploy var_server var_add var_init var_apps var_upgrade var_version \ -var_help; parse 115 "$@"; local prefix=${DOCOPT_PREFIX:-''} -unset "${prefix}__help" "${prefix}__stack" "${prefix}__env" \ -"${prefix}__skip_update" "${prefix}__skip_check" "${prefix}__status" \ -"${prefix}__server" "${prefix}__domain" "${prefix}__app_name" \ -"${prefix}__pass" "${prefix}__secrets" "${prefix}__all" "${prefix}__force" \ -"${prefix}__no_tty" "${prefix}__user" "${prefix}_type_" "${prefix}_app_" \ -"${prefix}_service_" "${prefix}_src_" "${prefix}_dst_" \ +exit 1; }'; unset var___skip_check var___verbose var___debug var___stack \ +var___env var___help var___skip_update var___status var___server var___domain \ +var___app_name var___pass var___secrets var___all var___force var___no_tty \ +var___user var__type_ var__app_ var__service_ var__src_ var__dst_ \ +var__backup_file_ var__args_ var__secret_ var__version_ var__cmd_ var__data_ \ +var__command_ var__host_ var__user_ var__port_ var__subcommands_ var_app \ +var_list var_ls var_new var_backup var_deploy var_check var_config var_cp \ +var_logs var_ps var_restore var_rm var_delete var_run var_secret var_generate \ +var_insert var_undeploy var_server var_add var_init var_apps var_upgrade \ +var_version var_help; parse 117 "$@"; local prefix=${DOCOPT_PREFIX:-''} +unset "${prefix}__skip_check" "${prefix}__verbose" "${prefix}__debug" \ +"${prefix}__stack" "${prefix}__env" "${prefix}__help" "${prefix}__skip_update" \ +"${prefix}__status" "${prefix}__server" "${prefix}__domain" \ +"${prefix}__app_name" "${prefix}__pass" "${prefix}__secrets" "${prefix}__all" \ +"${prefix}__force" "${prefix}__no_tty" "${prefix}__user" "${prefix}_type_" \ +"${prefix}_app_" "${prefix}_service_" "${prefix}_src_" "${prefix}_dst_" \ "${prefix}_backup_file_" "${prefix}_args_" "${prefix}_secret_" \ "${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" "${prefix}_command_" \ "${prefix}_host_" "${prefix}_user_" "${prefix}_port_" "${prefix}_subcommands_" \ @@ -230,11 +233,13 @@ unset "${prefix}__help" "${prefix}__stack" "${prefix}__env" \ "${prefix}delete" "${prefix}run" "${prefix}secret" "${prefix}generate" \ "${prefix}insert" "${prefix}undeploy" "${prefix}server" "${prefix}add" \ "${prefix}init" "${prefix}apps" "${prefix}upgrade" "${prefix}version" \ -"${prefix}help"; eval "${prefix}"'__help=${var___help:-false}' +"${prefix}help"; eval "${prefix}"'__skip_check=${var___skip_check:-false}' +eval "${prefix}"'__verbose=${var___verbose:-false}' +eval "${prefix}"'__debug=${var___debug:-false}' eval "${prefix}"'__stack=${var___stack:-}' eval "${prefix}"'__env=${var___env:-}' +eval "${prefix}"'__help=${var___help:-false}' eval "${prefix}"'__skip_update=${var___skip_update:-false}' -eval "${prefix}"'__skip_check=${var___skip_check:-false}' eval "${prefix}"'__status=${var___status:-false}' eval "${prefix}"'__server=${var___server:-}' eval "${prefix}"'__domain=${var___domain:-}' @@ -285,12 +290,12 @@ eval "${prefix}"'upgrade=${var_upgrade:-false}' eval "${prefix}"'version=${var_version:-false}' eval "${prefix}"'help=${var_help:-false}'; local docopt_i=1 [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do -declare -p "${prefix}__help" "${prefix}__stack" "${prefix}__env" \ -"${prefix}__skip_update" "${prefix}__skip_check" "${prefix}__status" \ -"${prefix}__server" "${prefix}__domain" "${prefix}__app_name" \ -"${prefix}__pass" "${prefix}__secrets" "${prefix}__all" "${prefix}__force" \ -"${prefix}__no_tty" "${prefix}__user" "${prefix}_type_" "${prefix}_app_" \ -"${prefix}_service_" "${prefix}_src_" "${prefix}_dst_" \ +declare -p "${prefix}__skip_check" "${prefix}__verbose" "${prefix}__debug" \ +"${prefix}__stack" "${prefix}__env" "${prefix}__help" "${prefix}__skip_update" \ +"${prefix}__status" "${prefix}__server" "${prefix}__domain" \ +"${prefix}__app_name" "${prefix}__pass" "${prefix}__secrets" "${prefix}__all" \ +"${prefix}__force" "${prefix}__no_tty" "${prefix}__user" "${prefix}_type_" \ +"${prefix}_app_" "${prefix}_service_" "${prefix}_src_" "${prefix}_dst_" \ "${prefix}_backup_file_" "${prefix}_args_" "${prefix}_secret_" \ "${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" "${prefix}_command_" \ "${prefix}_host_" "${prefix}_user_" "${prefix}_port_" "${prefix}_subcommands_" \ @@ -324,6 +329,20 @@ success() { echo "$(tput setaf 2)SUCCESS: $*$(tput sgr0)" } +info() { + if [ "$abra___verbose" = "false" ] && [ "$abra___debug" = "false" ]; then + return + fi + echo "$(tput setaf 4)INFO: $*$(tput sgr0)" +} + +debug() { + if [ "$abra___debug" = "false" ]; then + return + fi + echo "$(tput setaf 13)DEBUG: $*$(tput sgr0)" +} + ###### Default settings if [ -z "$COMPOSE_FILE" ]; then @@ -358,6 +377,8 @@ require_app_latest() { APP="$1" APP_DIR="$ABRA_DIR/apps/$APP" + debug "Checking for type '$APP'" + if [ ! -d "$APP_DIR" ]; then warning "The app type '$APP' was not found, fetching via Git" if ! git clone "$GIT_URL/$APP.git" "$ABRA_DIR/apps/$APP" > /dev/null 2>&1 ; then @@ -366,8 +387,12 @@ require_app_latest() { success "Fetched app configuration via Git" fi + debug "Using $APP_DIR" + if [ "$abra___skip_update" = "false" ]; then (cd "$APP_DIR" && git pull > /dev/null 2>&1) + else + info "Skipping \`git pull\`" fi } @@ -382,6 +407,7 @@ load_custom_commands() { if [ -f abra-commands.sh ]; then # shellcheck disable=SC1091 source abra-commands.sh + info "Loading custom commands from abra-commands.sh" fi if [ -n "$abra__app_" ]; then @@ -391,6 +417,7 @@ load_custom_commands() { fi if [ -f "$APP_DIR/abra-commands.sh" ]; then + info "Loading custom commands from $APP_DIR/abra-commands.sh" # shellcheck disable=SC1090,SC1091 source "$APP_DIR/abra-commands.sh" fi @@ -412,33 +439,48 @@ get_app_secrets() { load_instance() { APP="$abra__app_" - # FIXME 3wc: requires bash 4, use for loop instead + + # load all files matching "$APP.env" into ENV_FILES array mapfile -t ENV_FILES < <(find -L "$ABRA_DIR" -name "$APP.env") + # FIXME 3wc: requires bash 4, use for loop instead + case "${#ENV_FILES[@]}" in 1 ) ;; 0 ) error "Can't find app '$APP'"; return;; * ) error "Found $APP in multiple servers: ${ENV_FILES[*]}"; return;; esac + ENV_FILE="${ENV_FILES[0]}" + debug "Selected ENV_FILE $ENV_FILE" + if [ ! -f "$ENV_FILE" ]; then error "Can't open ENV_FILE '$ENV_FILE'" fi + + # split up the path by "/" IFS='/' read -r -a PARTS <<< "$ENV_FILE" + SERVER="${PARTS[-2]}" export STACK_NAME="${APP//./_}" } load_instance_env() { + # 3wc: using set -a means we don't need `export` in the env files set -a # shellcheck disable=SC1090 source "$ENV_FILE" set +a + debug "Loaded variables from $ENV_FILE" + if [ -z "$TYPE" ]; then error "TYPE not set, maybe $ENV_FILE is using an old format?" fi + APP_DIR="$ABRA_DIR/apps/$TYPE" export DOCKER_CONTEXT="$SERVER" + info "Setting DOCKER_CONTEXT=$DOCKER_CONTEXT" + export DOMAIN } @@ -860,8 +902,12 @@ sub_app_check (){ load_instance load_instance_env - APP_ENV=$(grep -v '^#' "$ENV_FILE" | cut -d' ' -f2 | cut -d'=' -f1 | sort) - STACK_ENV=$(grep -v '^#' "$APP_DIR/.env.sample" | cut -d' ' -f2 | cut -d'=' -f1 | sort) + APP_ENV=$(grep -v '^#' "$ENV_FILE" | cut -d' ' -f2 | cut -d'=' -f1 | sort -u) + STACK_ENV=$(grep -v '^#' "$APP_DIR/.env.sample" | cut -d' ' -f2 | cut -d'=' -f1 | sort -u) + + debug "APP_ENV: $APP_ENV" + + debug "STACK_ENV: $STACK_ENV" # Only show "1", items in STACK_ENV which aren't in APP_ENV MISSING_VARS=$(comm -23 <(echo "$STACK_ENV") <(echo "$APP_ENV")) @@ -1096,6 +1142,8 @@ sub_app_run(){ exit fi + debug "Using Container ID ${CONTAINER}" + # 3wc: we want the "splitting" that shellcheck warns us about, so that -u and # $RUN_USER aren't treated as a single argument: # shellcheck disable=SC2086 @@ -1199,6 +1247,8 @@ sub_app_cp() { exit fi + debug "Using Container ID ${CONTAINER}" + # Replace $SERVICE with $CONTAINER in the original args CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE:/$CONTAINER:/") # FIXME 3wc: this might cause problems for filenames with spaces.. @@ -1413,7 +1463,7 @@ abra() { abra___server abra___domain abra___force abra___pass \ abra___secrets abra___status abra___no_tty abra___app_name \ abra__subcommands_ abra___skip_update abra___skip_check \ - abra__backup_file_ + abra__backup_file_ abra___verbose abra___debug if ! type tput > /dev/null 2>&1; then tput() {