X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=files%2Fclever-wait-deploy.sh;h=a95c96fffcb30fb5a48c70967555a4fbf84f9bad;hb=c99eb17850e51327449e6fac5572c6cfc436d21c;hp=e0bbcf1691a1ae0575e035a6593f4656e46972b4;hpb=729e0bcf8bf2cfb9105af0e91edb0dc8535cc3f0;p=github%2Ffretlink%2Fansible-clever.git diff --git a/files/clever-wait-deploy.sh b/files/clever-wait-deploy.sh index e0bbcf1..a95c96f 100755 --- a/files/clever-wait-deploy.sh +++ b/files/clever-wait-deploy.sh @@ -1,4 +1,41 @@ -#!/bin/bash -e +#!/usr/bin/env bash + +set -ueo pipefail + +VERBOSE=${VERBOSE:-} +lastCleverActivity="" +timeout=2300 # 100 seconds less than the Ansible tasks' timeout +startTime=$(date +%s) + +function cleverActivity { + lastCleverActivity=$(clever activity) +} + +function ensure { + local lastReturnCode="$?" + + VERBOSE=true + verbose + + if isNotTimeout + then + if [ "$lastReturnCode" == "0" ] + then + echo "✓ Deployment done." + else + echo "✗ Deployment failed!" + fi + else + echo "⁈ Deployment timeout... Please check clever logs" + exit 1 + fi +} + +function isNotTimeout { + [ $(($(date +%s) - startTime)) -lt $timeout ] +} + +trap ensure EXIT ERR function deploying { checkStatus "$1" "IN PROGRESS" @@ -9,41 +46,43 @@ function deployed { } function inactive { - local commit="$1" - [[ "$(clever activity | grep "$commit" | grep "DEPLOY" | wc -l)" == "0" ]] + checkStatus "$1" "" "-v" } function checkStatus { local commit="$1" local status="$2" - [[ "$(clever activity | grep "$commit" | grep "${status}\s\+DEPLOY" | wc -l)" == "1" ]] + + cleverActivity + echo "${lastCleverActivity}" | tail -n1 | grep ${3:+ "${3}"} -q -E "${status}.*DEPLOY.*${commit}" +} + +function verbose { + if [ -n "${VERBOSE}" ]; then + echo -e "\\nLast clever activity:" + echo -e "${lastCleverActivity}\\n" + fi } function check { - local timeout=600 # 10 minutes local commit="$1" local samplingTime=5 - echo "Waiting for deployment start..." - while inactive "$commit" -a $timeout -gt 0 + echo "️▫ Waiting for deployment to start..." + while inactive "$commit" && isNotTimeout do + verbose sleep $samplingTime - let "timeout-=$samplingTime" done # Wait for completion - echo "Deployment in progress..." - while deploying "$commit" -a $timeout -gt 0 + echo "▪ Deployment in progress..." + while deploying "$commit" && isNotTimeout do + verbose sleep $samplingTime - let "timeout-=$samplingTime" done - if [ $samplingTime -eq 0 ] - then - echo "Timeout" - fi - deployed "$commit" } @@ -53,4 +92,5 @@ function getHeadRev { git --git-dir="$chdir" rev-parse HEAD } -check "$(getHeadRev "$@")" +workdir="$(pwd)" +check "$(getHeadRev "${workdir}")"