#!/bin/sh

set -eu

DEPLOY_ENV=$1
SYSTEM_DNS_ZONE_NAME=$2

TO=the-multi-cloud-paas-team@digital.cabinet-office.gov.uk
FROM=the-multi-cloud-paas-team@digital.cabinet-office.gov.uk
SMOKE_TEST_LOG=./smoke-tests-log/smoke-tests.log
LAST_COMMIT_LOG=./smoke-tests-log/last-commit.log

text_2_html(){
  TEXT_FILE=$1
  sed -e 's@$@<br/>@g' "${TEXT_FILE}" | tr -d '\n' | tr -d '"' | tr -d '\015'
}

write_message_json() {

  cat <<EOF > message.json
{
  "Subject": {
    "Data": "Smoke tests failed in ${DEPLOY_ENV}"
  },
  "Body": {
    "Html": {
      "Data": "The smoke tests have failed in environment <b>${DEPLOY_ENV}</b>. See \
      <a href='https://deployer.${SYSTEM_DNS_ZONE_NAME}/pipelines/create-bosh-cloudfoundry?groups=health'>Concourse</a> \
      for details<br/>\
      <h3>Last commit:</h3><br/>$(text_2_html "${LAST_COMMIT_LOG}")<br/>\
      <h3>Smoke test log:</h3><br/>$(text_2_html "${SMOKE_TEST_LOG}")"
    }
  }
}
EOF
}

write_message_json

aws ses send-email --to "${TO}" --message file://message.json --from "${FROM}"

Add script to send email notification
Add script to send email notification

To simplify the usage of AWS Simple Email Service we have used the
AWS CLI tool. We did not use the Concourse email resource type as it
does not support instance profiles.

This script creates an email with details from the smoke tests (link to
Concourse, last commit, smoke tests log) and send it using the AWS CLI:
http://docs.aws.amazon.com/cli/latest/reference/ses/send-email.html

The message is formatted as HTML. The content from git and log is
crudely processed so it can be displayed as html and still can fit
into the json payload (basically we remove double quotes and line
endings).
