aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorpaulrbr-fl <43074087+paulrbr-fl@users.noreply.github.com>2019-07-31 14:28:10 +0200
committerGitHub <noreply@github.com>2019-07-31 14:28:10 +0200
commite749969ff845089853920647b0da039938a2371c (patch)
tree42959db073ff88d2f5ddcd42ff97bc548d73bf05
parent8305eb92f5bb6445868fda6d4e3075011d1fd248 (diff)
parentcc5df7bf9c44828219eaae6d6de111d4603e19a7 (diff)
downloadansible-clever-e749969ff845089853920647b0da039938a2371c.tar.gz
ansible-clever-e749969ff845089853920647b0da039938a2371c.tar.zst
ansible-clever-e749969ff845089853920647b0da039938a2371c.zip
Merge pull request #49 from paulrbr-fl/clever-deploy-back-to-polling
deploy: revert back to git push + polling
-rw-r--r--.travis.yml3
-rwxr-xr-xfiles/clever-wait-deploy.sh80
-rwxr-xr-xscripts/dhall_check.sh24
-rw-r--r--tasks/deploy.yml93
-rw-r--r--tasks/setup.yml1
5 files changed, 124 insertions, 77 deletions
diff --git a/.travis.yml b/.travis.yml
index 720eade..36019b8 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,7 +4,7 @@ language: nix
4sudo: false 4sudo: false
5 5
6install: 6install:
7 - nix-env -i python2.7-ansible python2.7-ansible-lint 7 - nix-env -i python2.7-ansible python2.7-ansible-lint ShellCheck-0.6.0
8 - nix-env -if ./dhall-1.21.0.nix 8 - nix-env -if ./dhall-1.21.0.nix
9 9
10 # Check ansible version 10 # Check ansible version
@@ -20,6 +20,7 @@ script:
20 # Basic role syntax check 20 # Basic role syntax check
21 - ansible-playbook tests/test.yml -i tests/inventory --syntax-check 21 - ansible-playbook tests/test.yml -i tests/inventory --syntax-check
22 - ansible-lint . 22 - ansible-lint .
23 - shellcheck **/*.sh
23 - scripts/dhall_check.sh 24 - scripts/dhall_check.sh
24 25
25notifications: 26notifications:
diff --git a/files/clever-wait-deploy.sh b/files/clever-wait-deploy.sh
new file mode 100755
index 0000000..98c5c37
--- /dev/null
+++ b/files/clever-wait-deploy.sh
@@ -0,0 +1,80 @@
1#!/usr/bin/env bash
2
3set -ueo pipefail
4
5VERBOSE=${VERBOSE:-}
6lastCleverActivity=""
7
8function cleverActivity {
9 lastCleverActivity=$(clever activity)
10}
11
12function ensure {
13 if [ "$?" == "1" ]
14 then
15 echo "✗ Deployment failed!"
16 fi
17 VERBOSE=true
18 verbose
19}
20
21trap ensure EXIT ERR
22
23function deploying {
24 checkStatus "$1" "IN PROGRESS"
25}
26
27function deployed {
28 checkStatus "$1" "OK"
29}
30
31function inactive {
32 checkStatus "$1" "" "-v"
33}
34
35function checkStatus {
36 local commit="$1"
37 local status="$2"
38
39 cleverActivity
40 echo "${lastCleverActivity}" | tail -n1 | grep ${3:+ "${3}"} -q -E "${status}.*DEPLOY.*${commit}"
41}
42
43function verbose {
44 if [ -n "${VERBOSE}" ]; then
45 echo -e "\\nLast clever activity:"
46 echo -e "${lastCleverActivity}\\n"
47 fi
48}
49
50function check {
51 local commit="$1"
52 local samplingTime=5
53
54 echo "️▫ Waiting for deployment to start..."
55 while inactive "$commit"
56 do
57 verbose
58 sleep $samplingTime
59 done
60
61 # Wait for completion
62 echo "▪ Deployment in progress..."
63 while deploying "$commit"
64 do
65 verbose
66 sleep $samplingTime
67 done
68
69 deployed "$commit"
70 echo "✓ Deployment done."
71}
72
73function getHeadRev {
74 local chdir="$1/.git"
75
76 git --git-dir="$chdir" rev-parse HEAD
77}
78
79workdir="$(pwd)"
80check "$(getHeadRev "${workdir}")"
diff --git a/scripts/dhall_check.sh b/scripts/dhall_check.sh
index a3413ec..5a1ee1b 100755
--- a/scripts/dhall_check.sh
+++ b/scripts/dhall_check.sh
@@ -1,25 +1,19 @@
1#!/usr/bin/env bash 1#!/usr/bin/env bash
2 2
3pushd () { 3set -eo pipefail
4 command pushd "$@" > /dev/null
5}
6
7popd () {
8 command popd "$@" > /dev/null
9}
10 4
11go() { 5go() {
12 local ERROR=0; 6 local ERROR=0
13 for file in $(find -type f -name "*.dhall"); do 7 while IFS= read -r -d '' file
14 pushd $(dirname $file); 8 do
15 cat $(basename $file) | dhall --explain resolve > /dev/null; 9 cd "$(dirname "$file")" || exit
16 echo "Typechecking ${file}" 10 echo "Typechecking ${file}"
17 if [ "$?" -ne "0" ]; then 11 if ! dhall --explain resolve < "$(basename "$file")" >/dev/null; then
18 echo "Failed to resolve $file" 12 echo "Failed to resolve $file"
19 ERROR=1; 13 ERROR=1
20 fi; 14 fi;
21 popd; 15 cd - >/dev/null || exit
22 done; 16 done < <(find . -type f -name "*.dhall" -print0)
23 exit $ERROR; 17 exit $ERROR;
24} 18}
25 19
diff --git a/tasks/deploy.yml b/tasks/deploy.yml
index 74232a6..df02464 100644
--- a/tasks/deploy.yml
+++ b/tasks/deploy.yml
@@ -31,18 +31,18 @@
31 - skip_ansible_lint 31 - skip_ansible_lint
32 32
33- name: Deploy to Clever-Cloud 33- name: Deploy to Clever-Cloud
34 shell: "clever deploy --force" 34 command: "git push --force git+ssh://git@push-par-clevercloud-customers.services.clever-cloud.com/{{ clever_app }}.git HEAD:refs/heads/master"
35 args: 35 args:
36 chdir: "{{ clever_app_root }}" 36 chdir: "{{ clever_app_root }}"
37 environment:
38 CONFIGURATION_FILE: "{{ clever_login_file }}"
39 register: clever_deploy 37 register: clever_deploy
40 async: 900 # 15 minutes timeout
41 poll: 30
42 ignore_errors: true 38 ignore_errors: true
43 tags: 39 tags:
44 - skip_ansible_lint 40 - skip_ansible_lint
45 41
42##################################################################
43# Re-deploy only if its the first clever deploy for that project #
44##################################################################
45
46- name: First time push to Clever-Cloud needs a full git clone 46- name: First time push to Clever-Cloud needs a full git clone
47 command: "git fetch --unshallow" 47 command: "git fetch --unshallow"
48 args: 48 args:
@@ -55,72 +55,43 @@
55 - skip_ansible_lint 55 - skip_ansible_lint
56 56
57- name: Deploy to Clever-Cloud 57- name: Deploy to Clever-Cloud
58 shell: "clever deploy --force" 58 command: "git push --force git+ssh://git@push-par-clevercloud-customers.services.clever-cloud.com/{{ clever_app }}.git HEAD:refs/heads/master"
59 args: 59 args:
60 chdir: "{{ clever_app_root }}" 60 chdir: "{{ clever_app_root }}"
61 environment: 61 when:
62 CONFIGURATION_FILE: "{{ clever_login_file }}" 62 - clever_deploy is failed
63 when: clever_deploy is failed 63 - clever_deploy.stderr is defined
64 - clever_deploy.stderr is search("Failed to read git object")
64 register: clever_deploy 65 register: clever_deploy
65 async: 900 # 15 minutes timeout
66 poll: 30
67 ignore_errors: true
68 tags: 66 tags:
69 - skip_ansible_lint 67 - skip_ansible_lint
70 68
71- name: Return deployment logs 69##############################################################################
72 debug: 70# Poll deployment status from 'clever activity' command. #
73 var: clever_deploy.stdout 71# 'clever deploy' command is not yet 100% reliable to get a blocking command #
74 when: clever_deploy.stdout is defined 72# until the deployment is over. Hence the need for a custom waiting script. #
75 73##############################################################################
76- name: Return deployment errors
77 debug:
78 var: clever_deploy.stderr
79 when:
80 - clever_deploy is failed
81 - clever_deploy.stderr is defined
82 74
83- name: Retrieve clever activity 75- name: Watch deployment status
84 command: clever activity 76 command: "{{ ansible_env.HOME }}/{{ clever_user_path }}/clever-wait-deploy.sh"
85 args: 77 args:
86 chdir: "{{ clever_app_root }}" 78 chdir: "{{ clever_app_root }}"
87 environment: 79 environment:
88 CONFIGURATION_FILE: "{{ clever_login_file }}" 80 CONFIGURATION_FILE: "{{ clever_login_file }}"
89 changed_when: false 81 async: 2400 # 40 minutes
90 register: clever_activity_result 82 poll: 0
91 83 register: long_command
92- name: Display clever activity 84 changed_when: False
93 debug:
94 var: clever_activity_result.stdout_lines
95 85
96- name: Get current commit sha 86- name: Wait 40 minutes for deployment completion
97 command: git show -q --format=format:%H HEAD 87 async_status:
98 args: 88 jid: "{{ long_command.ansible_job_id }}"
99 chdir: "{{ clever_app_root }}" 89 register: job_result
100 warn: False 90 until: job_result.finished
101 changed_when: false 91 delay: 30
102 register: current_commit_sha 92 retries: 80 # 40 minutes (80 * 30 secs delay)
103 93
104# #### 94# Output of waiting script stdout
105# Expects all configuration to be located in the project's repository. 95- name: Output waiting script logs
106# Making a git commit bound to the same *configuration* and *executable* version. 96 debug:
107# ## 97 var: job_result.stdout_lines
108- name: Fail if current commit is not the last deployed one
109 fail:
110 msg: "The clever deployment failed! Please check latest deploy activity logs above."
111 when:
112 - clever_deploy is failed
113 - clever_deploy.stderr is defined
114 - clever_deploy.stderr is search("application is up-to-date")
115 - |
116 (current_commit_sha.stdout_lines[-1] not in clever_activity_result.stdout_lines[-1])
117 or
118 (clever_activity_valid_deploy_keyword not in clever_activity_result.stdout_lines[-1])
119
120- name: Fail on any other deployment errors
121 fail:
122 msg: "The clever deployment failed! Please check logs above."
123 when:
124 - clever_deploy is failed
125 - clever_deploy.stderr is defined
126 - clever_deploy.stderr is not search("application is up-to-date")
diff --git a/tasks/setup.yml b/tasks/setup.yml
index 9d1f8aa..c5abb81 100644
--- a/tasks/setup.yml
+++ b/tasks/setup.yml
@@ -27,5 +27,6 @@
27 dest: "{{ ansible_env.HOME }}/{{ clever_user_path }}/{{ item }}" 27 dest: "{{ ansible_env.HOME }}/{{ clever_user_path }}/{{ item }}"
28 mode: 0755 28 mode: 0755
29 with_items: 29 with_items:
30 - clever-wait-deploy.sh
30 - clever-set-domain.sh 31 - clever-set-domain.sh
31 - clever-set-drain.sh 32 - clever-set-drain.sh