diff options
-rw-r--r-- | defaults/main.yml | 2 | ||||
-rwxr-xr-x | files/clever-wait-deploy.sh | 96 | ||||
-rw-r--r-- | tasks/deploy.yml | 73 | ||||
-rw-r--r-- | tasks/redeploy.yml | 33 | ||||
-rw-r--r-- | tasks/setup.yml | 3 | ||||
-rwxr-xr-x | tests/fake.sh | 2 |
6 files changed, 55 insertions, 154 deletions
diff --git a/defaults/main.yml b/defaults/main.yml index 0e4bada..dcf9650 100644 --- a/defaults/main.yml +++ b/defaults/main.yml | |||
@@ -1,6 +1,6 @@ | |||
1 | --- | 1 | --- |
2 | # defaults file for clever | 2 | # defaults file for clever |
3 | clever_cli_version: 1.6.3 | 3 | clever_cli_version: 2.6.1 |
4 | clever_user_path: .local/bin | 4 | clever_user_path: .local/bin |
5 | clever_app_root: "{{ app_root | default(playbook_dir + '/..') }}" | 5 | clever_app_root: "{{ app_root | default(playbook_dir + '/..') }}" |
6 | clever_app_confdir: "{{ clever_app_root }}/.clever_cloud" | 6 | clever_app_confdir: "{{ clever_app_root }}/.clever_cloud" |
diff --git a/files/clever-wait-deploy.sh b/files/clever-wait-deploy.sh deleted file mode 100755 index a95c96f..0000000 --- a/files/clever-wait-deploy.sh +++ /dev/null | |||
@@ -1,96 +0,0 @@ | |||
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}")" | ||
diff --git a/tasks/deploy.yml b/tasks/deploy.yml index d5e5a45..287dd41 100644 --- a/tasks/deploy.yml +++ b/tasks/deploy.yml | |||
@@ -24,80 +24,45 @@ | |||
24 | CONFIGURATION_FILE: "{{ clever_login_file }}" | 24 | CONFIGURATION_FILE: "{{ clever_login_file }}" |
25 | changed_when: false | 25 | changed_when: false |
26 | 26 | ||
27 | #TODO: Improve ssh-key validation | ||
28 | - name: Accept Clever-Cloud servers | ||
29 | shell: "ssh-keyscan -H push-par-clevercloud-customers.services.clever-cloud.com >> ~/.ssh/known_hosts" | ||
30 | tags: | ||
31 | - skip_ansible_lint | ||
32 | |||
33 | - name: Deploy to Clever-Cloud | 27 | - name: Deploy to Clever-Cloud |
34 | command: "git push --force git+ssh://git@push-par-clevercloud-customers.services.clever-cloud.com/{{ clever_app }}.git HEAD:refs/heads/master" | 28 | shell: "clever deploy --force" |
35 | args: | ||
36 | chdir: "{{ clever_app_root }}" | ||
37 | register: clever_deploy | ||
38 | ignore_errors: true | ||
39 | tags: | ||
40 | - skip_ansible_lint | ||
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 | ||
47 | command: "git fetch --unshallow" | ||
48 | args: | ||
49 | chdir: "{{ clever_app_root }}" | ||
50 | when: | ||
51 | - clever_deploy is failed | ||
52 | - clever_deploy.stderr is defined | ||
53 | - clever_deploy.stderr is search("Failed to read git object") | ||
54 | tags: | ||
55 | - skip_ansible_lint | ||
56 | |||
57 | - name: Deploy to Clever-Cloud | ||
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: | ||
60 | chdir: "{{ clever_app_root }}" | ||
61 | when: | ||
62 | - clever_deploy is failed | ||
63 | - clever_deploy.stderr is defined | ||
64 | - clever_deploy.stderr is search("Failed to read git object") | ||
65 | register: clever_deploy | ||
66 | tags: | ||
67 | - skip_ansible_lint | ||
68 | |||
69 | ############################################################################## | ||
70 | # Poll deployment status from 'clever activity' command. # | ||
71 | # 'clever deploy' command is not yet 100% reliable to get a blocking command # | ||
72 | # until the deployment is over. Hence the need for a custom waiting script. # | ||
73 | ############################################################################## | ||
74 | |||
75 | - name: Watch deployment status | ||
76 | command: "{{ ansible_env.HOME }}/{{ clever_user_path }}/clever-wait-deploy.sh" | ||
77 | args: | 29 | args: |
78 | chdir: "{{ clever_app_root }}" | 30 | chdir: "{{ clever_app_root }}" |
79 | environment: | 31 | environment: |
80 | CONFIGURATION_FILE: "{{ clever_login_file }}" | 32 | CONFIGURATION_FILE: "{{ clever_login_file }}" |
81 | async: 2400 # 40 minutes | 33 | async: 2400 # 40 minutes |
82 | poll: 0 | 34 | poll: 0 |
83 | register: long_command | 35 | ignore_errors: true |
84 | changed_when: False | 36 | register: clever_deploy |
37 | tags: | ||
38 | - skip_ansible_lint | ||
85 | 39 | ||
86 | - name: Wait 40 minutes for deployment completion | 40 | - name: Wait 40 minutes for deployment completion |
87 | async_status: | 41 | async_status: |
88 | jid: "{{ long_command.ansible_job_id }}" | 42 | jid: "{{ clever_deploy.ansible_job_id }}" |
89 | register: job_result | 43 | register: job_result |
90 | until: job_result.finished | 44 | until: job_result.finished |
91 | ignore_errors: true | 45 | ignore_errors: true |
92 | delay: 30 | 46 | delay: 30 |
93 | retries: 80 # 40 minutes (80 * 30 secs delay) | 47 | retries: 80 # 40 minutes (80 * 30 secs delay) |
94 | 48 | ||
95 | - name: Waiting script logs (stdout) | 49 | ################################################################## |
50 | # Re-deploy only if its the first clever deploy for that project # | ||
51 | ################################################################## | ||
52 | |||
53 | - name: Retry deploy to clever-cloud | ||
54 | include_tasks: redeploy.yml | ||
55 | when: | ||
56 | - not job_result.rc == 0 | ||
57 | - job_result.stderr is defined | ||
58 | - job_result.stderr is search("Failed to read git object") | ||
59 | |||
60 | - name: Deploy logs (stdout) | ||
96 | debug: | 61 | debug: |
97 | var: job_result.stdout_lines | 62 | var: job_result.stdout_lines |
98 | when: job_result.stdout_lines is defined | 63 | when: job_result.stdout_lines is defined |
99 | 64 | ||
100 | - name: Waiting script logs (stderr) | 65 | - name: Deploy error logs (stderr) |
101 | debug: | 66 | debug: |
102 | var: job_result.stderr_lines | 67 | var: job_result.stderr_lines |
103 | when: job_result.stderr_lines is defined | 68 | when: job_result.stderr_lines is defined |
diff --git a/tasks/redeploy.yml b/tasks/redeploy.yml new file mode 100644 index 0000000..41c0a5c --- /dev/null +++ b/tasks/redeploy.yml | |||
@@ -0,0 +1,33 @@ | |||
1 | --- | ||
2 | ################################################################## | ||
3 | # Re-deploy only if its the first clever deploy for that project # | ||
4 | ################################################################## | ||
5 | |||
6 | - name: First time push to Clever-Cloud needs a full git clone | ||
7 | command: "git fetch --unshallow" | ||
8 | args: | ||
9 | chdir: "{{ clever_app_root }}" | ||
10 | tags: | ||
11 | - skip_ansible_lint | ||
12 | |||
13 | - name: Deploy to Clever-Cloud | ||
14 | shell: "clever deploy --force" | ||
15 | args: | ||
16 | chdir: "{{ clever_app_root }}" | ||
17 | environment: | ||
18 | CONFIGURATION_FILE: "{{ clever_login_file }}" | ||
19 | async: 2400 # 40 minutes | ||
20 | poll: 0 | ||
21 | ignore_errors: true | ||
22 | register: clever_deploy | ||
23 | tags: | ||
24 | - skip_ansible_lint | ||
25 | |||
26 | - name: Wait 40 minutes for deployment completion | ||
27 | async_status: | ||
28 | jid: "{{ clever_deploy.ansible_job_id }}" | ||
29 | register: job_result | ||
30 | until: job_result.finished | ||
31 | ignore_errors: true | ||
32 | delay: 30 | ||
33 | retries: 80 # 40 minutes (80 * 30 secs delay) | ||
diff --git a/tasks/setup.yml b/tasks/setup.yml index c5abb81..a2d5b90 100644 --- a/tasks/setup.yml +++ b/tasks/setup.yml | |||
@@ -17,7 +17,7 @@ | |||
17 | src: "{{ clever_repo }}/{{ clever_tools_archive }}" | 17 | src: "{{ clever_repo }}/{{ clever_tools_archive }}" |
18 | dest: "{{ ansible_env.HOME }}/{{ clever_user_path }}" | 18 | dest: "{{ ansible_env.HOME }}/{{ clever_user_path }}" |
19 | vars: | 19 | vars: |
20 | clever_repo: https://clever-tools.cellar.services.clever-cloud.com/releases | 20 | clever_repo: https://clever-tools.clever-cloud.com/releases |
21 | clever_tools_archive: "{{ clever_cli_version }}/clever-tools-{{ clever_cli_version }}_linux.tar.gz" | 21 | clever_tools_archive: "{{ clever_cli_version }}/clever-tools-{{ clever_cli_version }}_linux.tar.gz" |
22 | when: clever_returned_version is failed or clever_returned_version.stdout != clever_cli_version | 22 | when: clever_returned_version is failed or clever_returned_version.stdout != clever_cli_version |
23 | 23 | ||
@@ -27,6 +27,5 @@ | |||
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 | ||
31 | - clever-set-domain.sh | 30 | - clever-set-domain.sh |
32 | - clever-set-drain.sh | 31 | - clever-set-drain.sh |
diff --git a/tests/fake.sh b/tests/fake.sh index 2835e85..75bc051 100755 --- a/tests/fake.sh +++ b/tests/fake.sh | |||
@@ -4,7 +4,7 @@ binary="${0/*\/}" | |||
4 | fakeCommit="aaa000aaa000aaa000aaa000aaa000aaa000aaa0" | 4 | fakeCommit="aaa000aaa000aaa000aaa000aaa000aaa000aaa0" |
5 | 5 | ||
6 | if [ "${binary}" = "clever" ] && [ "${1}" = "--version" ]; then | 6 | if [ "${binary}" = "clever" ] && [ "${1}" = "--version" ]; then |
7 | echo "1.6.3" | 7 | echo "2.6.1" |
8 | elif [ "${binary}" = "clever" ] && [ "${1}" = "activity" ]; then | 8 | elif [ "${binary}" = "clever" ] && [ "${1}" = "activity" ]; then |
9 | echo "2020-02-02T20:20:02+02:00 OK DEPLOY ${fakeCommit} Git" | 9 | echo "2020-02-02T20:20:02+02:00 OK DEPLOY ${fakeCommit} Git" |
10 | elif [ "${binary}" = "git" ]; then | 10 | elif [ "${binary}" = "git" ]; then |