-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.sh
executable file
·203 lines (165 loc) · 6.9 KB
/
publish.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
195
196
197
198
199
200
201
#!/bin/bash
#
# Publish a resource type to all regions
# Run this from the resource directory, for example S3_DeleteBucketContents/
#
# See scripts/publish.sh for an example of how to call it
#
# Environment:
#
# AWS_PROFILE (If a default profile is not set)
#
# Args:
#
# $1 The region to publish to
set -eou pipefail
export AWS_REGION=$1
cfn validate
cfn generate
TYPE_NAME=$(cat .rpdk-config | jq -r .typeName)
# Create or update the setup stack
if [ -f "test/setup.yml" ]
then
SETUP_STACK_NAME="setup-prod-$(echo $TYPE_NAME | sed s/::/-/g | tr '[:upper:]' '[:lower:]')"
if ! aws cloudformation --region $AWS_REGION describe-stacks --stack-name $SETUP_STACK_NAME 2>&1 ; then
echo "Creating $SETUP_STACK_NAME"
aws cloudformation --region $AWS_REGION create-stack --stack-name $SETUP_STACK_NAME --template-body file://test/setup.yml
aws cloudformation --region $AWS_REGION wait stack-create-complete --stack-name $SETUP_STACK_NAME
else
echo "Updating $SETUP_STACK_NAME"
update_output=$(aws cloudformation --region $AWS_REGION update-stack --stack-name $SETUP_STACK_NAME --template-body file://test/setup.yml --capabilities CAPABILITY_IAM 2>&1 || [ $? -ne 0 ])
echo $update_output
if [[ $update_output == *"ValidationError"* && $update_output == *"No updates"* ]] ; then
echo "No updates to setup stack"
else
echo "Waiting for stack update to complete"
aws cloudformation --region $AWS_REGION wait stack-update-complete --stack-name $SETUP_STACK_NAME
# This just blocks forever if the previous command failed for another reason,
# since it never sees update stack complete
fi
fi
else
echo "Did not find test/setup.yml, skipping setup stack"
fi
# Overwrite the role stack to fix the broken Condition.
# test-type does not use the role we register, it re-deploys the stack
cp resource-role-prod.yaml resource-role.yaml
# Create the package
echo "About to run cfn submit --dry-run to create the package"
echo ""
cfn submit --dry-run
echo ""
# For example, awscommunity-s3-deletebucketcontents
TYPE_NAME_LOWER="$(echo $TYPE_NAME | sed s/::/-/g | tr '[:upper:]' '[:lower:]')"
echo "TYPE_NAME_LOWER is $TYPE_NAME_LOWER"
ZIPFILE="${TYPE_NAME_LOWER}.zip"
echo "ZIPFILE is $ZIPFILE"
# By default we won't actually publish, so that this script can be run in
# sandbox accounts that are not the actual publishing account
PUBLISHING_ENABLED=0
ACCOUNT_ID=$(aws sts get-caller-identity|jq -r .Account)
echo "ACCOUNT_ID is $ACCOUNT_ID"
if [ "$ACCOUNT_ID" == "387586997764" ]
then
PUBLISHING_ENABLED=1
fi
HANDLER_BUCKET="cep-handler-${ACCOUNT_ID}"
# Copy the package to S3
echo "Copying schema package handler to $HANDLER_BUCKET"
aws s3 cp $ZIPFILE s3://$HANDLER_BUCKET/$ZIPFILE
ROLE_STACK_NAME="$(echo $TYPE_NAME | sed s/::/-/g | tr '[:upper:]' '[:lower:]')-prod-role-stack"
echo "ROLE_STACK_NAME is $ROLE_STACK_NAME"
echo ""
# Create or update the role stack
if ! aws cloudformation --region $AWS_REGION describe-stacks --stack-name $ROLE_STACK_NAME 2>&1 ; then
echo "Creating role stack"
aws cloudformation --region $AWS_REGION create-stack --stack-name $ROLE_STACK_NAME --template-body file://resource-role-prod.yaml --capabilities CAPABILITY_IAM
echo ""
aws cloudformation --region $AWS_REGION wait stack-create-complete --stack-name $ROLE_STACK_NAME
else
echo "Updating role stack"
update_output=$(aws cloudformation --region $AWS_REGION update-stack --stack-name $ROLE_STACK_NAME --template-body file://resource-role-prod.yaml --capabilities CAPABILITY_IAM 2>&1 || [ $? -ne 0 ])
echo $update_output
if [[ $update_output == *"ValidationError"* && $update_output == *"No updates"* ]] ; then
echo "No updates to role stack"
else
aws cloudformation --region $AWS_REGION wait stack-update-complete --stack-name $ROLE_STACK_NAME
fi
fi
echo "About to describe stack to get the role arn"
ROLE_ARN=$(aws cloudformation --region $AWS_REGION describe-stacks --stack-name $ROLE_STACK_NAME | jq ".Stacks|.[0]|.Outputs|.[0]|.OutputValue" | sed s/\"//g)
echo ""
echo "ROLE_ARN is $ROLE_ARN"
echo ""
# Register the type
echo "About to run register-type"
echo ""
TOKEN=$(aws cloudformation --region $AWS_REGION register-type --type RESOURCE --type-name $TYPE_NAME --schema-handler-package s3://$HANDLER_BUCKET/$ZIPFILE --execution-role-arn $ROLE_ARN | jq -r .RegistrationToken)
echo "Registration token is $TOKEN"
STATUS="IN_PROGRESS"
check_status() {
STATUS=$(aws cloudformation --region $AWS_REGION describe-type-registration --registration-token $TOKEN | jq -r .ProgressStatus)
}
echo "About to poll status for $TOKEN"
# Check status
while [ "$STATUS" == "IN_PROGRESS" ]
do
sleep 5
check_status
done
echo $STATUS
echo "describe-type-registration"
aws cloudformation --region $AWS_REGION describe-type-registration --registration-token $TOKEN
echo "describe-type"
aws --no-cli-pager cloudformation --region $AWS_REGION describe-type --type RESOURCE --type-name $TYPE_NAME
sleep 5
# Set this version to be the default
echo "About to get latest version id"
VERSION_ID=$(aws cloudformation --region $AWS_REGION describe-type-registration --registration-token $TOKEN | jq -r .TypeVersionArn | awk -F/ '{print $NF}')
echo ""
echo "VERSION_ID is $VERSION_ID"
echo ""
echo "About to set-type-default-version"
aws cloudformation --region $AWS_REGION set-type-default-version --type RESOURCE --type-name $TYPE_NAME --version-id $VERSION_ID
echo ""
# Set the type configuration
if [ -f "../get_type_configuration.py" ]
then
echo "About to set type configuration"
TYPE_CONFIG_PATH=$(python ../get_type_configuration.py)
echo "TYPE_CONFIG_PATH is $TYPE_CONFIG_PATH"
aws cloudformation set-type-configuration --type RESOURCE --type-name $TYPE_NAME --configuration-alias default --configuration $(cat ${TYPE_CONFIG_PATH} | jq -c "")
else
echo "Did not find ../get_type_configuration.py, skipping type configuration"
fi
# Test the resource type
echo "About to run test-type"
echo ""
TYPE_VERSION_ARN=$(aws cloudformation --region $AWS_REGION test-type --type RESOURCE --type-name $TYPE_NAME --log-delivery-bucket $HANDLER_BUCKET | jq .TypeVersionArn | sed s/\"//g)
echo "TYPE_VERSION_ARN is $TYPE_VERSION_ARN"
echo ""
TEST_STATUS="IN_PROGRESS"
echo "About to poll test status for $TYPE_VERSION_ARN"
check_test_status() {
TEST_STATUS=$(aws cloudformation --region $AWS_REGION describe-type --arn $TYPE_VERSION_ARN | jq -r .TypeTestsStatus)
}
# Check status
while [ "$TEST_STATUS" == "IN_PROGRESS" ]
do
sleep 5
check_test_status
done
echo $TEST_STATUS
if [ "$TEST_STATUS" == "FAILED" ]
then
exit 1
fi
# Publish the type
if [ "$PUBLISHING_ENABLED" -eq 1 ]
then
echo "About to publish $TYPE_NAME in $AWS_REGION"
aws cloudformation --region $AWS_REGION publish-type --type RESOURCE --type-name $TYPE_NAME
else
echo "PUBLISHING_ENABLED is $PUBLISHING_ENABLED, not publishing"
fi
echo "Done"