Debian Codegraph @

tools/run-in-docker

raw symbols SCIP
1 #!/usr/bin/env bash
2 #
3 # run given command via Docker on all distros supported by ReaR, or only a single one
4
5 # command line args:
6 #
7 # $0 [os ...] -- <command> [args ...]
8 #
9 # Note: This script works also on MacOS and makes an effort to use cross-platform
10 # compatible options for tools like cp
11
12 # Define the list of supported images
13 declare -r IMAGES=(
14 ubuntu:{20.04,22.04,24.04}
15 debian:{10,11,unstable}
16 opensuse/leap:15
17 registry.suse.com/suse/sle15
18 centos:8 # discontinued
19 quay.io/centos/centos:stream9
20 # registry.access.redhat.com/ubi{7,8,9} basic packages like parted are missing
21 fedora:{40,41} # rawhide Docker image is broken, see https://github.com/fedora-cloud/docker-brew-fedora/issues/109
22 archlinux
23 manjarolinux/base
24 )
25
26 declare -r HELP_TEXT="
27 $0 [image ...] -- <command> [args ...]
28 specify image patterns or omit image to run in all supported images:
29
30 $(fold -sw 60 <<<"${IMAGES[*]}")
31
32 You can also specify completely different Docker images instead.
33
34 Without command it will show the Bash version in all images
35
36 Special commands:
37
38 --patch Patch the (given or all) images to contain the ReaR build and run dependencies
39 Add --continue-and-record-successful FILE to continue after a failure and record
40 the successful images in FILE
41 -i Interactive shell, used by default if image selection yields only a single image
42
43 Architecture defaults to the host platform, specify architecture via
44 -a <architecture>, e.g. -a amd64 on M1 Mac as part of the image selection
45 "
46
47
48 function gh_actions_output {
49 if test "$GITHUB_ACTIONS"; then
50 echo "$*"
51 fi
52 }
53
54 function die {
55 gh_actions_output "::error::$*"
56 echo -e "ERROR: $*" 1>&2
57 exit 1
58 }
59
60 function exit_handler {
61 echo "** SCRIPT RUN TIME $SECONDS SECONDS **"
62 }
63 trap exit_handler EXIT
64
65 extra_docker_args=()
66 use_images=()
67
68 # patch the images to contain all ReaR build and run dependencies
69 function patch_images() {
70 local continue_and_record_successful_file=""
71 while test $# -gt 0; do
72 case "$1" in
73 --continue-and-record-successful)
74 test $# -lt 2 && die "Missing filename for $1 argument"
75 continue_and_record_successful_file="$2"
76 rm -vf "$continue_and_record_successful_file"
77 shift 2
78 ;;
79 *)
80 die "Unknown option $1"
81 ;;
82 esac
83 done
84
85 for image in "${use_images[@]}"; do
86 local DOCKERFILE="
87 FROM $image
88 LABEL description=\"$image patched to build and run ReaR\"
89 SHELL [\"/bin/bash\", \"-xeuo\", \"pipefail\", \"-c\"]
90 ENV DEBIAN_FRONTEND=noninteractive
91
92 RUN type -p apt-get &>/dev/null || exit 0 ;\
93 apt-get -y update ;\
94 apt-get -y --allow-unauthenticated install </dev/null \
95 sysvinit-utils kbd cpio file procps ethtool iputils-ping net-tools dosfstools binutils parted openssl gawk attr bc psmisc nfs-client portmap xorriso isolinux gdisk syslinux syslinux-common syslinux-efi iproute2 \
96 make asciidoctor git build-essential debhelper devscripts ;\
97 apt-get -y --allow-unauthenticated install </dev/null fdisk ||\
98 apt-get -y --allow-unauthenticated install </dev/null util-linux
99
100 RUN type -p zypper &>/dev/null || exit 0 ;\
101 zypper --no-gpg-checks --quiet --non-interactive install \
102 sysvinit-tools kbd cpio binutils ethtool gzip iputils parted tar openssl gawk attr bc syslinux portmap rpcbind iproute2 nfs-client xorriso mkisofs util-linux psmisc procps \
103 make git rpm-build ;\
104 zypper --no-gpg-checks --quiet --non-interactive install 'rubygem(asciidoctor)' || \
105 zypper --no-gpg-checks --quiet --non-interactive install asciidoc xmlto
106
107 RUN type -p pacman &>/dev/null || exit 0 ;\
108 pacman --noconfirm -Sy \
109 sysvinit-tools kbd cpio binutils ethtool gzip iputils parted tar openssl gawk attr bc syslinux rpcbind iproute2 nfs-utils libisoburn cdrtools util-linux psmisc procps-ng util-linux diffutils less \
110 make binutils fakeroot git asciidoctor debugedit
111
112 # CentOS 8 doesn't have sysvinit-tools any more but it also doesn't have asciidoctor yet
113 RUN type -p yum &>/dev/null || exit 0 ;\
114 grep -E '(CentOS.*Final|CentOS Linux release 8)' /etc/redhat-release && \
115 sed -i -e 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|' -e '/^mirror/d' /etc/yum.repos.d/*.repo ; \
116 yum -q --nogpgcheck install -y \
117 kbd cpio binutils ethtool gzip iputils parted tar openssl gawk attr bc syslinux rpcbind iproute nfs-utils xorriso genisoimage util-linux psmisc procps-ng util-linux \
118 make binutils git rpm-build ; \
119 yum -q --nogpgcheck install -y sysvinit-tools mkisofs asciidoc xmlto || \
120 yum -q --nogpgcheck install -y asciidoctor || \
121 yum -q --nogpgcheck install -y mkisofs asciidoc xmlto
122
123 RUN git config --global --add safe.directory /rear
124 "
125
126 # TODO: Add support for GRUB2
127 gh_actions_output "::group::Patching $image"
128 printf "********** PATCHING %-40s **********\n" "$image" 1>&2
129 read oldsize junk < <(docker images --format '{{.VirtualSize}}' "$image")
130 docker buildx build -t "$image" \
131 "${extra_docker_args[@]}" \
132 - <<<"$DOCKERFILE"
133 local docker_build_ret=$?
134 if test "$continue_and_record_successful_file" ; then
135 if test $docker_build_ret -eq 0 ; then
136 echo "$image" >> "$continue_and_record_successful_file"
137 else
138 printf "********** FAILED %-40s **********\n" "$image" 1>&2
139 continue
140 fi
141 else
142 test $docker_build_ret -eq 0 || die "Failed building $image"
143 fi
144 read newsize junk < <(docker images --format '{{.VirtualSize}}' "$image")
145 test "$oldsize" != "$newsize" && printf "********** %-35s %7s -> %-7s *****\n" "$image" "$oldsize" "$newsize"
146 gh_actions_output "::endgroup::"
147 done
148 }
149
150 command_args=()
151 while test $# -gt 0; do
152 case "$1" in
153 -a)
154 echo "Using architecture $2 instead of default Docker architecture $(docker system info --format '{{.Architecture}}')"
155 extra_docker_args+=("--platform" "linux/$2")
156 shift 2
157 ;;
158 -h | --help)
159 echo "$HELP_TEXT"
160 exit 1
161 ;;
162 --)
163 shift
164 [[ $# -gt 0 ]] && command_args=("$@")
165 break
166 ;;
167 *)
168 #shellcheck disable=SC2207
169 use_images+=($(
170 ((c = 0))
171 for image in "${IMAGES[@]}"; do
172 if [[ "$image" == *$1* ]]; then
173 echo "$image"
174 ((c++))
175 fi
176 done
177 if ((c == 0)); then
178 echo "$1"
179 fi
180 ))
181 shift
182 ;;
183 esac
184 done
185
186 if test ${#use_images[@]} -eq 0; then
187 use_images=("${IMAGES[@]}")
188 fi
189
190 if test ${#command_args[@]} -eq 0; then
191 if test ${#use_images[@]} -eq 1; then # if only one image is given and no command then go interactive
192 command_args=(-i)
193 else
194 command_args=("echo" "Bash is \$BASH_VERSION")
195 fi
196 fi
197
198 rear_toplevel_dir=$(dirname $(dirname $(readlink -f "$0")))
199 bash_script=tools/run-in-docker-script.sh
200 bash_args=(-c "echo BUG: CONFIGURATION ERROR" )
201
202 # declare -p IMAGES use_images command_args rear_toplevel_dir ; exit 0
203
204 case "${command_args[0]}" in
205 --patch)
206 patch_images "${command_args[@]:1}"
207 exit $?
208 ;;
209 -i)
210 bash_args=( -i )
211 ;;
212 -*)
213 bash_args=( "${command_args[@]}" )
214 ;;
215 *)
216 echo "${command_args[*]}" >"$rear_toplevel_dir/$bash_script"
217 bash_args=("$bash_script")
218 ;;
219 esac
220
221
222 # Note: bash reads --rcfile <file> for interactive shells
223 # bash reads the file specified in BASH_ENV for non-interactive shells
224 # we set both so that our startup file run-in-docker.bashrc is ALWAYS read as it sets the PATH
225 for image in "${use_images[@]}"; do
226 gh_actions_output "::group::Running $image"
227 printf "********** %-40s **********\n" "$image" 1>&2
228 image_name="$(echo -n "$image" | tr -cs '0-9a-zA-Z-_' -)"
229 dist_dest="dist-all/$image_name"
230 mkdir -p "$rear_toplevel_dir/$dist_dest" || die "Could not mkdir $rear_toplevel_dir/$dist_dest"
231 docker run \
232 --rm $(test -t 0 && echo -i -t) \
233 --sig-proxy=false \
234 -h "$image_name" \
235 -v "$rear_toplevel_dir:/rear" \
236 -e REAR_VAR=/tmp/rear \
237 -e BASH_ENV=tools/run-in-docker.bashrc \
238 -w /rear \
239 "${extra_docker_args[@]}" \
240 "$image" \
241 /bin/bash --rcfile tools/run-in-docker.bashrc "${bash_args[@]}" || die "############### DOCKER RUN FAILED FOR $image"
242 if test "$(ls -l "$rear_toplevel_dir/dist" 2>/dev/null || :)"; then
243 echo "********** Copying dist to ${dist_dest}"
244 cp -R -x "$rear_toplevel_dir/dist/." "$rear_toplevel_dir/$dist_dest/" || die "Could not copy dist to $dist_dest"
245 fi
246 gh_actions_output "::endgroup::"
247 done