-
Notifications
You must be signed in to change notification settings - Fork 27
/
entrypoint.sh
executable file
·125 lines (107 loc) · 3.53 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
. ./scripts/entrypoint_helpers.sh
main () {
# Environment/arguments setup
local arg_action arg_cluster arg_machine_type
local opt_backend_bucket opt_quiet
local arg_var_file="${PWD}/input/terraform.tfvars"
{
entrypoint_helpers::parse_args "${@}" \
&& entrypoint_helpers::validate_args
} \
|| { echo; entrypoint_helpers::get_usage; return 1; } >&2
local -r module_path="$(
entrypoint_helpers::module_path \
"${arg_machine_type}" \
"${arg_cluster}")"
local -r tmp_var_file=$(mktemp)
cp "${arg_var_file}" "${tmp_var_file}"
# Auth setup
entrypoint_helpers::ensure_auth_token \
"${tmp_var_file}" || {
echo 'Failed to set up auth token.'
return 1
} >&2
# Backend setup
local -r deployment_path=$(
entrypoint_helpers::setup_backend \
"${arg_cluster}" \
"${tmp_var_file}" \
"${module_path}/backend.tf" \
"${opt_backend_bucket}") || {
echo 'Failed to set up backend.'
return 1
} >&2
echo "Terraform backend setup at '${deployment_path}'." >&2
# Logging setup
local -r log_file=$(mktemp)
local -r stdout_pipe=$(mktemp -u)
mkfifo -m 600 "${stdout_pipe}"
if [ "${opt_quiet}" = true ]; then
cat >"${log_file}" <"${stdout_pipe}" &
else
tee "${log_file}" <"${stdout_pipe}" &
fi
local -r log_pid="${!}"
# Call terraform
local terraform_success=true
case "${arg_action}" in
'create')
{
entrypoint_helpers::create \
"${arg_cluster}" \
"${tmp_var_file}" \
"${module_path}" \
&& echo "Successfully created Cluster...." >&2
} || {
echo "Failed to create Cluster...." >&2
terraform_success=false
}
;;
'destroy')
{
entrypoint_helpers::destroy \
"${arg_cluster}" \
"${tmp_var_file}" \
"${module_path}" \
&& echo "Successfully destroyed Cluster...." >&2
} || {
echo "Failed to destroy Cluster...." >&2
terraform_success=false
}
;;
esac >"${stdout_pipe}"
wait "${log_pid}"
rm -f "${stdout_pipe}"
# Copy files to GCS
echo -e '\n========================================================\n' >&2
echo "Copying tfvars to GCS bucket..." >&2
gsutil cp \
"${tmp_var_file}" \
"${deployment_path}/terraform.tfvars" || {
echo 'unable to upload tfvars to GCS bucket'
return 1
} >&2
echo "Copying terraform logs to GCS bucket..." >&2
gsutil cp \
"${log_file}" \
"${deployment_path}/terraform.log" || {
echo 'unable to upload terraform logs to GCS bucket'
return 1
} >&2
[ "${terraform_success}" = true ]
}
main "${@}"