From 2292283dca4a2c525d9d93c99a605f926e93585a Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 10 Jun 2019 16:01:26 -0300 Subject: Send logs via email after finishing provision.sh The email will be send for both sucessfull and failed runs. --- scripts/box/create-backup.env.sh | 2 ++ scripts/ci/mail.sh | 33 +++++++++++++++++++++++++++++++++ scripts/ci/provision.sh | 11 +++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100755 scripts/ci/mail.sh (limited to 'scripts') diff --git a/scripts/box/create-backup.env.sh b/scripts/box/create-backup.env.sh index 2c97594..f5cd3b0 100755 --- a/scripts/box/create-backup.env.sh +++ b/scripts/box/create-backup.env.sh @@ -6,7 +6,9 @@ export BORG_PASSPHRASE="${BORG_PASSPHRASE}" # The configured $BORG_REPO is already the rsync remote. # No need to send the files after the backup is done. borg create \ + --verbose \ --stats \ + --progress \ --compression lzma,6 \ "${BORG_REPO}::{hostname}-{now}-${VPS_COMMIT_SHA}" \ "${VOLUME_HOME}"/* diff --git a/scripts/ci/mail.sh b/scripts/ci/mail.sh new file mode 100755 index 0000000..d15c5db --- /dev/null +++ b/scripts/ci/mail.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -Eeuo pipefail +cd "$(dirname "${BASH_SOURCE[0]}")" +cd ../../ + +VPS_COMMIT_SHA="${1:-}" +[[ -z "${VPS_COMMIT_SHA}" ]] && { + echo 'Error: missing $VPS_COMMIT_SHA positional argument.' + exit 2 +} + +SUBJECT="VPS CI run #${JOB_ID} logs" +ATTACHMENT_PATH='logs.txt' +BODY=$(cat < "${ATTACHMENT_PATH}" + +curl "${MAILGUN_URL}" \ + -s \ + --user "${MAILGUN_USER}" \ + -F from="${MAILGUN_FROM}" \ + -F to="${MAILGUN_TO}" \ + -F subject="${SUBJECT}" \ + -F text="${BODY}" \ + -F attachment="@${ATTACHMENT_PATH}" diff --git a/scripts/ci/provision.sh b/scripts/ci/provision.sh index 8b058e2..ff71cb6 100755 --- a/scripts/ci/provision.sh +++ b/scripts/ci/provision.sh @@ -8,6 +8,13 @@ cd ../../ VPS_COMMIT_SHA="$(git rev-parse HEAD)" export VPS_COMMIT_SHA +mail_debug_log() { + echo "Sending logs via email..." + ./scripts/ci/mail.sh "${VPS_COMMIT_SHA}" + echo "Done." +} +trap mail_debug_log EXIT + create_known_hosts_file() { echo "${TLD},$(terraform output public_floating_ip) ssh-rsa $(awk '{print $2}' < ./secrets/ssh/vps-box-server.pub)" > ./generated-known-hosts.txt } @@ -29,7 +36,7 @@ echo "Shutting down running containers and backing up data..." create_known_hosts_file ssh "$TLD" "cd /home/vps/ && docker-compose down" setup_borg_files ./scripts/box/create-backup.env.sh create-backup.sh -ssh "$TLD" /home/vps/create-backup.sh +ssh "$TLD" /home/vps/create-backup.sh 2>&1 > ./logs/borg-create.txt echo "Done." echo "Initializing Terraform..." @@ -71,7 +78,7 @@ echo "Done." echo "Running the Ansible playbook..." create_known_hosts_file -ansible-playbook provision.yaml +ansible-playbook -vvv provision.yaml 2>&1 > ./logs/ansible.txt echo "Done." echo "Locking git-crypt repositories back..." -- cgit v1.2.3