From facc275a703d3bccc320e5577eb3d050f99dff0e Mon Sep 17 00:00:00 2001 From: Clement Delafargue Date: Wed, 24 Jun 2020 11:34:28 +0200 Subject: Don't run `clever deploy` if the app is up-to-date Running `git push` when the remote is up-to-date no-ops (it does not trigger a deployment, but it does not fail either). Running `clever deploy` when the remote is up-to-date triggers a failure. Here, we care about the _end result_ (ie the app is deployed on the correct commit), so no-oping is more appropriate. This behaviour is not baked in `clever-tools`, but I think it should. I opened an issue. This script is a temporary workaround. (at least I hope it's temporary) https://github.com/CleverCloud/clever-tools/issues/422 --- files/clever-deploy.sh | 15 +++++++++++++++ tasks/deploy.yml | 2 +- tasks/setup.yml | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100755 files/clever-deploy.sh diff --git a/files/clever-deploy.sh b/files/clever-deploy.sh new file mode 100755 index 0000000..c301a89 --- /dev/null +++ b/files/clever-deploy.sh @@ -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 diff --git a/tasks/deploy.yml b/tasks/deploy.yml index f9d8408..ae11921 100644 --- a/tasks/deploy.yml +++ b/tasks/deploy.yml @@ -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: diff --git a/tasks/setup.yml b/tasks/setup.yml index 8a11772..89bc518 100644 --- a/tasks/setup.yml +++ b/tasks/setup.yml @@ -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 -- cgit v1.2.3 From 783f91c2e0228ef9e057b810b4b60c4f669ebab4 Mon Sep 17 00:00:00 2001 From: Clement Delafargue Date: Tue, 30 Jun 2020 09:30:04 +0200 Subject: Make commit ids overridable in the script mock --- tests/fake.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/fake.sh b/tests/fake.sh index bbb4380..0303b61 100755 --- a/tests/fake.sh +++ b/tests/fake.sh @@ -1,14 +1,17 @@ #!/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: ${*}" -- cgit v1.2.3 From 75a2a2fbcc31766e2debab1c8bd5cdce42f14fb7 Mon Sep 17 00:00:00 2001 From: Clement Delafargue Date: Tue, 30 Jun 2020 09:33:18 +0200 Subject: add a test for `noop-if-up-to-date` It makes sure `clever deploy` is _not_ called if the application is up-to-date on clever cloud --- tests/test-all.yml | 1 + tests/test-noop-deploy.yml | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/test-noop-deploy.yml diff --git a/tests/test-all.yml b/tests/test-all.yml index 64eb239..145b16d 100644 --- a/tests/test-all.yml +++ b/tests/test-all.yml @@ -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 diff --git a/tests/test-noop-deploy.yml b/tests/test-noop-deploy.yml new file mode 100644 index 0000000..13eed20 --- /dev/null +++ b/tests/test-noop-deploy.yml @@ -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 }}" -- cgit v1.2.3 From 545c41a6694ac5273dbfe1a0e119be04eaeef468 Mon Sep 17 00:00:00 2001 From: Clement Delafargue Date: Tue, 30 Jun 2020 15:33:22 +0200 Subject: fix(test): log all commads to `$binary-commands` Previously, only the commands not directly handled by the stub were logged. We want to log all commands instead --- tests/fake.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/fake.sh b/tests/fake.sh index 0303b61..65ff819 100755 --- a/tests/fake.sh +++ b/tests/fake.sh @@ -13,6 +13,7 @@ elif [ "${binary}" = "clever" ] && [ "${1}" = "status" ]; then elif [ "${binary}" = "git" ]; then echo "${localCommit}" else - echo "${1}" >> "${binary}-commands" echo "${binary} called with arguments: ${*}" fi + +echo "${1}" >> "${binary}-commands" -- cgit v1.2.3 From db630aaa8b261503076eb7320ee3cee5a5582be8 Mon Sep 17 00:00:00 2001 From: Clement Delafargue Date: Tue, 30 Jun 2020 15:46:41 +0200 Subject: fix(tests): use `! grep` instead of `grep -v` `grep -v` makes sure a file contains other lines than the one that matched. This is different from `! grep` which makes sure the file does not contain a line that matched. --- tests/test-configure-app.yml | 4 ++-- tests/test-restart-app.yml | 6 +++--- tests/test-scalability.yml | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test-configure-app.yml b/tests/test-configure-app.yml index 15987fd..e9861a2 100644 --- a/tests/test-configure-app.yml +++ b/tests/test-configure-app.yml @@ -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-restart-app.yml b/tests/test-restart-app.yml index 4c13a14..bc880a8 100644 --- a/tests/test-restart-app.yml +++ b/tests/test-restart-app.yml @@ -15,16 +15,16 @@ 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 diff --git a/tests/test-scalability.yml b/tests/test-scalability.yml index 6bb90d2..258beaf 100644 --- a/tests/test-scalability.yml +++ b/tests/test-scalability.yml @@ -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 -- cgit v1.2.3