This repository has been archived by the owner on May 15, 2019. It is now read-only.
forked from cloudfoundry/stratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·341 lines (294 loc) · 9.48 KB
/
build.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
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
#!/usr/bin/env bash
set -eu
# Set defaults
PROD_RELEASE=false
DOCKER_REGISTRY=docker.io
DOCKER_ORG=splatform
BASE_IMAGE_TAG=opensuse
OFFICIAL_TAG=cap
TAG=$(date -u +"%Y%m%dT%H%M%SZ")
ADD_OFFICIAL_TAG="false"
TAG_LATEST="false"
STRATOS_BOWER=""
while getopts ":ho:r:t:Tclb:Ou:" opt; do
case $opt in
h)
echo
echo "--- To build images of the Console: "
echo
echo " ./build.sh -t 1.0.13"
echo
exit 0
;;
r)
DOCKER_REGISTRY="${OPTARG}"
;;
o)
DOCKER_ORG="${OPTARG}"
;;
t)
TAG="${OPTARG}"
;;
b)
BASE_IMAGE_TAG="${OPTARG}"
;;
T)
TAG="$(git describe $(git rev-list --tags --max-count=1))"
RELEASE_TAG="$(git describe $(git rev-list --tags --max-count=1))"
;;
c)
CONCOURSE_BUILD="true"
;;
O)
ADD_OFFICIAL_TAG="true"
;;
l)
TAG_LATEST="true"
;;
u)
STRATOS_BOWER="${OPTARG}"
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo
echo "PRODUCTION BUILD/RELEASE: ${PROD_RELEASE}"
echo "REGISTRY: ${DOCKER_REGISTRY}"
echo "ORG: ${DOCKER_ORG}"
echo "TAG: ${TAG}"
echo "BASE_IMAGE_TAG: ${BASE_IMAGE_TAG}"
echo
echo "Starting build"
# Copy values template
__DIRNAME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
STRATOS_UI_PATH=${__DIRNAME}/../../
# Proxy support
BUILD_ARGS=""
RUN_ARGS=""
if [ -n "${http_proxy:-}" -o -n "${HTTP_PROXY:-}" ]; then
BUILD_ARGS="${BUILD_ARGS} --build-arg http_proxy=${http_proxy:-${HTTP_PROXY}}"
RUN_ARGS="${RUN_ARGS} -e http_proxy=${http_proxy:-${HTTP_PROXY}}"
fi
if [ -n "${https_proxy:-}" -o -n "${HTTPS_PROXY:-}" ]; then
BUILD_ARGS="${BUILD_ARGS} --build-arg https_proxy=${https_proxy:-${HTTPS_PROXY}}"
RUN_ARGS="${RUN_ARGS} -e https_proxy=${https_proxy:-${HTTPS_PROXY}}"
fi
# Trim leading/trailing whitespace
BUILD_ARGS="$(echo -e "${BUILD_ARGS}" | sed -r -e 's@^[[:space:]]*@@' -e 's@[[:space:]]*$@@')"
RUN_ARGS="$(echo -e "${RUN_ARGS}" | sed -r -e 's@^[[:space:]]*@@' -e 's@[[:space:]]*$@@')"
if [ -n "${BUILD_ARGS}" ]; then
echo "Web Proxy detected from environment. Running Docker with:"
echo -e "- BUILD_ARGS:\t'${BUILD_ARGS}'"
echo -e "- RUN_ARGS:\t'${RUN_ARGS}'"
fi
function buildAndPublishImage {
NAME=${1}
DOCKER_FILE=${2}
FOLDER=${3}
if [ ! -d "${FOLDER}" ]; then
echo "Project ${FOLDER} hasn't been checked out";
exit 1
fi
# Patch Dockerfile
patchDockerfile ${DOCKER_FILE} ${FOLDER}
IMAGE_URL=${DOCKER_REGISTRY}/${DOCKER_ORG}/${NAME}:${TAG}
echo Building Docker Image for ${NAME}
pushd ${FOLDER} > /dev/null 2>&1
pwd
docker build ${BUILD_ARGS} -t $NAME -f $DOCKER_FILE .
docker tag ${NAME} ${IMAGE_URL}
echo Pushing Docker Image ${IMAGE_URL}
docker push ${IMAGE_URL}
if [ "${TAG_LATEST}" = "true" ]; then
docker tag ${IMAGE_URL} ${DOCKER_REGISTRY}/${DOCKER_ORG}/${NAME}:latest
docker push ${DOCKER_REGISTRY}/${DOCKER_ORG}/${NAME}:latest
fi
unPatchDockerfile ${DOCKER_FILE} ${FOLDER}
popd > /dev/null 2>&1
}
function cleanup {
# Cleanup the SDL/instance defs
echo
echo "-- Cleaning up older values.yaml"
rm -f values.yaml
# Cleanup prior to generating the UI container
echo
echo "-- Cleaning up ${STRATOS_UI_PATH}"
rm -rf ${STRATOS_UI_PATH}/dist
rm -rf ${STRATOS_UI_PATH}/node_modules
rm -rf ${STRATOS_UI_PATH}/bower_components
echo
echo "-- Cleaning up ${STRATOS_UI_PATH}/deploy/containers/nginx/dist"
rm -rf ${STRATOS_UI_PATH}/deploy/containers/nginx/dist
}
function preloadImage {
docker pull ${DOCKER_REGISTRY}/$1
docker tag ${DOCKER_REGISTRY}/$1 $1
}
function updateTagForRelease {
# Reset the TAG variable for a release to be of the form:
# <version>-<commit#>-<prefix><hash>
# where:
# <version> = semantic, in the form major#.minor#.patch#
# <commit#> = number of commits since tag - always 0
# <prefix> = git commit prefix - always 'g'
# <hash> = git commit hash for the current branch
# Reference: See the examples section here -> https://git-scm.com/docs/git-describe
pushd ${STRATOS_UI_PATH} > /dev/null 2>&1
GIT_HASH=$(git rev-parse --short HEAD)
echo "GIT_HASH: ${GIT_HASH}"
TAG="${TAG}-g${GIT_HASH}"
if [ "${ADD_OFFICIAL_TAG}" = "true" ]; then
TAG=${OFFICIAL_TAG}-${TAG}
fi
echo "New TAG: ${TAG}"
popd > /dev/null 2>&1
}
function pushGitTag {
pushd ${1} > /dev/null 2>&1
LOCATION=$(pwd -P)
echo "LOCATION: ${LOCATION}"
# Create a local tag
git tag "${TAG}"
# Push the tag to the shared repo
git push origin "${TAG}"
popd > /dev/null 2>&1
}
function patchDockerfile {
DOCKER_FILE=${1}
FOLDER=${2}
# Replace registry/organization
pushd ${FOLDER} > /dev/null 2>&1
pwd
sed -i "s@splatform@${DOCKER_REGISTRY}/${DOCKER_ORG}@g" ${FOLDER}/${DOCKER_FILE}
sed -i "s/opensuse/${BASE_IMAGE_TAG}/g" ${FOLDER}/${DOCKER_FILE}
popd > /dev/null 2>&1
}
function unPatchDockerfile {
DOCKER_FILE=${1}
FOLDER=${2}
# Replace registry/organization
pushd ${FOLDER} > /dev/null 2>&1
pwd
sed -i "s@${DOCKER_REGISTRY}/${DOCKER_ORG}@splatform@g" ${FOLDER}/${DOCKER_FILE}
sed -i "s/${BASE_IMAGE_TAG}/opensuse/g" ${FOLDER}/${DOCKER_FILE}
popd > /dev/null 2>&1
}
function buildProxy {
# Use the existing build container to compile the proxy executable, and leave
# it on the local filesystem.
echo
echo "-- Building the Console Proxy"
echo
echo "-- Run the build container to build the Console backend"
pushd ${STRATOS_UI_PATH} > /dev/null 2>&1
pushd $(git rev-parse --show-toplevel) > /dev/null 2>&1
docker run -e "APP_VERSION=${TAG}" \
${RUN_ARGS} \
-it \
--rm \
-e USER_NAME=$(id -nu) \
-e USER_ID=$(id -u) \
-e GROUP_ID=$(id -g) \
--name stratos-proxy-builder \
--volume $(pwd):/go/src/github.com/SUSE/stratos-ui \
${DOCKER_REGISTRY}/${DOCKER_ORG}/stratos-proxy-builder:${BASE_IMAGE_TAG}
popd > /dev/null 2>&1
popd > /dev/null 2>&1
# Copy the previously compiled executable into the container and
# publish the container image for the portal proxy
echo
echo "-- Build & publish the runtime container image for the Console Proxy"
buildAndPublishImage stratos-proxy deploy/Dockerfile.bk.k8s ${STRATOS_UI_PATH}
}
function buildPostflightJob {
# Build the postflight container
echo
echo "-- Build & publish the runtime container image for the postflight job"
pushd ${STRATOS_UI_PATH} > /dev/null 2>&1
docker run \
${RUN_ARGS} \
-it \
--rm \
-e USER_NAME=$(id -nu) \
-e USER_ID=$(id -u) \
-e GROUP_ID=$(id -g) \
-e BUILD_DB_MIGRATOR="true" \
--name stratos-proxy-builder \
--volume $(pwd):/go/src/github.com/SUSE/stratos-ui \
${DOCKER_REGISTRY}/${DOCKER_ORG}/stratos-proxy-builder:${BASE_IMAGE_TAG}
buildAndPublishImage stratos-postflight-job deploy/db/Dockerfile.k8s.postflight-job ${STRATOS_UI_PATH}
popd > /dev/null 2>&1
}
function buildMariaDb {
echo
echo "-- Building/publishing MariaDB"
# Download and retag image to save bandwidth
buildAndPublishImage stratos-mariadb Dockerfile.mariadb ${STRATOS_UI_PATH}/deploy/db
}
function buildUI {
# Prepare the nginx server
CURRENT_USER=$
echo
echo "-- Provision the UI"
docker run --rm \
${RUN_ARGS} \
-v ${STRATOS_UI_PATH}:/usr/src/app \
-e CREATE_USER="true" \
-e USER_NAME=$(id -nu) \
-e USER_ID=$(id -u) \
-e GROUP_ID=$(id -g) \
-e STRATOS_BOWER="${STRATOS_BOWER}" \
-w /usr/src/app \
${DOCKER_REGISTRY}/${DOCKER_ORG}/stratos-ui-build-base:${BASE_IMAGE_TAG} \
/bin/bash ./deploy/provision.sh
# Copy the artifacts from the above to the nginx container
echo
echo "-- Copying the Console UI artifacts to the web server (nginx) container"
cp -R ${STRATOS_UI_PATH}/dist ${STRATOS_UI_PATH}/deploy/containers/nginx/dist
# Build and push an image based on the nginx container
echo
echo "-- Building/publishing the runtime container image for the Console web server"
# Download and retag image to save bandwidth
buildAndPublishImage stratos-console Dockerfile.k8s ${STRATOS_UI_PATH}/deploy/containers/nginx
}
# MAIN ------------------------------------------------------
#
# Set the path to the portal proxy
STRATOS_UI_PATH=${STRATOS_UI_PATH}
# cleanup output, intermediate artifacts
cleanup
updateTagForRelease
# Build all of the components that make up the Console
buildProxy
buildPostflightJob
buildMariaDb
buildUI
if [ ${CONCOURSE_BUILD:-"not-set"} == "not-set" ]; then
# Patch Values.yaml file
cp values.yaml.tmpl values.yaml
sed -i -e 's/CONSOLE_VERSION/'"${TAG}"'/g' values.yaml
sed -i -e 's/DOCKER_REGISTRY/'"${DOCKER_REGISTRY}"'/g' values.yaml
sed -i -e 's/DOCKER_ORGANISATION/'"${DOCKER_ORG}"'/g' values.yaml
else
sed -i -e 's/consoleVersion: latest/consoleVersion: '"${TAG}"'/g' console/values.yaml
sed -i -e 's/organization: splatform/organization: '"${DOCKER_ORG}"'/g' console/values.yaml
sed -i -e 's/hostname: docker.io/hostname: '"${DOCKER_REGISTRY}"'/g' console/values.yaml
sed -i -e 's/version: 0.1.0/version: '"${RELEASE_TAG}"'/g' console/Chart.yaml
fi
echo
echo "Build complete...."
echo "Registry: ${DOCKER_REGISTRY}"
echo "Org: ${DOCKER_ORG}"
echo "Tag: ${TAG}"
if [ ${CONCOURSE_BUILD:-"not-set"} == "not-set" ]; then
echo "To deploy using Helm, execute the following: "
echo "helm install console -f values.yaml --namespace console --name my-console"
fi