forked from perfsonar/debian-docker-buildmachines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-docker
executable file
·92 lines (80 loc) · 3.28 KB
/
setup-docker
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
#!/usr/bin/env bash
# This script sets up all the Docker images needed to build perfSONAR packages
# for one given perfSONAR repository ($REPO)
# By default, the setup is to build packages for the AMD64 architecture only
# This uses the single_build target from docker-bake.hcl
# If you want to build for all architectures, you can pass 'full_build' as arg
# Or 'dual_build' to build only for AMD64 and ARM64
# See docker-bake.hcl for details and other bake targets
# We use debian:buster as the build OS
declare -a OSimages=("debian:buster")
#declare -a OSimages=("debian:buster" "ubuntu:bionic" "ubuntu:focal")
# Make sure we have a repository
if [ -z "$REPO" ]; then
REPO="perfsonar-5.0-snapshot"
fi
# Check what build we should do
if [ $# -gt 0 ]; then
build_type=$1
else
build_type="single_build"
fi
### Check current status
echo -e "\033[1mCurrent Docker setup is as follows:\033[0m"
docker buildx ls
echo
docker ps -a
echo
docker images
echo
### Clear Docker caches
echo -e "\033[1mLet's clear caches\033[0m"
docker builder prune -af
echo
docker system prune -f
echo
docker volume prune -f
echo
### Create and setup the buildx instance for the multi arch builds
# See https://docs.docker.com/desktop/multi-arch/
echo -e "\033[1mMake sure docker buildx is ready for multi arch builds\033[0m"
docker buildx create --driver docker-container --name perfbuild --use
# Setup buildx for all supported architectures by installing and registering the latest qemu image
# It uses the images from https://github.com/multiarch/qemu-user-static
# Also see https://medium.com/@artur.klauser/building-multi-architecture-docker-images-with-buildx-27d80f7e2408 for more context
# This should only be needed once per Docker daemon run,
# but it shouldn't be a problem running it everytime we run the setup
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
echo
### Verify new status
echo -e "\033[1mNew Docker setup is as follows:\033[0m"
docker buildx ls
echo
docker ps -a
echo
docker images
echo
### Build the new images on the different platforms
export OSimage REPO
# Loop on all OS we want to have build machines
for OSimage in ${OSimages[@]}; do
echo -e "\033[1mNow let's build Docker images for \033[1;32m$OSimage \033[1;37m- target: \033[1;32m$build_type\033[0m"
# Prepare and push the images
docker buildx bake $build_type
# Once images are pushed, we need to modify the docker-compose.yml file with the new SHA256
for LINE in `(docker buildx imagetools inspect "ntw0n/psbuild.$REPO.$OSimage"; echo) | perl -nle '$sha=$1 if /$ENV{OSimage}\@sha256:([a-z0-9]*)/; print "$1:$sha" if /(linux\/(amd64|arm64|arm\/v7|ppc64le))/;'`; do
arch=${LINE%:*}
arch="${arch//\//\\/}"
sha=${LINE#*:}
export arch sha
perl -nli -e '$sha = $ENV{sha} if /$ENV{arch}/; if (/\/psbuild\..*\@sha256.*/ && $sha ne "") {print " image: docker.io/ntw0n/psbuild.\$REPO.\$OSimage\@sha256:$sha"; $sha = "";} else {print;}' docker-compose.yml
done
echo
done
git commit -m "Updating Docker multiarch image sha256" docker-compose.yml
# Display result
docker images
echo
echo -e "\033[1mAll images built and pushed!\033[0m"
echo "If new images have been generated and pushed to Docker Hub, you'll also need to push the docker-compose.yml file change to the GitHub repo."
git status