This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.inc.sh
194 lines (160 loc) · 6.6 KB
/
app.inc.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env bash
[[ ! -x "${GIT_ROOT}/bin/get-env-name" ]] || ENV_NAME=${ENV_NAME:-$(${GIT_ROOT}/bin/get-env-name)}
# Add path to support-firecloud/venv/bin to find aws and other CI bootstrapped python tools.
# The venv works without being activated, note that installations using pip3 ought to first active the venv.
export PATH=$PATH:/usr/local/support-firecloud/venv/bin
function app_get_snapshot() {
# deprecated
${GIT_ROOT}/bin/get-snapshot
}
function app_reset_to_snapshot() {
# deprecated
make reset-to-snapshot
}
function app_get_dist() {
# deprecated
${GIT_ROOT}/bin/get-dist
}
function app_reset_to_dist() {
# deprecated
make reset-to-dist
}
function app_assume_aws_credentials() {
echo_do "Impersonating ${AWS_ACCOUNT_PREFIX} credentials..."
for PREFIX_AWS_VAR in $(printenv | grep -o "^${AWS_ACCOUNT_PREFIX}_AWS[^=]\+" ); do
AWS_VAR=${PREFIX_AWS_VAR/#${AWS_ACCOUNT_PREFIX}_/}
export eval "${AWS_VAR}=${!PREFIX_AWS_VAR}"
done
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-${GLOBAL_AWS_REGION}}
export AWS_REGION=${AWS_REGION:-${AWS_DEFAULT_REGION}}
echo_done
}
function app_provision_cfn_stack() {
local STACK_STEM=$1
local TRAVIS_WAIT_INTERVAL=$2
local TRAVIS_WAIT_MAX=$3
local TRAVIS_WAIT_LOG="${STACK_STEM}.${STACK_ITERATION}.travis-wait.log"
local STACK_ITERATION=${STACK_ITERATION:-1}
local STACK_NAME=${STACK_STEM/#env/${ENV_NAME}}
local STACK_ID=$(${SUPPORT_FIRECLOUD_DIR}/bin/aws-get-stack-id --stack-name ${STACK_NAME} || true)
local AWS_CFN_URL=https://${AWS_REGION}.console.aws.amazon.com/cloudformation/home
echo_do "Provisioning CloudFormation ${STACK_NAME}... DRYRUN=${DRYRUN:-}"
(
cd cfn
if [[ -z "${STACK_ID}" ]]; then
echo_info "Stack ${STACK_NAME} did not exist..."
make ${STACK_STEM}.cfn.json
[[ "${DRYRUN:-}" = "true" ]] || {
# FIXME see https://github.com/m3t/travis_wait
# make ${STACK_STEM}.cfn.json/exec
${SUPPORT_FIRECLOUD_DIR}/bin/travis-wait \
-i ${TRAVIS_WAIT_INTERVAL} \
-l ${TRAVIS_WAIT_MAX} \
"make ${STACK_STEM}.cfn.json/exec" \
"${TRAVIS_WAIT_LOG}" || {
cat "${TRAVIS_WAIT_LOG}"
exit 1
}
cat "${TRAVIS_WAIT_LOG}"
}
else
echo_info "Stack ${STACK_NAME} already exists..."
make ${STACK_STEM}.change-set.json
[[ "${DRYRUN:-}" = "true" ]] || {
# FIXME see https://github.com/m3t/travis_wait
# make ${STACK_STEM}.change-set.json/exec
${SUPPORT_FIRECLOUD_DIR}/bin/travis-wait \
-i ${TRAVIS_WAIT_INTERVAL} \
-l ${TRAVIS_WAIT_MAX} \
"make ${STACK_STEM}.change-set.json/exec" \
"${TRAVIS_WAIT_LOG}" || {
cat "${TRAVIS_WAIT_LOG}"
exit 1
}
cat "${TRAVIS_WAIT_LOG}"
[[ ! -f ${STACK_STEM}.drift.json ]] || {
echo_warn "${STACK_NAME} has drifted."
[[ -n "${SLACK_WEBHOOK:-}" ]] || {
echo_info "SLACK_WEBHOOK is undefined."
echo_skip "Notifying Slack..."
exit 0
}
[[ -n "${SLACK_CHANNELS:-}" ]] || SLACK_CHANNELS=${SLACK_CHANNEL:-};
[[ -n "${SLACK_CHANNELS:-}" ]] || {
echo_info "SLACK_CHANNEL{S} is undefined."
echo_skip "Notifying Slack..."
exit 0
};
for SLACK_CHANNEL in $(echo ${SLACK_CHANNELS} | sed "s/[, ]+/\n/g"); do
echo_info "Notifying Slack #${SLACK_CHANNEL}..."
STACK_ID=$(${SUPPORT_FIRECLOUD_DIR}/bin/aws-get-stack-id --stack-name ${STACK_NAME} || true)
${SUPPORT_FIRECLOUD_DIR}/bin/slack-echo --to "#${SLACK_CHANNEL}" " \
Stack ${STACK_NAME} has drifted. \
See ${AWS_CFN_URL}?region=${AWS_REGION}#/stack/detail/drift?stackId=${STACK_ID//\//%2f} . \
"
done
}
}
fi
)
echo_done
}
function app_provision_cfn_stacks() {
local STACK_STEMS=$1
STACK_STEMS="$(echo "${STACK_STEMS}" | tr "," " ")"
shift
# Running stacks provision in multiple iterations
# STACK_ITERATION is needed when you have cross-stack mutual blocking dependencies
declare -A STACK_ITERATIONS
for STACK_STEM in ${STACK_STEMS}; do
STACK_ITERATIONS[${STACK_STEM}]=$((${STACK_ITERATIONS[${STACK_STEM}]:-0} + 1))
export STACK_ITERATION=${STACK_ITERATIONS[${STACK_STEM}]}
app_provision_cfn_stack ${STACK_STEM} $@
done
}
function app_teardown_cfn_stack() {
local STACK_STEM=$1
local TRAVIS_WAIT_INTERVAL=$2
local TRAVIS_WAIT_MAX=$3
local TRAVIS_WAIT_LOG="${STACK_STEM}.travis-wait.log"
local TRAVIS_WAIT_ALLOW_APPEND=1
local STACK_NAME=${STACK_STEM/#env-/${ENV_NAME}-}
local STACK_ID=$(${SUPPORT_FIRECLOUD_DIR}/bin/aws-get-stack-id --stack-name ${STACK_NAME} || true)
[ -n "${STACK_ID}" ] || {
echo_skip "Stack ${STACK_NAME} for env ${ENV_NAME} not found"
}
[ -z "${STACK_ID}" ] || (
echo_info "Found CFN stack ${STACK_ID}"
echo_do "Tearing down stack ${STACK_NAME}..."
exe cd ${GIT_ROOT}/cfn
${SUPPORT_FIRECLOUD_DIR}/bin/travis-wait \
-i ${TRAVIS_WAIT_INTERVAL} \
-l ${TRAVIS_WAIT_MAX} \
-a ${TRAVIS_WAIT_ALLOW_APPEND} \
"make ${STACK_STEM}.cfn.json/rm" \
"${TRAVIS_WAIT_LOG}" || {
cat "${TRAVIS_WAIT_LOG}"
exit 1
}
cat "${TRAVIS_WAIT_LOG}"
echo_done
)
STACK_ID=$(${SUPPORT_FIRECLOUD_DIR}/bin/aws-get-stack-id --stack-name ${STACK_NAME} || true)
[ -z "${STACK_ID}" ] || (
echo_err "Teardown of stack ${STACK_NAME} failed"
exit 1
)
}
function app_teardown_cfn_stacks() {
local STACK_STEMS=$1
STACK_STEMS="$(echo "${STACK_STEMS}" | tr "," " ")"
shift
# Running stacks provision in multiple iterations
# STACK_ITERATION is needed when you have cross-stack mutual blocking dependencies
declare -A STACK_ITERATIONS
for STACK_STEM in ${STACK_STEMS}; do
STACK_ITERATIONS[${STACK_STEM}]=$((${STACK_ITERATIONS[${STACK_STEM}]:-0} + 1))
export STACK_ITERATION=${STACK_ITERATIONS[${STACK_STEM}]}
app_teardown_cfn_stack ${STACK_STEM} $@
done
}