-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cron: rewrite ansible-daily-run script
- Loading branch information
Showing
1 changed file
with
36 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
#!/bin/bash | ||
set -eo pipefail | ||
|
||
readonly LOGFILE=$(mktemp) | ||
trap "rm -f ${LOGFILE}" EXIT | ||
readonly ANSIBLE_LOCAL_REPO=${ANSIBLE_LOCAL_REPO:-'/etc/ansible'} | ||
readonly GIT_BRANCH=${GIT_BRANCH:-'main'} | ||
readonly GIT_REMOTE=${GIT_REMOTE:-'origin'} | ||
readonly ZEUS_STATUS_FILE=${ZEUS_STATUS_FILE:-'/etc/ansible/zeus_daily_run_status.txt'} | ||
|
||
rm "${ZEUS_STATUS_FILE}" | ||
touch "${ZEUS_STATUS_FILE}" | ||
|
||
readonly LOGFILE="$(mktemp)" | ||
echo "Ansible Daily run on Olympus" > "${LOGFILE}" | ||
|
||
cd /etc/ansible | ||
git pull origin olympus >> "${LOGFILE}" | ||
# TODO: update /etc/ansible/vars too | ||
zeus_status() { | ||
if [ -s "${ZEUS_STATUS_FILE}" ]; then | ||
cat "${ZEUS_STATUS_FILE}" | ||
else | ||
echo "Ansible did not ran" | ||
fi | ||
} | ||
|
||
set +e | ||
ansible-playbook -D /etc/ansible/zeus.yml 2>&1 >> "${LOGFILE}" | ||
echo "${?}" > /etc/ansible/zeus_daily_run_status.txt | ||
set -e | ||
mail_report() { | ||
local subject=$(head -1 "${LOGFILE}" | sed -e "s;$; (Status: $(zeus_status));") | ||
cat "${LOGFILE}" | \ | ||
mailx -r "{{ mailer.to }}" -s "{{ jobs.name }}:${subject}" \ | ||
-S smtp="{{ mailer.smtp.host }}:{{mailer.smtp.port }}" "{{ mailer.replyTo }}" | ||
} | ||
|
||
run_zeus() { | ||
echo "Set working directory ${ANSIBLE_LOCAL_REPO}." | ||
cd /etc/ansible | ||
echo "Update local Zeus repository ($(pwd))" | ||
git pull "${GIT_REMOTE}" "${GIT_BRANCH}" | ||
ansible-playbook -D /etc/ansible/zeus.yml 2>&1 | ||
echo "${?}" > "${ZEUS_STATUS_FILE}" | ||
} | ||
|
||
trap mail_report EXIT | ||
trap "rm -f ${LOGFILE}" EXIT | ||
|
||
readonly SUBJECT=$(tail -2 "${LOGFILE}" | sed '/^$/d' | head -1) | ||
cat "${LOGFILE}" | \ | ||
mailx -r "{{ mailer.to }}" -s "{{ jobs.name }}:${SUBJECT}" -S smtp="{{ mailer.smtp.host }}:{{mailer.smtp.port }}" "{{ mailer.replyTo }}" | ||
run_zeus >> "${LOGFILE}" |