-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild-tile
executable file
·400 lines (339 loc) · 12.1 KB
/
build-tile
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env bash
set -eu
readonly TILE_TYPE_EXAMPLE_PRODUCT="example-product"
readonly TILE_TYPE_RUNTIME_CONFIG_ONLY="runtime-config-only"
readonly TILE_TYPE_SERVICE_BROKER="service-broker"
readonly TILE_TYPE_WINDOWS_EXAMPLE_PRODUCT="windows"
readonly ALLOWED_TILE_TYPES="${TILE_TYPE_EXAMPLE_PRODUCT} ${TILE_TYPE_SERVICE_BROKER} ${TILE_TYPE_RUNTIME_CONFIG_ONLY} ${TILE_TYPE_WINDOWS_EXAMPLE_PRODUCT}"
readonly TILE_TYPES_REQUIRING_STEMCELL="${TILE_TYPE_EXAMPLE_PRODUCT} ${TILE_TYPE_SERVICE_BROKER} ${TILE_TYPE_WINDOWS_EXAMPLE_PRODUCT}"
readonly DEFAULT_TILE_TYPE="${TILE_TYPE_EXAMPLE_PRODUCT}"
readonly DEFAULT_OUTPUT_DIR="/tmp"
readonly DEFAULT_TILE_VERSION="1.0-build.0"
readonly DEFAULT_FILE_EXTENSION="pivotal"
readonly DEFAULT_VERBOSE=""
readonly DEFAULT_WITH_OS_CONF_COMPILED_RELEASE=false
readonly DEFAULT_WITH_BPM_COMPILED_RELEASE=false
DEFAULT_STEMCELL_OS="ubuntu-xenial"
DEFAULT_STEMCELL_VERSION="170.16"
TMP_DIR="$(mktemp -d)"
CACHE_DIR="/tmp/build-tile"
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
PARENT_DIR="$(dirname "$SCRIPT_DIR")"
OUTPUT_DIR="${DEFAULT_OUTPUT_DIR}"
default_ops_manager_repo_path="${PARENT_DIR}/$(ls "${PARENT_DIR}" | grep -E '^(installation|ops-manager)$' | head -n1)"
: "${OPS_MANAGER_REPO_PATH:=${default_ops_manager_repo_path}}"
SUFFIX=""
PRODUCT_NAME=""
COMPILED_RELEASE_BUCKET="ops-manager-compiled-releases"
BPM_VERSION=$(bosh interpolate "${OPS_MANAGER_REPO_PATH}/web/config/auto-versions.yml" --path /bpm_release/version)
OS_CONF_VERSION=$(bosh interpolate "${OPS_MANAGER_REPO_PATH}/web/config/auto-versions.yml" --path /os_conf_release/version)
TILE_TYPE="${DEFAULT_TILE_TYPE}"
FILE_EXTENSION="${DEFAULT_FILE_EXTENSION}"
STEMCELL_OS="${DEFAULT_STEMCELL_OS}"
STEMCELL_VERSION="${DEFAULT_STEMCELL_VERSION}"
TILE_VERSION="${DEFAULT_TILE_VERSION}"
PREVIOUS_TILE_VERSION=""
VERBOSE="${DEFAULT_VERBOSE}"
WITH_OS_CONF_COMPILED_RELEASE=${DEFAULT_WITH_OS_CONF_COMPILED_RELEASE}
WITH_BPM_COMPILED_RELEASE=${DEFAULT_WITH_BPM_COMPILED_RELEASE}
log() {
if [[ -n ${VERBOSE} ]]; then
echo "$1"
fi
}
setup_tmp_workspace() {
mkdir -p "${CACHE_DIR}"
mkdir -p "${TMP_DIR}/"{metadata,migrations,migrations/v1,releases}
}
get_tmp_releases(){
basename "$(get_tmp_releases_path)"/*.tgz
}
get_tmp_metadata_path() {
echo "${TMP_DIR}/metadata/${PRODUCT_NAME}.yml"
}
get_tmp_releases_path() {
echo "${TMP_DIR}/releases"
}
get_tmp_migrations_path() {
echo "${TMP_DIR}/migrations"
}
get_tile_destination_path() {
local suffix
suffix=""
if [ -n "${SUFFIX}" ]; then
suffix="-${SUFFIX}"
fi
echo "${OUTPUT_DIR}/${PRODUCT_NAME}${suffix}-${TILE_VERSION}.${FILE_EXTENSION}"
}
set_default_stemcell_os() {
local os
os=$(ruby -r "yaml" -e "puts YAML.load_file('${OPS_MANAGER_REPO_PATH}/web/config/auto-versions.yml')['opsman_stemcell']['os'] rescue ''")
if [[ -z ${os} ]]; then
log "Could not find a default stemcell OS at ${OPS_MANAGER_REPO_PATH}. Try setting OPS_MANAGER_REPO_PATH. Using default ${DEFAULT_STEMCELL_OS}"
else
DEFAULT_STEMCELL_OS="${os}"
STEMCELL_OS=$DEFAULT_STEMCELL_OS
fi
}
set_default_stemcell_version() {
local version
version=$(ruby -r "yaml" -e "puts YAML.load_file('${OPS_MANAGER_REPO_PATH}/web/config/auto-versions.yml')['opsman_stemcell']['version'] rescue ''")
if [[ -z $version ]]; then
log "Could not find a default stemcell version at ${OPS_MANAGER_REPO_PATH}. Try setting OPS_MANAGER_REPO_PATH. Using default ${DEFAULT_STEMCELL_VERSION}"
else
DEFAULT_STEMCELL_VERSION="${version}"
STEMCELL_VERSION=$DEFAULT_STEMCELL_VERSION
fi
}
parse_inputs() {
for i in "$@"
do
case $i in
--help|-h)
print_help
exit
;;
--stemcell-os=*)
STEMCELL_OS="${i#*=}"
shift
;;
--stemcell-version=*)
STEMCELL_VERSION="${i#*=}"
;;
--type=*)
TILE_TYPE="${i#*=}"
shift
;;
--output-dir=*)
OUTPUT_DIR="${i#*=}"
shift
;;
--tile-version=*)
TILE_VERSION="${i#*=}"
shift
;;
--previous-tile-version=*)
PREVIOUS_TILE_VERSION="${i#*=}"
shift
;;
--file-extension=*)
FILE_EXTENSION="${i#*=}"
shift
;;
--suffix=*)
SUFFIX="${i#*=}"
shift
;;
--verbose|-v)
VERBOSE="true"
shift
;;
--with-os-conf-compiled-release)
WITH_OS_CONF_COMPILED_RELEASE=true
shift
;;
--with-bpm-compiled-release)
WITH_BPM_COMPILED_RELEASE=true
shift
;;
*)
;;
esac
done
}
validate_inputs() {
local error
error=""
if [ -z "${TILE_TYPE}" ]; then
echo "Error: --type is required. Allowed types are: ${ALLOWED_TILE_TYPES}"
error="true"
else
if [[ ! "${ALLOWED_TILE_TYPES}" == *"${TILE_TYPE}"* ]]; then
echo "Error: --type=${TILE_TYPE} is invalid. Allowed types are: ${ALLOWED_TILE_TYPES}"
error="true"
fi
fi
if [ -z "${STEMCELL_OS}" ] && [[ "${TILE_TYPES_REQUIRING_STEMCELL}" == *"${TILE_TYPE}"* ]]; then
echo "Error: --stemcell-os is required"
error="true"
fi
if [ -z "${STEMCELL_VERSION}" ] && [[ "${TILE_TYPES_REQUIRING_STEMCELL}" == *"${TILE_TYPE}"* ]]; then
echo "Error: --stemcell-version is required"
error="true"
fi
if [ -n "${error}" ]; then
echo
print_help
exit 1
fi
}
check_metadata_syntax() {
log "checking metadata syntax in '$(get_tmp_metadata_path)'..."
if ! ruby -r yaml -e "YAML.load_file('$(get_tmp_metadata_path)')" >&2; then
printf "\n\e[0;1;31mError: invalid YAML syntax in metadata at %s\e[0m\n" "$(get_tmp_metadata_path)" >&2
exit 1;
fi
}
package_pivotal_file() {
pushd "${TMP_DIR}" > /dev/null
log "zipping tile to $(get_tile_destination_path)..."
zip -qr "$(get_tile_destination_path)" "migrations" "metadata" "releases"
popd > /dev/null
}
build_product_metadata() {
log "building ${PRODUCT_NAME} metadata..."
"${SCRIPT_DIR}/generate-metadata" \
"${TILE_VERSION}" \
"${STEMCELL_VERSION}" \
"$(get_tmp_releases)" \
"${PRODUCT_NAME}" \
"${SUFFIX}" \
"${STEMCELL_OS}" \
"${BPM_VERSION}" \
"${OS_CONF_VERSION}" \
> "$(get_tmp_metadata_path)"
}
build_example_product_release() {
log "building example product release..."
"${SCRIPT_DIR}/example-release/build-release-tarball" "$(get_tmp_releases_path)" > /dev/null
}
build_service_broker_migrations() {
log "building ${PRODUCT_NAME} migrations..."
git clean -Xf "${SCRIPT_DIR}/example-broker-product/migrations" > /dev/null
cp -r "${SCRIPT_DIR}"/example-broker-product/migrations/* "$(get_tmp_migrations_path)"
}
copy_runtime_config_only_release() {
log "copying dummy release(s) to temporary dir..."
cp "${SCRIPT_DIR}"/example-runtime-config-product/releases/*.tgz "$(get_tmp_releases_path)"
}
build_example_product_migrations() {
log "building ${PRODUCT_NAME} migrations..."
git clean -Xf "${SCRIPT_DIR}/example-product/migrations" > /dev/null
"${SCRIPT_DIR}/example-product/generate-migrations" "${TILE_VERSION}" "${PREVIOUS_TILE_VERSION}" "${STEMCELL_OS}" > /dev/null
cp -r "${SCRIPT_DIR}"/example-product/migrations/* "$(get_tmp_migrations_path)"
}
build_example_runtime_product_migrations() {
log "building ${PRODUCT_NAME} migrations..."
git clean -Xf "${SCRIPT_DIR}/example-runtime-config-product/migrations" > /dev/null
cp -r "${SCRIPT_DIR}"/example-runtime-config-product/migrations/* "$(get_tmp_migrations_path)"
}
fetch_example_broker_product_dependency_releases() {
log "fetching ${PRODUCT_NAME} dependency releases..."
fetch_bosh_release "bpm" "${BPM_VERSION}"
}
fetch_bosh_release() {
local release_name
local release_version
release_name="$1"
release_version="$2"
if [ ! -f "${CACHE_DIR}/${release_name}-${release_version}" ]; then
curl -fLo "${CACHE_DIR}/${release_name}-${release_version}" "https://bosh.io/d/github.com/cloudfoundry/${release_name}-release?v=${release_version}"
fi
cp "${CACHE_DIR}/${release_name}-${release_version}" "$(get_tmp_releases_path)/${release_name}"
}
fetch_bosh_compiled_release() {
local release_name
local release_version
release_name="$1"
release_version="$2"
if [ ! -f "${CACHE_DIR}/${release_name}-${release_version}" ]; then
echo "aws s3api get-object '${COMPILED_RELEASE_BUCKET}/${release_name}-${release_version}-${STEMCELL_OS}-${STEMCELL_VERSION}.tgz'"
aws --no-sign-request s3api get-object --bucket ${COMPILED_RELEASE_BUCKET} --key "${release_name}-${release_version}-${STEMCELL_OS}-${STEMCELL_VERSION}.tgz" "${CACHE_DIR}/${release_name}-${release_version}" > aws_download_result.txt
fi
cp "${CACHE_DIR}/${release_name}-${release_version}" "$(get_tmp_releases_path)/${release_name}"
}
fetch_example_product_dependency_releases() {
log "fetching ${PRODUCT_NAME} dependency releases..."
if ${WITH_OS_CONF_COMPILED_RELEASE} ; then
fetch_bosh_compiled_release "os-conf" "${OS_CONF_VERSION}"
else
fetch_bosh_release "os-conf" "${OS_CONF_VERSION}"
fi
if ${WITH_BPM_COMPILED_RELEASE} ; then
fetch_bosh_compiled_release "bpm" "${BPM_VERSION}"
else
fetch_bosh_release "bpm" "${BPM_VERSION}"
fi
}
fetch_windows_example_product_dependency_releases() {
log "fetching ${PRODUCT_NAME} dependency releases..."
wget -qO "$(get_tmp_releases_path)/windows-utilities-0.12.0.tgz" https://github.com/cloudfoundry-incubator/windows-utilities-release/releases/download/0.12.0/windows-utilities-0.12.0.tgz
wget -qO "$(get_tmp_releases_path)/sample-windows-bosh-release-0.0.2.tgz" https://github.com/madamkiwi/sample-windows-bosh-release/releases/download/0.0.2/sample-windows-bosh-release-0.0.2.tgz
}
##################### Main #####################
print_help() {
cat <<HERE >&2
Usage: ${0} <--flag=value>
Application Options (Optional):
--type Type of tile to build. Available options: [${ALLOWED_TILE_TYPES}]. Default: ${DEFAULT_TILE_TYPE}
--stemcell-os Stemcell operating system. Default: ${DEFAULT_STEMCELL_OS}
--stemcell-version Stemcell version. Default: ${DEFAULT_STEMCELL_VERSION}
--tile-version Version of tile. Default: ${DEFAULT_TILE_VERSION}
--output-dir Destination directory of tile. Default: ${DEFAULT_OUTPUT_DIR}
--file-extension File extension to attach to tile. Default: ${DEFAULT_FILE_EXTENSION}
--suffix Suffix to attach to file name. Default: ""
Help Options (do not pass a value):
-h, --help Show this help message
-v, --verbose Print some extra logging messages
Example Commands:
Example Product $ ./build-tile
Service Broker $ ./build-tile --type=service-broker
Runtime Config $ ./build-tile --type=runtime-config-only
Windows $ ./build-tile --type=windows --stemcell-os=windows2016 --stemcell-version=1709.17
HERE
}
print_success() {
printf "\e[0;1;32mFinished:\e[0m %s\n" "$(get_tile_destination_path)" >&2
}
main() {
cd "${SCRIPT_DIR}"
set_default_stemcell_os
set_default_stemcell_version
parse_inputs "$@"
validate_inputs
setup_tmp_workspace
if [ "${TILE_TYPE}" = "${TILE_TYPE_SERVICE_BROKER}" ]; then
PRODUCT_NAME="example-broker-product"
fetch_example_broker_product_dependency_releases
build_example_product_release
build_service_broker_migrations
build_product_metadata
check_metadata_syntax
package_pivotal_file
print_success
exit 0
fi
if [ "${TILE_TYPE}" = "${TILE_TYPE_RUNTIME_CONFIG_ONLY}" ]; then
PRODUCT_NAME="example-runtime-config-product"
copy_runtime_config_only_release
build_example_runtime_product_migrations
build_product_metadata
check_metadata_syntax
package_pivotal_file
print_success
exit 0
fi
if [ "${TILE_TYPE}" = "${TILE_TYPE_EXAMPLE_PRODUCT}" ]; then
PRODUCT_NAME="example-product"
build_example_product_release
fetch_example_product_dependency_releases
build_example_product_migrations
build_product_metadata
check_metadata_syntax
package_pivotal_file
print_success
exit 0
fi
if [ "${TILE_TYPE}" = "${TILE_TYPE_WINDOWS_EXAMPLE_PRODUCT}" ]; then
PRODUCT_NAME="example-windows-product"
fetch_windows_example_product_dependency_releases
build_product_metadata
check_metadata_syntax
package_pivotal_file
print_success
exit 0
fi
}
# shellcheck disable=SC2064
trap "rm -rf ${TMP_DIR}" EXIT
main $@