-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.sh
executable file
·131 lines (101 loc) · 3.24 KB
/
stack.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
#!/bin/bash
# A script to create a Cloudformation stack.
# Run this script like:
# ./stack.sh <stack name> <command> <bucket name>
# Where "command" is one of the following:
entryFuncs=("delete" "create")
# "delete": Delete the existing stack and all of its resources.
# "create": Create the stack. An error occurs if a stack already
# exists with the provided name, and no update occurs.
# For command=create, the optional 4th argument is a space-separated list
# of parameter-overrides to pass to cloudformation deploy,
# which become template parameters.
############################################################
STACK_NAME=$1
WEB_BUCKET_NAME=$3
ARG4=$4
if [[ -z $STACK_NAME ]]; then
echo ERROR: Please set STACK_NAME
return 1
else
# Convert to lower-case
STACK_NAME_LOWER="$(echo $STACK_NAME | tr '[A-Z]' '[a-z]')"
fi
if [[ -z $WEB_BUCKET_NAME ]]; then
echo ERROR: Please set WEB_BUCKET_NAME
return 1
fi
# Prevent terminal output waiting:
export AWS_PAGER=""
_make_names() {
RAND_ID=$(dd if=/dev/random bs=3 count=6 2>/dev/null \
| od -An -tx1 | tr -d ' \t\n')
TEMP_BUCKET_NAME="${STACK_NAME_LOWER}-bucket-${RAND_ID}"
}
_delete_files() {
rm -f out.yml *.zip
}
delete() {
_delete_files
_make_names
echo "Deleting stack $STACK_NAME and its resources..."
# First must delete the bucket:
aws s3 rb --force s3://$WEB_BUCKET_NAME &> /dev/null
aws cloudformation delete-stack --stack-name $STACK_NAME
STACK_DELETED=
while [[ -z $STACK_DELETED ]]; do
export STACK_DELETED=$(aws cloudformation list-stacks | \
python3 -c \
"import sys, json
complete = True
for item in json.load(sys.stdin)['StackSummaries']:
if item['StackName'] == '$STACK_NAME' and item['StackStatus'] != 'DELETE_COMPLETE':
complete = False
if complete:
print('true')")
sleep 1
done
echo "Deleted $STACK_NAME"
}
create() {
_make_names
echo "Creating $STACK_NAME..."
aws s3 mb s3://$TEMP_BUCKET_NAME
echo Made temporary S3 bucket $TEMP_BUCKET_NAME
aws cloudformation package \
--template-file template.yml \
--s3-bucket $TEMP_BUCKET_NAME \
--output-template-file out.yml &> /dev/null
aws cloudformation deploy \
--template-file out.yml \
--stack-name $STACK_NAME \
--capabilities CAPABILITY_NAMED_IAM \
--parameter-overrides bucketName=$WEB_BUCKET_NAME $ARG4
if [[ "$?" -ne 0 ]]; then
aws cloudformation describe-stack-events \
--stack-name $STACK_NAME
fi
echo "Waiting for stack creation to complete..."
aws cloudformation wait stack-create-complete \
--stack-name $STACK_NAME --no-paginate
echo "Done."
aws s3 rb --force s3://$TEMP_BUCKET_NAME
echo Deleted the temporary S3 bucket
# upload the static website files to the bucket:
aws s3 cp index.html s3://${WEB_BUCKET_NAME}/index.html
aws s3 cp image.png s3://${WEB_BUCKET_NAME}/image.png
aws s3 cp test_captcha.html s3://${WEB_BUCKET_NAME}/test_captcha.html
}
################################################
ok=0
for i in "${entryFuncs[@]}"
do
if [ "$i" == "$2" ]; then
echo "Executing $i()"
$i
ok=1
fi
done
if (( ok == 0 )); then
echo "Error: command not recognised"
fi