-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdeploy.sh
executable file
·175 lines (139 loc) · 4.25 KB
/
deploy.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
#!/bin/bash
#
# File locations
#
TEMPLATE_IN_FILE=resources/cf.json
USERDATA_IN_FILE=resources/user-data
TEMPLATE_OUT_FILE=template.json
#AWS_CMD_PROFILE=" --profile nephoeng "
#
# AWS stack create command line and options
#
usage() {
echo Usage:
echo $0 [subcommand]
echo
echo Where subcommand is 'create-stack', 'update-stack', or 'delete-stack'
exit 1
}
if [ "$#" -ne 1 ]; then
usage
fi
CloudCommand=$1
#
# Ensure that a value for Label has been set
#
if [ -z "${Label}" ]; then
echo "Please set the \"Label\" environment variable to proceed."
exit 1
fi
#
# Ensure that a value for CloudCommand has been set to either create-stack, update-stack, or delete-stack
#
if [ -z "${CloudCommand}" ]; then
echo ""
echo "You have not set the CloudCommand environment variable."
echo ""
echo "Please set the \"CloudCommand\" environment variable to create-stack, update-stack, or delete-stack and then re-run deploy.sh to proceed."
echo ""
#
# Get a listing of all cloudformation
# stacks current in AWS and display them
# to ensure the user picks the corret stack to
# perform operations on.
#
CMD="aws ${AWS_CMD_PROFILE} cloudformation list-stacks --stack-status-filter CREATE_COMPLETE"
echo ""
echo "Listing of current CloudFormation Stacks that completed successfully"
echo "-----------------------"
echo ""
echo "In 5 seconds I will display all Completed Cloud Formation Stacks in Amazon..."
echo ""
sleep 5
echo "Listing............"
echo ""
echo $CMD
$CMD
echo ""
echo "You have not set the CloudCommand environment variable."
echo ""
echo "Please set the \"CloudCommand\" environment variable to create-stack, update-stack, or delete-stack and then re-run deploy.sh to proceed."
echo ""
exit 1
fi
if [ "${CloudCommand}" != 'delete-stack' ]; then
#
# Generate CF JSON (this is ugly! REP)
#
USERDATA1=$(mktemp -t user-data.XXXXXXXXXX)
grep -v "^\W*#" ${USERDATA_IN_FILE} | grep -v "^$" > ${USERDATA1}
USERDATA2=$(mktemp -t user-data.XXXXXXXXXX)
cat ${USERDATA1} | sed 's/LaunchConfig1/AdminLaunchConfig/g' > ${USERDATA2}
TMPFILE=$(mktemp -t cf.XXXXXXXXXX)
./bin/gen_cf_json "${TEMPLATE_IN_FILE}" "${USERDATA1}" LaunchConfig1 > ${TMPFILE}
./bin/gen_cf_json "${TMPFILE}" "${USERDATA2}" AdminLaunchConfig > ${TEMPLATE_OUT_FILE}
rm -f ${USERDATA1} ${USERDATA2} ${TMPFILE}
#
# Load parameters from environment and build params argument line for
#
PARAM_ARGS=
PARAM_NAMES=$( ./bin/find_cf_params ${TEMPLATE_IN_FILE} )
for P in ${PARAM_NAMES}
do
V=$( eval echo \${${P}} )
if ! [ -z $V ]; then
PARAM_ARGS="$PARAM_ARGS ParameterKey=${P},ParameterValue=${V}"
fi
done
if ! [ x = "x${PARAM_ARGS}" ]; then
PARAM_ARGS="--parameters $PARAM_ARGS"
fi
fi
#
# Build my create-stack command with Arguments
#
#
# If CloudCommand has been set to create-stack then set ROLLBACK_ARG
#
if [ "${CloudCommand}" == 'create-stack' ]; then
ROLLBACK_ARG=" --disable-rollback "
CMD="aws ${AWS_CMD_PROFILE} cloudformation ${CloudCommand} \
--capabilities CAPABILITY_IAM \
--stack-name ${Label} \
${ROLLBACK_ARG} \
--template-body file://${TEMPLATE_OUT_FILE}"
echo "Performing an create-stack in AWS of the Cloudstack named ${Label}!"
#
# Build my create-stack command with Arguments
#
CMD="$CMD $PARAM_ARGS"
fi
#
# If CloudCommand has been set to update-stack then remove ROLLBACK_ARG and all PARAM_ARGS
#
if [ "${CloudCommand}" == 'update-stack' ]; then
CMD="aws ${AWS_CMD_PROFILE} cloudformation ${CloudCommand} \
--capabilities CAPABILITY_IAM \
--stack-name ${Label} \
--template-body file://${TEMPLATE_OUT_FILE}"
echo "Performing an update-stack in AWS of the Cloudstack named ${Label}!"
#
# Build my update-stack command with Arguments and PARAM_ARGS
#
CMD="$CMD $PARAM_ARGS"
fi
#
# If CloudCommand has been set to delete-stack then remove ROLLBACK_ARG and all PARAM_ARGS
#
if [ "${CloudCommand}" == 'delete-stack' ]; then
CMD="aws ${AWS_CMD_PROFILE} cloudformation ${CloudCommand} \
--stack-name ${Label}"
echo "Performing an delete-stack in AWS of the Cloudstack named ${Label}!"
fi
#
# Report and then run it
#
echo "Command to execute:"
echo "-----------------------"
echo $CMD
$CMD