]> git.immae.eu Git - github/fretlink/ansible-clever.git/commitdiff
deploy: try to restart the app when the CLI asks to do it 75/head
authorPaul Bonaud <paul.bonaud@fretlink.com>
Thu, 6 Aug 2020 09:44:26 +0000 (11:44 +0200)
committerPaul Bonaud <paul.bonaud@fretlink.com>
Thu, 6 Aug 2020 13:54:27 +0000 (15:54 +0200)
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.

tasks/deploy.yml
tasks/restart.yml
tests/fake.sh
tests/test-all.yml
tests/test-restart-deploy.yml [new file with mode: 0644]

index 8dda5e0f7dc82673e91a1da91525a32708692e98..39b5845bc8f4734d5b66fb3504e15be6c1e93425 100644 (file)
     - job_result.stderr is defined
     - job_result.stderr is search("Failed to read git object")
 
+- name: Restart app on clever-cloud when error asks to restart
+  include_tasks: restart.yml
+  when:
+    - not job_result.finished or not job_result.rc == 0
+    - job_result.stderr is defined
+    - job_result.stderr is search("clever restart")
+
 - name: Deploy logs (stdout)
   debug:
     var: job_result.stdout_lines
index e2a88913e27e6ae228ef01ecfd93f4de2e3d8cd3..1c520321e36f4c8a50dec525c75660de13ae3343 100644 (file)
@@ -1,6 +1,13 @@
 ---
+- name: Get current HEAD commit revision
+  command: "git rev-parse HEAD" # noqa 303
+  register: git_head_revision
+  args:
+    chdir: "{{ clever_app_root }}"
+  changed_when: false
+
 - name: Restart app on Clever-Cloud
-  shell: "clever restart"
+  shell: "clever restart --commit {{ git_head_revision.stdout_lines | first }}"
   args:
     chdir: "{{ clever_app_root }}"
   environment:
index 65ff819ffd4b538c71d5dc91ed89829dc9fbf673..776907b7ac36a405cdcf59fe82045114fed71b90 100755 (executable)
@@ -3,6 +3,9 @@
 binary="${0/*\/}"
 localCommit="${PRETEND_LOCAL_COMMIT:-aaa000aaa000aaa000aaa000aaa000aaa000aaa0}"
 remoteCommit="${PRETEND_REMOTE_COMMIT:-bbb000bbb000bbb000bbb000bbb000bbb000bbb0}"
+uptodateDeployment="${DEPLOY_UP_TO_DATE_APP:-}"
+
+echo "${1}" >> "${binary}-commands"
 
 if [ "${binary}" = "clever" ] && [ "${1}" = "--version" ]; then
     echo "2.6.1"
@@ -10,10 +13,17 @@ elif [ "${binary}" = "clever" ] && [ "${1}" = "activity" ]; then
     echo "2020-02-02T20:20:02+02:00  OK         DEPLOY     ${remoteCommit}  Git"
 elif [ "${binary}" = "clever" ] && [ "${1}" = "status" ]; then
     echo "test-app: running (1*pico,  Commit: ${remoteCommit})"
+elif [ "${binary}" = "clever" ] && [ "${1}" = "deploy" ]; then
+    if [ -z "${uptodateDeployment}" ]; then
+        echo "Clever deploy done."
+    else
+        # Mimic the current behavior until https://github.com/CleverCloud/clever-tools/issues/422 is solved
+        >&2 echo "The clever-cloud application is up-to-date. Try this command to restart the application:"
+        >&2 echo "        clever restart"
+        exit 1
+    fi
 elif [ "${binary}" = "git" ]; then
     echo "${localCommit}"
 else
     echo "${binary} called with arguments: ${*}"
 fi
-
-echo "${1}" >> "${binary}-commands"
index 8a6c0ba362552ea5f8bbad5695ff56cca47824db..b9c5dea2ea6b975498ee31b0c23aa017540e639b 100644 (file)
@@ -3,6 +3,7 @@
 - import_playbook: ./test-configure-app.yml
 - import_playbook: ./test-haskell-app.yml
 - import_playbook: ./test-noop-deploy.yml
+- import_playbook: ./test-restart-deploy.yml
 - import_playbook: ./test-scalability.yml
 - import_playbook: ./test-service-deps.yml
 - import_playbook: ./test-restart-app.yml
diff --git a/tests/test-restart-deploy.yml b/tests/test-restart-deploy.yml
new file mode 100644 (file)
index 0000000..cbd6cdd
--- /dev/null
@@ -0,0 +1,37 @@
+---
+- name: Launch a restart when commit is already up-to-date on clever
+  hosts: localhost
+  remote_user: root
+  pre_tasks:
+    - file:
+        state: absent
+        path: ../clever-commands
+  roles:
+    - role: clever
+      vars:
+        clever_token: 123abc
+        clever_secret: cba321
+        clever_app: app_00000000-0000-0000-0000-000000000000
+      environment:
+        DEPLOY_UP_TO_DATE_APP: true
+  post_tasks:
+    - name: Check stubbed commands
+      shell: "{{ item.cmd }}"
+      ignore_errors: true
+      with_list:
+        - cmd: "grep deploy ../clever-commands"
+          display: "Expected 'clever deploy' command to be called"
+        - cmd: "grep restart ../clever-commands"
+          display: "Expected 'clever restart' command to be called"
+      vars:
+        display: "{{ item.display }}"
+      register: tests_results
+    - name: show results
+      debug:
+        msg:
+          - "failed_results: {{ failed_results }}"
+          - "success_results: {{ success_results }}"
+      failed_when: tests_results is failed
+      vars:
+        failed_results: "{{ tests_results.results | selectattr('failed') | map(attribute='item.display') | list }}"
+        success_results: "{{ tests_results.results | rejectattr('failed') | map(attribute='item.display') | list }}"