forked from leaf-ai/studio-go-runner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·360 lines (323 loc) · 15.1 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash -e
set -x pipefail
[ -z "$USER" ] && echo "Error: env variable USER must be set" && exit 1;
[ -z "$GOPATH" ] && echo "Error: env variable GOPATH must be set" && exit 1;
[ -z "$GITHUB_TOKEN" ] && echo "Warning : env variable GITHUB_TOKEN should be set in the event that a release is to be generated" ;
[ -z ${azure_registry_name+x} ] && echo "Warning : env variable azure_registry_name not set";
if [[ ":$PATH:" != *":$GOPATH/bin:"* ]]; then
export PATH=$PATH:$GOPATH/bin
fi
export LOGXI="*=DBG"
export LOGXI_FORMAT="happy,maxcol=1024"
[ -z "$TERM" ] && export TERM=xterm+256color;
if [ -n "$(type -t travis_fold)" ] && [ "$(type -t travis_fold)" = function ]; then
type travis_fold
type travis_nanoseconds
type travis_time_start
type travis_time_finish
:
else
declare -i travis_start_time
declare -i travis_end_time
function travis_nanoseconds () {
local cmd="date";
local format="+%s%N";
local os=$(uname);
if hash gdate > /dev/null 2>&1; then
cmd="gdate";
else
if [[ "$os" = Darwin ]]; then
format="+%s000000000";
fi;
fi;
$cmd -u $format
}
function travis_fold() {
local action=$1;
local name=$2;
echo -en "travis_fold:${action}:${name}\r${ANSI_CLEAR}"
}
function travis_time_start() {
travis_timer_id=$(printf %08x $(( RANDOM * RANDOM )));
travis_start_time=$(travis_nanoseconds);
echo -en "travis_time:start:$travis_timer_id\r${ANSI_CLEAR}"
}
function travis_time_finish() {
local result=$?;
travis_end_time=$(travis_nanoseconds);
local duration=$(($travis_end_time-$travis_start_time));
echo -en "\ntravis_time:end:$travis_timer_id:start=$travis_start_time,finish=$travis_end_time,duration=$duration\r${ANSI_CLEAR}";
return $result
}
fi
go get github.com/karlmutch/petname
#go install github.com/ekalinin/github-markdown-toc.go
go install github.com/sigstore/cosign/cmd/[email protected]
# Get the documentation files with tables of contents
declare -a tocs=("README.md" "docs/azure.md" "docs/interface.md" "docs/ci.md" "docs/message_privacy.md" "examples/docker/README.md" "examples/local/README.md" "docs/queuing.md" "docs/workstation_k8s.md" "docs/app-note/model-serving.md")
md_temp=$(mktemp -d)
for fn in "${tocs[@]}"
do
set +x
if [ -z "$GITHUB_TOKEN" ]
then
github-markdown-toc.go $fn --hide-footer > $md_temp/header.md
else
github-markdown-toc.go $fn --hide-footer --token=$GITHUB_TOKEN > $md_temp/header.md
fi
set -x
awk -v data="$(<$md_temp/header.md)" '
BEGIN {p=1}
/^<!--ts-->/ {print;print data;p=0}
/^<!--te-->/ {p=1}
p' $fn > $md_temp/document.md
cp $md_temp/document.md $fn
rm $md_temp/document.md
rm $md_temp/header.md
done
#go get -u github.com/gomarkdown/mdtohtml
#mdtohtml README.md $md_temp/README.html
#awk -v data="$(<$md_temp/README.html)" '/<!--bs-->/ {f=1} /<!--be-->/ && f {print data; f=0}1' docs/assets/README.tmpl > README.html
#rm $md_temp/README.html
rmdir $md_temp
bash -c "while true; do echo \$(date) - building ...; sleep 180s; done" &
PING_LOOP_PID=$!
function cleanup {
# nicely terminate the ping output loop
kill $PING_LOOP_PID
}
trap cleanup EXIT
function ExitWithError
{
echo "$*">&2
rm -f $working_file
exit 1
}
function Tidyup
{
ExitWithError "Abort"
}
umask 077
working_file=$$.studio-go-runner-working
rm -f $working_file
trap Tidyup 1 2 3 15
export SEMVER=`semver`
export GIT_BRANCH=`echo '{{.duat.gitBranch}}'|stencil -supress-warnings - | tr '_' '-' | tr '\/' '-'`
GIT_COMMIT=`git rev-parse HEAD`
export RUNNER_BUILD_LOG=build-$GIT_BRANCH.log
exit_code=0
export BaseRepoVersion=`grep registry.version Dockerfile_base | cut -d= -f2 | cut -d\ -f1`
export StackRepoVersion=`grep registry.version Dockerfile_stack | cut -d= -f2 | cut -d\ -f1`
# See if the reference build base images exist
set +e
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect quay.io/leafai/studio-go-runner-dev-base:$BaseRepoVersion > /dev/null
exit_code=$?
set -e
if [ $exit_code -ne 0 ]; then
# See if we have the base build image locally
set +e
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect leafai/studio-go-runner-dev-base:$BaseRepoVersion > /dev/null
exit_code=$?
set -e
if [ $exit_code -eq 0 ]; then
docker tag leafai/studio-go-runner-dev-base:$BaseRepoVersion quay.io/leafai/studio-go-runner-dev-base:$BaseRepoVersion
docker push quay.io/leafai/studio-go-runner-dev-base:$BaseRepoVersion
else
# Build the base image that other images will derive from for development style images
docker build -t studio-go-runner-dev-base:working -f Dockerfile_base .
RepoImage=`docker inspect studio-go-runner-dev-base:working --format '{{ index .Config.Labels "registry.repo" }}:{{ index .Config.Labels "registry.version"}}'`
RepoBaseImage=`docker inspect studio-go-runner-dev-base:working --format '{{ index .Config.Labels "registry.base" }}:{{ index .Config.Labels "registry.version"}}'`
docker tag studio-go-runner-dev-base:working $RepoImage
docker rmi studio-go-runner-dev-base:working
docker tag $RepoImage quay.io/leafai/$RepoBaseImage
docker push quay.io/leafai/$RepoBaseImage
fi
fi
# See if the reference build base images exist
set +e
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect quay.io/leafai/studio-go-runner-dev-stack:$StackRepoVersion > /dev/null
exit_code=$?
set -e
if [ $exit_code -ne 0 ]; then
# See if we have the stack build image locally
set +e
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect leafai/studio-go-runner-dev-stack:$StackRepoVersion > /dev/null
exit_code=$?
set -e
if [ $exit_code -eq 0 ]; then
docker tag leafai/studio-go-runner-dev-stack:$StackRepoVersion quay.io/leafai/studio-go-runner-dev-stack:$StackRepoVersion
docker push quay.io/leafai/studio-go-runner-dev-stack:$StackRepoVersion
else
# Build the stack image that other images will derive from for development style images
docker build -t studio-go-runner-dev-stack:working -f Dockerfile_stack .
RepoImage=`docker inspect studio-go-runner-dev-stack:working --format '{{ index .Config.Labels "registry.repo" }}:{{ index .Config.Labels "registry.version"}}'`
RepoBaseImage=`docker inspect studio-go-runner-dev-stack:working --format '{{ index .Config.Labels "registry.base" }}:{{ index .Config.Labels "registry.version"}}'`
docker tag studio-go-runner-dev-stack:working $RepoImage
docker rmi studio-go-runner-dev-stack:working
docker tag $RepoImage quay.io/leafai/$RepoBaseImage
docker push quay.io/leafai/$RepoBaseImage
fi
fi
travis_fold start "build.image"
travis_time_start
# The workstation version uses the linux user ID of the builder to enable sharing of files between the
# build container and the local file system of the user
stencil -input Dockerfile_developer | docker build -t leafai/studio-go-runner-developer-build:$GIT_BRANCH -
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
# Information about safely working with temporary files in shell scripts can be found at
# https://dev.to/philgibbs/avoiding-temporary-files-in-shell-scripts
{
stencil -input Dockerfile_standalone > $working_file
[[ $? != 0 ]] && ExitWithError "stencil processing of Dockerfile_standalone failed"
} | tee $working_file > /dev/null
[[ $? != 0 ]] && ExitWithError "Error writing to $working_file"
docker build -t leafai/studio-go-runner-standalone-build:$GIT_BRANCH -f $working_file .
rm -f $working_file
docker tag leafai/studio-go-runner-standalone-build:$GIT_BRANCH leafai/studio-go-runner-standalone-build
docker tag leafai/studio-go-runner-standalone-build:$GIT_BRANCH localhost:32000/leafai/studio-go-runner-standalone-build:latest
docker tag leafai/studio-go-runner-standalone-build:$GIT_BRANCH localhost:32000/leafai/studio-go-runner-standalone-build:$GIT_BRANCH
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
travis_time_finish
travis_fold end "build.image"
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
# Running build.go inside of a container will result in a compilation, light testing, and release however no docker images
travis_fold start "build"
travis_time_start
container_name=`petname`
# Dont release until after we check is microk8s is available for downstream testing
# The following is using the image built from Dockerfile_developer
docker run --name $container_name --user $(id -u):$(id -g) -e AMQP_URL="$AMQP_URL" -e DEBUG="$DEBUG" -e TERM="$TERM" -e LOGXI="$LOGXI" -e LOGXI_FORMAT="$LOGXI_FORMAT" -v $GOPATH:/project leafai/studio-go-runner-developer-build:$GIT_BRANCH
exit_code=`docker inspect $container_name --format='{{.State.ExitCode}}'`
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
travis_time_finish
travis_fold end "build"
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
# Automatically produces images without compilation, or releases when run outside of a container
if docker image ls 2>/dev/null 1>/dev/null; then
travis_fold start "image.build"
travis_time_start
cd cmd/runner && docker build -f Dockerfile.stock -t leafai/studio-go-runner:$SEMVER . ; cd ../..
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
trivy --severity HIGH --ignore-unfixed --exit-code -1 leafai/studio-go-runner:$SEMVER
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
trivy --severity CRITICAL --exit-code -1 leafai/studio-go-runner:$SEMVER
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
if az account list -otsv --all 2>/dev/null 1>/dev/null; then
cd cmd/runner && docker build -f Dockerfile.azure -t leafai/azure-studio-go-runner:$SEMVER . ; cd ../..
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
trivy --severity HIGH --ignore-unfixed --exit-code -1 leafai/azure-studio-go-runner:$SEMVER
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
trivy --severity CRITICAL --exit-code -1 leafai/azure-studio-go-runner:$SEMVER
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
fi
cd tools/serving-bridge && docker build -f Dockerfile -t leafai/studio-serving-bridge:$SEMVER . ; cd ../..
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
trivy --severity HIGH --ignore-unfixed --exit-code -1 leafai/studio-serving-bridge:$SEMVER
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
trivy --severity CRITICAL --exit-code -1 leafai/studio-serving-bridge:$SEMVER
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
travis_time_finish
travis_fold end "image.build"
fi
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
# In the event that the following command was successful then we know a microk8s registry is present
# and we can defer any releases to the pipeline it is using rather than releasing from out
# current pipeline process
travis_fold start "image.ci_start"
travis_time_start
RegistryIP=`kubectl --namespace container-registry get pod --selector=app=registry -o jsonpath="{.items[*].status.hostIP}"||true`
if [ $exit_code -eq 0 ]; then
if [[ ! -z "$RegistryIP" ]]; then
docker tag localhost:32000/leafai/studio-go-runner-standalone-build:$GIT_BRANCH \
$RegistryIP:32000/leafai/studio-go-runner-standalone-build:$GIT_BRANCH|| true
docker push $RegistryIP:32000/leafai/studio-go-runner-standalone-build:$GIT_BRANCH || true
docker tag localhost:32000/leafai/studio-go-runner-standalone-build:$GIT_BRANCH \
$RegistryIP:32000/leafai/studio-go-runner-standalone-build:latest|| true
docker push $RegistryIP:32000/leafai/studio-go-runner-standalone-build:latest || true
if [ $exit_code -eq 0 ]; then
exit $exit_code
fi
fi
fi
travis_time_finish
travis_fold end "image.ci_start"
travis_fold start "image.build"
travis_time_start
container_name=`petname`
docker run --name $container_name --user $(id -u):$(id -g) -e "RELEASE_ONLY"="true" -e DEBUG="$DEBUG" -e TERM="$TERM" -e LOGXI="$LOGXI" -e LOGXI_FORMAT="$LOGXI_FORMAT" -e GITHUB_TOKEN=$GITHUB_TOKEN -v $GOPATH:/project leafai/studio-go-runner-developer-build:$GIT_BRANCH
exit_code=`docker inspect $container_name --format='{{.State.ExitCode}}'`
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
travis_fold end "image.build"
travis_fold start "image.sign"
#if docker image inspect leafai/studio-go-runner:$SEMVER 2>/dev/null 1>/dev/null; then
#echo -n "secret" | cosign sign -key ~/.ssh/cosign.key leafai/studio-go-runner:$SEMVER
#echo -n "secret" | cosign sign -key ~/.ssh/cosign.key leafai/azure-studio-go-runner:$SEMVER
#echo -n "secret" | cosign sign -key ~/.ssh/cosign.key leafai/studio-serving-bridge:$SEMVER
#fi
travis_fold end "image.sign"
travis_fold end "image.push"
if docker image inspect leafai/studio-go-runner:$SEMVER 2>/dev/null 1>/dev/null; then
if type docker 2>/dev/null ; then
dockerLines=`docker system info 2>/dev/null | egrep "Registry: .*index.docker.io.*|User" | wc -l`
if [ $dockerLines -eq 2 ]; then
docker push leafai/studio-go-runner:$SEMVER
docker push leafai/azure-studio-go-runner:$SEMVER
docker push leafai/studio-serving-bridge:$SEMVER
fi
docker tag leafai/studio-go-runner:$SEMVER quay.io/leafai/studio-go-runner:$SEMVER
docker tag leafai/azure-studio-go-runner:$SEMVER quay.io/leafai/azure-studio-go-runner:$SEMVER
docker tag leafai/studio-serving-bridge:$SEMVER quay.io/leafai/studio-serving-bridge:$SEMVER
# There is simply no reliable way to know if a docker login has been done unless, for example
# config.json is not placed into your login directory, snap redirects etc so try and simply
# silently fail.
docker push quay.io/leafai/studio-go-runner:$SEMVER || true
docker push quay.io/leafai/azure-studio-go-runner:$SEMVER || true
docker push quay.io/leafai/studio-serving-bridge:$SEMVER || true
fi
fi
travis_time_finish
travis_fold end "image.push"
exit 0