From 152dfe93496c9c515be775f13957f9c75260b458 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Tue, 2 Mar 2021 13:05:39 +0100 Subject: [PATCH] Support basic version checking Closes https://git.autonomic.zone/coop-cloud/abra/issues/82. --- abra | 235 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 134 insertions(+), 101 deletions(-) diff --git a/abra b/abra index b5a26aa5..6fd58800 100755 --- a/abra +++ b/abra @@ -16,7 +16,7 @@ Usage: abra [options] app (list|ls) [--status] [--server=] abra [options] app new [--server=] [--domain=] [--app-name=] [--pass] [--secrets] abra [options] app backup (|--all) - abra [options] app deploy + abra [options] app deploy [--update] abra [options] app check abra [options] app config abra [options] app cp @@ -152,96 +152,98 @@ 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:1800} -usage=${DOC:40:1337}; digest=55732 -shorts=(-U -h -e -v -s -C -d '' '' '' '' '' '' '' '' '' '') -longs=(--skip-update --help --env --verbose --stack --skip-check --debug --status --server --domain --app-name --pass --secrets --all --force --no-tty --user) -argcounts=(0 0 1 0 1 0 0 0 1 1 1 0 0 0 0 0 1); node_0(){ switch __skip_update 0 -}; node_1(){ switch __help 1; }; node_2(){ value __env 2; }; node_3(){ -switch __verbose 3; }; node_4(){ value __stack 4; }; node_5(){ -switch __skip_check 5; }; node_6(){ switch __debug 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(){ +printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1811} +usage=${DOC:40:1348}; digest=4fa25 +shorts=(-v -s -C -U -h -e -d '' '' '' '' '' '' '' '' '' '' '') +longs=(--verbose --stack --skip-check --skip-update --help --env --debug --status --server --domain --app-name --pass --secrets --all --update --force --no-tty --user) +argcounts=(0 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 1); node_0(){ switch __verbose 0; } +node_1(){ value __stack 1; }; node_2(){ switch __skip_check 2; }; node_3(){ +switch __skip_update 3; }; node_4(){ switch __help 4; }; node_5(){ value __env 5 +}; node_6(){ switch __debug 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(){ required 60; } -node_117(){ -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 116 -}; node_118(){ required 117; }; cat <<<' docopt_exit() { -[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:40:1337}" >&2 -exit 1; }'; unset var___skip_update var___help var___env var___verbose \ -var___stack var___skip_check var___debug var___status var___server \ -var___domain var___app_name var___pass var___secrets var___all var___force \ +switch __update 14; }; node_15(){ switch __force 15; }; node_16(){ +switch __no_tty 16; }; node_17(){ value __user 17; }; node_18(){ value _type_ a +}; node_19(){ value _app_ a; }; node_20(){ value _service_ a; }; node_21(){ +value _src_ a; }; node_22(){ value _dst_ a; }; node_23(){ value _backup_file_ a +}; node_24(){ value _args_ a true; }; node_25(){ value _secret_ a; }; node_26(){ +value _version_ a; }; node_27(){ value _cmd_ a; }; node_28(){ value _data_ a; } +node_29(){ value _command_ a; }; node_30(){ value _host_ a; }; node_31(){ +value _user_ a; }; node_32(){ value _port_ a; }; node_33(){ +value _subcommands_ a true; }; node_34(){ _command app; }; node_35(){ +_command list; }; node_36(){ _command ls; }; node_37(){ _command new; } +node_38(){ _command backup; }; node_39(){ _command deploy; }; node_40(){ +_command check; }; node_41(){ _command config; }; node_42(){ _command cp; } +node_43(){ _command logs; }; node_44(){ _command ps; }; node_45(){ +_command restore; }; node_46(){ _command rm; }; node_47(){ _command delete; } +node_48(){ _command run; }; node_49(){ _command secret; }; node_50(){ +_command generate; }; node_51(){ _command insert; }; node_52(){ +_command undeploy; }; node_53(){ _command server; }; node_54(){ _command add; } +node_55(){ _command init; }; node_56(){ _command apps; }; node_57(){ +_command upgrade; }; node_58(){ _command version; }; node_59(){ _command help; } +node_60(){ optional 0 1 2 3 4 5 6; }; node_61(){ optional 60; }; node_62(){ +either 35 36; }; node_63(){ required 62; }; node_64(){ optional 7; }; node_65(){ +optional 8; }; node_66(){ required 61 34 63 64 65; }; node_67(){ optional 9; } +node_68(){ optional 10; }; node_69(){ optional 11; }; node_70(){ optional 12; } +node_71(){ required 61 34 37 65 67 68 69 70 18; }; node_72(){ either 20 13; } +node_73(){ required 72; }; node_74(){ required 61 34 19 38 73; }; node_75(){ +optional 14; }; node_76(){ required 61 34 19 39 75; }; node_77(){ +required 61 34 19 40; }; node_78(){ required 61 34 19 41; }; node_79(){ +required 61 34 19 42 21 22; }; node_80(){ optional 20; }; node_81(){ +required 61 34 19 43 80; }; node_82(){ required 61 34 19 44; }; node_83(){ +required 61 34 19 45 73; }; node_84(){ either 46 47; }; node_85(){ required 84 +}; node_86(){ optional 15; }; node_87(){ required 61 34 19 85 86; }; node_88(){ +optional 23; }; node_89(){ required 61 34 19 45 20 88; }; node_90(){ optional 16 +}; node_91(){ optional 17; }; node_92(){ oneormore 24; }; node_93(){ +required 61 34 19 48 90 91 20 92; }; node_94(){ required 25 26; }; node_95(){ +either 94 13; }; node_96(){ required 95; }; node_97(){ optional 27; } +node_98(){ required 61 34 19 49 50 96 97 69; }; node_99(){ +required 61 34 19 49 51 25 26 28 69; }; node_100(){ either 25 13; }; node_101(){ +required 100; }; node_102(){ required 61 34 19 49 85 101 69 86; }; node_103(){ +required 61 34 19 52; }; node_104(){ optional 92; }; node_105(){ +required 61 34 19 29 104; }; node_106(){ optional 31; }; node_107(){ optional 32 +}; node_108(){ required 61 53 54 30 106 107; }; node_109(){ required 61 53 63; } +node_110(){ required 61 53 30 46; }; node_111(){ required 61 53 30 55; } +node_112(){ required 61 53 30 56 64; }; node_113(){ required 61 57; } +node_114(){ required 61 58; }; node_115(){ oneormore 33; }; node_116(){ +optional 115; }; node_117(){ required 61 59 116; }; node_118(){ required 61; } +node_119(){ +either 66 71 74 76 77 78 79 81 82 83 87 89 93 98 99 102 103 105 108 109 110 111 112 113 114 117 118 +}; node_120(){ required 119; }; cat <<<' docopt_exit() { +[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:40:1348}" >&2 +exit 1; }'; unset var___verbose var___stack var___skip_check var___skip_update \ +var___help var___env var___debug var___status var___server var___domain \ +var___app_name var___pass var___secrets var___all var___update 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 118 "$@"; local prefix=${DOCOPT_PREFIX:-''} -unset "${prefix}__skip_update" "${prefix}__help" "${prefix}__env" \ -"${prefix}__verbose" "${prefix}__stack" "${prefix}__skip_check" \ -"${prefix}__debug" "${prefix}__status" "${prefix}__server" "${prefix}__domain" \ +var_version var_help; parse 120 "$@"; local prefix=${DOCOPT_PREFIX:-''} +unset "${prefix}__verbose" "${prefix}__stack" "${prefix}__skip_check" \ +"${prefix}__skip_update" "${prefix}__help" "${prefix}__env" "${prefix}__debug" \ +"${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_" \ -"${prefix}app" "${prefix}list" "${prefix}ls" "${prefix}new" "${prefix}backup" \ -"${prefix}deploy" "${prefix}check" "${prefix}config" "${prefix}cp" \ -"${prefix}logs" "${prefix}ps" "${prefix}restore" "${prefix}rm" \ -"${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}"'__skip_update=${var___skip_update:-false}' -eval "${prefix}"'__help=${var___help:-false}' -eval "${prefix}"'__env=${var___env:-}' +"${prefix}__update" "${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_" "${prefix}app" "${prefix}list" "${prefix}ls" \ +"${prefix}new" "${prefix}backup" "${prefix}deploy" "${prefix}check" \ +"${prefix}config" "${prefix}cp" "${prefix}logs" "${prefix}ps" \ +"${prefix}restore" "${prefix}rm" "${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}"'__verbose=${var___verbose:-false}' eval "${prefix}"'__stack=${var___stack:-}' eval "${prefix}"'__skip_check=${var___skip_check:-false}' +eval "${prefix}"'__skip_update=${var___skip_update:-false}' +eval "${prefix}"'__help=${var___help:-false}' +eval "${prefix}"'__env=${var___env:-}' eval "${prefix}"'__debug=${var___debug:-false}' eval "${prefix}"'__status=${var___status:-false}' eval "${prefix}"'__server=${var___server:-}' @@ -250,6 +252,7 @@ eval "${prefix}"'__app_name=${var___app_name:-}' eval "${prefix}"'__pass=${var___pass:-false}' eval "${prefix}"'__secrets=${var___secrets:-false}' eval "${prefix}"'__all=${var___all:-false}' +eval "${prefix}"'__update=${var___update:-false}' eval "${prefix}"'__force=${var___force:-false}' eval "${prefix}"'__no_tty=${var___no_tty:-false}' eval "${prefix}"'__user=${var___user:-}' @@ -293,22 +296,22 @@ 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}__skip_update" "${prefix}__help" "${prefix}__env" \ -"${prefix}__verbose" "${prefix}__stack" "${prefix}__skip_check" \ -"${prefix}__debug" "${prefix}__status" "${prefix}__server" "${prefix}__domain" \ +declare -p "${prefix}__verbose" "${prefix}__stack" "${prefix}__skip_check" \ +"${prefix}__skip_update" "${prefix}__help" "${prefix}__env" "${prefix}__debug" \ +"${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_" \ -"${prefix}app" "${prefix}list" "${prefix}ls" "${prefix}new" "${prefix}backup" \ -"${prefix}deploy" "${prefix}check" "${prefix}config" "${prefix}cp" \ -"${prefix}logs" "${prefix}ps" "${prefix}restore" "${prefix}rm" \ -"${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"; done; } +"${prefix}__update" "${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_" "${prefix}app" "${prefix}list" "${prefix}ls" \ +"${prefix}new" "${prefix}backup" "${prefix}deploy" "${prefix}check" \ +"${prefix}config" "${prefix}cp" "${prefix}logs" "${prefix}ps" \ +"${prefix}restore" "${prefix}rm" "${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"; done; } # docopt parser above, complete command for generating this parser is `docopt.sh abra` PROGRAM_NAME=$(basename "$0") @@ -863,11 +866,11 @@ _abra_backup_mysql() { ###### .. app deploy help_app_deploy (){ - echo "abra [options] app deploy [--skip-check] + echo "abra [options] app deploy [--update] [--skip-check] -Deploy app to the configured server. + Deploy app to the configured server. -POWERED BY + POWERED BY docker stack deploy -c compose.yml " } @@ -877,7 +880,32 @@ sub_app_deploy (){ require_app_latest "$TYPE" - echo "About to deploy:" + CONSENT_TO_UPDATE=$abra___update + + deployed_version_digest=$(docker service inspect \ + -f "{{index .Spec.Labels \"coop-cloud.${STACK_NAME}.version\" }}" "${STACK_NAME}_app") + deployed_version=$(echo "$deployed_version_digest" | cut -d- -f1) + deployed_digest=$(echo "$deployed_version_digest" | cut -d- -f2) + + if [ "$CONSENT_TO_UPDATE" = "false" ] && [ "$ABRA_TYPE_VERSION" != "$deployed_version" ]; then + error "This deployment will be an app update from version ${deployed_version} -> ${ABRA_TYPE_VERSION} + Please pass the --update option to the deploy to deploy the update" + fi + + if [ "$CONSENT_TO_UPDATE" = "false" ] && \ + [ "$ABRA_TYPE_VERSION" = "$deployed_version" ] && \ + [ "$ABRA_TYPE_DIGEST" != "$deployed_digest" ]; then + error "The app version is the same but the image digest has changed from ${deployed_digest} -> ${ABRA_TYPE_DIGEST} + This should not happen! This may mean that the upstream app developers are overwriting their image tags + Get in touch and let's make the builds stable together: https://docs.cloud.autonomic.zone/contact/ + Please pass the --update option to the deploy to deploy the update anyway" + fi + + if [ "$ABRA_TYPE_VERSION" != "$deployed_version" ]; then + echo "About to deploy (NEW VERSION):" + else + echo "About to deploy:" + fi echo " Server: $(tput setaf 4)${SERVER}$(tput sgr0)" if [ "${COMPOSE_FILE/:/}" == "${COMPOSE_FILE}" ]; then echo " Compose: $(tput setaf 3)${APP_DIR}/${COMPOSE_FILE}$(tput sgr0)" @@ -893,7 +921,11 @@ sub_app_deploy (){ echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)" fi if [ -n "$ABRA_TYPE_VERSION" ] && [ -n "$ABRA_TYPE_DIGEST" ]; then - echo " Version: $(tput setaf 2)${ABRA_TYPE_VERSION} (digest: ${ABRA_TYPE_DIGEST})$(tput sgr0)" + if [ "$ABRA_TYPE_VERSION" != "$deployed_version" ]; then + echo " Version: $(tput setaf 2)$deployed_version -> ${ABRA_TYPE_VERSION} (digest: $deployed_digest -> ${ABRA_TYPE_DIGEST})$(tput sgr0)" + else + echo " Version: $(tput setaf 2)${ABRA_TYPE_VERSION} (digest: ${ABRA_TYPE_DIGEST})$(tput sgr0)" + fi fi echo " Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)" @@ -1553,7 +1585,8 @@ 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___verbose abra___debug abra___help + abra__backup_file_ abra___verbose abra___debug abra___help \ + abra___update if ! type tput > /dev/null 2>&1; then tput() {