|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# this script is meant to be sourced |
| 4 | + |
| 5 | +SCRIPT_PATH=$(dirname "${BASH_SOURCE[0]}") |
| 6 | + |
| 7 | +# shellcheck source=./common.sh |
| 8 | +source "${SCRIPT_PATH}/common.sh" |
| 9 | + |
| 10 | +if [ -n "${DESTROY-}" ]; then |
| 11 | + export TF_CLI_ARGS_plan="-destroy" |
| 12 | +fi |
| 13 | + |
| 14 | +function check_workspace() { |
| 15 | + local workspace=$1 |
| 16 | + if [[ $(terraform workspace show) != "${workspace}" ]]; then |
| 17 | + log_error "Expected to be in [${workspace}]. We are in [$(terraform workspace show)]" |
| 18 | + return "${ERROR_WRONG_WORKSPACE}" |
| 19 | + fi |
| 20 | +} |
| 21 | + |
| 22 | +function set_workspace() { |
| 23 | + local workspace=$1 |
| 24 | + if terraform workspace list | grep -q "${workspace}"; then |
| 25 | + terraform workspace select "${workspace}" |
| 26 | + else |
| 27 | + terraform workspace new "${workspace}" |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +function delete_workspace() { |
| 32 | + local workspace=$1 |
| 33 | + if [[ $(terraform workspace show) == "${workspace}" ]]; then |
| 34 | + terraform workspace select default |
| 35 | + fi |
| 36 | + |
| 37 | + exists=0 |
| 38 | + terraform workspace list | grep -q "${workspace}" || exists=$? |
| 39 | + if [[ "${exists}" == 0 ]]; then |
| 40 | + terraform workspace delete "${workspace}" |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +function terraform_init() { |
| 45 | + local target_dir=${1:-$TARGET_DIR} |
| 46 | + if [ -z "${target_dir-}" ]; then |
| 47 | + log_error "Must provide TARGET_DIR for init" |
| 48 | + return "${ERROR_NO_DIR}" |
| 49 | + fi |
| 50 | + pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}" |
| 51 | + |
| 52 | + terraform init |
| 53 | + if [ -n "${WORKSPACE-}" ]; then |
| 54 | + set_workspace "${WORKSPACE}" |
| 55 | + fi |
| 56 | + |
| 57 | + popd || return "${ERROR_CHANGE_DIR}" |
| 58 | +} |
| 59 | + |
| 60 | +function terraform_plan() { |
| 61 | + local target_dir=${1:-$TARGET_DIR} |
| 62 | + if [ -z "${target_dir-}" ]; then |
| 63 | + log_error "Must provide TARGET_DIR for plan" |
| 64 | + return "${ERROR_NO_DIR}" |
| 65 | + fi |
| 66 | + |
| 67 | + local static_plan |
| 68 | + static_plan="$(realpath "${TARGET_DIR}")/$(basename "${TARGET_DIR}").plan" |
| 69 | + local plan_location=${PLAN_LOCATION:-$static_plan} |
| 70 | + |
| 71 | + pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}" |
| 72 | + |
| 73 | + # check if we should be in a workspace, and bail otherwise |
| 74 | + if [ -n "${WORKSPACE-}" ]; then |
| 75 | + check_workspace "${WORKSPACE}" |
| 76 | + fi |
| 77 | + |
| 78 | + # -detailed-exitcode will be 0=success no changes/1=failure/2=success changes |
| 79 | + # therefore we capture the output so our function doesn't cause a script to terminate if the caller has `set -e` |
| 80 | + EXIT_CODE=0 |
| 81 | + terraform plan -detailed-exitcode -out="${plan_location}" || EXIT_CODE=$? |
| 82 | + |
| 83 | + if [[ ${EXIT_CODE} = 2 ]]; then |
| 84 | + terraform show "${plan_location}" |
| 85 | + fi |
| 86 | + |
| 87 | + popd || exit "${ERROR_CHANGE_DIR}" |
| 88 | + |
| 89 | + return "${EXIT_CODE}" |
| 90 | +} |
| 91 | + |
| 92 | +function terraform_apply() { |
| 93 | + local target_dir=${1:-$TARGET_DIR} |
| 94 | + if [ -z "${target_dir-}" ]; then |
| 95 | + log_error "Must provide TARGET_DIR for apply" |
| 96 | + return "${ERROR_NO_DIR}" |
| 97 | + fi |
| 98 | + |
| 99 | + local static_plan |
| 100 | + static_plan="$(realpath "${TARGET_DIR}")/$(basename "${TARGET_DIR}").plan" |
| 101 | + local plan_location=${PLAN_LOCATION:-$static_plan} |
| 102 | + |
| 103 | + pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}" |
| 104 | + |
| 105 | + # check if we should be in a workspace, and bail otherwise |
| 106 | + if [ -n "${WORKSPACE-}" ]; then |
| 107 | + check_workspace "${WORKSPACE}" |
| 108 | + fi |
| 109 | + |
| 110 | + if [ -z "${plan_location-}" ]; then |
| 111 | + log_error "Must provide PLAN_LOCATION for apply" |
| 112 | + return "${ERROR_NO_PLAN}" |
| 113 | + fi |
| 114 | + |
| 115 | + # check if we should be in a workspace, and bail otherwise |
| 116 | + if [ -n "${WORKSPACE-}" ]; then |
| 117 | + check_workspace "${WORKSPACE}" |
| 118 | + fi |
| 119 | + |
| 120 | + terraform apply "${plan_location}" |
| 121 | + |
| 122 | + popd || return "${ERROR_CHANGE_DIR}" |
| 123 | +} |
0 commit comments