From 7ceb5d83f04daeb81fb07814a48dd61aca41430f Mon Sep 17 00:00:00 2001 From: Paul Bonaud Date: Wed, 17 Jun 2020 11:06:08 +0200 Subject: deploy: remove custom polling script now that the CLI does it for us See recent changes (https://github.com/CleverCloud/clever-tools/pull/415) introduced in 2.5.0+ version of the clever cli tools --- defaults/main.yml | 2 +- files/clever-wait-deploy.sh | 96 --------------------------------------------- tasks/deploy.yml | 73 +++++++++------------------------- tasks/redeploy.yml | 33 ++++++++++++++++ tasks/setup.yml | 3 +- tests/fake.sh | 2 +- 6 files changed, 55 insertions(+), 154 deletions(-) delete mode 100755 files/clever-wait-deploy.sh create mode 100644 tasks/redeploy.yml 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 @@ --- # defaults file for clever -clever_cli_version: 1.6.3 +clever_cli_version: 2.6.1 clever_user_path: .local/bin clever_app_root: "{{ app_root | default(playbook_dir + '/..') }}" 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 @@ -#!/usr/bin/env bash - -set -ueo pipefail - -VERBOSE=${VERBOSE:-} -lastCleverActivity="" -timeout=2300 # 100 seconds less than the Ansible tasks' timeout -startTime=$(date +%s) - -function cleverActivity { - lastCleverActivity=$(clever activity) -} - -function ensure { - local lastReturnCode="$?" - - VERBOSE=true - verbose - - if isNotTimeout - then - if [ "$lastReturnCode" == "0" ] - then - echo "✓ Deployment done." - else - echo "✗ Deployment failed!" - fi - else - echo "⁈ Deployment timeout... Please check clever logs" - exit 1 - fi -} - -function isNotTimeout { - [ $(($(date +%s) - startTime)) -lt $timeout ] -} - -trap ensure EXIT ERR - -function deploying { - checkStatus "$1" "IN PROGRESS" -} - -function deployed { - checkStatus "$1" "OK" -} - -function inactive { - checkStatus "$1" "" "-v" -} - -function checkStatus { - local commit="$1" - local status="$2" - - cleverActivity - echo "${lastCleverActivity}" | tail -n1 | grep ${3:+ "${3}"} -q -E "${status}.*DEPLOY.*${commit}" -} - -function verbose { - if [ -n "${VERBOSE}" ]; then - echo -e "\\nLast clever activity:" - echo -e "${lastCleverActivity}\\n" - fi -} - -function check { - local commit="$1" - local samplingTime=5 - - echo "️▫ Waiting for deployment to start..." - while inactive "$commit" && isNotTimeout - do - verbose - sleep $samplingTime - done - - # Wait for completion - echo "▪ Deployment in progress..." - while deploying "$commit" && isNotTimeout - do - verbose - sleep $samplingTime - done - - deployed "$commit" -} - -function getHeadRev { - local chdir="$1/.git" - - git --git-dir="$chdir" rev-parse HEAD -} - -workdir="$(pwd)" -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 @@ CONFIGURATION_FILE: "{{ clever_login_file }}" changed_when: false -#TODO: Improve ssh-key validation -- name: Accept Clever-Cloud servers - shell: "ssh-keyscan -H push-par-clevercloud-customers.services.clever-cloud.com >> ~/.ssh/known_hosts" - tags: - - skip_ansible_lint - - name: Deploy to Clever-Cloud - command: "git push --force git+ssh://git@push-par-clevercloud-customers.services.clever-cloud.com/{{ clever_app }}.git HEAD:refs/heads/master" - args: - chdir: "{{ clever_app_root }}" - register: clever_deploy - ignore_errors: true - tags: - - skip_ansible_lint - -################################################################## -# Re-deploy only if its the first clever deploy for that project # -################################################################## - -- name: First time push to Clever-Cloud needs a full git clone - command: "git fetch --unshallow" - args: - chdir: "{{ clever_app_root }}" - when: - - clever_deploy is failed - - clever_deploy.stderr is defined - - clever_deploy.stderr is search("Failed to read git object") - tags: - - skip_ansible_lint - -- name: Deploy to Clever-Cloud - command: "git push --force git+ssh://git@push-par-clevercloud-customers.services.clever-cloud.com/{{ clever_app }}.git HEAD:refs/heads/master" - args: - chdir: "{{ clever_app_root }}" - when: - - clever_deploy is failed - - clever_deploy.stderr is defined - - clever_deploy.stderr is search("Failed to read git object") - register: clever_deploy - tags: - - skip_ansible_lint - -############################################################################## -# Poll deployment status from 'clever activity' command. # -# 'clever deploy' command is not yet 100% reliable to get a blocking command # -# until the deployment is over. Hence the need for a custom waiting script. # -############################################################################## - -- name: Watch deployment status - command: "{{ ansible_env.HOME }}/{{ clever_user_path }}/clever-wait-deploy.sh" + shell: "clever deploy --force" args: chdir: "{{ clever_app_root }}" environment: CONFIGURATION_FILE: "{{ clever_login_file }}" async: 2400 # 40 minutes poll: 0 - register: long_command - changed_when: False + ignore_errors: true + register: clever_deploy + tags: + - skip_ansible_lint - name: Wait 40 minutes for deployment completion async_status: - jid: "{{ long_command.ansible_job_id }}" + jid: "{{ clever_deploy.ansible_job_id }}" register: job_result until: job_result.finished ignore_errors: true delay: 30 retries: 80 # 40 minutes (80 * 30 secs delay) -- name: Waiting script logs (stdout) +################################################################## +# Re-deploy only if its the first clever deploy for that project # +################################################################## + +- name: Retry deploy to clever-cloud + include_tasks: redeploy.yml + when: + - not job_result.rc == 0 + - job_result.stderr is defined + - job_result.stderr is search("Failed to read git object") + +- name: Deploy logs (stdout) debug: var: job_result.stdout_lines when: job_result.stdout_lines is defined -- name: Waiting script logs (stderr) +- name: Deploy error logs (stderr) debug: var: job_result.stderr_lines 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 @@ +--- +################################################################## +# Re-deploy only if its the first clever deploy for that project # +################################################################## + +- name: First time push to Clever-Cloud needs a full git clone + command: "git fetch --unshallow" + args: + chdir: "{{ clever_app_root }}" + tags: + - skip_ansible_lint + +- name: Deploy to Clever-Cloud + shell: "clever deploy --force" + args: + chdir: "{{ clever_app_root }}" + environment: + CONFIGURATION_FILE: "{{ clever_login_file }}" + async: 2400 # 40 minutes + poll: 0 + ignore_errors: true + register: clever_deploy + tags: + - skip_ansible_lint + +- name: Wait 40 minutes for deployment completion + async_status: + jid: "{{ clever_deploy.ansible_job_id }}" + register: job_result + until: job_result.finished + ignore_errors: true + delay: 30 + 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 @@ src: "{{ clever_repo }}/{{ clever_tools_archive }}" dest: "{{ ansible_env.HOME }}/{{ clever_user_path }}" vars: - clever_repo: https://clever-tools.cellar.services.clever-cloud.com/releases + clever_repo: https://clever-tools.clever-cloud.com/releases clever_tools_archive: "{{ clever_cli_version }}/clever-tools-{{ clever_cli_version }}_linux.tar.gz" when: clever_returned_version is failed or clever_returned_version.stdout != clever_cli_version @@ -27,6 +27,5 @@ dest: "{{ ansible_env.HOME }}/{{ clever_user_path }}/{{ item }}" mode: 0755 with_items: - - clever-wait-deploy.sh - clever-set-domain.sh - 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/*\/}" fakeCommit="aaa000aaa000aaa000aaa000aaa000aaa000aaa0" if [ "${binary}" = "clever" ] && [ "${1}" = "--version" ]; then - echo "1.6.3" + echo "2.6.1" elif [ "${binary}" = "clever" ] && [ "${1}" = "activity" ]; then echo "2020-02-02T20:20:02+02:00 OK DEPLOY ${fakeCommit} Git" elif [ "${binary}" = "git" ]; then -- cgit v1.2.3