#!/bin/bash
#
# deploy-logger:
# Call to log whenever a deploy action happens.
#
# Argument is the message to log, as in echo(1), plus an optional Slack thread ID.

HOST=$(hostname)
WHO=$(logname)

MESSAGE="$1"
THREAD="$2"

# Send information to syslog.
logger -p local6.notice -t mysociety_deploylogger -- "${WHO} ${MESSAGE}"

# Post notice to Slack about log entry.
SLACK=`cat /etc/mysociety/slack.deploy.token`
if [[ "$THREAD" ]]; then
    TEXT="[$HOST] $MESSAGE"
    curl --silent -d "token=$SLACK" -d "channel=deploys" --data-urlencode "text=$TEXT" -d "thread_ts=$THREAD" https://slack.com/api/chat.postMessage >/dev/null
elif [[ "$@" =~ "on all servers" ]]; then
    # We need to extract the thread ID for passing to subcommands
    TEXT="$WHO $MESSAGE"
    curl --silent -d "token=$SLACK" -d "channel=deploys" --data-urlencode "text=$TEXT" https://slack.com/api/chat.postMessage | jq .ts
else
    TEXT="[$HOST] $WHO $MESSAGE"
    curl --silent -d "token=$SLACK" -d "channel=deploys" --data-urlencode "text=$TEXT" https://slack.com/api/chat.postMessage >/dev/null
fi
curl --silent -d "token=$SLACK" -d "channel=activity" --data-urlencode "text=$TEXT" https://slack.com/api/chat.postMessage >/dev/null
