-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaws-cloudformation-cu-stack
executable file
·228 lines (205 loc) · 8.75 KB
/
aws-cloudformation-cu-stack
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
#- aws-cloudformation-cu-stack 1.0
## Usage: aws-cloudformation-cu-stack [OPTION]
## Create or update a CloudFormation stack.
##
## --stack-name See `aws cloudformation create-stack help`
## --template-body Translated to --template-url. This works around the lower size limits of template-body vs template-url
## --timeout-in-minutes See `aws cloudformation create-stack help`. Defaults to 60 minutes
## --on-failure See `aws cloudformation create-stack help`. Defaults to DELETE
## --disable-rollback See `aws cloudformation update-stack help`
## --capabilities See `aws cloudformation create-stack help` or `aws cloudformation update-stack help`. Defaults to CAPABILITY_IAM CAPABILITY_NAMED_IAM
##
## --template-url-prefix Location to upload template body
## --wait Wait for stack to be completely created/updated
## --create-change-set Create change-set for deferred update, named via --change-set-file or --change-set-name
## --execute-change-set Execute change-set as named via --change-set-file or --change-set-name
## --change-set-name Change-set name
## --change-set-file Change-set filename
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
TEMPLATE_BODY_ARG=
TIMEOUT_ARG="--timeout-in-minutes 60"
ON_FAILURE_ARG=
CAPABILITIES_ARG="--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM"
TEMPLATE_BODY=
TEMPLATE_URL_PREFIX=
WAIT_FOR_STACK=
CREATE_CHANGE_SET=
EXECUTE_CHANGE_SET=
CHANGE_SET_NAME=
CHANGE_SET_FILE=/dev/null
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hv -l help,version,stack-name:,template-body:,template-url-prefix:,timeout-in-minutes:,on-failure:,disable-rollback,capabilities:,wait,create-change-set,execute-change-set,change-set-name:,change-set-file: -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage # editorconfig-checker-disable-line
eval set -- "${ARGS}"
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--stack-name)
STACK_NAME=$2
STACK_NAME_ARG="$1 $2"
shift 2
;;
--template-body)
TEMPLATE_BODY="$2"
TEMPLATE_BODY_ARG="$1 $2"
shift 2
;;
--template-url-prefix)
TEMPLATE_URL_PREFIX="$2"
TEMPLATE_URL="$2/$(uuidgen)"
TEMPLATE_BODY_ARG="--template-url https://s3.amazonaws.com/${TEMPLATE_URL}"
shift 2
;;
--timeout-in-minutes)
TIMEOUT_ARG="$1 $2"
shift 2
;;
--on-failure)
ON_FAILURE_ARG="$1 $2"
shift 2
;;
--disable-rollback)
ON_FAILURE_ARG="--on-failure DO_NOTHING"
shift
;;
--capabilities)
CAPABILITIES_ARG="$1 $2"
shift 2
;;
--wait)
WAIT_FOR_STACK=true
shift
;;
--create-change-set)
CREATE_CHANGE_SET=true
shift
;;
--execute-change-set)
EXECUTE_CHANGE_SET=true
shift
;;
--change-set-name)
CHANGE_SET_NAME=$2
CHANGE_SET_NAME_ARG="$1 $2"
shift 2
;;
--change-set-file)
CHANGE_SET_FILE=$2
shift 2
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
[[ $# -eq 0 ]] || sh_script_usage
STACK_ID=$(${YP_DIR}/bin/aws-get-stack-id --stack-name ${STACK_NAME} || :)
CU=create
[[ -z ${STACK_ID} ]] || CU=update
if [[ ${CU} = create ]]; then
ON_FAILURE_ARG=${FAILURE_ARG:-"--on-failure DELETE"}
fi
LAST_EVENT_ID=
if [[ ${CU} = update ]]; then
LAST_EVENT_ID=$(aws cloudformation describe-stack-events ${STACK_NAME_ARG} --max-items 1 | jq -r ".StackEvents[0].EventId")
fi
function on_exit() {
if [[ -n ${TEMPLATE_URL_PREFIX} ]]; then
aws s3 rm s3://${TEMPLATE_URL} || true
fi
}
function cancel_create_stack() {
on_exit
aws cloudformation delete-stack ${STACK_NAME_ARG}
}
function cancel_update_stack() {
on_exit
aws cloudformation cancel-update-stack ${STACK_NAME_ARG}
}
trap on_exit EXIT
if [[ -n ${TEMPLATE_URL_PREFIX} ]]; then
aws s3 cp ${TEMPLATE_BODY#file://} s3://${TEMPLATE_URL}
fi
if [[ -n ${CREATE_CHANGE_SET} ]]; then
if [[ ${CU} = create ]]; then
echo_err "Stack does not exist, thus change-sets are not applicable."
exit 1
fi
echo_info "Creating change-set for ${STACK_ID} aka ${STACK_NAME} stack..."
CHANGE_SET_ID=$(aws cloudformation create-change-set ${STACK_NAME_ARG} ${TEMPLATE_BODY_ARG} ${CAPABILITIES_ARG} ${CHANGE_SET_NAME_ARG} "$@" | jq -r ".Id")
echo_info "Change-set URL: https://${AWS_REGION}.console.aws.amazon.com/cloudformation/home?region=${AWS_REGION}#/changeset/detail?changeSetId=${CHANGE_SET_ID}"
CHANGE_SET_NAME_ARG="--change-set-name ${CHANGE_SET_ID}"
TMP_CHANGE_SET_FILE=$(mktemp -t aws-cloudformation-cu-stack.XXXXXXXXXX)
aws cloudformation wait change-set-create-complete ${STACK_NAME_ARG} ${CHANGE_SET_NAME_ARG} || true
aws cloudformation describe-change-set ${STACK_NAME_ARG} ${CHANGE_SET_NAME_ARG} | tee ${TMP_CHANGE_SET_FILE}
cp ${TMP_CHANGE_SET_FILE} ${CHANGE_SET_FILE}
exit 0
elif [[ -n ${EXECUTE_CHANGE_SET} ]]; then
if [[ ${CU} = create ]]; then
echo_err "Stack does not exist, thus change-sets are not applicable."
exit 1
fi
if [[ -n ${CHANGE_SET_FILE} ]]; then
CHANGE_SET_NAME=$(cat ${CHANGE_SET_FILE} | jq -r ".ChangeSetName")
CHANGE_SET_ID=$(cat ${CHANGE_SET_FILE} | jq -r ".ChangeSetId")
CHANGE_SET_NAME_ARG="--change-set-name ${CHANGE_SET_ID}"
fi
echo_info "Executing ${CHANGE_SET_ID} aka ${CHANGE_SET_NAME} change-set for ${STACK_ID} aka ${STACK_NAME} stack..."
trap cancel_update_stack INT
aws cloudformation execute-change-set ${STACK_NAME_ARG} ${CHANGE_SET_NAME_ARG}
else
if [[ ${CU} = create ]]; then
# echo_info "Creating ${STACK_NAME} stack..."
trap cancel_create_stack INT
STACK_ID=$(aws cloudformation ${CU}-stack ${STACK_NAME_ARG} ${TEMPLATE_BODY_ARG} ${TIMEOUT_ARG} ${CAPABILITIES_ARG} ${ON_FAILURE_ARG} "$@" | jq -r ".StackId")
STACK_NAME_ARG="--stack-name ${STACK_ID}"
else
# echo_info "Updating ${STACK_ID} aka ${STACK_NAME} stack..."
# aws cloudformation ${CU}-stack ${STACK_NAME_ARG} "$@"
echo_info "Creating change-set for ${STACK_ID} aka ${STACK_NAME} stack..."
CHANGE_SET_ID=$(aws cloudformation create-change-set ${STACK_NAME_ARG} ${TEMPLATE_BODY_ARG} ${CAPABILITIES_ARG} ${CHANGE_SET_NAME_ARG} "$@" | jq -r ".Id")
echo_info "Change-set URL: https://${AWS_REGION}.console.aws.amazon.com/cloudformation/home?region=${AWS_REGION}#/changeset/detail?changeSetId=${CHANGE_SET_ID}"
CHANGE_SET_NAME_ARG="--change-set-name ${CHANGE_SET_ID}"
TMP_CHANGE_SET_FILE=$(mktemp -t aws-cloudformation-cu-stack.XXXXXXXXXX)
aws cloudformation wait change-set-create-complete ${STACK_NAME_ARG} ${CHANGE_SET_NAME_ARG} || true
aws cloudformation describe-change-set ${STACK_NAME_ARG} ${CHANGE_SET_NAME_ARG} | tee ${TMP_CHANGE_SET_FILE}
cp ${TMP_CHANGE_SET_FILE} ${CHANGE_SET_FILE}
echo_info "Executing change-set ${CHANGE_SET_ID} aka ${CHANGE_SET_NAME} for ${STACK_ID} aka ${STACK_NAME} stack..."
STATUS_REASON=$(cat ${TMP_CHANGE_SET_FILE} | jq -r ".StatusReason")
if [[ ${STATUS_REASON} = "The submitted information didn't contain changes. Submit different information to create a change set." ]]; then
echo_info ${STATUS_REASON}
exit 0
fi
trap cancel_update_stack INT
aws cloudformation execute-change-set ${STACK_NAME_ARG} ${CHANGE_SET_NAME_ARG}
fi
fi
echo_info "Progress URL: https://${AWS_REGION}.console.aws.amazon.com/cloudformation/home?region=${AWS_REGION}#/stack/detail?stackId=${STACK_ID}"
[[ -n ${WAIT_FOR_STACK} ]] || exit 0
echo_info "Waiting for stack-${CU}-complete..."
aws cloudformation wait stack-${CU}-complete ${STACK_NAME_ARG} || {
aws cloudformation describe-stack-events ${STACK_NAME_ARG} | \
jq ".StackEvents" | \
json -c "global.isOldEvent = global.isOldEvent || this.EventId === '${LAST_EVENT_ID}'; return !global.isOldEvent;" | \
jq "[.[] | select(.ResourceStatus|test(\"FAILED|ROLLBACK\"))]"
exit 1
}
# TODO wait for rollback complete on error