]> git.immae.eu Git - github/fretlink/ansible-clever.git/blame - files/clever-wait-deploy.sh
deploy: add a timeout in the polling waiting script
[github/fretlink/ansible-clever.git] / files / clever-wait-deploy.sh
CommitLineData
fa045db6
PB
1#!/usr/bin/env bash
2
3set -ueo pipefail
4
5VERBOSE=${VERBOSE:-}
6lastCleverActivity=""
5da7f0d8
PB
7timeout=2300 # 100 seconds less than the Ansible tasks' timeout
8startTime=$(date +%s)
fa045db6
PB
9
10function cleverActivity {
11 lastCleverActivity=$(clever activity)
12}
13
14function ensure {
5da7f0d8
PB
15 local lastReturnCode="$?"
16
fa045db6
PB
17 VERBOSE=true
18 verbose
5da7f0d8
PB
19
20 if isNotTimeout
21 then
22 if [ "$lastReturnCode" == "0" ]
23 then
24 echo "✓ Deployment done."
25 else
26 echo "✗ Deployment failed!"
27 fi
28 else
29 echo "⁈ Deployment timeout... Please check clever logs"
30 exit 1
31 fi
32}
33
34function isNotTimeout {
35 [ $(($(date +%s) - startTime)) -lt $timeout ]
fa045db6
PB
36}
37
38trap ensure EXIT ERR
39
40function deploying {
41 checkStatus "$1" "IN PROGRESS"
42}
43
44function deployed {
45 checkStatus "$1" "OK"
46}
47
48function inactive {
49 checkStatus "$1" "" "-v"
50}
51
52function checkStatus {
53 local commit="$1"
54 local status="$2"
55
56 cleverActivity
57 echo "${lastCleverActivity}" | tail -n1 | grep ${3:+ "${3}"} -q -E "${status}.*DEPLOY.*${commit}"
58}
59
60function verbose {
61 if [ -n "${VERBOSE}" ]; then
62 echo -e "\\nLast clever activity:"
63 echo -e "${lastCleverActivity}\\n"
64 fi
65}
66
67function check {
68 local commit="$1"
69 local samplingTime=5
70
71 echo "️▫ Waiting for deployment to start..."
5da7f0d8 72 while inactive "$commit" && isNotTimeout
fa045db6
PB
73 do
74 verbose
75 sleep $samplingTime
76 done
77
78 # Wait for completion
79 echo "▪ Deployment in progress..."
5da7f0d8 80 while deploying "$commit" && isNotTimeout
fa045db6
PB
81 do
82 verbose
83 sleep $samplingTime
84 done
85
86 deployed "$commit"
fa045db6
PB
87}
88
89function getHeadRev {
90 local chdir="$1/.git"
91
92 git --git-dir="$chdir" rev-parse HEAD
93}
94
95workdir="$(pwd)"
96check "$(getHeadRev "${workdir}")"