]> git.immae.eu Git - github/fretlink/ansible-clever.git/blob - files/clever-wait-deploy.sh
deploy: add a timeout in the polling waiting script
[github/fretlink/ansible-clever.git] / files / clever-wait-deploy.sh
1 #!/usr/bin/env bash
2
3 set -ueo pipefail
4
5 VERBOSE=${VERBOSE:-}
6 lastCleverActivity=""
7 timeout=2300 # 100 seconds less than the Ansible tasks' timeout
8 startTime=$(date +%s)
9
10 function cleverActivity {
11 lastCleverActivity=$(clever activity)
12 }
13
14 function ensure {
15 local lastReturnCode="$?"
16
17 VERBOSE=true
18 verbose
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
34 function isNotTimeout {
35 [ $(($(date +%s) - startTime)) -lt $timeout ]
36 }
37
38 trap ensure EXIT ERR
39
40 function deploying {
41 checkStatus "$1" "IN PROGRESS"
42 }
43
44 function deployed {
45 checkStatus "$1" "OK"
46 }
47
48 function inactive {
49 checkStatus "$1" "" "-v"
50 }
51
52 function 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
60 function verbose {
61 if [ -n "${VERBOSE}" ]; then
62 echo -e "\\nLast clever activity:"
63 echo -e "${lastCleverActivity}\\n"
64 fi
65 }
66
67 function check {
68 local commit="$1"
69 local samplingTime=5
70
71 echo "️▫ Waiting for deployment to start..."
72 while inactive "$commit" && isNotTimeout
73 do
74 verbose
75 sleep $samplingTime
76 done
77
78 # Wait for completion
79 echo "▪ Deployment in progress..."
80 while deploying "$commit" && isNotTimeout
81 do
82 verbose
83 sleep $samplingTime
84 done
85
86 deployed "$commit"
87 }
88
89 function getHeadRev {
90 local chdir="$1/.git"
91
92 git --git-dir="$chdir" rev-parse HEAD
93 }
94
95 workdir="$(pwd)"
96 check "$(getHeadRev "${workdir}")"