workflow-script-commons.sh 4.4 KB
Newer Older
Andrew Newdigate's avatar
Andrew Newdigate committed
1 2
#!/usr/bin/env bash

3 4 5 6 7
# Used for sanity checking the scripts
if [[ -n ${SANITY_CHECK_ONLY:=} ]]; then
  exit 0
fi

Andrew Newdigate's avatar
Andrew Newdigate committed
8 9 10 11 12 13 14 15 16 17 18 19 20
# Everything is logged!
if [[ -z ${LOGGING_CONFIGURED:=} ]]; then
  export LOGGING_CONFIGURED=1
  # TODO(andrewn): once we have a destination, we can also tap these logs to another place
  $0 "$@"  2>&1 | ruby -pe 'print Time.now.strftime("%Y-%m-%d %H:%M:%S.%L: ")'
  exit
fi

function die() {
  >&2 echo "Fatal:" "$@"
  exit 1
}

John Jarvis's avatar
John Jarvis committed
21 22 23 24 25
function ssh_host() {
  # shellcheck disable=SC2068
  ssh -o StrictHostKeyChecking=no $@ 2>/dev/null
}

Andrew Newdigate's avatar
Andrew Newdigate committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
function PRODUCTION_ONLY() {
  if [[ $FAILOVER_ENVIRONMENT != "prd" ]]; then
    die "This step is production only"
  fi
}

# Basic support for OSX, helpful for testing
function gnu_date() {
  if command -v gdate >/dev/null; then
    gdate "$@"
  else
    date "$@"
  fi
}

# Basic support for OSX, helpful for testing
function gnu_readlink() {
  if command -v greadlink >/dev/null; then
    greadlink "$@"
  else
    readlink "$@"
  fi
}

function ensure_valid() {
Andrew Newdigate's avatar
Andrew Newdigate committed
51
  source_vars_file=$(gnu_readlink -f "${SOURCE_VARS_DIR}/source_vars")
Andrew Newdigate's avatar
Andrew Newdigate committed
52
  grep -Eho '(\w+)="__REQUIRED__"' "${SOURCE_VARS_DIR}/source_vars_template.sh" |cut -d= -f1 | while read -r i; do
Andrew Newdigate's avatar
Andrew Newdigate committed
53
    if [[ ${!i:=__REQUIRED__} = "__REQUIRED__" ]]; then
Andrew Newdigate's avatar
Andrew Newdigate committed
54
      die "Variable ${i} has not been configured. You may need to update ${source_vars_file}"
Andrew Newdigate's avatar
Andrew Newdigate committed
55 56 57
    fi
  done

Andrew Newdigate's avatar
Andrew Newdigate committed
58 59 60 61 62 63
  case ${FAILOVER_ENVIRONMENT} in
    "prd") ;;
    "stg") ;;
    *) die "Unknown environment. Must be 'prd' or 'std': update ${source_vars_file}";;
  esac

Andrew Newdigate's avatar
Andrew Newdigate committed
64 65 66
  FAILOVER_DATE=$(gnu_date --date="$FAILOVER_DATE" "+%Y-%m-%d")
  TODAY=$(gnu_date "+%Y-%m-%d")
  if [[ "${FAILOVER_DATE}" < "${TODAY}" ]]; then
Andrew Newdigate's avatar
Andrew Newdigate committed
67
    die "Failover date is in the past ${FAILOVER_DATE}. Have you updated ${source_vars_file}?"
Andrew Newdigate's avatar
Andrew Newdigate committed
68 69 70
  fi

  case $(hostname -f) in
Andrew Newdigate's avatar
Andrew Newdigate committed
71
    "deploy-01-sv-gprd.c.gitlab-production.internal")
Andrew Newdigate's avatar
Andrew Newdigate committed
72
      if [[ ${FAILOVER_ENVIRONMENT} != "prd" ]]; then
Andrew Newdigate's avatar
Andrew Newdigate committed
73
        die "FAILOVER_ENVIRONMENT is ${FAILOVER_ENVIRONMENT}, but environment is detected as production. Have you updated ${source_vars_file}?"
Andrew Newdigate's avatar
Andrew Newdigate committed
74 75
      fi
      ;;
Andrew Newdigate's avatar
Andrew Newdigate committed
76
    "deploy-01-sv-gstg.c.gitlab-staging-1.internal")
Andrew Newdigate's avatar
Andrew Newdigate committed
77
      if [[ ${FAILOVER_ENVIRONMENT} != "stg" ]]; then
Andrew Newdigate's avatar
Andrew Newdigate committed
78
        die "FAILOVER_ENVIRONMENT is ${FAILOVER_ENVIRONMENT}, but environment is detected as staging. Have you updated ${source_vars_file}?"
Andrew Newdigate's avatar
Andrew Newdigate committed
79 80 81 82
      fi
      ;;
    *)
      if [[ ${SKIP_HOST_CHECK:=} != "true" ]]; then
Andrew Newdigate's avatar
Andrew Newdigate committed
83 84 85 86 87
        case ${FAILOVER_ENVIRONMENT} in
          "prd") die "Unrecognised $(hostname -f): please run this from the GCP deploy host: deploy-01-sv-gprd.c.gitlab-production.internal. Set SKIP_HOST_CHECK=true if you know what you're doing." ;;
          "stg") die "Unrecognised $(hostname -f): please run this from the GCP deploy host: deploy-01-sv-gstg.c.gitlab-staging-1.internal. Set SKIP_HOST_CHECK=true if you know what you're doing." ;;
          *) die "Unrecognised $(hostname -f). Please review ${source_vars_file}" ;;
        esac
Andrew Newdigate's avatar
Andrew Newdigate committed
88 89 90
      fi
      ;;
  esac
91 92 93 94

  if [[ -z "${SSH_AUTH_SOCK:=}" ]]; then
    >&2 echo "Warning: you appear to not have SSH agent forwarding setup. You may need to add \`ForwardAgent yes\` to your ~/.ssh/config"
  fi
Andrew Newdigate's avatar
Andrew Newdigate committed
95 96
}

Andrew Newdigate's avatar
Andrew Newdigate committed
97 98 99 100
function log_command() {
  (set -x; "$@")
}

101 102 103 104 105 106
function remote_gitlab_runner() {
  host=$1
  script=$2
  temp="$(ssh "${host}" mktemp).rb"
  scp "$script" "${host}:${temp}"
  log_command ssh "${host}" bash -c "set -x; sudo gitlab-rails runner \"\$(cat ${temp})\" && rm ${temp}"
107 108
}

Andrew Newdigate's avatar
Andrew Newdigate committed
109 110 111 112 113 114 115
function header() {
  local full_path
  full_path=$(gnu_readlink -f "${BASH_SOURCE[2]}")
  cat <<EOD
========================================================
Script: $full_path
User: ${SUDO_USER:=$USER}
116
Rev: $(git -C "$(dirname "${BASH_SOURCE[0]}" )/.." rev-parse --short HEAD)
Andrew Newdigate's avatar
Andrew Newdigate committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
========================================================

EOD
}

function footer() {
  cat <<EOD

--------------------------------------------------------
Exit Status: $?
--------------------------------------------------------
EOD
}

header
trap "footer" EXIT

SOURCE_VARS_DIR=$(dirname "${BASH_SOURCE[0]}")

if ! [[ -f "${SOURCE_VARS_DIR}/source_vars" ]]; then
  target=$(gnu_readlink -f "${SOURCE_VARS_DIR}/source_vars")
  source=$(gnu_readlink -f "${SOURCE_VARS_DIR}/source_vars_template.sh")

  die "${target} not found. Please initialise by with 'cp ${source} ${target}'"
fi

# Load the defaults
# shellcheck disable=SC1091,SC1090
source "${SOURCE_VARS_DIR}/source_vars_template.sh"

# Load the specific values
# shellcheck disable=SC1091,SC1090
source "${SOURCE_VARS_DIR}/source_vars"

ensure_valid