From e714e47e2a3b592a1cdbb6b95a2c691dc02524df Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Fri, 3 Nov 2023 16:08:29 +0100 Subject: [PATCH] Pass exit codes in environments/manager/run.sh Closes osism/issues#744 Signed-off-by: Christian Berendt --- environments/manager/run.sh | 23 +++++++++++-------- .../pass-exit-code-0f5f47b31efe5e2b.yaml | 7 ++++++ 2 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 releasenotes/notes/pass-exit-code-0f5f47b31efe5e2b.yaml diff --git a/environments/manager/run.sh b/environments/manager/run.sh index cbc2098..36681bb 100755 --- a/environments/manager/run.sh +++ b/environments/manager/run.sh @@ -43,6 +43,14 @@ command -v ansible-galaxy >/dev/null 2>&1 || { echo >&2 "ansible-galaxy not inst ANSIBLE_USER=${ANSIBLE_USER:-dragon} CLEANUP=${CLEANUP:-false} +cleanup () { + if [[ $CLEANUP == "true" ]]; then + rm id_rsa.operator + rm -rf "$VENV_PATH" + fi +} +trap cleanup ERR EXIT + if [[ $INSTALL_ANSIBLE_ROLES == "true" ]]; then ansible-galaxy collection install -f "git+https://github.com/osism/ansible-collection-commons,${ANSIBLE_COLLECTION_COMMONS_VERSION}" ansible-galaxy collection install -f "git+https://github.com/osism/ansible-collection-services,${ANSIBLE_COLLECTION_SERVICES_VERSION}" @@ -54,7 +62,7 @@ if [[ ! -e id_rsa.operator ]]; then -i localhost, \ -e @../secrets.yml \ -e "keypair_dest=$(pwd)/id_rsa.operator" \ - osism.manager.keypair "$@" + osism.manager.keypair "$@" || exit $? fi if [[ $playbook == "k8s" || $playbook == "netbox" || $playbook == "traefik" ]]; then @@ -71,7 +79,7 @@ if [[ $playbook == "k8s" || $playbook == "netbox" || $playbook == "traefik" ]]; -e @configuration.yml \ -e @secrets.yml \ -u "$ANSIBLE_USER" \ - osism.manager."$playbook" "$@" + osism.manager."$playbook" "$@" || exit $? elif [[ $playbook == "operator" ]]; then if [[ $ANSIBLE_ASK_PASS == "True" ]]; then ansible-playbook \ @@ -83,7 +91,7 @@ elif [[ $playbook == "operator" ]]; then -e @configuration.yml \ -e @secrets.yml \ -u "$ANSIBLE_USER" \ - osism.manager."$playbook" "$@" + osism.manager."$playbook" "$@" || exit $? else ansible-playbook \ --private-key id_rsa.operator \ @@ -95,7 +103,7 @@ elif [[ $playbook == "operator" ]]; then -e @configuration.yml \ -e @secrets.yml \ -u "$ANSIBLE_USER" \ - osism.manager."$playbook" "$@" + osism.manager."$playbook" "$@" || exit $? fi else ansible-playbook \ @@ -108,10 +116,5 @@ else -e @configuration.yml \ -e @secrets.yml \ -u "$ANSIBLE_USER" \ - osism.manager."$playbook" "$@" -fi - -if [[ $CLEANUP == "true" ]]; then - rm id_rsa.operator - rm -rf "$VENV_PATH" + osism.manager."$playbook" "$@" || exit $? fi diff --git a/releasenotes/notes/pass-exit-code-0f5f47b31efe5e2b.yaml b/releasenotes/notes/pass-exit-code-0f5f47b31efe5e2b.yaml new file mode 100644 index 0000000..32ff44b --- /dev/null +++ b/releasenotes/notes/pass-exit-code-0f5f47b31efe5e2b.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + In the `run.sh` script in `environments/manager`, the execution is now interrupted + directly in the event of an error in an Ansible run and the exit code is passed through. + A trap is used to keep things clean as before (only if a cleanup was requested with + `CLEANUP=True`).