From d0da412ef1a9312022122a765d8f393ec94581b3 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Thu, 27 Sep 2018 17:52:05 -0500 Subject: [PATCH 01/19] [skip-ci] WIP --- config/core/adhoc/ansible.cfg | 12 ++ src/roles/base/templates/config.php.j2 | 34 ++++ src/roles/base/templates/config.sh.j2 | 35 ++++ src/scripts/autodeployer/check-for-changes.sh | 180 ++++++++++++++++++ src/scripts/autodeployer/do-deploy.sh | 48 +++++ src/scripts/autodeployer/git-fetch.sh | 17 ++ src/scripts/autodeployer/slack-notify.sh | 93 +++++++++ 7 files changed, 419 insertions(+) create mode 100644 config/core/adhoc/ansible.cfg create mode 100644 src/scripts/autodeployer/check-for-changes.sh create mode 100644 src/scripts/autodeployer/do-deploy.sh create mode 100644 src/scripts/autodeployer/git-fetch.sh create mode 100644 src/scripts/autodeployer/slack-notify.sh diff --git a/config/core/adhoc/ansible.cfg b/config/core/adhoc/ansible.cfg new file mode 100644 index 000000000..e0c9ee54e --- /dev/null +++ b/config/core/adhoc/ansible.cfg @@ -0,0 +1,12 @@ +; This file is used when performing ad hoc commands, e.g. +; `ansible localhost -m git -a "repo=http://repo.com dest=/opt/abc version=dev"` +; Scripts (or humans) must `cd` into this directory so Ansible knows to use this +; file versus any other. + +[defaults] + +; makes output for ad hoc commands in json, so jq can process it +stdout_callback = json + +; use callbacks on ad hoc commands +bin_ansible_callbacks=True diff --git a/src/roles/base/templates/config.php.j2 b/src/roles/base/templates/config.php.j2 index c6a45ef15..159923924 100644 --- a/src/roles/base/templates/config.php.j2 +++ b/src/roles/base/templates/config.php.j2 @@ -345,3 +345,37 @@ $slack_webhook_token_disk_usage="{{ slack_webhook_token_disk_usage }}"; # Slack username to display for webhook $slack_username_disk_usage="{{ slack_username_disk_usage }}"; {% endif %} + + +# +# Auto-deployer +# +{% if local_config_repo.repo is defined %} +# Info about local public config +$local_config_repo_repo="{{ local_config_repo.repo }}"; +$local_config_repo_version="{{ local_config_repo.version | default('master') }}"; +$local_config_repo_force="{{ local_config_repo.force | default(false) | bool }}"; +{% endif %} + +{% if enforce_meza_version is defined %} +# What Meza version to track +$enforce_meza_version="{{ enforce_meza_version }}"; +{% endif %} + +{% if autodeployer.slack_channel is defined %} +autodeployer_slack_channel="{{ autodeployer.slack_channel }}" +{% endif %} + +{% if autodeployer.slack_token is defined %} +autodeployer_slack_token="{{ autodeployer.slack_token }}" +{% endif %} + +{% if autodeployer.slack_username is defined %} +autodeployer_slack_username="{{ autodeployer.slack_username }}" +{% endif %} + +{% if autodeployer.slack_icon_url is defined %} +autodeployer_slack_icon_url="{{ autodeployer.slack_icon_url }}" +{% endif %} + + diff --git a/src/roles/base/templates/config.sh.j2 b/src/roles/base/templates/config.sh.j2 index 035da8550..25d76e01c 100644 --- a/src/roles/base/templates/config.sh.j2 +++ b/src/roles/base/templates/config.sh.j2 @@ -173,3 +173,38 @@ slack_webhook_token_disk_usage="{{ slack_webhook_token_disk_usage }}" # Slack username to display for webhook slack_username_disk_usage="{{ slack_username_disk_usage }}" {% endif %} + + + +# +# Auto-deployer +# + +{% if local_config_repo.repo is defined %} +# Info about local public config +local_config_repo_repo="{{ local_config_repo.repo }}" +local_config_repo_version="{{ local_config_repo.version | default('master') }}" +local_config_repo_force="{{ local_config_repo.force | default(false) | bool }}" +{% endif %} + +{% if enforce_meza_version is defined %} +# What Meza version to track +enforce_meza_version="{{ enforce_meza_version }}" +{% endif %} + +{% if autodeployer.slack_channel is defined %} +autodeployer_slack_channel="{{ autodeployer.slack_channel }}" +{% endif %} + +{% if autodeployer.slack_token is defined %} +autodeployer_slack_token="{{ autodeployer.slack_token }}" +{% endif %} + +{% if autodeployer.slack_username is defined %} +autodeployer_slack_username="{{ autodeployer.slack_username }}" +{% endif %} + +{% if autodeployer.slack_icon_url is defined %} +autodeployer_slack_icon_url="{{ autodeployer.slack_icon_url }}" +{% endif %} + diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh new file mode 100644 index 000000000..0d0b7f4fd --- /dev/null +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -0,0 +1,180 @@ +#!/bin/sh + +# Don't allow errors +set -e + +echo "Start meza auto-deployer" +echo $(date "+%Y-%m-%d %H:%M:%S") + +# must be root or sudoer +if [ "$(whoami)" != "root" ]; then + echo "Try running this script with sudo: \"sudo bash unite-the-wikis.sh\"" + exit 1 +fi + +# Gets info about public config +source /opt/.deploy-meza/config.sh + +GIT_FETCH="$m_scripts/git-fetch.sh" +SLACK_NOTIFY="$m_scripts/slack-notify.sh" + +# Set Slack notify variables that are the same for all notifications +if [ ! -z "$autodeployer_slack_token" ]; then SLACK_TOKEN="$autodeployer_slack_token"; fi +if [ ! -z "$autodeployer_slack_username" ]; then SLACK_USERNAME="$autodeployer_slack_username"; fi +if [ ! -z "$autodeployer_slack_channel" ]; then SLACK_CHANNEL="$autodeployer_slack_channel"; fi +if [ ! -z "$autodeployer_slack_icon_url" ]; then SLACK_ICON_URL="$autodeployer_slack_icon_url"; fi + +# FIXME: For now, don't touch secret config. At some point find a way to +# configure it's repo and version. + +if [ -z "$local_config_repo_repo" ]; then + >&2 echo "Auto-deploy requires 'local_config_repo' var set in secret config" + exit 1; +fi + +if [ -z "$enforce_meza_version" ]; then + >&2 echo "Auto-deploy requires 'enforce_meza_version' var set in public config" + exit 1; +fi + +# Set PUBLIC config version +# +# Could optionally set public config's repo in secret config, but since that is +# not done universally, not going to enforce it here. Just use whatever repo is +# currently being used as origin. +PUBLIC_CONFIG_DEST="/opt/conf-meza/public" +PUBLIC_CONFIG_REPO="$local_config_repo_repo" +PUBLIC_CONFIG_VERSION="$local_config_repo_version" +PUBLIC_CONFIG_CHANGE=$($GIT_FETCH "$PUBLIC_CONFIG_REPO" "$PUBLIC_CONFIG_DEST" "$PUBLIC_CONFIG_VERSION") + +# +# Check if attempt to git-pull PUBLIC CONFIG failed +# +echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.failed' -e +if [ $? -eq 0 ]; then + FAILED_MSG=$(echo "$PUBLIC_CONFIG_CHANGE" | jq .plays[0].tasks[0].hosts.localhost.msg -r) + FULL_MSG="Updating public config failed with following message:\n $FAILED_MSG" + >&2 echo -e "$FULL_MSG" + + if [ ! -z "SLACK_TOKEN" ]; then + SLACK_MESSAGE="$FULL_MSG" + SLACK_COLOR="danger" + source $SLACK_NOTIFY + fi + exit 1; +fi + +# +# Check if changes were made to PUBLIC CONFIG +# +echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.changed' -e +if [ $? -eq 0 ]; then + PUBLIC_CONFIG_BEFORE_HASH=$(echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.before' -r) + PUBLIC_CONFIG_AFTER_HASH=$(echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.after' -r) + PUBLIC_CONFIG_DIFF=$(cd "$DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH..$PUBLIC_CONFIG_AFTER_HASH") +else + PUBLIC_CONFIG_DIFF="" +fi + +# Set MEZA version +MEZA_REPO="https://github.com/enterprisemediawiki/meza" +MEZA_DEST="/opt/meza" +MEZA_VERSION="$enforce_meza_version" +MEZA_CHANGE=$($GIT_FETCH "$MEZA_REPO" "$MEZA_DEST" "$MEZA_VERSION") + +# +# Check if attempt to git-pull MEZA failed +# +echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.failed' -e +if [ $? -eq 0 ]; then + FAILED_MSG=$(echo "$MEZA_CHANGE" | jq .plays[0].tasks[0].hosts.localhost.msg -r) + FULL_MSG="Updating Meza failed with following message:\n $FAILED_MSG" + >&2 echo -e "$FULL_MSG" + + if [ ! -z "SLACK_TOKEN" ]; then + SLACK_MESSAGE="$FULL_MSG" + SLACK_COLOR="danger" + source $SLACK_NOTIFY + fi + exit 1; +fi + +# +# Check if changes were made to MEZA +# +echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.changed' -e +if [ $? -eq 0 ]; then + MEZA_BEFORE_HASH=$(echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.before' -r) + MEZA_AFTER_HASH=$(echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.after' -r) +else + MEZA_AFTER_HASH="" +fi + + +# +# Neither Meza mor config changed? Exit. +# +if [ -z "$PUBLIC_CONFIG_DIFF$MEZA_AFTER_HASH" ]; then + echo "Nothing to deploy" + exit 0; +fi + +# +# Notify if PUBLIC CONFIG changed +# +if [ ! -z "$PUBLIC_CONFIG_DIFF" ]; then + + MESSAGE=$(cat <<-END + Public config changed versions: + FROM: $PUBLIC_CONFIG_BEFORE_HASH + TO: $PUBLIC_CONFIG_AFTER_HASH + + Tracking version: $PUBLIC_CONFIG_VERSION + + Diff: + + $PUBLIC_CONFIG_DIFF +END +) + + echo -e "$MESSAGE" + + if [ ! -z "SLACK_TOKEN" ]; then + SLACK_MESSAGE="$MESSAGE" + SLACK_COLOR="good" + source $SLACK_NOTIFY + fi +fi + +# +# Notify if MEZA changed +# +if [ ! -z "$MEZA_AFTER_HASH" ]; then + + MESSAGE=$(cat <<-END + Meza application changed versions: + FROM: $MEZA_BEFORE_HASH + TO: $MEZA_AFTER_HASH + + Tracking version: $MEZA_VERSION +END +) + + echo -e "$MESSAGE" + + if [ ! -z "SLACK_TOKEN" ]; then + SLACK_MESSAGE="$MESSAGE" + SLACK_COLOR="good" + source $SLACK_NOTIFY + fi +fi + + +# +# Do deploy +# +echo "Deploying" +DEPLOY_TYPE="Deploy" +DEPLOY_ARGS="" # autodeploy deploys everything +DEPLOY_LOG_PREFIX="deploy-after-config-change-" +source "$DIR/do-deploy.sh" diff --git a/src/scripts/autodeployer/do-deploy.sh b/src/scripts/autodeployer/do-deploy.sh new file mode 100644 index 000000000..463665d81 --- /dev/null +++ b/src/scripts/autodeployer/do-deploy.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# +# Do deploy. Notify on success. Notify and retry on fail. +# +# # DEPLOY_TYPE="Deploy" bash /opt/meza-backup-notifier/do-deploy.sh "" "deploy-after-config-change-" + + +# Path to this file's directory +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + + +NOTIFY="$DIR/slack-notify.sh" + + +###FIXME DESPLOY TYPE### +SLACK_MESSAGE="$DEPLOY_TYPE starting" +source $NOTIFY + + +if [ -z "$LOG_PREFIX" ]; then + LOG_PREFIX="deploy-" +fi + +# Make two attempts at deploy +meza deploy "$ENVIRONMENT" $DEPLOY_ARGS \ + > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 \ + && source $NOTIFY success \ + || ( \ + (source $NOTIFY retry && meza deploy "$ENVIRONMENT" $DEPLOY_ARGS) \ + > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 \ + && source $NOTIFY success \ + || source $NOTIFY fail + ) + + + +auto_backup_from_source: + my_env_name: + cron_time: "0 2 * * *" + yes_i_want_this_env_overwritten: True + my_other_env: + cron_time: "0 2 * * 6" + yes_i_want_this_env_overwritten: True + +autodeploy_times: + my_env_name: "0 * * * 1-5" + my_other_env: "30 * * * 1-5" + my_prod_env: "0 19 * * 1-4" diff --git a/src/scripts/autodeployer/git-fetch.sh b/src/scripts/autodeployer/git-fetch.sh new file mode 100644 index 000000000..abbab7769 --- /dev/null +++ b/src/scripts/autodeployer/git-fetch.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# +# Use Ansible's Git module plus jq to easily set repository versions + +# Get directory to return to at end +CWD=$(pwd) + +# change to directory holding appropriate ansible.cfg file for this operation +cd /opt/meza/config/core/adhoc + +REPO="$1" +DEST="$2" +VERSION="$3" + +ansible localhost -m git -a "repo=$REPO dest=$DEST version=$VERSION" + +cd "$CWD" diff --git a/src/scripts/autodeployer/slack-notify.sh b/src/scripts/autodeployer/slack-notify.sh new file mode 100644 index 000000000..9b0185c23 --- /dev/null +++ b/src/scripts/autodeployer/slack-notify.sh @@ -0,0 +1,93 @@ +#!/bin/sh +# +# Use Ansible's Slack module to send messages. +# +# Required variables +# SLACK_TOKEN="" # Exits script if you don't set this. +# +# Recommended variables +# SLACK_MESSAGE="" # Defaults to a pointless message. Set this one. +# SLACK_COLOR="" # Defaults to "good" which means "green". Also available +# are "warning" (orange) and "danger" (red) or using hex +# code (e.g. #439FE0 = light blue) +# +# Optional variables +# SLACK_USERNAME="" # Will use your Slack token's default username by default +# SLACK_CHANNEL="" # Will use your Slack token's default channel by default +# SLACK_ICON_URL="" # will use Meza logo by default + +# Get directory to return to at end +CWD=$(pwd) + +# change to directory holding appropriate ansible.cfg file for this operation +cd /opt/meza/config/core/adhoc + + + +if [ -z "$SLACK_TOKEN" ]; + >&2 echo "You need to set a SLACK_TOKEN variable" + exit 1; +fi + +if [ -z "$SLACK_CHANNEL" ]; then + SLACK_CHANNEL_WITH_PARAM="channel=$SLACK_CHANNEL" +else + SLACK_CHANNEL_WITH_PARAM="" # use default for token +fi + +if [ -z "$SLACK_MESSAGE" ]; then + SLACK_MESSAGE="Empty message." +fi + +if [ -z "$SLACK_USERNAME" ]; then + SLACK_USERNAME_WITH_PARAM="username='$SLACK_USERNAME'" +else + SLACK_USERNAME_WITH_PARAM="" # use default for token +fi + +if [ -z "$SLACK_ICON_URL" ]; then + SLACK_ICON_URL="https://github.com/enterprisemediawiki/meza/raw/master/src/roles/configure-wiki/files/logo.png" +fi + +if [ -z "$SLACK_COLOR" ]; then + SLACK_COLOR="good" # assume all is well +fi + + +# Escape single quotes +SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s/'/\\\'/g") + +# Turn on allowing failures +set +e + +# Attempt to send message +ansible localhost -m slack -a \ + "token=$SLACK_TOKEN \ + $SLACK_CHANNEL_WITH_PARAM \ + msg='$SLACK_MESSAGE' \ + $SLACK_USERNAME_WITH_PARAM \ + icon_url=$SLACK_ICON_URL \ + link_names=1 \ + color=$SLACK_COLOR" + +# If message fails, send a generic message +if [ $? -eq 0 ]; then + echo "Slack notify success" +else + echo "Slack notify fail. Attempted message was:" + echo "$SLACK_MESSAGE" + SLACK_MESSAGE="Slack message failed. See logs for attempted message." + ansible localhost -m slack -a \ + "token=$SLACK_TOKEN \ + $SLACK_CHANNEL_WITH_PARAM \ + msg='$SLACK_MESSAGE' \ + $SLACK_USERNAME_WITH_PARAM \ + icon_url=$SLACK_ICON_URL \ + link_names=1 \ + color=$SLACK_COLOR" +fi + +# Turn off allowing errors +set -e + +cd "$CWD" From 33b36f14bfae91c9e5e6ecd81e516bd9c0cbcde8 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 08:59:54 -0500 Subject: [PATCH 02/19] [skip ci] Try migrating meza-backup-notifier into Meza --- src/scripts/autodeployer/check-for-changes.sh | 13 ++++- src/scripts/autodeployer/do-deploy.sh | 52 +++++++++---------- src/scripts/autodeployer/slack-notify.sh | 22 +++++--- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 0d0b7f4fd..1fb3c1498 100644 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -6,12 +6,21 @@ set -e echo "Start meza auto-deployer" echo $(date "+%Y-%m-%d %H:%M:%S") +# Path to this file's directory +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + # must be root or sudoer if [ "$(whoami)" != "root" ]; then echo "Try running this script with sudo: \"sudo bash unite-the-wikis.sh\"" exit 1 fi +# Check if a deploy is happening. +# +# This will cause the script to exit if a deploy is currently underway, thus +# preventing two deploys from happening at once. +source "$DIR/check-deploy.sh" + # Gets info about public config source /opt/.deploy-meza/config.sh @@ -19,9 +28,9 @@ GIT_FETCH="$m_scripts/git-fetch.sh" SLACK_NOTIFY="$m_scripts/slack-notify.sh" # Set Slack notify variables that are the same for all notifications -if [ ! -z "$autodeployer_slack_token" ]; then SLACK_TOKEN="$autodeployer_slack_token"; fi +if [ ! -z "$autodeployer_slack_token" ]; then SLACK_TOKEN="$autodeployer_slack_token"; fi if [ ! -z "$autodeployer_slack_username" ]; then SLACK_USERNAME="$autodeployer_slack_username"; fi -if [ ! -z "$autodeployer_slack_channel" ]; then SLACK_CHANNEL="$autodeployer_slack_channel"; fi +if [ ! -z "$autodeployer_slack_channel" ]; then SLACK_CHANNEL="$autodeployer_slack_channel"; fi if [ ! -z "$autodeployer_slack_icon_url" ]; then SLACK_ICON_URL="$autodeployer_slack_icon_url"; fi # FIXME: For now, don't touch secret config. At some point find a way to diff --git a/src/scripts/autodeployer/do-deploy.sh b/src/scripts/autodeployer/do-deploy.sh index 463665d81..c4cc117e6 100644 --- a/src/scripts/autodeployer/do-deploy.sh +++ b/src/scripts/autodeployer/do-deploy.sh @@ -9,40 +9,38 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -NOTIFY="$DIR/slack-notify.sh" +SLACK_NOTIFY="$DIR/slack-notify.sh" ###FIXME DESPLOY TYPE### -SLACK_MESSAGE="$DEPLOY_TYPE starting" -source $NOTIFY +source $SLACK_NOTIFY "$DEPLOY_TYPE starting" "good" if [ -z "$LOG_PREFIX" ]; then LOG_PREFIX="deploy-" fi -# Make two attempts at deploy + +# First try at deploy. Allow failures so we can capture them later +set +e meza deploy "$ENVIRONMENT" $DEPLOY_ARGS \ - > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 \ - && source $NOTIFY success \ - || ( \ - (source $NOTIFY retry && meza deploy "$ENVIRONMENT" $DEPLOY_ARGS) \ - > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 \ - && source $NOTIFY success \ - || source $NOTIFY fail - ) - - - -auto_backup_from_source: - my_env_name: - cron_time: "0 2 * * *" - yes_i_want_this_env_overwritten: True - my_other_env: - cron_time: "0 2 * * 6" - yes_i_want_this_env_overwritten: True - -autodeploy_times: - my_env_name: "0 * * * 1-5" - my_other_env: "30 * * * 1-5" - my_prod_env: "0 19 * * 1-4" + > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 + +# If deploy success, notify. Else retry once. +if [ $? -eq 0 ]; then + source $SLACK_NOTIFY "$DEPLOY_TYPE complete" "good" +else + source $SLACK_NOTIFY "$DEPLOY_TYPE attempt failed. Retrying..." "warning" + meza deploy "$ENVIRONMENT" $DEPLOY_ARGS \ + > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 + + if [ $? -eq 0 ]; then + source $SLACK_NOTIFY "$DEPLOY_TYPE complete" "good" + else + source $SLACK_NOTIFY "$DEPLOY_TYPE failed" "danger" + fi +fi + + + + diff --git a/src/scripts/autodeployer/slack-notify.sh b/src/scripts/autodeployer/slack-notify.sh index 9b0185c23..1347d91bb 100644 --- a/src/scripts/autodeployer/slack-notify.sh +++ b/src/scripts/autodeployer/slack-notify.sh @@ -29,16 +29,26 @@ if [ -z "$SLACK_TOKEN" ]; exit 1; fi +# if first param not empty, use it for SLACK_MESSAGE +if [ ! -z "$1" ]; then + SLACK_MESSAGE="$1" +elif [ -z "$SLACK_MESSAGE" ]; then + SLACK_MESSAGE="Empty message." +fi + +# if second param not empty, use it for SLACK_COLOR +if [ ! -z "$2" ]; then + SLACK_COLOR="$2" +if [ -z "$SLACK_COLOR" ]; then + SLACK_COLOR="good" # assume all is well +fi + if [ -z "$SLACK_CHANNEL" ]; then SLACK_CHANNEL_WITH_PARAM="channel=$SLACK_CHANNEL" else SLACK_CHANNEL_WITH_PARAM="" # use default for token fi -if [ -z "$SLACK_MESSAGE" ]; then - SLACK_MESSAGE="Empty message." -fi - if [ -z "$SLACK_USERNAME" ]; then SLACK_USERNAME_WITH_PARAM="username='$SLACK_USERNAME'" else @@ -49,10 +59,6 @@ if [ -z "$SLACK_ICON_URL" ]; then SLACK_ICON_URL="https://github.com/enterprisemediawiki/meza/raw/master/src/roles/configure-wiki/files/logo.png" fi -if [ -z "$SLACK_COLOR" ]; then - SLACK_COLOR="good" # assume all is well -fi - # Escape single quotes SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s/'/\\\'/g") From 21c898ae4d41f95ecccd33f7dcfe32a9149c73d6 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 10:59:48 -0500 Subject: [PATCH 03/19] [skip-ci] Fix --- src/scripts/autodeployer/do-deploy.sh | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/scripts/autodeployer/do-deploy.sh b/src/scripts/autodeployer/do-deploy.sh index c4cc117e6..4c1dd3216 100644 --- a/src/scripts/autodeployer/do-deploy.sh +++ b/src/scripts/autodeployer/do-deploy.sh @@ -11,15 +11,31 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SLACK_NOTIFY="$DIR/slack-notify.sh" +# Setting configuration for DEPLOY_TYPE, DEPLOY_ARGS, and LOG_PREFIX +# +# If any of them is being set via script argument $1, $2, or $3, respectively, +# then use that value. Otherwise check if they already have a value set earlier +# within the enviroment, and set a default if not. +if [ ! -z "$1" ]; then + DEPLOY_TYPE="$1" +elif [ -z "$DEPLOY_TYPE" ]; then + DEPLOY_TYPE="Deploy" +fi -###FIXME DESPLOY TYPE### -source $SLACK_NOTIFY "$DEPLOY_TYPE starting" "good" - +if [ ! -z "$2" ]; then + DEPLOY_ARGS="$2" +elif [ -z "$DEPLOY_ARGS" ]; then + DEPLOY_ARGS="" +fi -if [ -z "$LOG_PREFIX" ]; then +if [ ! -z "$3" ]; then + LOG_PREFIX="$3" +elif [ -z "$LOG_PREFIX" ]; then LOG_PREFIX="deploy-" fi +source $SLACK_NOTIFY "$DEPLOY_TYPE starting" "good" + # First try at deploy. Allow failures so we can capture them later set +e From e487252d8792eee8d6bf3186739a36e1628665dd Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 11:25:18 -0500 Subject: [PATCH 04/19] [skip-ci] Fix execute mode, other fixes --- src/scripts/autodeployer/check-for-changes.sh | 0 src/scripts/autodeployer/do-deploy.sh | 26 ++++++++++++++----- src/scripts/autodeployer/git-fetch.sh | 0 src/scripts/autodeployer/no-notify.sh | 5 ++++ src/scripts/autodeployer/slack-notify.sh | 2 +- 5 files changed, 25 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/scripts/autodeployer/check-for-changes.sh mode change 100644 => 100755 src/scripts/autodeployer/do-deploy.sh mode change 100644 => 100755 src/scripts/autodeployer/git-fetch.sh create mode 100755 src/scripts/autodeployer/no-notify.sh mode change 100644 => 100755 src/scripts/autodeployer/slack-notify.sh diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh old mode 100644 new mode 100755 diff --git a/src/scripts/autodeployer/do-deploy.sh b/src/scripts/autodeployer/do-deploy.sh old mode 100644 new mode 100755 index 4c1dd3216..a06815d50 --- a/src/scripts/autodeployer/do-deploy.sh +++ b/src/scripts/autodeployer/do-deploy.sh @@ -9,8 +9,6 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -SLACK_NOTIFY="$DIR/slack-notify.sh" - # Setting configuration for DEPLOY_TYPE, DEPLOY_ARGS, and LOG_PREFIX # # If any of them is being set via script argument $1, $2, or $3, respectively, @@ -34,8 +32,22 @@ elif [ -z "$LOG_PREFIX" ]; then LOG_PREFIX="deploy-" fi -source $SLACK_NOTIFY "$DEPLOY_TYPE starting" "good" +# Gets info about public config +source /opt/.deploy-meza/config.sh + +# If SLACK_TOKEN not set from outside this script, grab from config.sh +if [ -z "$SLACK_TOKEN" ]; then + SLACK_TOKEN="$autodeployer_slack_token" +fi + +# If SLACK_TOKEN is set, send notification via slack. Else, use no-notify script +if [ -z "$SLACK_TOKEN" ]; then + NOTIFY="$DIR/slack-notify.sh" +else + NOTIFY="$DIR/no-notify.sh" +fi +source $NOTIFY "$DEPLOY_TYPE starting" "good" # First try at deploy. Allow failures so we can capture them later set +e @@ -44,16 +56,16 @@ meza deploy "$ENVIRONMENT" $DEPLOY_ARGS \ # If deploy success, notify. Else retry once. if [ $? -eq 0 ]; then - source $SLACK_NOTIFY "$DEPLOY_TYPE complete" "good" + source $NOTIFY "$DEPLOY_TYPE complete" "good" else - source $SLACK_NOTIFY "$DEPLOY_TYPE attempt failed. Retrying..." "warning" + source $NOTIFY "$DEPLOY_TYPE attempt failed. Retrying..." "warning" meza deploy "$ENVIRONMENT" $DEPLOY_ARGS \ > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 if [ $? -eq 0 ]; then - source $SLACK_NOTIFY "$DEPLOY_TYPE complete" "good" + source $NOTIFY "$DEPLOY_TYPE complete" "good" else - source $SLACK_NOTIFY "$DEPLOY_TYPE failed" "danger" + source $NOTIFY "$DEPLOY_TYPE failed" "danger" fi fi diff --git a/src/scripts/autodeployer/git-fetch.sh b/src/scripts/autodeployer/git-fetch.sh old mode 100644 new mode 100755 diff --git a/src/scripts/autodeployer/no-notify.sh b/src/scripts/autodeployer/no-notify.sh new file mode 100755 index 000000000..0ffa22356 --- /dev/null +++ b/src/scripts/autodeployer/no-notify.sh @@ -0,0 +1,5 @@ +#!/bin/sh +# +# Script used to handle when no notification system is set + +echo -e "No notification method set.\nColor: $2\nMessage: $1" >> /opt/data-meza/logs/missing-notifier.log diff --git a/src/scripts/autodeployer/slack-notify.sh b/src/scripts/autodeployer/slack-notify.sh old mode 100644 new mode 100755 index 1347d91bb..19451f8ea --- a/src/scripts/autodeployer/slack-notify.sh +++ b/src/scripts/autodeployer/slack-notify.sh @@ -24,7 +24,7 @@ cd /opt/meza/config/core/adhoc -if [ -z "$SLACK_TOKEN" ]; +if [ -z "$SLACK_TOKEN" ]; then >&2 echo "You need to set a SLACK_TOKEN variable" exit 1; fi From 5351d573d622f79ebe6a467172c9576982ba7e7f Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 11:42:38 -0500 Subject: [PATCH 05/19] [skip-ci] Add env to config.sh; fixes --- src/roles/base/templates/config.php.j2 | 3 +++ src/roles/base/templates/config.sh.j2 | 3 +++ src/scripts/autodeployer/do-deploy.sh | 6 +++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/roles/base/templates/config.php.j2 b/src/roles/base/templates/config.php.j2 index 159923924..1f4014f7e 100644 --- a/src/roles/base/templates/config.php.j2 +++ b/src/roles/base/templates/config.php.j2 @@ -1,6 +1,9 @@ /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 # If deploy success, notify. Else retry once. @@ -59,7 +59,7 @@ if [ $? -eq 0 ]; then source $NOTIFY "$DEPLOY_TYPE complete" "good" else source $NOTIFY "$DEPLOY_TYPE attempt failed. Retrying..." "warning" - meza deploy "$ENVIRONMENT" $DEPLOY_ARGS \ + meza deploy "$m_environment" $DEPLOY_ARGS \ > /opt/data-meza/logs/${LOG_PREFIX}`date "+%Y%m%d%H%M%S"`.log 2>&1 if [ $? -eq 0 ]; then From b79550f7500e6f697381b78f42f747938ab94a85 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 13:59:57 -0500 Subject: [PATCH 06/19] [skip-ci] WIP --- src/roles/base/templates/config.php.j2 | 4 +- src/roles/base/templates/config.sh.j2 | 4 +- src/scripts/autodeployer/check-for-changes.sh | 39 ++++++++++++------- src/scripts/autodeployer/do-deploy.sh | 22 ++++++++++- src/scripts/autodeployer/no-notify.sh | 2 +- 5 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/roles/base/templates/config.php.j2 b/src/roles/base/templates/config.php.j2 index 1f4014f7e..ff63ec9af 100644 --- a/src/roles/base/templates/config.php.j2 +++ b/src/roles/base/templates/config.php.j2 @@ -353,7 +353,7 @@ $slack_username_disk_usage="{{ slack_username_disk_usage }}"; # # Auto-deployer # -{% if local_config_repo.repo is defined %} +{% if local_config_repo is defined and local_config_repo.repo is defined %} # Info about local public config $local_config_repo_repo="{{ local_config_repo.repo }}"; $local_config_repo_version="{{ local_config_repo.version | default('master') }}"; @@ -365,6 +365,7 @@ $local_config_repo_force="{{ local_config_repo.force | default(false) | bool }}" $enforce_meza_version="{{ enforce_meza_version }}"; {% endif %} +{% if autodeployer is defined %} {% if autodeployer.slack_channel is defined %} autodeployer_slack_channel="{{ autodeployer.slack_channel }}" {% endif %} @@ -380,5 +381,6 @@ autodeployer_slack_username="{{ autodeployer.slack_username }}" {% if autodeployer.slack_icon_url is defined %} autodeployer_slack_icon_url="{{ autodeployer.slack_icon_url }}" {% endif %} +{% endif %} diff --git a/src/roles/base/templates/config.sh.j2 b/src/roles/base/templates/config.sh.j2 index 2d3bf6a44..fa874fafe 100644 --- a/src/roles/base/templates/config.sh.j2 +++ b/src/roles/base/templates/config.sh.j2 @@ -183,7 +183,7 @@ slack_username_disk_usage="{{ slack_username_disk_usage }}" # Auto-deployer # -{% if local_config_repo.repo is defined %} +{% if local_config_repo is defined and local_config_repo.repo is defined %} # Info about local public config local_config_repo_repo="{{ local_config_repo.repo }}" local_config_repo_version="{{ local_config_repo.version | default('master') }}" @@ -195,6 +195,7 @@ local_config_repo_force="{{ local_config_repo.force | default(false) | bool }}" enforce_meza_version="{{ enforce_meza_version }}" {% endif %} +{% if autodeployer is defined %} {% if autodeployer.slack_channel is defined %} autodeployer_slack_channel="{{ autodeployer.slack_channel }}" {% endif %} @@ -210,4 +211,5 @@ autodeployer_slack_username="{{ autodeployer.slack_username }}" {% if autodeployer.slack_icon_url is defined %} autodeployer_slack_icon_url="{{ autodeployer.slack_icon_url }}" {% endif %} +{% endif %} diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 1fb3c1498..981c22954 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -1,4 +1,8 @@ #!/bin/sh +# +# Check for changes to Meza and public config repository, then deploy as needed +# +# # Don't allow errors set -e @@ -24,15 +28,7 @@ source "$DIR/check-deploy.sh" # Gets info about public config source /opt/.deploy-meza/config.sh -GIT_FETCH="$m_scripts/git-fetch.sh" -SLACK_NOTIFY="$m_scripts/slack-notify.sh" - -# Set Slack notify variables that are the same for all notifications -if [ ! -z "$autodeployer_slack_token" ]; then SLACK_TOKEN="$autodeployer_slack_token"; fi -if [ ! -z "$autodeployer_slack_username" ]; then SLACK_USERNAME="$autodeployer_slack_username"; fi -if [ ! -z "$autodeployer_slack_channel" ]; then SLACK_CHANNEL="$autodeployer_slack_channel"; fi -if [ ! -z "$autodeployer_slack_icon_url" ]; then SLACK_ICON_URL="$autodeployer_slack_icon_url"; fi - +# # FIXME: For now, don't touch secret config. At some point find a way to # configure it's repo and version. @@ -46,6 +42,21 @@ if [ -z "$enforce_meza_version" ]; then exit 1; fi +# Set Slack notify variables that are the same for all notifications +if [ ! -z "$autodeployer_slack_token" ]; then SLACK_TOKEN="$autodeployer_slack_token"; fi +if [ ! -z "$autodeployer_slack_username" ]; then SLACK_USERNAME="$autodeployer_slack_username"; fi +if [ ! -z "$autodeployer_slack_channel" ]; then SLACK_CHANNEL="$autodeployer_slack_channel"; fi +if [ ! -z "$autodeployer_slack_icon_url" ]; then SLACK_ICON_URL="$autodeployer_slack_icon_url"; fi + +# If SLACK_TOKEN is set, send notification via slack. Else, use no-notify script +if [ ! -z "$SLACK_TOKEN" ]; then + NOTIFY="$DIR/slack-notify.sh" +else + NOTIFY="$DIR/no-notify.sh" +fi + +GIT_FETCH="$DIR/git-fetch.sh" + # Set PUBLIC config version # # Could optionally set public config's repo in secret config, but since that is @@ -68,7 +79,7 @@ if [ $? -eq 0 ]; then if [ ! -z "SLACK_TOKEN" ]; then SLACK_MESSAGE="$FULL_MSG" SLACK_COLOR="danger" - source $SLACK_NOTIFY + source $NOTIFY fi exit 1; fi @@ -103,7 +114,7 @@ if [ $? -eq 0 ]; then if [ ! -z "SLACK_TOKEN" ]; then SLACK_MESSAGE="$FULL_MSG" SLACK_COLOR="danger" - source $SLACK_NOTIFY + source $NOTIFY fi exit 1; fi @@ -151,7 +162,7 @@ END if [ ! -z "SLACK_TOKEN" ]; then SLACK_MESSAGE="$MESSAGE" SLACK_COLOR="good" - source $SLACK_NOTIFY + source $NOTIFY fi fi @@ -174,7 +185,7 @@ END if [ ! -z "SLACK_TOKEN" ]; then SLACK_MESSAGE="$MESSAGE" SLACK_COLOR="good" - source $SLACK_NOTIFY + source $NOTIFY fi fi @@ -184,6 +195,6 @@ fi # echo "Deploying" DEPLOY_TYPE="Deploy" -DEPLOY_ARGS="" # autodeploy deploys everything +DEPLOY_ARGS="--tags base --skip-tags mediawiki" # autodeploy deploys everything ... but while testing keep it really light DEPLOY_LOG_PREFIX="deploy-after-config-change-" source "$DIR/do-deploy.sh" diff --git a/src/scripts/autodeployer/do-deploy.sh b/src/scripts/autodeployer/do-deploy.sh index edecc8e3a..61fc0647c 100755 --- a/src/scripts/autodeployer/do-deploy.sh +++ b/src/scripts/autodeployer/do-deploy.sh @@ -2,8 +2,26 @@ # # Do deploy. Notify on success. Notify and retry on fail. # -# # DEPLOY_TYPE="Deploy" bash /opt/meza-backup-notifier/do-deploy.sh "" "deploy-after-config-change-" - +# To use this script, either pass in DEPLOY_TYPE, DEPLOY_ARGS, AND LOG_PREFIX, +# or set them ahead of time and `source` the script: +# +# 1. ./do-deploy.sh "Backup" "--skip-tags search-index" "test-deploy-" +# +# 2. DEPLOY_TYPE="Backup" +# DEPLOY_ARGS="--skip-tags search-index" +# LOG_PREFIX="test-deploy-" +# +# Args: +# DEPLOY_TYPE: Just used in notifications for type of deploy, e.g. "Backup +# starting" or "Deploy starting" where "Backup" and "Deploy" are +# the DEPLOY_TYPE +# DEPLOY_ARGS: Any arguments that are going to get added to the deploy +# command. So if you want to do: +# `meza deploy dev --tags mediawiki --skip-tags latest` +# The DEPLOY_ARGS would be "--tags mediawiki --skip-tags latest" +# LOG_PREFIX: Logs are written to /opt/data-meza/logs to a file ending in +# the date/time and ".log". Prefix it with something like +# "nightly-backup-" to make "nightly-backup-$DATETIME.log" # Path to this file's directory DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" diff --git a/src/scripts/autodeployer/no-notify.sh b/src/scripts/autodeployer/no-notify.sh index 0ffa22356..f73d123bc 100755 --- a/src/scripts/autodeployer/no-notify.sh +++ b/src/scripts/autodeployer/no-notify.sh @@ -2,4 +2,4 @@ # # Script used to handle when no notification system is set -echo -e "No notification method set.\nColor: $2\nMessage: $1" >> /opt/data-meza/logs/missing-notifier.log +echo -e "$(date "+\%Y-\%m-\%d"): No notification method set.\n Color: $2\n Message: $1" >> /opt/data-meza/logs/missing-notifier.log From 546d157d64c0bd9c757064b8ae05fb7fbdc7ef79 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 14:17:40 -0500 Subject: [PATCH 07/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 981c22954..2b1f483d3 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -2,6 +2,13 @@ # # Check for changes to Meza and public config repository, then deploy as needed # +# Run this command without args: +# +# sudo ./check-for-changes.sh +# +# Or in a passwordless sudoer's (e.g. root) crontab like: +# +# 22 * * * * /opt/meza/src/scripts/autodeployer/check-for-changes.sh >> /opt/data-meza/logs/autodeploy-`date "+\%Y-\%m-\%d"`.log 2>&1 # # Don't allow errors @@ -23,7 +30,9 @@ fi # # This will cause the script to exit if a deploy is currently underway, thus # preventing two deploys from happening at once. -source "$DIR/check-deploy.sh" +# +# FIXME: not implemented yet +# source "$DIR/check-deploy.sh" # Gets info about public config source /opt/.deploy-meza/config.sh @@ -33,12 +42,12 @@ source /opt/.deploy-meza/config.sh # configure it's repo and version. if [ -z "$local_config_repo_repo" ]; then - >&2 echo "Auto-deploy requires 'local_config_repo' var set in secret config" + >&2 echo "Auto-deploy requires 'local_config_repo' set in secret or public config" exit 1; fi if [ -z "$enforce_meza_version" ]; then - >&2 echo "Auto-deploy requires 'enforce_meza_version' var set in public config" + >&2 echo "Auto-deploy requires 'enforce_meza_version' var set in public or secret config" exit 1; fi From 2021f86e095a3c5065bd8ad3e05a83f9b5bc3151 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 15:40:53 -0500 Subject: [PATCH 08/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 2b1f483d3..e45c1e221 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -79,6 +79,10 @@ PUBLIC_CONFIG_CHANGE=$($GIT_FETCH "$PUBLIC_CONFIG_REPO" "$PUBLIC_CONFIG_DEST" "$ # # Check if attempt to git-pull PUBLIC CONFIG failed # +# FIXME: For some reason the jq command below was not working if it was within +# the conditional, so it has to be out here, where it forces us to +# temporarily allow errors. +set +e echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.failed' -e if [ $? -eq 0 ]; then FAILED_MSG=$(echo "$PUBLIC_CONFIG_CHANGE" | jq .plays[0].tasks[0].hosts.localhost.msg -r) @@ -138,6 +142,7 @@ if [ $? -eq 0 ]; then else MEZA_AFTER_HASH="" fi +set -e # end FIXME from above. # From 1673fbab09f7d36a9f5c261cb4c85b8ee789f4cd Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 15:53:38 -0500 Subject: [PATCH 09/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index e45c1e221..580608062 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -83,6 +83,7 @@ PUBLIC_CONFIG_CHANGE=$($GIT_FETCH "$PUBLIC_CONFIG_REPO" "$PUBLIC_CONFIG_DEST" "$ # the conditional, so it has to be out here, where it forces us to # temporarily allow errors. set +e +echo "Did git fetch fail on public config?" echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.failed' -e if [ $? -eq 0 ]; then FAILED_MSG=$(echo "$PUBLIC_CONFIG_CHANGE" | jq .plays[0].tasks[0].hosts.localhost.msg -r) @@ -100,11 +101,14 @@ fi # # Check if changes were made to PUBLIC CONFIG # +echo "Were there changes to public config?" echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.changed' -e if [ $? -eq 0 ]; then PUBLIC_CONFIG_BEFORE_HASH=$(echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.before' -r) PUBLIC_CONFIG_AFTER_HASH=$(echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.after' -r) - PUBLIC_CONFIG_DIFF=$(cd "$DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH..$PUBLIC_CONFIG_AFTER_HASH") + echo "Before hash: $PUBLIC_CONFIG_BEFORE_HASH" + echo "After hash: $PUBLIC_CONFIG_BEFORE_HASH" + PUBLIC_CONFIG_DIFF=$(cd "$DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) else PUBLIC_CONFIG_DIFF="" fi @@ -118,6 +122,7 @@ MEZA_CHANGE=$($GIT_FETCH "$MEZA_REPO" "$MEZA_DEST" "$MEZA_VERSION") # # Check if attempt to git-pull MEZA failed # +echo "Did git fetch fail on Meza?" echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.failed' -e if [ $? -eq 0 ]; then FAILED_MSG=$(echo "$MEZA_CHANGE" | jq .plays[0].tasks[0].hosts.localhost.msg -r) @@ -135,10 +140,13 @@ fi # # Check if changes were made to MEZA # +echo "Were there changes to Meza?" echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.changed' -e if [ $? -eq 0 ]; then MEZA_BEFORE_HASH=$(echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.before' -r) MEZA_AFTER_HASH=$(echo "$MEZA_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.after' -r) + echo "Before hash: $MEZA_BEFORE_HASH" + echo "After hash: $MEZA_AFTER_HASH" else MEZA_AFTER_HASH="" fi From 424227a9f242b56df4c8f3b706a5a24097a49eb7 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 16:00:30 -0500 Subject: [PATCH 10/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 580608062..be38ce9f8 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -108,7 +108,7 @@ if [ $? -eq 0 ]; then PUBLIC_CONFIG_AFTER_HASH=$(echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.after' -r) echo "Before hash: $PUBLIC_CONFIG_BEFORE_HASH" echo "After hash: $PUBLIC_CONFIG_BEFORE_HASH" - PUBLIC_CONFIG_DIFF=$(cd "$DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) + PUBLIC_CONFIG_DIFF=$(cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) else PUBLIC_CONFIG_DIFF="" fi @@ -220,3 +220,4 @@ DEPLOY_TYPE="Deploy" DEPLOY_ARGS="--tags base --skip-tags mediawiki" # autodeploy deploys everything ... but while testing keep it really light DEPLOY_LOG_PREFIX="deploy-after-config-change-" source "$DIR/do-deploy.sh" +echo "Done" From 7de4e33680a9b5a3d12e629a57f6ca8afdc67a33 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 17:43:57 -0500 Subject: [PATCH 11/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 3 ++- src/scripts/autodeployer/slack-notify.sh | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index be38ce9f8..0572e88a9 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -174,8 +174,9 @@ if [ ! -z "$PUBLIC_CONFIG_DIFF" ]; then Tracking version: $PUBLIC_CONFIG_VERSION Diff: - + ``` $PUBLIC_CONFIG_DIFF + ``` END ) diff --git a/src/scripts/autodeployer/slack-notify.sh b/src/scripts/autodeployer/slack-notify.sh index 19451f8ea..9c7532e5c 100755 --- a/src/scripts/autodeployer/slack-notify.sh +++ b/src/scripts/autodeployer/slack-notify.sh @@ -39,7 +39,7 @@ fi # if second param not empty, use it for SLACK_COLOR if [ ! -z "$2" ]; then SLACK_COLOR="$2" -if [ -z "$SLACK_COLOR" ]; then +elif [ -z "$SLACK_COLOR" ]; then SLACK_COLOR="good" # assume all is well fi From ebe262a19abec719e97393066390fec073e793a9 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 17:53:13 -0500 Subject: [PATCH 12/19] [skip-ci] WIP --- src/scripts/autodeployer/slack-notify.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/scripts/autodeployer/slack-notify.sh b/src/scripts/autodeployer/slack-notify.sh index 9c7532e5c..1dc96e1ee 100755 --- a/src/scripts/autodeployer/slack-notify.sh +++ b/src/scripts/autodeployer/slack-notify.sh @@ -66,6 +66,15 @@ SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s/'/\\\'/g") # Turn on allowing failures set +e +# for debug +echo "token=$SLACK_TOKEN \ + $SLACK_CHANNEL_WITH_PARAM \ + msg='$SLACK_MESSAGE' \ + $SLACK_USERNAME_WITH_PARAM \ + icon_url=$SLACK_ICON_URL \ + link_names=1 \ + color=$SLACK_COLOR" + # Attempt to send message ansible localhost -m slack -a \ "token=$SLACK_TOKEN \ From 9dfff0167e32be13c6d0605c222c85faf77717d7 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 18:13:09 -0500 Subject: [PATCH 13/19] [skip-ci] WIP --- src/scripts/autodeployer/slack-notify.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/scripts/autodeployer/slack-notify.sh b/src/scripts/autodeployer/slack-notify.sh index 1dc96e1ee..483630b44 100755 --- a/src/scripts/autodeployer/slack-notify.sh +++ b/src/scripts/autodeployer/slack-notify.sh @@ -44,7 +44,7 @@ elif [ -z "$SLACK_COLOR" ]; then fi if [ -z "$SLACK_CHANNEL" ]; then - SLACK_CHANNEL_WITH_PARAM="channel=$SLACK_CHANNEL" + SLACK_CHANNEL_WITH_PARAM="channel='$SLACK_CHANNEL'" else SLACK_CHANNEL_WITH_PARAM="" # use default for token fi @@ -67,7 +67,9 @@ SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s/'/\\\'/g") set +e # for debug -echo "token=$SLACK_TOKEN \ +echo "DEBUG OUTPUT OF SLACK NOTIFY COMMAND" +echo \ + "token='$SLACK_TOKEN' \ $SLACK_CHANNEL_WITH_PARAM \ msg='$SLACK_MESSAGE' \ $SLACK_USERNAME_WITH_PARAM \ @@ -77,7 +79,7 @@ echo "token=$SLACK_TOKEN \ # Attempt to send message ansible localhost -m slack -a \ - "token=$SLACK_TOKEN \ + "token='$SLACK_TOKEN' \ $SLACK_CHANNEL_WITH_PARAM \ msg='$SLACK_MESSAGE' \ $SLACK_USERNAME_WITH_PARAM \ @@ -93,7 +95,7 @@ else echo "$SLACK_MESSAGE" SLACK_MESSAGE="Slack message failed. See logs for attempted message." ansible localhost -m slack -a \ - "token=$SLACK_TOKEN \ + "token='$SLACK_TOKEN' \ $SLACK_CHANNEL_WITH_PARAM \ msg='$SLACK_MESSAGE' \ $SLACK_USERNAME_WITH_PARAM \ From 7b212cfe2c19baf7916f68d587fd9209803f3ad0 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 18:33:59 -0500 Subject: [PATCH 14/19] [skip-ci] WIP --- src/scripts/autodeployer/slack-notify.sh | 42 ++++++++++++------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/scripts/autodeployer/slack-notify.sh b/src/scripts/autodeployer/slack-notify.sh index 483630b44..fa4e14fad 100755 --- a/src/scripts/autodeployer/slack-notify.sh +++ b/src/scripts/autodeployer/slack-notify.sh @@ -69,23 +69,23 @@ set +e # for debug echo "DEBUG OUTPUT OF SLACK NOTIFY COMMAND" echo \ - "token='$SLACK_TOKEN' \ - $SLACK_CHANNEL_WITH_PARAM \ - msg='$SLACK_MESSAGE' \ - $SLACK_USERNAME_WITH_PARAM \ - icon_url=$SLACK_ICON_URL \ - link_names=1 \ - color=$SLACK_COLOR" +"token='$SLACK_TOKEN' \ +$SLACK_CHANNEL_WITH_PARAM \ +msg='$SLACK_MESSAGE' \ +$SLACK_USERNAME_WITH_PARAM \ +icon_url=$SLACK_ICON_URL \ +link_names=1 \ +color=$SLACK_COLOR" # Attempt to send message ansible localhost -m slack -a \ - "token='$SLACK_TOKEN' \ - $SLACK_CHANNEL_WITH_PARAM \ - msg='$SLACK_MESSAGE' \ - $SLACK_USERNAME_WITH_PARAM \ - icon_url=$SLACK_ICON_URL \ - link_names=1 \ - color=$SLACK_COLOR" +"token='$SLACK_TOKEN' \ +$SLACK_CHANNEL_WITH_PARAM \ +msg='$SLACK_MESSAGE' \ +$SLACK_USERNAME_WITH_PARAM \ +icon_url=$SLACK_ICON_URL \ +link_names=1 \ +color=$SLACK_COLOR" # If message fails, send a generic message if [ $? -eq 0 ]; then @@ -95,13 +95,13 @@ else echo "$SLACK_MESSAGE" SLACK_MESSAGE="Slack message failed. See logs for attempted message." ansible localhost -m slack -a \ - "token='$SLACK_TOKEN' \ - $SLACK_CHANNEL_WITH_PARAM \ - msg='$SLACK_MESSAGE' \ - $SLACK_USERNAME_WITH_PARAM \ - icon_url=$SLACK_ICON_URL \ - link_names=1 \ - color=$SLACK_COLOR" +"token='$SLACK_TOKEN' \ +$SLACK_CHANNEL_WITH_PARAM \ +msg='$SLACK_MESSAGE' \ +$SLACK_USERNAME_WITH_PARAM \ +icon_url=$SLACK_ICON_URL \ +link_names=1 \ +color=$SLACK_COLOR" fi # Turn off allowing errors From 165c53c7300fee066a88b147310bac0ee6f9f8e5 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 18:45:16 -0500 Subject: [PATCH 15/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 0572e88a9..4394e7607 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -108,9 +108,21 @@ if [ $? -eq 0 ]; then PUBLIC_CONFIG_AFTER_HASH=$(echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.after' -r) echo "Before hash: $PUBLIC_CONFIG_BEFORE_HASH" echo "After hash: $PUBLIC_CONFIG_BEFORE_HASH" + echo + echo + echo "WHAT IS GOING ON" + echo "================" + echo cd "\"$PUBLIC_CONFIG_DEST\" && git diff \"$PUBLIC_CONFIG_BEFORE_HASH\" \"$PUBLIC_CONFIG_AFTER_HASH\" 2>&1" + echo + cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1 + echo PUBLIC_CONFIG_DIFF=$(cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) + echo + echo "===" + echo "END" else PUBLIC_CONFIG_DIFF="" + PUBLIC_CONFIG_AFTER_HASH="" fi # Set MEZA version @@ -156,7 +168,7 @@ set -e # end FIXME from above. # # Neither Meza mor config changed? Exit. # -if [ -z "$PUBLIC_CONFIG_DIFF$MEZA_AFTER_HASH" ]; then +if [ -z "$PUBLIC_CONFIG_AFTER_HASH$MEZA_AFTER_HASH" ]; then echo "Nothing to deploy" exit 0; fi @@ -164,7 +176,7 @@ fi # # Notify if PUBLIC CONFIG changed # -if [ ! -z "$PUBLIC_CONFIG_DIFF" ]; then +if [ ! -z "$PUBLIC_CONFIG_AFTER_HASH" ]; then MESSAGE=$(cat <<-END Public config changed versions: From c35ed94b42447fe92c78a1031273880ad51bacfc Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 18:48:33 -0500 Subject: [PATCH 16/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 4394e7607..08ab9122e 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -117,6 +117,12 @@ if [ $? -eq 0 ]; then cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1 echo PUBLIC_CONFIG_DIFF=$(cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) + + echo "first:" + echo $PUBLIC_CONFIG_DIFF + echo + echo "second:" + echo "$PUBLIC_CONFIG_DIFF" echo echo "===" echo "END" From 20a4d28d45fe25da4fefc382793f561a962a8d8f Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 18:58:07 -0500 Subject: [PATCH 17/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 08ab9122e..9d7c8f90b 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -116,7 +116,10 @@ if [ $? -eq 0 ]; then echo cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1 echo - PUBLIC_CONFIG_DIFF=$(cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) + + pushd "$PUBLIC_CONFIG_DEST" + PUBLIC_CONFIG_DIFF=$(git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) + pushd echo "first:" echo $PUBLIC_CONFIG_DIFF From ac5e601efc9d75fde28ad226cb8e6364bc3549c4 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 18:59:33 -0500 Subject: [PATCH 18/19] [skip-ci] WIP --- src/scripts/autodeployer/check-for-changes.sh | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index 9d7c8f90b..dafce7fb7 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -108,27 +108,10 @@ if [ $? -eq 0 ]; then PUBLIC_CONFIG_AFTER_HASH=$(echo "$PUBLIC_CONFIG_CHANGE" | jq '.plays[0].tasks[0].hosts.localhost.after' -r) echo "Before hash: $PUBLIC_CONFIG_BEFORE_HASH" echo "After hash: $PUBLIC_CONFIG_BEFORE_HASH" - echo - echo - echo "WHAT IS GOING ON" - echo "================" - echo cd "\"$PUBLIC_CONFIG_DEST\" && git diff \"$PUBLIC_CONFIG_BEFORE_HASH\" \"$PUBLIC_CONFIG_AFTER_HASH\" 2>&1" - echo - cd "$PUBLIC_CONFIG_DEST" && git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1 - echo pushd "$PUBLIC_CONFIG_DEST" PUBLIC_CONFIG_DIFF=$(git diff "$PUBLIC_CONFIG_BEFORE_HASH" "$PUBLIC_CONFIG_AFTER_HASH" 2>&1) pushd - - echo "first:" - echo $PUBLIC_CONFIG_DIFF - echo - echo "second:" - echo "$PUBLIC_CONFIG_DIFF" - echo - echo "===" - echo "END" else PUBLIC_CONFIG_DIFF="" PUBLIC_CONFIG_AFTER_HASH="" @@ -195,9 +178,9 @@ if [ ! -z "$PUBLIC_CONFIG_AFTER_HASH" ]; then Tracking version: $PUBLIC_CONFIG_VERSION Diff: - ``` + \`\`\` $PUBLIC_CONFIG_DIFF - ``` + \`\`\` END ) From accdfb4e6ab87f05e191866cc31e6f3d3e25d2c1 Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Fri, 28 Sep 2018 19:03:27 -0500 Subject: [PATCH 19/19] Maybe final autodeployer commit, maybe --- src/scripts/autodeployer/check-for-changes.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/scripts/autodeployer/check-for-changes.sh b/src/scripts/autodeployer/check-for-changes.sh index dafce7fb7..9601202cb 100755 --- a/src/scripts/autodeployer/check-for-changes.sh +++ b/src/scripts/autodeployer/check-for-changes.sh @@ -172,10 +172,10 @@ if [ ! -z "$PUBLIC_CONFIG_AFTER_HASH" ]; then MESSAGE=$(cat <<-END Public config changed versions: - FROM: $PUBLIC_CONFIG_BEFORE_HASH - TO: $PUBLIC_CONFIG_AFTER_HASH + FROM: \`$PUBLIC_CONFIG_BEFORE_HASH\` + TO: \`$PUBLIC_CONFIG_AFTER_HASH\` - Tracking version: $PUBLIC_CONFIG_VERSION + Tracking version: \`$PUBLIC_CONFIG_VERSION\` Diff: \`\`\` @@ -200,10 +200,10 @@ if [ ! -z "$MEZA_AFTER_HASH" ]; then MESSAGE=$(cat <<-END Meza application changed versions: - FROM: $MEZA_BEFORE_HASH - TO: $MEZA_AFTER_HASH + FROM: \`$MEZA_BEFORE_HASH\` + TO: \`$MEZA_AFTER_HASH\` - Tracking version: $MEZA_VERSION + Tracking version: \`$MEZA_VERSION\` END )