forked from cloudfoundry/diego-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_aws_environment
executable file
·287 lines (237 loc) · 8.17 KB
/
deploy_aws_environment
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
set -e
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
function usage() {
>&2 cat <<EOF
SYNOPSIS:
Create a CloudFormation stack, deploy BOSH, and create initial CF and Diego stubs.
USAGE:
$0 (create|update|skip) CF_RELEASE_DIR DEPLOYMENT_DIR [STACK_NAME]
MANDATORY ARGUMENTS:
CF_RELEASE_DIR: Path to the local cf-release repository.
DEPLOYMENT_DIR: Directory to store files for this deployment.
OPTIONAL ARGUMENTS:
STACK_NAME: Name of the CloudFormation stack to create (default: cf-diego-stack)
EOF
exit 1
}
indent() {
sed -e 's/^/ /'
}
indent_contents_of() {
indent < "$1"
}
block() {
cat <<-EOF
$1: |
$(indent_contents_of "$2")
EOF
}
cf_credentials() {
cat <<-EOF
# GENERATED: NO TOUCHING
---
cf_credentials:
ssh_host_key_fingerprint: "$(cat keypair/ssh-proxy-host-key-fingerprint)"
consul:
$(block ca_cert certs/consul-certs/server-ca.crt | indent | indent)
$(block agent_cert certs/consul-certs/agent.crt | indent | indent)
$(block agent_key certs/consul-certs/agent.key | indent | indent)
$(block server_cert certs/consul-certs/server.crt | indent | indent)
$(block server_key certs/consul-certs/server.key | indent | indent)
uaa:
$(block signing_key keypair/uaa | indent | indent)
$(block verification_key keypair/uaa.pub | indent | indent)
EOF
}
diego_credentials() {
cat <<-EOF
# GENERATED: NO TOUCHING
---
diego_credentials:
$(block diego_ca certs/diego-ca.crt | indent)
$(block ssh_proxy_host_key keypair/ssh-proxy-host-key.pem | indent)
bbs:
$(block client_cert certs/bbs-certs/client.crt | indent | indent)
$(block client_key certs/bbs-certs/client.key | indent | indent)
$(block server_cert certs/bbs-certs/server.crt | indent | indent)
$(block server_key certs/bbs-certs/server.key | indent | indent)
rep:
$(block client_cert certs/rep-certs/client.crt | indent | indent)
$(block client_key certs/rep-certs/client.key | indent | indent)
$(block server_cert certs/rep-certs/server.crt | indent | indent)
$(block server_key certs/rep-certs/server.key | indent | indent)
EOF
}
if [ "$1" == "create" ]; then
UPDATE_OR_CREATE=create-stack
elif [ "$1" == "update" ]; then
UPDATE_OR_CREATE=update-stack
elif [ "$1" == "skip" ]; then
UPDATE_OR_CREATE="skip"
else
usage
exit 1
fi
cf_release_dir=$2
deployment_dir=$3
stack_name=${4:-cf-diego-stack}
if [ -z ${deployment_dir} ]; then
usage
fi
command -v aws >/dev/null || { echo "aws is required"; exit 1; }
command -v jq >/dev/null || { echo "jq is required"; exit 1; }
command -v spiff >/dev/null || { echo "spiff is required"; exit 1; }
command -v bosh-init >/dev/null || { echo "bosh-init is required"; exit 1; }
set -x
pushd ${deployment_dir}
mkdir -p stubs/cf
mkdir -p stubs/infrastructure
mkdir -p deployments/bosh-init
source bootstrap_environment
# install certs for ELB
if ! aws iam get-server-certificate --server-certificate-name cfrouter; then
aws iam upload-server-certificate \
--server-certificate-name cfrouter \
--private-key file://certs/elb-cfrouter.key \
--certificate-body file://certs/elb-cfrouter.pem
fi
# generate stub to be fed into template for cloudformation
cat > stubs/infrastructure/certificates.yml <<EOF
# GENERATED: NO TOUCHING
EOF
aws iam get-server-certificate --server-certificate-name cfrouter \
>> stubs/infrastructure/certificates.yml
function extra_subnets {
cat <<EOF
---
EOF
if [ -f stubs/infrastructure/extra_subnets.yml ]; then
spiff merge \
stubs/infrastructure/extra_subnets.yml \
$SCRIPT_DIR/templates/infrastructure/meta.yml \
stubs/infrastructure/availability_zones.yml
fi
}
# generate cloudformation template
spiff merge \
$SCRIPT_DIR/templates/infrastructure/boosh.yml \
$SCRIPT_DIR/templates/infrastructure/boosh-internal.yml \
stubs/bosh-init/keypair.yml \
stubs/domain.yml \
stubs/infrastructure/certificates.yml \
$SCRIPT_DIR/templates/infrastructure/meta.yml \
stubs/infrastructure/availability_zones.yml \
<(extra_subnets) \
| boosh generate | jq -c -S . \
> stubs/infrastructure/cloudformation.json
# deploy infrastructure
if [ $UPDATE_OR_CREATE != "skip" ]; then
aws cloudformation $UPDATE_OR_CREATE \
--stack-name $stack_name \
--template-body file://stubs/infrastructure/cloudformation.json
# ensure that create or update was successful to aws
boosh watch --name $stack_name
# generate AWS resources stub for shared purposes
cat > stubs/aws-resources.yml <<EOF
# GENERATED: NO TOUCHING
EOF
boosh resources --name $stack_name >> stubs/aws-resources.yml
fi
cat > deployments/bosh-init/bosh-init.yml <<EOF
# GENERATED: NO TOUCHING
EOF
spiff merge \
$SCRIPT_DIR/templates/bosh-init/bosh-init.yml \
$SCRIPT_DIR/templates/bosh-init/bosh-init-internal.yml \
stubs/aws-resources.yml \
stubs/bosh-init/*.yml \
>> deployments/bosh-init/bosh-init.yml
bosh-init deploy deployments/bosh-init/bosh-init.yml
bosh -n target $(cat stubs/aws-resources.yml | grep BoshInit | awk '{ gsub(/"/, "", $NF); print $NF }')
# generate director uuid stub for template to create deployment stub
cat > stubs/director-uuid.yml <<EOF
# GENERATED: NO TOUCHING
---
director_uuid: $(bosh status --uuid | tr -d '\n')
EOF
# generate stub with deployment base domain
cat > stubs/cf/domain.yml <<EOF
# GENERATED: NO TOUCHING
EOF
spiff merge $SCRIPT_DIR/templates/cf/domain.yml \
$SCRIPT_DIR/templates/cf/domain-internal.yml \
stubs/domain.yml \
>> stubs/cf/domain.yml
# generate deployment stub
cat > stubs/cf/stub.yml <<EOF
# GENERATED: NO TOUCHING
EOF
spiff merge \
$SCRIPT_DIR/templates/cf/stub.yml \
$SCRIPT_DIR/templates/cf/stub-internal.yml \
stubs/aws-resources.yml \
stubs/cf/domain.yml \
stubs/director-uuid.yml \
<(cf_credentials) \
>> stubs/cf/stub.yml
# copy CF property stub if not already present
if [ ! -f stubs/cf/properties.yml ]; then
cp $SCRIPT_DIR/stubs/cf/properties.yml stubs/cf/properties.yml
fi
mkdir -p stubs/diego
mkdir -p stubs/diego-windows
# generate Diego property-override stub with certs
if [ -f stubs/diego/property-overrides.yml ]; then
# update BBS certs and keys in existing property-overrides stub
temp_property_overrides=$(mktemp stubs/diego/property-overrides.yml.XXXXX)
spiff merge \
stubs/diego/property-overrides.yml \
$SCRIPT_DIR/templates/diego/property-overrides-internal.yml \
<(diego_credentials) \
> "${temp_property_overrides}"
mv "${temp_property_overrides}" stubs/diego/property-overrides.yml
else
# create new property-overrides stub with default overrides
spiff merge \
$SCRIPT_DIR/templates/diego/property-overrides.yml \
$SCRIPT_DIR/templates/diego/property-overrides-internal.yml \
<(diego_credentials) \
> stubs/diego/property-overrides.yml
fi
# generate Diego IaaS-settings stub
aws_instance_types=""
if [ -f stubs/aws-instance-types.yml ]; then
aws_instance_types="stubs/aws-instance-types.yml"
fi
spiff merge \
$SCRIPT_DIR/../../manifest-generation/misc-templates/aws-iaas-settings.yml \
$SCRIPT_DIR/templates/diego/iaas-settings-internal.yml \
$aws_instance_types stubs/aws-resources.yml \
> stubs/diego/iaas-settings.yml
# generate Diego Windows IaaS-settings stub
spiff merge \
$SCRIPT_DIR/../../manifest-generation/misc-templates/windows-iaas-settings.yml \
$SCRIPT_DIR/templates/diego-windows/iaas-settings-internal.yml \
$aws_instance_types stubs/aws-resources.yml \
> stubs/diego-windows/iaas-settings.yml
mkdir -p stubs/cf-mysql
# generate CF-MySQL IaaS-settings stub
spiff merge \
$SCRIPT_DIR/templates/mysql/iaas-settings.yml \
$SCRIPT_DIR/templates/mysql/iaas-settings-internal.yml \
stubs/aws-resources.yml \
> stubs/cf-mysql/iaas-settings.yml
# generate datadog firehose nozzle manifest if the user stubs exist
if [ -d stubs/datadog-firehose-nozzle ]; then
spiff merge \
$SCRIPT_DIR/templates/datadog-firehose-nozzle/datadog-firehose-nozzle.yml \
stubs/aws-resources.yml \
stubs/cf/properties.yml \
stubs/cf/domain.yml \
stubs/director-uuid.yml \
stubs/datadog-firehose-nozzle/datadog.yml \
stubs/datadog-firehose-nozzle/property-overrides.yml \
> deployments/datadog-firehose-nozzle.yml
fi
popd