Support ubuntu-22.04 docker exector image building & vpp jobs
[ci-management.git] / docker / scripts / lib_csit.sh
1 # lib_csit.sh - Docker build script CSIT library.
2 #               For import only.
3
4 # Copyright (c) 2021 Cisco and/or its affiliates.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Don't import more than once.
18 if [ -n "$(alias lib_csit_imported 2> /dev/null)" ] ; then
19     return 0
20 fi
21 alias lib_csit_imported=true
22
23 export CIMAN_DOCKER_SCRIPTS="${CIMAN_DOCKER_SCRIPTS:-$(dirname $BASH_SOURCE)}"
24 . "$CIMAN_DOCKER_SCRIPTS/lib_common.sh"
25 . "$CIMAN_DOCKER_SCRIPTS/lib_apt.sh"
26
27 CSIT_SUPPORTED_EXECUTOR_CLASSES="builder csit_dut"
28 csit_supported_executor_class() {
29     if ! grep -q "${1:-}" <<< "$CSIT_SUPPORTED_EXECUTOR_CLASSES" ; then
30         return 1
31     fi
32     return 0
33 }
34
35 csit_supported_os() {
36     case "$1" in
37         # TODO: fix to be only ubuntu-22.04 once migration is complete.
38         ubuntu-2[02].04) return 0 ;;
39                    *) ;;
40     esac
41     return 1
42 }
43
44 csit_checkout_branch_for_vpp() {
45     local vpp_branch="$1"
46     local csit_dir="$DOCKER_CSIT_DIR"
47     local csit_bash_function_dir="$csit_dir/resources/libraries/bash/function"
48
49     # import checkout_csit_for_vpp() if not defined
50     set +e && [ -z "$(declare -f checkout_csit_for_vpp)" ] \
51         && source "$csit_bash_function_dir/branch.sh"
52     CSIT_DIR="$csit_dir" checkout_csit_for_vpp "$vpp_branch"
53
54     csit_branch="$(git branch | grep -e '^*' | mawk '{print $2}')"
55 }
56
57 csit_install_packages() {
58     local branch="$1"
59     local branchname="$(echo $branch | sed -e 's,/,_,')"
60     local csit_dir="$DOCKER_CSIT_DIR"
61     local csit_ansible_dir="$csit_dir/fdio.infra.ansible"
62     if [ ! -d "$csit_ansible_dir" ] ; then
63         csit_ansible_dir="$csit_dir/resources/tools/testbed-setup/ansible"
64     fi
65     local bld_log="$DOCKER_BUILD_LOG_DIR/$FDIOTOOLS_IMAGENAME"
66     bld_log="${bld_log}-$branchname-csit_install_packages-bld.log"
67
68     git clean -qfdx
69     python3 -m pip install pyyaml
70
71     local exclude_roles="-e calibration -e kernel -e mellanox -e nomad -e consul"
72     [ "$OS_ARCH" = "aarch64" ] && exclude_roles="$exclude_roles -e iperf"
73
74     # Not in double quotes to let bash remove newline characters
75     local yaml_files="$(grep -r packages_by $csit_ansible_dir | cut -d: -f1 | sort -u | grep -v $exclude_roles)"
76     packages="$(dbld_csit_find_ansible_packages.py --$OS_ID --$OS_ARCH $yaml_files)"
77     packages="${packages/bionic /}"
78     packages="${packages/focal /}"
79     packages="${packages/libmbedcrypto1/libmbedcrypto3}"
80     packages="${packages/libmbedtls10/libmbedtls12}"
81     packages="$(echo ${packages//python\-/python3\-} | tr ' ' '\n' | sort -u | xargs)"
82
83     if [ -n "$packages" ] ; then
84         case "$OS_NAME" in
85             ubuntu*)
86                 apt_install_packages $packages
87                 ;;
88             debian*)
89                 apt_install_packages $packages
90                 ;;
91             *)
92                 echo "Unsupported OS ($OS_ID): CSIT packages NOT INSTALLED!"
93                 ;;
94         esac
95     fi
96 }
97
98 csit_pip_cache() {
99     local branch="$1"
100     local VENV_OPTS=""
101     # ensure PS1 is defined (used by virtualenv activate script)
102     PS1=${PS1:-"#"}
103     CSIT_DIR="$DOCKER_CSIT_DIR"
104
105     if [ -f "$CSIT_DIR/VPP_REPO_URL" ] \
106            && [ -f "$CSIT_DIR/requirements.txt" ]; then
107
108         local csit_bash_function_dir="$CSIT_DIR/resources/libraries/bash/function"
109         local branchname="$(echo $branch | sed -e 's,/,_,')"
110         local bld_log="$DOCKER_BUILD_LOG_DIR"
111         bld_log="${bld_log}/$FDIOTOOLS_IMAGENAME-$branchname-csit_pip_cache-bld.log"
112         local pip_cmd="python3 -m pip --disable-pip-version-check"
113         export PYTHONPATH=$CSIT_DIR
114
115         description="Install CSIT python packages from $branch branch"
116         echo_log "    Starting  $description..."
117         git clean -qfdx
118         rm -rf "$PYTHONPATH/env"
119
120         # TODO: Update CSIT release branches to avoid build breakage
121         #       Fixes https://github.com/pypa/pip/issues/8260
122         $pip_cmd install pip==21.0.1
123         #       rls2009_lts-* branches missing cherry-pick of
124         #       https://gerrit.fd.io/r/c/csit/+/31338
125         sed -i 's/scipy==1.1.0/scipy==1.5.4/' "$PYTHONPATH/requirements.txt"
126
127         # Virtualenv version is pinned in common.sh in newer csit branches.
128         # (note: xargs removes leading/trailing spaces)
129         local common_sh="$csit_bash_function_dir/common.sh"
130         install_virtualenv="$(grep 'virtualenv' $common_sh | grep pip | grep install | cut -d'|' -f1 | xargs)"
131         $install_virtualenv
132         virtualenv --no-download --python=$(which python3) "$CSIT_DIR/env"
133         source "$CSIT_DIR/env/bin/activate"
134
135         if [ "$OS_ARCH" = "aarch64" ] ; then
136             local numpy_ver="$(grep numpy $PYTHONPATH/requirements.txt)"
137             [ -n "$numpy_ver" ] && $pip_cmd install $numpy_ver 2>&1 | \
138                 tee -a $bld_log
139         fi
140
141         # Install csit python requirements
142         $pip_cmd install -r "$CSIT_DIR/requirements.txt" 2>&1 | \
143             tee -a "$bld_log"
144         # Install tox python requirements
145         $pip_cmd install -r "$CSIT_DIR/tox-requirements.txt" 2>&1 | \
146             tee -a "$bld_log"
147         # Run tox which installs pylint requirments
148         pushd $CSIT_DIR >& /dev/null
149         tox || true
150         popd >& /dev/null
151
152         # Clean up virtualenv directories
153         deactivate
154         git checkout -q -- .
155         git clean -qfdx
156         echo_log "    Completed $description!"
157     else
158         echo_log "ERROR: Missing or invalid CSIT_DIR: '$CSIT_DIR'!"
159         return 1
160     fi
161 }
162
163 docker_build_setup_csit() {
164     if csit_supported_executor_class "$EXECUTOR_CLASS" ; then
165         if [ ! -d "$DOCKER_CSIT_DIR" ] ; then
166             echo_log "Cloning CSIT into $DOCKER_CSIT_DIR..."
167             git clone -q https://gerrit.fd.io/r/csit "$DOCKER_CSIT_DIR"
168         fi
169         clean_git_repo "$DOCKER_CSIT_DIR"
170     fi
171 }
172
173 csit_dut_generate_docker_build_files() {
174     local build_files_dir="$DOCKER_BUILD_FILES_DIR"
175
176     mkdir -p "$build_files_dir"
177     cat <<EOF >"$build_files_dir/supervisord.conf"
178 [unix_http_server]
179 file = /tmp/supervisor.sock
180 chmod = 0777
181
182 [rpcinterface:supervisor]
183 supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
184
185 [supervisorctl]
186 serverurl = unix:///tmp/supervisor.sock
187
188 [supervisord]
189 pidfile = /tmp/supervisord.pid
190 identifier = supervisor
191 directory = /tmp
192 logfile = /tmp/supervisord.log
193 loglevel = debug
194 nodaemon = false
195
196 [program:vpp]
197 command = /usr/bin/vpp -c /etc/vpp/startup.conf
198 autostart = false
199 autorestart = true
200 redirect_stderr = true
201 priority = 1
202 EOF
203 }
204
205 csit_builder_generate_docker_build_files() {
206     local build_files_dir="$DOCKER_BUILD_FILES_DIR"
207     local dashes="-----"
208     local dbeg="${dashes}BEGIN"
209     local dend="${dashes}END"
210     local pvt="PRIVATE"
211     local kd="KEY$dashes"
212
213     # TODO: Verify why badkey is required & figure out how to avoid it.
214     mkdir -p "$build_files_dir"
215     cat <<EOF >"$build_files_dir/badkey"
216 $dbeg RSA $pvt $kd
217 MIIEowIBAAKCAQEAslDXf4kZOQI8OGQQdIF8o83nBM0B4fzHLYLxxiY2rKiQ5MGM
218 mQa7p1KKzmd5/NlvFRnXefnjSDQljjPxEY7mh457rX2nXvqHD4GUXZPpBIE73rQ1
219 TViIAXdDzFXJ6ee4yX8ewmVakzYBnlUPDidkWyRnjm/xCg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cEjtV6P
236 ORBCNZg1wGEIEfYK3XIorpECgYBubXfQj8KhUs0fdx3Y3Ehdni/ZdlG7F1qx4YBq
237 mx5e7d+Wd6Hn5Z3fcxO9+yrvypS3YN5YrJzuZSiuCSWdP9RcY7y5r1ZQRv1g0nTZ
238 MDWZUiNea4cddTd8xKxFB3tV4SkIZi8LustuzDVWa0Mlh4EOmP6uf6c5WxtqRsEL
239 UwORFwKBgEjZsfmZGBurjOtSrcsteulOB0D2nOqPVRWXmbSNJT/l73DkEllvVyA/
240 wdW39nyFrA2Qw1K2F+l8DkzMd/WEjmioSWCsvTkXlvrqPfByKg01zCbYy/mhRW7d
241 7sQrPOIl8ygsc3JrxmvzibdWmng1MehvpAM1ogWeTUa1lsDTNJ/6
242 $dend RSA $pvt $kd
243 EOF
244     chmod 600 "$build_files_dir/badkey"
245     cat <<EOF >"$build_files_dir/sshconfig"
246 Host 172.17.0.*
247         StrictHostKeyChecking no
248         UserKnownHostsFile=/dev/null
249 EOF
250 }
251
252 csit_shim_generate_docker_build_files() {
253     local build_files_dir="$DOCKER_BUILD_FILES_DIR"
254     # TODO: Verify why badkey is required & figure out how to avoid it.
255     local badkey='AAAAB3NzaC1yc2EAAAADAQABAAABAQCyUNd/iRk5Ajw4ZBB0gXyjzecEzQHh/MctgvHGJjasqJDkwYyZBrunUorOZ3n82W8VGdd5+eNINCWOM/ERjuaHjnutfade+ocPgZRdk+kEgTvetDVNWIgBd0PMVcnp57jJfx7CZVqTNgGeVQ8OJ2RbJGeOb/EKApQI74IPkAfc0PSieSw5gC0eqEOHb39Awgp0ycrzsUHF/OEicfCmo+6vvrMGenDe7frKUoTKYMWs7l3DOyFC8NaOxhGD3J1Ne5u3A/r4w6mN1HVI0rFwIcoms+t0B4lb2ODWKZiZikQdn8/eqwsmbSEZZsWN3FkshgjPS83+dNqVwB6pPY5Yqte7'
256
257     mkdir -p "$build_files_dir"
258     # TODO: Verify why badkeypub is required & figure out how to avoid it.
259     echo "ssh-rsa $badkey ejk@bhima.local" >"$build_files_dir/badkeypub"
260
261     cat <<EOF >"$build_files_dir/sshconfig"
262 Host 172.17.0.*
263         StrictHostKeyChecking no
264         UserKnownHostsFile=/dev/null
265 EOF
266     cat <<EOF >"$build_files_dir/wrapdocker"
267 #!/bin/bash
268
269 # Ensure that all nodes in /dev/mapper correspond to mapped devices currently loaded by the device-mapper kernel driver
270 dmsetup mknodes
271
272 # First, make sure that cgroups are mounted correctly.
273 CGROUP=/sys/fs/cgroup
274 : {LOG:=stdio}
275
276 [ -d \$CGROUP ] ||
277     mkdir \$CGROUP
278
279 mountpoint -q \$CGROUP ||
280     mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup \$CGROUP || {
281         echo "Could not make a tmpfs mount. Did you use --privileged?"
282         exit 1
283     }
284
285 if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security
286 then
287     mount -t securityfs none /sys/kernel/security || {
288         echo "Could not mount /sys/kernel/security."
289         echo "AppArmor detection and --privileged mode might break."
290     }
291 fi
292
293 # Mount the cgroup hierarchies exactly as they are in the parent system.
294 for SUBSYS in \$(cut -d: -f2 /proc/1/cgroup)
295 do
296         [ -d \$CGROUP/\$SUBSYS ] || mkdir \$CGROUP/\$SUBSYS
297         mountpoint -q \$CGROUP/\$SUBSYS ||
298                 mount -n -t cgroup -o \$SUBSYS cgroup \$CGROUP/\$SUBSYS
299
300         # The two following sections address a bug which manifests itself
301         # by a cryptic "lxc-start: no ns_cgroup option specified" when
302         # trying to start containers withina container.
303         # The bug seems to appear when the cgroup hierarchies are not
304         # mounted on the exact same directories in the host, and in the
305         # container.
306
307         # Named, control-less cgroups are mounted with "-o name=foo"
308         # (and appear as such under /proc/<pid>/cgroup) but are usually
309         # mounted on a directory named "foo" (without the "name=" prefix).
310         # Systemd and OpenRC (and possibly others) both create such a
311         # cgroup. To avoid the aforementioned bug, we symlink "foo" to
312         # "name=foo". This shouldn't have any adverse effect.
313         echo \$SUBSYS | grep -q ^name= && {
314                 NAME=\$(echo \$SUBSYS | sed s/^name=//)
315                 ln -s \$SUBSYS \$CGROUP/\$NAME
316         }
317
318         # Likewise, on at least one system, it has been reported that
319         # systemd would mount the CPU and CPU accounting controllers
320         # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu"
321         # but on a directory called "cpu,cpuacct" (note the inversion
322         # in the order of the groups). This tries to work around it.
323         [ \$SUBSYS = cpuacct,cpu ] && ln -s \$SUBSYS \$CGROUP/cpu,cpuacct
324 done
325
326 # Note: as I write those lines, the LXC userland tools cannot setup
327 # a "sub-container" properly if the "devices" cgroup is not in its
328 # own hierarchy. Let's detect this and issue a warning.
329 grep -q :devices: /proc/1/cgroup ||
330     echo "WARNING: the 'devices' cgroup should be in its own hierarchy."
331 grep -qw devices /proc/1/cgroup ||
332     echo "WARNING: it looks like the 'devices' cgroup is not mounted."
333
334 # Now, close extraneous file descriptors.
335 pushd /proc/self/fd >/dev/null
336 for FD in *
337 do
338     case "\$FD" in
339     # Keep stdin/stdout/stderr
340     [012])
341         ;;
342     # Nuke everything else
343     *)
344         eval exec "\$FD>&-"
345         ;;
346     esac
347 done
348 popd >/dev/null
349
350
351 # If a pidfile is still around (for example after a container restart),
352 # delete it so that docker can start.
353 rm -rf /var/run/docker.pid
354
355 # If we were given a PORT environment variable, start as a simple daemon;
356 # otherwise, spawn a shell as well
357 if [ "\$PORT" ]
358 then
359     exec dockerd -H 0.0.0.0:\$PORT -H unix:///var/run/docker.sock \
360         \$DOCKER_DAEMON_ARGS
361 else
362     if [ "\$LOG" == "file" ]
363     then
364         dockerd \$DOCKER_DAEMON_ARGS &>/var/log/docker.log &
365     else
366         dockerd \$DOCKER_DAEMON_ARGS &
367     fi
368     (( timeout = 60 + SECONDS ))
369     until docker info >/dev/null 2>&1
370     do
371         if (( SECONDS >= timeout )); then
372             echo 'Timed out trying to connect to internal docker host.' >&2
373             break
374         fi
375         sleep 1
376     done
377     [[ \$1 ]] && exec "\$@"
378     exec bash --login
379 fi
380 EOF
381 }