diff options
author | Paul Bonaud <paul.bonaud@fretlink.com> | 2020-08-06 11:44:26 +0200 |
---|---|---|
committer | Paul Bonaud <paul.bonaud@fretlink.com> | 2020-08-06 15:54:27 +0200 |
commit | c3e2ff5e0f31c61eae330a1a41d7a8a83435d9bd (patch) | |
tree | f03e1db57031f574657838a8a15c41319b1c18be | |
parent | 315c7f51af48b54719bd793d9d28a4bb396a926a (diff) | |
download | ansible-clever-c3e2ff5e0f31c61eae330a1a41d7a8a83435d9bd.tar.gz ansible-clever-c3e2ff5e0f31c61eae330a1a41d7a8a83435d9bd.tar.zst ansible-clever-c3e2ff5e0f31c61eae330a1a41d7a8a83435d9bd.zip |
deploy: try to restart the app when the CLI asks to do it
In some cases (rollback or deployment errored on clever side),
clever-tools will fail when calling `clever deploy` because it thinks
the currently being deployed commit is already deployed. However in
some cases the git pushed commit is not the currently running commit.
Leading to a failure in our deployment process. The proposed solution
from the clever tools error is to launch `clever restart`.
Until we wait for
https://github.com/CleverCloud/clever-tools/issues/422 to be fixed,
let's do what the error message say: try to restart in case of a
deployment failure.
-rw-r--r-- | tasks/deploy.yml | 7 | ||||
-rw-r--r-- | tasks/restart.yml | 9 | ||||
-rwxr-xr-x | tests/fake.sh | 14 | ||||
-rw-r--r-- | tests/test-all.yml | 1 | ||||
-rw-r--r-- | tests/test-restart-deploy.yml | 37 |
5 files changed, 65 insertions, 3 deletions
diff --git a/tasks/deploy.yml b/tasks/deploy.yml index 8dda5e0..39b5845 100644 --- a/tasks/deploy.yml +++ b/tasks/deploy.yml | |||
@@ -85,6 +85,13 @@ | |||
85 | - job_result.stderr is defined | 85 | - job_result.stderr is defined |
86 | - job_result.stderr is search("Failed to read git object") | 86 | - job_result.stderr is search("Failed to read git object") |
87 | 87 | ||
88 | - name: Restart app on clever-cloud when error asks to restart | ||
89 | include_tasks: restart.yml | ||
90 | when: | ||
91 | - not job_result.finished or not job_result.rc == 0 | ||
92 | - job_result.stderr is defined | ||
93 | - job_result.stderr is search("clever restart") | ||
94 | |||
88 | - name: Deploy logs (stdout) | 95 | - name: Deploy logs (stdout) |
89 | debug: | 96 | debug: |
90 | var: job_result.stdout_lines | 97 | var: job_result.stdout_lines |
diff --git a/tasks/restart.yml b/tasks/restart.yml index e2a8891..1c52032 100644 --- a/tasks/restart.yml +++ b/tasks/restart.yml | |||
@@ -1,6 +1,13 @@ | |||
1 | --- | 1 | --- |
2 | - name: Get current HEAD commit revision | ||
3 | command: "git rev-parse HEAD" # noqa 303 | ||
4 | register: git_head_revision | ||
5 | args: | ||
6 | chdir: "{{ clever_app_root }}" | ||
7 | changed_when: false | ||
8 | |||
2 | - name: Restart app on Clever-Cloud | 9 | - name: Restart app on Clever-Cloud |
3 | shell: "clever restart" | 10 | shell: "clever restart --commit {{ git_head_revision.stdout_lines | first }}" |
4 | args: | 11 | args: |
5 | chdir: "{{ clever_app_root }}" | 12 | chdir: "{{ clever_app_root }}" |
6 | environment: | 13 | environment: |
diff --git a/tests/fake.sh b/tests/fake.sh index 65ff819..776907b 100755 --- a/tests/fake.sh +++ b/tests/fake.sh | |||
@@ -3,6 +3,9 @@ | |||
3 | binary="${0/*\/}" | 3 | binary="${0/*\/}" |
4 | localCommit="${PRETEND_LOCAL_COMMIT:-aaa000aaa000aaa000aaa000aaa000aaa000aaa0}" | 4 | localCommit="${PRETEND_LOCAL_COMMIT:-aaa000aaa000aaa000aaa000aaa000aaa000aaa0}" |
5 | remoteCommit="${PRETEND_REMOTE_COMMIT:-bbb000bbb000bbb000bbb000bbb000bbb000bbb0}" | 5 | remoteCommit="${PRETEND_REMOTE_COMMIT:-bbb000bbb000bbb000bbb000bbb000bbb000bbb0}" |
6 | uptodateDeployment="${DEPLOY_UP_TO_DATE_APP:-}" | ||
7 | |||
8 | echo "${1}" >> "${binary}-commands" | ||
6 | 9 | ||
7 | if [ "${binary}" = "clever" ] && [ "${1}" = "--version" ]; then | 10 | if [ "${binary}" = "clever" ] && [ "${1}" = "--version" ]; then |
8 | echo "2.6.1" | 11 | echo "2.6.1" |
@@ -10,10 +13,17 @@ elif [ "${binary}" = "clever" ] && [ "${1}" = "activity" ]; then | |||
10 | echo "2020-02-02T20:20:02+02:00 OK DEPLOY ${remoteCommit} Git" | 13 | echo "2020-02-02T20:20:02+02:00 OK DEPLOY ${remoteCommit} Git" |
11 | elif [ "${binary}" = "clever" ] && [ "${1}" = "status" ]; then | 14 | elif [ "${binary}" = "clever" ] && [ "${1}" = "status" ]; then |
12 | echo "test-app: running (1*pico, Commit: ${remoteCommit})" | 15 | echo "test-app: running (1*pico, Commit: ${remoteCommit})" |
16 | elif [ "${binary}" = "clever" ] && [ "${1}" = "deploy" ]; then | ||
17 | if [ -z "${uptodateDeployment}" ]; then | ||
18 | echo "Clever deploy done." | ||
19 | else | ||
20 | # Mimic the current behavior until https://github.com/CleverCloud/clever-tools/issues/422 is solved | ||
21 | >&2 echo "The clever-cloud application is up-to-date. Try this command to restart the application:" | ||
22 | >&2 echo " clever restart" | ||
23 | exit 1 | ||
24 | fi | ||
13 | elif [ "${binary}" = "git" ]; then | 25 | elif [ "${binary}" = "git" ]; then |
14 | echo "${localCommit}" | 26 | echo "${localCommit}" |
15 | else | 27 | else |
16 | echo "${binary} called with arguments: ${*}" | 28 | echo "${binary} called with arguments: ${*}" |
17 | fi | 29 | fi |
18 | |||
19 | echo "${1}" >> "${binary}-commands" | ||
diff --git a/tests/test-all.yml b/tests/test-all.yml index 8a6c0ba..b9c5dea 100644 --- a/tests/test-all.yml +++ b/tests/test-all.yml | |||
@@ -3,6 +3,7 @@ | |||
3 | - import_playbook: ./test-configure-app.yml | 3 | - import_playbook: ./test-configure-app.yml |
4 | - import_playbook: ./test-haskell-app.yml | 4 | - import_playbook: ./test-haskell-app.yml |
5 | - import_playbook: ./test-noop-deploy.yml | 5 | - import_playbook: ./test-noop-deploy.yml |
6 | - import_playbook: ./test-restart-deploy.yml | ||
6 | - import_playbook: ./test-scalability.yml | 7 | - import_playbook: ./test-scalability.yml |
7 | - import_playbook: ./test-service-deps.yml | 8 | - import_playbook: ./test-service-deps.yml |
8 | - import_playbook: ./test-restart-app.yml | 9 | - import_playbook: ./test-restart-app.yml |
diff --git a/tests/test-restart-deploy.yml b/tests/test-restart-deploy.yml new file mode 100644 index 0000000..cbd6cdd --- /dev/null +++ b/tests/test-restart-deploy.yml | |||
@@ -0,0 +1,37 @@ | |||
1 | --- | ||
2 | - name: Launch a restart when commit is already up-to-date on clever | ||
3 | hosts: localhost | ||
4 | remote_user: root | ||
5 | pre_tasks: | ||
6 | - file: | ||
7 | state: absent | ||
8 | path: ../clever-commands | ||
9 | roles: | ||
10 | - role: clever | ||
11 | vars: | ||
12 | clever_token: 123abc | ||
13 | clever_secret: cba321 | ||
14 | clever_app: app_00000000-0000-0000-0000-000000000000 | ||
15 | environment: | ||
16 | DEPLOY_UP_TO_DATE_APP: true | ||
17 | post_tasks: | ||
18 | - name: Check stubbed commands | ||
19 | shell: "{{ item.cmd }}" | ||
20 | ignore_errors: true | ||
21 | with_list: | ||
22 | - cmd: "grep deploy ../clever-commands" | ||
23 | display: "Expected 'clever deploy' command to be called" | ||
24 | - cmd: "grep restart ../clever-commands" | ||
25 | display: "Expected 'clever restart' command to be called" | ||
26 | vars: | ||
27 | display: "{{ item.display }}" | ||
28 | register: tests_results | ||
29 | - name: show results | ||
30 | debug: | ||
31 | msg: | ||
32 | - "failed_results: {{ failed_results }}" | ||
33 | - "success_results: {{ success_results }}" | ||
34 | failed_when: tests_results is failed | ||
35 | vars: | ||
36 | failed_results: "{{ tests_results.results | selectattr('failed') | map(attribute='item.display') | list }}" | ||
37 | success_results: "{{ tests_results.results | rejectattr('failed') | map(attribute='item.display') | list }}" | ||