This repository was archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanageStack.sh
More file actions
executable file
·151 lines (133 loc) · 5.1 KB
/
manageStack.sh
File metadata and controls
executable file
·151 lines (133 loc) · 5.1 KB
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
#!/bin/bash
if [[ -n "${GENERATION_DEBUG}" ]]; then set ${GENERATION_DEBUG}; fi
trap '. ${GENERATION_DIR}/cleanupContext.sh; exit ${RESULT:-1}' EXIT SIGHUP SIGINT SIGTERM
STACK_INITIATE_DEFAULT="true"
STACK_MONITOR_DEFAULT="true"
STACK_OPERATION_DEFAULT="update"
STACK_WAIT_DEFAULT=30
function usage() {
echo -e "\nManage a CloudFormation stack"
echo -e "\nUsage: $(basename $0) -t TYPE -s SLICE -i -m -w STACK_WAIT -r REGION -d\n"
echo -e "\nwhere\n"
echo -e "(o) -d (STACK_OPERATION=delete) to delete the stack"
echo -e " -h shows this text"
echo -e "(o) -i (STACK_MONITOR=false) initiates but does not monitor the stack operation"
echo -e "(o) -m (STACK_INITIATE=false) monitors but does not initiate the stack operation"
echo -e "(o) -r REGION is the AWS region identifier for the region in which the stack should be managed"
echo -e "(m) -s SLICE is the slice used to determine the stack template"
echo -e "(m) -t TYPE is the stack type - \"account\", \"product\", \"segment\", \"solution\" or \"application\""
echo -e "(o) -w STACK_WAIT is the interval between checking the progress of the stack operation"
echo -e "\nDEFAULTS:\n"
echo -e "STACK_INITIATE = ${STACK_INITIATE_DEFAULT}"
echo -e "STACK_MONITOR = ${STACK_MONITOR_DEFAULT}"
echo -e "STACK_OPERATION = ${STACK_OPERATION_DEFAULT}"
echo -e "STACK_WAIT = ${STACK_WAIT_DEFAULT} seconds"
echo -e "\nNOTES:\n"
echo -e "1. You must be in the correct directory corresponding to the requested stack type"
echo -e "2. REGION is only relevant for the \"product\" type, where multiple product stacks are necessary"
echo -e " if the product uses resources in multiple regions"
echo -e "3. \"segment\" is now used in preference to \"container\" to avoid confusion with docker"
echo -e "4. If stack doesn't exist in AWS, the update operation will create the stack"
echo -e ""
exit
}
# Parse options
while getopts ":dhimr:s:t:w:" opt; do
case $opt in
d)
STACK_OPERATION=delete
;;
h)
usage
;;
i)
STACK_MONITOR=false
;;
m)
STACK_INITIATE=false
;;
r)
REGION="${OPTARG}"
;;
s)
SLICE="${OPTARG}"
;;
t)
TYPE="${OPTARG}"
;;
w)
STACK_WAIT="${OPTARG}"
;;
\?)
echo -e "\nInvalid option: -${OPTARG}"
usage
;;
:)
echo -e "\nOption -${OPTARG} requires an argument"
usage
;;
esac
done
# Apply defaults
STACK_OPERATION=${STACK_OPERATION:-${STACK_OPERATION_DEFAULT}}
STACK_WAIT=${STACK_WAIT:-${STACK_WAIT_DEFAULT}}
STACK_INITIATE=${STACK_INITIATE:-${STACK_INITIATE_DEFAULT}}
STACK_MONITOR=${STACK_MONITOR:-${STACK_MONITOR_DEFAULT}}
# Set up the context
. ${GENERATION_DIR}/setStackContext.sh
pushd ${CF_DIR} > /dev/null 2>&1
if [[ ! -f "$TEMPLATE" ]]; then
echo -e "\n\"${TEMPLATE}\" not found. Are we in the correct place in the directory tree?"
usage
fi
# Assume all good
RESULT=0
if [[ "${STACK_INITIATE}" = "true" ]]; then
case ${STACK_OPERATION} in
delete)
aws --region ${REGION} cloudformation delete-stack --stack-name $STACKNAME 2>/dev/null
# For delete, we don't check result as stack may not exist
;;
update)
# Compress the template to avoid aws cli size limitations
cat $TEMPLATE | jq -c '.' > stripped_${TEMPLATE}
# Check if stack needs to be created
aws --region ${REGION} cloudformation describe-stacks --stack-name $STACKNAME > $STACK 2>/dev/null
RESULT=$?
if [[ "$RESULT" -ne 0 ]]; then
STACK_OPERATION="create"
fi
# Initiate the required operation
aws --region ${REGION} cloudformation ${STACK_OPERATION,,}-stack --stack-name $STACKNAME --template-body file://stripped_${TEMPLATE} --capabilities CAPABILITY_IAM
# Check result of operation
RESULT=$?
if [[ "$RESULT" -ne 0 ]]; then exit; fi
;;
*)
echo -e "\n\"${STACK_OPERATION}\" is not one of the known stack operations."
usage
;;
esac
fi
if [[ "${STACK_MONITOR}" = "true" ]]; then
while true; do
aws --region ${REGION} cloudformation describe-stacks --stack-name $STACKNAME > $STACK 2>/dev/null
if [[ ("${STACK_OPERATION}" == "delete") && ("$?" -eq 255) ]]; then
# Assume stack doesn't exist
RESULT=0
break
fi
grep "StackStatus" $STACK > STATUS.txt
cat STATUS.txt
grep "${STACK_OPERATION^^}_COMPLETE" STATUS.txt >/dev/null 2>&1
RESULT=$?
if [[ "$RESULT" -eq 0 ]]; then break;fi
grep "${STACK_OPERATION^^}_IN_PROGRESS" STATUS.txt >/dev/null 2>&1
RESULT=$?
if [[ "$RESULT" -ne 0 ]]; then break;fi
sleep ${STACK_WAIT}
done
fi
if [[ ("${STACK_OPERATION}" == "delete") && ("${RESULT}" -eq 0) ]]; then
rm -f $STACK
fi