]> git.immae.eu Git - github/fretlink/ansible-clever.git/commitdiff
deploy: add a timeout in the polling waiting script 50/head
authorPaul Bonaud <paul.bonaud@fretlink.com>
Thu, 1 Aug 2019 08:38:53 +0000 (10:38 +0200)
committerPaul Bonaud <paul.bonaud@fretlink.com>
Thu, 1 Aug 2019 15:47:02 +0000 (17:47 +0200)
files/clever-wait-deploy.sh
tasks/deploy.yml

index 98c5c37a4d8bb0f266870a3935f08fd958d260db..a95c96fffcb30fb5a48c70967555a4fbf84f9bad 100755 (executable)
@@ -4,18 +4,35 @@ 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 {
-  if [ "$?" == "1" ]
-  then
-    echo "✗ Deployment failed!"
-  fi
+  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
@@ -52,7 +69,7 @@ function check {
   local samplingTime=5
 
   echo "️▫ Waiting for deployment to start..."
-  while inactive "$commit"
+  while inactive "$commit" && isNotTimeout
   do
     verbose
     sleep $samplingTime
@@ -60,14 +77,13 @@ function check {
 
   # Wait for completion
   echo "▪ Deployment in progress..."
-  while deploying "$commit"
+  while deploying "$commit" && isNotTimeout
   do
     verbose
     sleep $samplingTime
   done
 
   deployed "$commit"
-  echo "✓ Deployment done."
 }
 
 function getHeadRev {
index df0246462b89896b9f8587f91f34e68d6f11b305..d5e5a4555fe6e9515dcd4b01a758d1cb92701b99 100644 (file)
     jid: "{{ long_command.ansible_job_id }}"
   register: job_result
   until: job_result.finished
+  ignore_errors: true
   delay: 30
   retries: 80 # 40 minutes (80 * 30 secs delay)
 
-# Output of waiting script stdout
-- name: Output waiting script logs
+- name: Waiting script logs (stdout)
   debug:
     var: job_result.stdout_lines
+  when: job_result.stdout_lines is defined
+
+- name: Waiting script logs (stderr)
+  debug:
+    var: job_result.stderr_lines
+  when: job_result.stderr_lines is defined
+
+- name: Fail in case of timeout or failure
+  fail:
+    msg: "Deployment failed. Please check logs above."
+  when: not job_result.finished or not job_result.rc == 0