Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/preview/workflow/lib/terraform.sh
2500 views
1
#!/bin/bash
2
3
# this script is meant to be sourced
4
5
FILE_PATH=$(dirname "${BASH_SOURCE[0]}")
6
7
# shellcheck source=./common.sh
8
source "${FILE_PATH}/common.sh"
9
10
TF_CLI_ARGS_plan=${TF_CLI_ARGS_plan:-""}
11
TF_CLI_ARGS_apply=${TF_CLI_ARGS_apply:-""}
12
13
export TF_CLI_ARGS_plan="${TF_CLI_ARGS_plan} -lock-timeout=5m"
14
export TF_CLI_ARGS_apply="${TF_CLI_ARGS_apply} -lock-timeout=5m"
15
16
if [ -n "${DESTROY-}" ]; then
17
export TF_CLI_ARGS_plan="${TF_CLI_ARGS_plan} -destroy"
18
fi
19
20
function delete_workspace() {
21
local workspace=$1
22
local exists=0
23
terraform workspace list | grep -q "${workspace}" || exists=$?
24
if [[ "${exists}" == 0 ]]; then
25
TF_WORKSPACE="default" terraform workspace delete "${workspace}"
26
fi
27
}
28
29
function create_workspace() {
30
local workspace=$1
31
if terraform workspace list | grep -q "${workspace}"; then
32
return 0
33
else
34
terraform workspace new "${workspace}"
35
fi
36
}
37
38
function terraform_init() {
39
local target_dir=${1:-$TARGET_DIR}
40
if [ -z "${target_dir-}" ]; then
41
log_error "Must provide TARGET_DIR for init"
42
return "${ERROR_NO_DIR}"
43
fi
44
pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}"
45
46
TF_WORKSPACE="default" terraform init
47
48
if [ -n "${TF_WORKSPACE}" ]; then
49
create_workspace "${TF_WORKSPACE}"
50
fi
51
52
popd || return "${ERROR_CHANGE_DIR}"
53
}
54
55
function terraform_plan() {
56
local target_dir=${1:-$TARGET_DIR}
57
if [ -z "${target_dir-}" ]; then
58
log_error "Must provide TARGET_DIR for plan"
59
return "${ERROR_NO_DIR}"
60
fi
61
62
local static_plan
63
static_plan="$(realpath "${TARGET_DIR}")/$(basename "${TARGET_DIR}").plan"
64
local plan_location=${PLAN_LOCATION:-$static_plan}
65
66
pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}"
67
68
# -detailed-exitcode will be 0=success no changes/1=failure/2=success changes
69
# therefore we capture the output so our function doesn't cause a script to terminate if the caller has `set -e`
70
EXIT_CODE=0
71
terraform plan -detailed-exitcode -out="${plan_location}" || EXIT_CODE=$?
72
73
popd || exit "${ERROR_CHANGE_DIR}"
74
75
return "${EXIT_CODE}"
76
}
77
78
function terraform_apply() {
79
local target_dir=${1:-$TARGET_DIR}
80
if [ -z "${target_dir-}" ]; then
81
log_error "Must provide TARGET_DIR for apply"
82
return "${ERROR_NO_DIR}"
83
fi
84
85
local static_plan
86
static_plan="$(realpath "${TARGET_DIR}")/$(basename "${TARGET_DIR}").plan"
87
local plan_location=${PLAN_LOCATION:-$static_plan}
88
89
pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}"
90
91
if [ -z "${plan_location-}" ]; then
92
log_error "Must provide PLAN_LOCATION for apply"
93
return "${ERROR_NO_PLAN}"
94
fi
95
96
timeout --signal=INT --foreground 10m terraform apply "${plan_location}"
97
98
popd || return "${ERROR_CHANGE_DIR}"
99
}
100
101
function terraform_output() {
102
local var=${1:-}
103
local format=${2:-raw}
104
105
if [ -z "${TARGET_DIR-}" ]; then
106
log_error "Must provide TARGET_DIR"
107
return "${ERROR_NO_DIR}"
108
fi
109
110
pushd "${TARGET_DIR}" >/dev/null || return "${ERROR_CHANGE_DIR}"
111
112
terraform output -${format} "${var}" 2>/dev/null
113
114
popd >/dev/null || return "${ERROR_CHANGE_DIR}"
115
}
116
117