aboutsummaryrefslogtreecommitdiffhomepage
path: root/files/clever-wait-deploy.sh
blob: b9110694c6cd9cd4fbf835c7ed32988b1f2ab2e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash -e

function deploying {
  checkStatus "$1" "IN PROGRESS"
}

function deployed {
  checkStatus "$1" "OK"
}

function inactive {
  local commit="$1"
  [[ "$(clever activity | grep "$commit" | grep "DEPLOY" | wc -l)" == "0" ]]
}

function checkStatus {
  local commit="$1"
  local status="$2"
  [[ "$(clever activity | grep "$commit" | grep "${status}\s\+DEPLOY" | wc -l)" == "1" ]]
}

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
  do
    sleep $samplingTime
    let "timeout-=$samplingTime"
  done

  # Wait for completion
  echo "Deployment in progress..."
  while deploying "$commit" -a $timeout -gt 0
  do
    sleep $samplingTime
    let "timeout-=$samplingTime"
  done

  if [ $samplingTime -eq 0 ]
  then
    echo "Timeout"
  fi

  deployed "$commit"
}

function getHeadRev {
  git rev-parse HEAD
}

check "$(getHeadRev)"