forked from marty-sullivan/CodePipeline-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit
executable file
·150 lines (101 loc) · 3.66 KB
/
init
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
#!/bin/bash
source ./config.sh
export ENVIRONMENT="$1"
if [[ ! "$APPLICATION" =~ ^[a-z][a-z0-9]*$ ]]
then
echo "ERROR: APPLICATION must start with a lowercase letter and contain only lowercase letters and numbers"
exit 1
fi
if [[ ! "$ENVIRONMENT" =~ ^[a-z][a-z0-9]*$ ]]
then
echo "ERROR: ENVIRONMENT (\$1) must start with a lowercase letter and contain only lowercase letters and numbers"
exit 1
fi
# if [[ ! "$GITHUB_REPO" =~ ^https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$ ]]
# then
# echo "ERROR: GITHUB_REPO must be a valid HTTPS URL pointing to a GitHub repository"
# exit 1
# fi
if [[ $ENVIRONMENT != "master" ]]
then
echo "Setting Non-Master Domain Name: $ENVIRONMENT.$DOMAIN_NAME"
DOMAIN_NAME="$ENVIRONMENT.$DOMAIN_NAME"
fi
STACK_NAME="$APPLICATION-$ENVIRONMENT"
if ! [ -x "$(command -v aws)" ]
then
echo "ERROR: You must install the awscli"
echo "https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html"
exit 1
fi
STACK_DESCRIPTION=$(aws cloudformation describe-stacks --stack-name $STACK_NAME 2>&1)
if [[ $STACK_DESCRIPTION == *"locate credentials"* || $STACK_DESCRIPTION == *"specify a region"* ]]
then
echo "ERROR: You must set your AWS credentials/profile and default region via Environment Variables"
echo "https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html"
exit 1
fi
if [[ $STACK_DESCRIPTION != *"ValidationError"* ]]
then
echo "$STACK_NAME exists, no need to create/build..."
else
echo "Initializing $STACK_NAME CloudFormation Stack..."
aws cloudformation deploy \
--stack-name "$STACK_NAME" \
--template-file ./build/init.yml \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
--parameter-overrides \
"Application=$APPLICATION" \
"Environment=$ENVIRONMENT" \
"DomainName=$DOMAIN_NAME" \
"GitHubOwner=$GITHUB_OWNER" \
"GitHubRepo=$GITHUB_REPO" \
"AlertEmail=$ALERT_EMAIL" \
"AlertPhone=$ALERT_PHONE" \
--tags \
"Application=$APPLICATION" \
"Environment=$ENVIRONMENT" \
# if [ "$ENVIRONMENT" == "master" ]
# then
# echo "Enabling Termination Protection for master branch stack $STACK_NAME"
# aws cloudformation update-termination-protection \
# --stack-name "$STACK_NAME" \
# --enable-termination-protection > /dev/null
# fi
# BUILD_PROJECT=$(aws cloudformation describe-stacks \
# --stack-name "$STACK_NAME" \
# --query 'Stacks[0].Outputs[?OutputKey==`BuildProject`].OutputValue' \
# --output text)
# echo "Setting GitHub branch filter for CodeBuild Project $BUILD_PROJECT..."
# aws codebuild update-webhook \
# --project-name "$BUILD_PROJECT" \
# --branch-filter "$ENVIRONMENT" \
# --rotate-secret > /dev/null
# echo "Building full application stack via AWS CodeBuild..."
# BUILD_ID=$(aws codebuild start-build \
# --project-name "$BUILD_PROJECT" \
# --source-version "$ENVIRONMENT" \
# --query 'build.id' \
# --output text)
# echo "You can watch progress at the following URI:"
# echo "https://console.aws.amazon.com/codesuite/codebuild/projects/$BUILD_PROJECT/build/$BUILD_ID/log"
# while [ true ]
# do
# sleep 10
# BUILD_STATUS=$(aws codebuild batch-get-builds \
# --ids "$BUILD_ID" \
# --query 'builds[0].buildStatus' \
# --output text)
# if [ "$BUILD_STATUS" != "IN_PROGRESS" ]
# then
# break
# fi
# done
# if [ "$BUILD_STATUS" != "SUCCEEDED" ]
# then
# echo "There was a problem with the build! Check the console for logs..."
# exit 1
# else
# echo "$STACK_NAME Fully Initialized!"
# fi
fi