-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsetup.sh
executable file
·177 lines (147 loc) · 6.87 KB
/
setup.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
#!/bin/bash
# This is a simple bash script for the BBB Application Infrastructure deployment.
# It basically glues together the parts running in loose coupeling during the deployment and helps to speed things up which
# otherwise would have to be noted down and put into the command line.
# This can be migrated into real orchestration / automation toolsets if needed (e.g. Ansible, Puppet or Terraform)
# created by David Surey - [email protected]
# Disclaimber: NOT FOR PRODUCTION USE - Only for demo and testing purposes
ERROR_COUNT=0;
if [[ $# -lt 5 ]] ; then
echo 'arguments missing, please provide at least email (-e), the aws profile string (-p), the domain name (-d), the deployment Stack Name (-s) and the hosted zone to be used (-h)'
exit 1
fi
while getopts ":p:e:h:s:d:" opt; do
case $opt in
p) BBBPROFILE="$OPTARG"
;;
e) OPERATOREMAIL="$OPTARG"
;;
h) HOSTEDZONE="$OPTARG"
;;
s) BBBSTACK="$OPTARG"
;;
d) DOMAIN="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
if ! [ -x "$(command -v aws)" ]; then
echo 'ERROR: aws cli is not installed.' >&2
exit 1
fi
if ! docker ps -q 2>/dev/null; then
echo "ERROR: Docker is not running. Please start the docker runtime on your system and try again"
exit 1
fi
echo "using AWS Profile $BBBPROFILE"
echo "##################################################"
echo "Validating AWS CloudFormation templates..."
echo "##################################################"
# Loop through the YAML templates in this repository
for TEMPLATE in $(find . -name 'bbb-on-aws-*.template.yaml'); do
# Validate the template with CloudFormation
ERRORS=$(aws cloudformation validate-template --profile=$BBBPROFILE --template-body file://$TEMPLATE 2>&1 >/dev/null);
if [ "$?" -gt "0" ]; then
((ERROR_COUNT++));
echo "[fail] $TEMPLATE: $ERRORS";
else
echo "[pass] $TEMPLATE";
fi;
done;
# Error out if templates are not validate.
echo "$ERROR_COUNT template validation error(s)";
if [ "$ERROR_COUNT" -gt 0 ];
then exit 1;
fi
echo "##################################################"
echo "Validating of AWS CloudFormation templates finished"
echo "##################################################"
# Deploy the Needed Buckets for the later build
echo "deploy the Prerequisites of the BBB Environment and Application if needed"
echo "##################################################"
BBBPREPSTACK="${BBBSTACK}-Sources"
aws cloudformation deploy --stack-name $BBBPREPSTACK --profile=$BBBPROFILE --template ./templates/bbb-on-aws-buildbuckets.template.yaml
echo "##################################################"
echo "deployment done"
# get the s3 bucket name out of the deployment.
SOURCE=`aws cloudformation describe-stacks --profile=$BBBPROFILE --query "Stacks[0].Outputs[0].OutputValue" --stack-name $BBBPREPSTACK`
SOURCE=`echo "${SOURCE//\"}"`
# we will upload the needed CFN Templates to S3 containing the IaaC Code which deploys the actual infrastructure.
# This will error out if the source files are missing.
echo "##################################################"
echo "Copy Files to the S3 Bucket for further usage"
echo "##################################################"
if [ -e . ]
then
echo "##################################################"
echo "copy BBB code source file"
aws s3 sync --profile=$BBBPROFILE --exclude=".DS_Store" ./templates s3://$SOURCE
aws s3 sync --profile=$BBBPROFILE --exclude=".DS_Store" ./scripts s3://$SOURCE
echo "##################################################"
else
echo "BBB code source file missing"
echo "##################################################"
exit 1
fi
echo "##################################################"
echo "File Copy finished"
ENVIRONMENTTYPE=$(jq -r ".Parameters.BBBEnvironmentType" bbb-on-aws-param.json)
if [ "$ENVIRONMENTTYPE" == 'scalable' ]
then
BBBECRStack="${BBBSTACK}-registry"
aws cloudformation deploy --profile=$BBBPROFILE --stack-name $BBBECRStack \
--parameter-overrides $PARAMETERS \
$(jq -r '.Parameters | to_entries | map("\(.key)=\(.value)") | join(" ")' bbb-on-aws-param.json) \
--template ./templates/bbb-on-aws-registry.template.yaml
GREENLIGHTREGISTRY=`aws cloudformation describe-stacks --profile=$BBBPROFILE --query "Stacks[0].Outputs[0].OutputValue" --stack-name $BBBECRStack`
GREENLIGHTREGISTRY=`echo "${GREENLIGHTREGISTRY//\"}"`
SCALEILITEREGISTRY=`aws cloudformation describe-stacks --profile=$BBBPROFILE --query "Stacks[0].Outputs[1].OutputValue" --stack-name $BBBECRStack`
SCALEILITEREGISTRY=`echo "${SCALEILITEREGISTRY//\"}"`
# we will mirror the needed images from dockerhub and push towards ECR
echo "##################################################"
echo "Mirror docker images to ECR for further usage"
echo "##################################################"
IMAGES=( BBBgreenlightImage BBBScaleliteNginxImage BBBScaleliteApiImage BBBScalelitePollerImage BBBScaleliteImporterImage )
if [[ -z "${AWS_REGION}" ]]; then
REGION=$(aws configure get region --profile=$BBBPROFILE)
echo "Using default region ${REGION} from profile ${BBBPROFILE}"
else
REGION=${AWS_REGION}
echo "Using region ${REGION} from AWS_REGION environment variable"
fi
ACCOUNTID=$(aws sts get-caller-identity --query Account --output text --profile=$BBBPROFILE)
REGISTRY=$ACCOUNTID.dkr.ecr.$REGION.amazonaws.com
SCALEILITEREGISTRY=$ACCOUNTID.dkr.ecr.$REGION.amazonaws.com/$SCALEILITEREGISTRY
GREENLIGHTREGISTRY=$ACCOUNTID.dkr.ecr.$REGION.amazonaws.com/$GREENLIGHTREGISTRY
aws ecr get-login-password --profile=$BBBPROFILE | docker login --username AWS --password-stdin $SCALEILITEREGISTRY
aws ecr get-login-password --profile=$BBBPROFILE | docker login --username AWS --password-stdin $GREENLIGHTREGISTRY
for IMAGE in "${IMAGES[@]}"
do
IMAGE=$(jq -r ".Parameters.$IMAGE" bbb-on-aws-param.json)
docker pull $IMAGE
docker tag $IMAGE $REGISTRY/$IMAGE
docker push $REGISTRY/$IMAGE
done
echo "##################################################"
echo "Registry Preperation finished"
else
REGISTRY="Dockerhub"
fi
# Setting the dynamic Parameters for the Deployment
PARAMETERS=" BBBOperatorEMail=$OPERATOREMAIL \
BBBStackBucketStack=$BBBSTACK-Sources \
BBBDomainName=$DOMAIN \
BBBHostedZone=$HOSTEDZONE \
BBBECRRegistry=$REGISTRY"
# Deploy the BBB infrastructure.
echo "Building the BBB Environment"
echo "##################################################"
aws cloudformation deploy --profile=$BBBPROFILE --stack-name $BBBSTACK \
--capabilities CAPABILITY_NAMED_IAM \
--parameter-overrides $PARAMETERS \
$(jq -r '.Parameters | to_entries | map("\(.key)=\(.value)") | join(" ")' bbb-on-aws-param.json) \
--template ./bbb-on-aws-root.template.yaml
echo "##################################################"
echo "Deployment finished"
exit 0