-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
builder.sh
executable file
·978 lines (843 loc) · 29.1 KB
/
builder.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
#!/usr/bin/env bashio
######################
# Home Assistant Build-env
######################
set -e
set +u
#### Variable ####
DOCKER_TIMEOUT=20
DOCKER_PID=-1
DOCKER_HUB=
DOCKER_HUB_CHECK=false
DOCKER_CACHE=true
DOCKER_LATEST=true
DOCKER_PUSH=true
DOCKER_USER=
DOCKER_PASSWORD=
DOCKER_LOCAL=false
SELF_CACHE=false
CUSTOM_CACHE_TAG=
RELEASE_TAG=false
GIT_REPOSITORY=
GIT_BRANCH="master"
TARGET=
VERSION=
VERSION_BASE=
VERSION_FROM=
IMAGE=
RELEASE=
ADDITIONAL_TAGS=()
BUILD_LIST=()
BUILD_TYPE="addon"
BUILD_TASKS=()
BUILD_ERROR=()
declare -A BUILD_MACHINE=(
[generic-x86-64]="amd64" \
[intel-nuc]="amd64" \
[khadas-vim3]="aarch64" \
[odroid-c2]="aarch64" \
[odroid-c4]="aarch64" \
[odroid-n2]="aarch64" \
[odroid-xu]="armv7" \
[qemuarm]="armhf" \
[qemuarm-64]="aarch64" \
[qemux86]="i386" \
[qemux86-64]="amd64" \
[raspberrypi]="armhf" \
[raspberrypi2]="armv7" \
[raspberrypi3]="armv7" \
[raspberrypi3-64]="aarch64" \
[raspberrypi4]="armv7" \
[raspberrypi4-64]="aarch64" \
[tinker]="armv7" )
#### Misc functions ####
function print_help() {
cat << EOF
Hass.io build-env for ecosystem:
docker run --rm homeassistant/{arch}-builder:latest [options]
Options:
-h, --help
Display this help and exit.
Repository / Data
-r, --repository <REPOSITORY>
Set git repository to load data from.
-b, --branch <BRANCH>
Set git branch for repository.
-t, --target <PATH_TO_BUILD>
Set local folder or path inside repository for build.
Version/Image handling
-v, --version <VERSION>
Overwrite version/tag of build.
-i, --image <IMAGE_NAME>
Overwrite image name of build / support {arch}.
--release <VERSION>
Additional version information like for base images.
--release-tag
Use this as main tag.
--additional-tag
Add additional tags that will be published
--version-from <VERSION>
Use this to set build_from tag if not specified.
Architecture
--armhf
Build for arm v6.
--armv7
Build for arm v7.
--amd64
Build for intel/amd 64bit.
--aarch64
Build for arm 64bit.
--i386
Build for intel/amd 32bit.
--all
Build all architecture.
Build handling
--test
Disable push to dockerhub.
--no-latest
Do not tag images as latest.
--no-cache
Disable cache for the build (from latest).
--self-cache
Use same tag as cache tag instead latest.
--cache-tag <TAG>
Use a custom tag for the build cache.
-d, --docker-hub <DOCKER_REPOSITORY>
Set or overwrite the docker repository.
--docker-hub-check
Check if the version already exists before starting the build.
--docker-user <USER>
Username to login into docker with
--docker-password <PASSWORD>
Password to login into docker with
Use the host docker socket if mapped into container:
/var/run/docker.sock
Internals:
--addon
Default on. Run all things for an addon build.
--generic <VERSION>
Build based on the build.json
--base <VERSION>
Build our base images.
--machine <VERSION=ALL,X,Y>
Build the machine based image for a release/landingpage.
Security:
Enable signing images with Codenotary. Need set follow env:
- CAS_API_KEY
EOF
bashio::exit.nok
}
#### Docker functions ####
function start_docker() {
local starttime
local endtime
if [ -S "/var/run/docker.sock" ]; then
bashio::log.info "Using host docker setup with '/var/run/docker.sock'"
DOCKER_LOCAL="true"
return 0
fi
bashio::log.info "Starting docker."
dockerd 2> /dev/null &
DOCKER_PID=$!
bashio::log.info "Waiting for docker to initialize..."
starttime="$(date +%s)"
endtime="$(date +%s)"
until docker info >/dev/null 2>&1; do
if [ $((endtime - starttime)) -le $DOCKER_TIMEOUT ]; then
sleep 1
endtime=$(date +%s)
else
bashio::exit.nok "Timeout while waiting for docker to come up"
fi
done
bashio::log.info "Docker was initialized"
}
function stop_docker() {
local starttime
local endtime
if [ "$DOCKER_LOCAL" == "true" ]; then
return 0
fi
bashio::log.info "Stopping in container docker..."
if [ "$DOCKER_PID" -gt 0 ] && kill -0 "$DOCKER_PID" 2> /dev/null; then
starttime="$(date +%s)"
endtime="$(date +%s)"
# Now wait for it to die
kill "$DOCKER_PID"
while kill -0 "$DOCKER_PID" 2> /dev/null; do
if [ $((endtime - starttime)) -le $DOCKER_TIMEOUT ]; then
sleep 1
endtime=$(date +%s)
else
bashio::exit.nok "Timeout while waiting for container docker to die"
fi
done
else
bashio::log.warning "Your host might have been left with unreleased resources"
fi
}
function run_build() {
local build_dir=$1
local repository=$2
local image=$3
local version=$4
local build_from=$5
local build_arch=$6
local docker_cli=("${!7}")
local docker_tags=("${!8}")
local shadow_repository=${9}
local codenotary_base=${10}
local codenotary_sign=${11}
local push_images=()
local cache_tag="latest"
local metadata
local release="${version}"
# Overwrites
if bashio::var.has_value "${DOCKER_HUB}"; then repository="${DOCKER_HUB@L}"; fi
if bashio::var.has_value "${IMAGE}"; then image="${IMAGE@L}"; fi
if bashio::var.has_value "${RELEASE}"; then release="${RELEASE}"; fi
# Replace {arch} with build arch for image
# shellcheck disable=SC1117
image="$(echo "${image}" | sed -r "s/\{arch\}/${build_arch}/g")"
# Check if image exists on docker hub
if bashio::var.true "$DOCKER_HUB_CHECK"; then
metadata="$(curl -s "https://hub.docker.com/v2/repositories/${repository}/${image}/tags/${version}/")"
if bashio::var.has_value "${metadata}" && [[ "$(echo "${metadata}" | jq --raw-output '.name')" == "${version}" ]]; then
bashio::log.info "Skip build, found ${image}:${version} on dockerhub"
return 0
else
bashio::log.info "Start build, ${image}:${version} is not on dockerhub"
fi
fi
# Init Cache
if bashio::var.true "${DOCKER_CACHE}"; then
if bashio::var.has_value "${CUSTOM_CACHE_TAG}"; then
cache_tag="${CUSTOM_CACHE_TAG}"
elif bashio::var.true "${SELF_CACHE}"; then
cache_tag="${version}"
fi
bashio::log.info "Init cache for ${repository}/${image}:${version} with tag ${cache_tag}"
if docker pull "${repository}/${image}:${cache_tag}" > /dev/null 2>&1 && codenotary_validate "${codenotary_sign}" "${repository}/${image}:${cache_tag}" "false"; then
docker_cli+=("--cache-from" "${repository}/${image}:${cache_tag}")
else
docker_cli+=("--no-cache")
bashio::log.warning "No cache image found. Disabling cache for this build."
fi
else
docker_cli+=("--no-cache")
fi
# Set Labels
docker_cli+=("--label" "io.hass.arch=${build_arch}")
docker_cli+=("--label" "org.opencontainers.image.created=$(date --rfc-3339=seconds --utc)")
docker_cli+=("--label" "io.hass.version=${release}")
docker_cli+=("--label" "org.opencontainers.image.version=${release}")
# Validate the base image
if ! codenotary_validate "${codenotary_base}" "${build_from}" "true"; then
bashio::exit.nok "Invalid base image ${build_from}"
fi
# Build image
bashio::log.info "Run build for ${repository}/${image}:${version}"
docker build --pull -t "${repository}/${image}:${version}" \
--build-arg "BUILD_FROM=${build_from}" \
--build-arg "BUILD_VERSION=${version}" \
--build-arg "BUILD_ARCH=${build_arch}" \
"${docker_cli[@]}" \
"${build_dir}"
# Success?
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
BUILD_ERROR+=("${repository}/${image}:${version}")
return 0
fi
push_images+=("${repository}/${image}:${version}")
bashio::log.info "Finish build for ${repository}/${image}:${version}"
# Tag latest
if bashio::var.true "${DOCKER_LATEST}"; then
docker_tags+=("latest")
fi
# Add additional tags
for tag_image in "${ADDITIONAL_TAGS[@]}"; do
docker_tags+=("${tag_image}")
done
# Tag images
for tag_image in "${docker_tags[@]}"; do
bashio::log.info "Create image tag: ${tag_image}"
docker tag "${repository}/${image}:${version}" "${repository}/${image}:${tag_image}"
push_images+=("${repository}/${image}:${tag_image}")
done
# Use shaddow repository
if bashio::var.has_value "${shadow_repository}"; then
bashio::log.info "Generate repository shadow images"
docker tag "${repository}/${image}:${version}" "${shadow_repository}/${image}:${version}"
for tag_image in "${docker_tags[@]}"; do
bashio::log.info "Create shadow-image tag: ${shadow_repository}/${image}:${tag_image}"
docker tag "${repository}/${image}:${version}" "${shadow_repository}/${image}:${tag_image}"
push_images+=("${shadow_repository}/${image}:${tag_image}")
done
push_images+=("${shadow_repository}/${image}:${version}")
fi
# Singing image
codenotary_sign "${codenotary_sign}" "${repository}/${image}:${version}"
# Push images
if bashio::var.true "${DOCKER_PUSH}"; then
for i in "${push_images[@]}"; do
for j in {1..3}; do
bashio::log.info "Start upload of ${i} (attempt #${j}/3)"
if docker push "${i}" > /dev/null 2>&1; then
bashio::log.info "Upload succeeded on attempt #${j}"
break
fi
if [[ "${j}" == "3" ]]; then
bashio::exit.nok "Upload failed on attempt #${j}"
else
bashio::log.warning "Upload failed on attempt #${j}"
sleep 30
fi
done
done
fi
}
function convert_to_json() {
# Convert [build|config].[yml|yaml] to json in a temp directory
mkdir -p /tmp/build_config
for file in config build; do
if bashio::fs.file_exists "${TARGET}/${file}.yml"; then
yq e -N -M -o=json "${TARGET}/${file}.yml" > "/tmp/build_config/${file}.json"
elif bashio::fs.file_exists "${TARGET}/${file}.yaml"; then
yq e -N -M -o=json "${TARGET}/${file}.yaml" > "/tmp/build_config/${file}.json"
fi
done
}
function copy_config_tmp() {
# Copy [build|config].json to a temp directory
mkdir -p /tmp/build_config
for file in config build; do
if bashio::fs.file_exists "${TARGET}/${file}.json"; then
cp "${TARGET}/${file}.json" "/tmp/build_config/${file}.json"
fi
done
}
#### Build functions ####
function build_base() {
local build_arch=${1}
local build_from=
local image=
local repository=
local shadow_repository=
local raw_image=
local args=
local codenotary_base=
local codenotary_sign=
local docker_cli=()
local docker_tags=()
# Read build.json
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.${build_arch} // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "/tmp/build_config/build.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"
codenotary_base="$(jq --raw-output '.codenotary.base_image // empty' "/tmp/build_config/build.json")"
codenotary_sign="$(jq --raw-output '.codenotary.signer // empty' "/tmp/build_config/build.json")"
fi
# Set defaults build things
if ! bashio::var.has_value "${build_from}"; then
bashio::log.error "${build_arch} not supported for this build"
return 1
fi
# Modify build_from
if [[ "${build_from}" =~ :$ ]]; then
if bashio::var.has_value "${VERSION_FROM}"; then
build_from="${build_from}${VERSION_FROM}"
else
build_from="${build_from}${VERSION_BASE}"
fi
fi
bashio::log.info "Use BUILD_FROM: ${build_from}"
# Read data from image
if ! bashio::var.has_value "${raw_image}"; then
bashio::log.error "Can't find the image tag on build.json"
return 1
fi
repository="$(echo "${raw_image}" | cut -f 1 -d '/')"
image="$(echo "${raw_image}" | cut -f 2 -d '/')"
# Additional build args
if bashio::var.has_value "${args}"; then
for arg in ${args}; do
value="$(jq --raw-output ".args.${arg}" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "${arg}=${value}")
done
fi
# Additional build labels
if bashio::var.has_value "${labels}"; then
for label in ${labels}; do
value="$(jq --raw-output ".labels.\"${label}\"" "/tmp/build_config/build.json")"
docker_cli+=("--label" "${label}=${value}")
done
fi
# Tag with version/build
if bashio::var.true "${RELEASE_TAG}"; then
docker_tags=("${VERSION}")
fi
# Set type
docker_cli+=("--label" "io.hass.type=base")
docker_cli+=("--label" "io.hass.base.version=${RELEASE}")
docker_cli+=("--label" "io.hass.base.arch=${build_arch}")
docker_cli+=("--label" "io.hass.base.image=${build_from}")
# Start build
run_build "${TARGET}" "${repository}" "${image}" "${VERSION_BASE}" \
"${build_from}" "${build_arch}" docker_cli[@] docker_tags[@] "${shadow_repository}" \
"${codenotary_base}" "${codenotary_sign}"
}
function build_addon() {
local build_arch=$1
local build_from=
local version=
local image=
local repository=
local shadow_repository=
local raw_image=
local name=
local description=
local url=
local args=
local codenotary_base=
local codenotary_sign=
local docker_cli=()
local docker_tags=()
# Read addon build.json
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.$build_arch // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"
codenotary_base="$(jq --raw-output '.codenotary.base_image // empty' "/tmp/build_config/build.json")"
codenotary_sign="$(jq --raw-output '.codenotary.signer // empty' "/tmp/build_config/build.json")"
fi
# Set defaults build things
if [ -z "$build_from" ]; then
build_from="homeassistant/${build_arch}-base:latest"
fi
# Additional build args
if [ -n "$args" ]; then
for arg in $args; do
value="$(jq --raw-output ".args.$arg" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "$arg=$value")
done
fi
# Read addon config.json
name="$(jq --raw-output '.name // empty' "/tmp/build_config/config.json" | sed "s/'//g")"
description="$(jq --raw-output '.description // empty' "/tmp/build_config/config.json" | sed "s/'//g")"
url="$(jq --raw-output '.url // empty' "/tmp/build_config/config.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/config.json")"
mapfile -t supported_arch < <(jq --raw-output '.arch // empty' "/tmp/build_config/config.json")
# Read version from config.json when VERSION is not set
if [ -n "$VERSION" ]; then
version="$VERSION"
else
version="$(jq --raw-output '.version' "/tmp/build_config/config.json")"
fi
# Check arch
if [[ ! ${supported_arch[*]} =~ ${build_arch} ]]; then
bashio::log.error "$build_arch not supported for this add-on"
return 1
fi
# Read data from image
if [ -n "$raw_image" ]; then
repository="$(echo "$raw_image" | cut -f 1 -d '/')"
image="$(echo "$raw_image" | cut -f 2 -d '/')"
fi
# Set additional labels
docker_cli+=("--label" "io.hass.name=$name")
docker_cli+=("--label" "io.hass.description=$description")
docker_cli+=("--label" "io.hass.type=addon")
if [ -n "$url" ]; then
docker_cli+=("--label" "io.hass.url=$url")
fi
# Start build
run_build "$TARGET" "$repository" "$image" "$version" \
"$build_from" "$build_arch" docker_cli[@] docker_tags[@] "${shadow_repository}" \
"${codenotary_base}" "${codenotary_sign}"
}
function build_generic() {
local build_arch=$1
local build_from=
local image=
local repository=
local shadow_repository=
local raw_image=
local args=
local codenotary_base=
local codenotary_sign=
local docker_cli=()
local docker_tags=()
# Read build.json
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.$build_arch // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "/tmp/build_config/build.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"
codenotary_base="$(jq --raw-output '.codenotary.base_image // empty' "/tmp/build_config/build.json")"
codenotary_sign="$(jq --raw-output '.codenotary.signer // empty' "/tmp/build_config/build.json")"
fi
# Set defaults build things
if ! bashio::var.has_value "$build_from"; then
bashio::log.error "$build_arch not supported for this build"
return 1
fi
# Read data from image
if ! bashio::var.has_value "$raw_image"; then
bashio::log.error "Can't find the image tag on build.json"
return 1
fi
repository="$(echo "$raw_image" | cut -f 1 -d '/')"
image="$(echo "$raw_image" | cut -f 2 -d '/')"
# Additional build args
if bashio::var.has_value "$args"; then
for arg in $args; do
value="$(jq --raw-output ".args.$arg" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "$arg=$value")
done
fi
# Additional build labels
if bashio::var.has_value "$labels"; then
for label in $labels; do
value="$(jq --raw-output ".labels.\"$label\"" "/tmp/build_config/build.json")"
docker_cli+=("--label" "$label=$value")
done
fi
# Start build
run_build "$TARGET" "$repository" "$image" "$VERSION" \
"$build_from" "$build_arch" docker_cli[@] docker_tags[@] "${shadow_repository}" \
"${codenotary_base}" "${codenotary_sign}"
}
function build_machine() {
local build_machine=$1
local build_arch=$2
local args=
local image=
local repository=
local raw_image=
local build_from=
local shadow_repository=
local codenotary_base=
local codenotary_sign=
local docker_cli=()
local docker_tags=()
# Read build.json
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.${build_arch} // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "/tmp/build_config/build.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"
codenotary_base="$(jq --raw-output '.codenotary.base_image // empty' "/tmp/build_config/build.json")"
codenotary_sign="$(jq --raw-output '.codenotary.signer // empty' "/tmp/build_config/build.json")"
fi
# Modify build_from
if [[ "${build_from}" =~ :$ ]]; then
build_from="${build_from}${VERSION}"
fi
bashio::log.info "Use BUILD_FROM: ${build_from}"
# Change dockerfile
if bashio::fs.file_exists "${TARGET}/${build_machine}"; then
docker_cli+=("--file" "${TARGET}/${build_machine}")
fi
repository="$(echo "${raw_image}" | cut -f 1 -d '/')"
image="$(echo "${raw_image}" | cut -f 2 -d '/')"
# Replace {machine} with build machine for image
# shellcheck disable=SC1117
image="$(echo "${image}" | sed -r "s/\{machine\}/${build_machine}/g")"
# Additional build args
if bashio::var.has_value "${args}"; then
for arg in ${args}; do
value="$(jq --raw-output ".args.${arg}" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "${arg}=${value}")
done
fi
# Additional build labels
if bashio::var.has_value "${labels}"; then
for label in ${labels}; do
value="$(jq --raw-output ".labels.\"${label}\"" "/tmp/build_config/build.json")"
docker_cli+=("--label" "${label}=${value}")
done
fi
# Set labels
docker_cli+=("--label" "io.hass.machine=${build_machine}")
# Start build
run_build "${TARGET}" "${repository}" "${image}" "${VERSION}" \
"${build_from}" "${build_arch}" docker_cli[@] docker_tags[@] "${shadow_repository}" \
"${codenotary_base}" "${codenotary_sign}"
}
function extract_machine_build() {
local list=$1
local array=()
local remove=()
if [ "$list" != "ALL" ]; then
IFS="," read -ra array <<<"$list"
for i in "${!BUILD_MACHINE[@]}"; do
skip=
for j in "${array[@]}"; do
[[ $i == "$j" ]] && { skip=1; break; }
done
[[ -n $skip ]] || remove+=("$i")
done
for i in "${remove[@]}"; do
unset "BUILD_MACHINE[$i]"
done
fi
}
#### initialized cross-build ####
function init_crosscompile() {
if [[ "$(uname -m)" != "x86_64" ]]; then
bashio::log.info "No crossbuild support on host"
return 0
fi
bashio::log.info "Setup crosscompiling feature"
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes \
> /dev/null 2>&1 || bashio::log.warning "Can't enable crosscompiling feature"
}
#### Security CodeNotary ####
function codenotary_setup() {
if bashio::var.false "${DOCKER_PUSH}" || bashio::var.is_empty "${CAS_API_KEY+x}"; then
return 0
fi
for j in {1..15}; do
if cas login > /dev/null 2>&1; then
return 0
fi
sleep $((5 * j))
done
bashio::exit.nok "Login to Codenotary fails!"
}
function codenotary_sign() {
local trust=$1
local image=$2
local success=false
if bashio::var.false "${DOCKER_PUSH}" || bashio::var.is_empty "${CAS_API_KEY+x}"; then
return 0
fi
for j in {1..15}; do
if ! cas authenticate --signerID "${trust}" --silent "docker://${image}"; then
cas notarize --ci-attr "docker://${image}" || true
else
success=true
break
fi
sleep $((5 * j))
done
if bashio::var.false "${success}"; then
bashio::exit.nok "Failed to sign the image"
fi
bashio::log.info "Signed ${image} with ${trust}"
}
function codenotary_validate() {
local trust=$1
local image=$2
local pull=$3
local success=false
if ! bashio::var.has_value "${trust}"; then
return 0
fi
if bashio::var.true "${pull}"; then
bashio::log.info "Download image ${image} for CodeNotary validation"
docker pull "${image}" > /dev/null 2>&1 || bashio::exit.nok "Can't pull image ${image}"
fi
for j in {1..15}; do
# shellcheck disable=SC1007
if CAS_API_KEY= cas authenticate --signerID "${trust}" --silent "docker://${image}" ; then
success=true
break
fi
sleep $((5 * j))
done
if bashio::var.false "${success}"; then
bashio::log.warning "Validation of ${image} fails!"
return 1
fi
bashio::log.info "Image ${image} is trusted"
}
#### Error handling ####
function error_handling() {
stop_docker
bashio::exit.nok "Abort by User"
}
trap 'error_handling' SIGINT SIGTERM
#### Parse arguments ####
while [[ $# -gt 0 ]]; do
key=$1
case $key in
-h|--help)
print_help
;;
-r|--repository)
GIT_REPOSITORY=$2
shift
;;
-b|--branch)
GIT_BRANCH=$2
shift
;;
-t|--target)
TARGET=$2
shift
;;
-v|--version)
VERSION=$2
shift
;;
--release)
RELEASE=$2
shift
;;
-i|--image)
IMAGE=$2
shift
;;
--no-latest)
DOCKER_LATEST=false
;;
--test)
DOCKER_PUSH=false
;;
--additional-tag)
ADDITIONAL_TAGS+=("$2")
shift
;;
--no-cache)
DOCKER_CACHE=false
;;
--self-cache)
SELF_CACHE=true
;;
--cache-tag)
CUSTOM_CACHE_TAG=$2
shift
;;
--release-tag)
RELEASE_TAG=true
;;
-d|--docker-hub)
DOCKER_HUB=$2
shift
;;
--version-from)
VERSION_FROM=$2
shift
;;
--docker-hub-check)
DOCKER_HUB_CHECK=true
;;
--docker-user)
DOCKER_USER=$2
shift
;;
--docker-password)
DOCKER_PASSWORD=$2
shift
;;
--armhf)
BUILD_LIST+=("armhf")
;;
--armv7)
BUILD_LIST+=("armv7")
;;
--amd64)
BUILD_LIST+=("amd64")
;;
--i386)
BUILD_LIST+=("i386")
;;
--aarch64)
BUILD_LIST+=("aarch64")
;;
--all)
BUILD_LIST=("armhf" "armv7" "amd64" "i386" "aarch64")
;;
--addon)
BUILD_TYPE="addon"
;;
--base)
BUILD_TYPE="base"
SELF_CACHE=true
VERSION_BASE=$2
shift
;;
--generic)
BUILD_TYPE="generic"
VERSION=$2
shift
;;
--machine)
BUILD_TYPE="machine"
SELF_CACHE=true
VERSION="$(echo "$2" | cut -d '=' -f 1)"
extract_machine_build "$(echo "$2" | cut -d '=' -f 2)"
shift
;;
*)
bashio::exit.nok "$0 : Argument '$1' unknown"
;;
esac
shift
done
# Check if an architecture is available
if [[ "${#BUILD_LIST[@]}" -eq 0 && ! "$BUILD_TYPE" == "machine" ]]; then
bashio::exit.nok "You need select an architecture for build!"
fi
#### Main ####
mkdir -p /data
# Setup docker env
init_crosscompile
start_docker
# Load external repository
if [ -n "$GIT_REPOSITORY" ]; then
bashio::log.info "Checkout repository $GIT_REPOSITORY"
git clone --depth 1 --branch "$GIT_BRANCH" "$GIT_REPOSITORY" /data/git 2> /dev/null
TARGET="/data/git/$TARGET"
fi
# Convert configuration files to json if needed
convert_to_json
# Copy configuration files to tmp
copy_config_tmp
# Login into dockerhub & setup CodeNotary
if [ -n "$DOCKER_USER" ] && [ -n "$DOCKER_PASSWORD" ]; then
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD"
fi
codenotary_setup
# Select arch build
if [ "${#BUILD_LIST[@]}" -ne 0 ]; then
bashio::log.info "Run $BUILD_TYPE build for: ${BUILD_LIST[*]}"
for arch in "${BUILD_LIST[@]}"; do
if [ "$BUILD_TYPE" == "addon" ]; then
(build_addon "$arch") &
elif [ "$BUILD_TYPE" == "generic" ]; then
(build_generic "$arch") &
elif [ "$BUILD_TYPE" == "base" ]; then
(build_base "$arch") &
elif [[ "$BUILD_TYPE" == "machine" ]]; then
continue # Handled in the loop below
else
bashio::exit.nok "Invalid build type: $BUILD_TYPE"
fi
BUILD_TASKS+=($!)
done
fi
# Select machine build
if [[ "$BUILD_TYPE" == "machine" ]]; then
bashio::log.info "Machine builds: ${!BUILD_MACHINE[*]}"
for machine in "${!BUILD_MACHINE[@]}"; do
machine_arch="${BUILD_MACHINE["$machine"]}"
(build_machine "$machine" "$machine_arch") &
BUILD_TASKS+=($!)
done
fi
# Wait until all build jobs are done
wait "${BUILD_TASKS[@]}"
# Cleanup docker env
stop_docker
# No Errors
if [ ${#BUILD_ERROR[@]} -eq 0 ]; then
bashio::exit.ok
fi
bashio::exit.nok "Some build fails: ${BUILD_ERROR[*]}"