]> git.immae.eu Git - github/fretlink/ansible-clever.git/commitdiff
Merge pull request #72 from clementd-fretlink/noop-if-up-to-date
authorClément Delafargue <39330590+clementd-fretlink@users.noreply.github.com>
Tue, 30 Jun 2020 14:31:26 +0000 (16:31 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Jun 2020 14:31:26 +0000 (16:31 +0200)
don't run `clever deploy` if the app is up-to-date

files/clever-deploy.sh [new file with mode: 0755]
tasks/deploy.yml
tasks/setup.yml
tests/fake.sh
tests/test-all.yml
tests/test-configure-app.yml
tests/test-noop-deploy.yml [new file with mode: 0644]
tests/test-restart-app.yml
tests/test-scalability.yml

diff --git a/files/clever-deploy.sh b/files/clever-deploy.sh
new file mode 100755 (executable)
index 0000000..c301a89
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+
+function getHeadRev {
+  git rev-parse HEAD
+}
+
+target_commit="$(getHeadRev)"
+running_commit=$(clever status | grep running | sed 's/^.*Commit: //' | sed 's/)$//')
+
+if [ "${running_commit}" != "${target_commit}" ]; then
+  echo "Deploying commit ${target_commit}"
+  clever deploy --force
+else
+  echo "${target_commit} is already deployed, nothing to do"
+fi
index f9d84088d6d8bedc50945e34394b84f8ee7a6a1e..ae119218a77e034cec3d813f0e72263cdfb7b4ce 100644 (file)
@@ -47,7 +47,7 @@
   changed_when: false
 
 - name: Deploy to Clever-Cloud
-  shell: "clever deploy --force"
+  command: "{{ ansible_env.HOME }}/{{ clever_user_path }}/clever-deploy.sh"
   args:
     chdir: "{{ clever_app_root }}"
   environment:
index 8a11772639258333bfac385f1feda783e9371ae0..89bc5185945d7fc8dbfab70b849d6c7dc0a1ae16 100644 (file)
@@ -27,6 +27,7 @@
     dest: "{{ ansible_env.HOME }}/{{ clever_user_path }}/{{ item }}"
     mode: 0755
   with_items:
+    - clever-deploy.sh
     - clever-set-domain.sh
     - clever-set-drain.sh
     - clever-set-scalability.sh
index bbb4380637bf691669764043ccd1856cc9ec4c6e..65ff819ffd4b538c71d5dc91ed89829dc9fbf673 100755 (executable)
@@ -1,15 +1,19 @@
 #!/usr/bin/env bash
 
 binary="${0/*\/}"
-fakeCommit="aaa000aaa000aaa000aaa000aaa000aaa000aaa0"
+localCommit="${PRETEND_LOCAL_COMMIT:-aaa000aaa000aaa000aaa000aaa000aaa000aaa0}"
+remoteCommit="${PRETEND_REMOTE_COMMIT:-bbb000bbb000bbb000bbb000bbb000bbb000bbb0}"
 
 if [ "${binary}" = "clever" ] && [ "${1}" = "--version" ]; then
     echo "2.6.1"
 elif [ "${binary}" = "clever" ] && [ "${1}" = "activity" ]; then
-    echo "2020-02-02T20:20:02+02:00  OK         DEPLOY     ${fakeCommit}  Git"
+    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}" = "git" ]; then
-    echo "${fakeCommit}"
+    echo "${localCommit}"
 else
-    echo "${1}" >> "${binary}-commands"
     echo "${binary} called with arguments: ${*}"
 fi
+
+echo "${1}" >> "${binary}-commands"
index 64eb239d58a7cec672d2ea5137cf94f78645353a..145b16d2b1f867d6aa0453dc012779b94ff95f11 100644 (file)
@@ -2,5 +2,6 @@
 - import_playbook: ./test-simple-app.yml
 - import_playbook: ./test-configure-app.yml
 - import_playbook: ./test-haskell-app.yml
+- import_playbook: ./test-noop-deploy.yml
 - import_playbook: ./test-scalability.yml
 - import_playbook: ./test-restart-app.yml
index 15987fd8248288743e2d529e8b2027205a064e99..e9861a2e12359063b5381407a27ff7a2d084a220 100644 (file)
@@ -21,7 +21,7 @@
         msg: "CC_RUN_COMMAND env variable should not be present in the environment dict"
       when: clever_env.CC_RUN_COMMAND is defined
     - name: Check stubbed commands
-      command: "{{ item.cmd }}"
+      shell: "{{ item.cmd }}"
       ignore_errors: true
       vars:
         display: "{{ item.display }}"
@@ -34,7 +34,7 @@
           display: "Expected 'clever domain' command to be called"
         - cmd: "grep drain ../clever-commands"
           display: "Expected 'clever drain' command to be called"
-        - cmd: "grep -v restart ../clever-commands"
+        - cmd: "! grep restart ../clever-commands"
           display: "Expected 'clever restart' command to NOT be called"
       register: tests_results
     - name: show results
diff --git a/tests/test-noop-deploy.yml b/tests/test-noop-deploy.yml
new file mode 100644 (file)
index 0000000..13eed20
--- /dev/null
@@ -0,0 +1,35 @@
+---
+- name: Do nothing if the app is 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:
+        PRETEND_REMOTE_COMMIT: aaa000aaa000aaa000aaa000aaa000aaa000aaa0
+  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 NOT 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 }}"
index 4c13a14fd01230120b4ddb30f0d841ede4e8454f..bc880a8fb1a9a8f7d137c18be6b207ff1181a60f 100644 (file)
         clever_restart_only: true
   post_tasks:
     - name: Check stubbed commands
-      command: "{{ item.cmd }}"
+      shell: "{{ item.cmd }}"
       ignore_errors: true
       vars:
         display: "{{ item.display }}"
       with_list:
         - cmd: "grep restart ../clever-commands"
           display: "Expected 'clever restart' command to be called"
-        - cmd: "grep -v deploy ../clever-commands"
+        - cmd: "! grep deploy ../clever-commands"
           display: "Expected 'clever deploy' command to NOT be called"
-        - cmd: "grep -v scale ../clever-commands"
+        - cmd: "! grep scale ../clever-commands"
           display: "Expected 'clever scale' command to NOT be called"
       register: tests_results
     - name: show results
index 6bb90d2ac97ecd04116bcd7402f900e8daf67237..258beaf5dcabb69bd5fef50d8b8ac62734a91055 100644 (file)
@@ -29,7 +29,7 @@
           flavors: { min: "nano", max: "XS" }
   post_tasks:
     - name: Check stubbed commands
-      command: "{{ item.cmd }}"
+      shell: "{{ item.cmd }}"
       ignore_errors: true
       vars:
         display: "{{ item.display }}"
@@ -38,7 +38,7 @@
           display: "Expected 'clever deploy' command to be called"
         - cmd: "grep scale ../clever-commands"
           display: "Expected 'clever scale' command to be called"
-        - cmd: "grep -v restart ../clever-commands"
+        - cmd: "! grep restart ../clever-commands"
           display: "Expected 'clever restart' command to NOT be called"
       register: tests_results
     - name: show results