From b28460cf842e0ad09fb52f7a1776da852a24333e Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 25 Mar 2021 23:56:16 +0100 Subject: [PATCH] Add wait and domain check logic Closes https://git.autonomic.zone/coop-cloud/abra/issues/116. Also see https://git.autonomic.zone/coop-cloud/abra/issues/113. --- CHANGELOG.md | 2 ++ abra | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d18fdbd..33ca417 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ - Fix git branch handling when not passing `-b ` ([#122](https://git.autonomic.zone/coop-cloud/abra/issues/122)) - Add work-around to correctly git clone non-master default branch app repositories ([#122](https://git.autonomic.zone/coop-cloud/abra/issues/122)) - Replace `--force` with a global `--no-prompt` for avoiding interactive questions ([#118](https://git.autonomic.zone/coop-cloud/abra/issues/118)) +- Use [docker-stack-wait-deploy](https://github.com/vitalets/docker-stack-wait-deploy) inspired logic to deploy apps ([#116](https://git.autonomic.zone/coop-cloud/abra/issues/116)) +- Add a domain polling check when deploying apps ([#113](https://git.autonomic.zone/coop-cloud/abra/issues/113)) # abra 0.6.0 (2021-03-17) diff --git a/abra b/abra index 4231846..4aaa137 100755 --- a/abra +++ b/abra @@ -632,6 +632,64 @@ output_version_summary() { fi } +# Note(decentral1se): inspired by https://github.com/vitalets/docker-stack-wait-deploy +ensure_stack_deployed() { + STACK_NAME=$1 + + info "Waiting for deployment to succeed" + + while true; do + all_services_done=1 + has_errors=0 + + service_ids=$(docker stack services -q "$STACK_NAME") + + for service_id in $service_ids; do + # see: https://github.com/moby/moby/issues/28012 + service_state=$(docker service inspect --format "{{if .UpdateStatus}}{{.UpdateStatus.State}}{{else}}created{{end}}" "$service_id") + + case "$service_state" in + created|completed) + ;; + paused|rollback_completed) + has_errors=1 + ;; + *) + all_services_done=0 + ;; + esac + done + + if [ "$all_services_done" == "1" ]; then + if [ "$has_errors" == "1" ]; then + debug "Deployment appears to have failed" + break + else + debug "Deployment appears to have suceeded" + break + fi + else + sleep 1 + fi + done +} + +ensure_domain_deployed() { + DOMAIN=$1 + + info "Waiting for $DOMAIN to come up" + + idx=1 + until curl --output /dev/null --silent --head --fail "$DOMAIN"; do + debug "Polled $DOMAIN $idx time(s) already" + sleep 3 + idx=$(("$idx" + 1)) + if [[ $idx -gt 10 ]]; then + error "$DOMAIN still isn't up, check status by running \"abra app ${STACK_NAME} ps\"" + fi + done +} + ###### FIXME 3wc: name this section get_servers() { @@ -1132,7 +1190,9 @@ sub_app_deploy (){ (cd "$APP_DIR" || error "\$APP_DIR '$APP_DIR' not found") # shellcheck disable=SC2086 if (cd "$APP_DIR" && docker stack deploy -c ${COMPOSE_FILE//:/ -c } "$STACK_NAME"); then + ensure_stack_deployed "$STACK_NAME" if [ -n "$DOMAIN" ]; then + ensure_domain_deployed "https://${DOMAIN}" success "Yay! App should be available at https://${DOMAIN}" else success "Yay! That worked. No \$DOMAIN defined, check status by running \"abra app ${STACK_NAME} ps\""