|
| 1 | +#!/bin/bash |
| 2 | +# This script is used for initializing the host environment for CI. |
| 3 | +# Supports Fedora and EL-based distributions. |
| 4 | +set -eux -o pipefail |
| 5 | + |
| 6 | +# BATS_VERSION is only consumed for the EL8 platform as its bats package is too old. |
| 7 | +: "${BATS_VERSION:=v1.12.0}" |
| 8 | + |
| 9 | +SCRIPTDIR="$(dirname "${BASH_SOURCE[0]}")" |
| 10 | + |
| 11 | +# PLATFORM_ID is not available on Fedora |
| 12 | +PLATFORM_ID= |
| 13 | +grep -q ^PLATFORM_ID /etc/os-release && PLATFORM_ID="$(grep -oP '^PLATFORM_ID="\K[^"]+' /etc/os-release)" |
| 14 | + |
| 15 | +# Initialize DNF |
| 16 | +DNF=(dnf -y --setopt=install_weak_deps=False --setopt=tsflags=nodocs) |
| 17 | +case "$PLATFORM_ID" in |
| 18 | +platform:el8) |
| 19 | + # DNF+=(--exclude="kernel,kernel-core") seems to fail |
| 20 | + "${DNF[@]}" config-manager --set-enabled powertools # for glibc-static |
| 21 | + "${DNF[@]}" install epel-release |
| 22 | + ;; |
| 23 | +platform:el9 | platform:el10) |
| 24 | + DNF+=(--exclude="kernel,kernel-core") |
| 25 | + "${DNF[@]}" config-manager --set-enabled crb # for glibc-static |
| 26 | + "${DNF[@]}" install epel-release |
| 27 | + ;; |
| 28 | +*) |
| 29 | + # Fedora |
| 30 | + DNF+=(--exclude="kernel,kernel-core") |
| 31 | + ;; |
| 32 | +esac |
| 33 | + |
| 34 | +# Install common packages |
| 35 | +RPMS=(container-selinux fuse-sshfs git-core glibc-static golang iptables jq libseccomp-devel lld make policycoreutils wget) |
| 36 | +# Work around dnf mirror failures by retrying a few times. |
| 37 | +for i in $(seq 0 2); do |
| 38 | + sleep "$i" |
| 39 | + "${DNF[@]}" update && "${DNF[@]}" install "${RPMS[@]}" && break |
| 40 | +done |
| 41 | +# shellcheck disable=SC2181 |
| 42 | +[ $? -eq 0 ] # fail if dnf failed |
| 43 | + |
| 44 | +# Install CRIU |
| 45 | +if [ "$PLATFORM_ID" = "platform:el8" ]; then |
| 46 | + # Use newer criu (with https://github.com/checkpoint-restore/criu/pull/2545). |
| 47 | + # Alas we have to disable container-tools for that. |
| 48 | + "${DNF[@]}" module disable container-tools |
| 49 | + "${DNF[@]}" copr enable adrian/criu-el8 |
| 50 | +fi |
| 51 | +"${DNF[@]}" install criu |
| 52 | + |
| 53 | +# Install BATS |
| 54 | +if [ "$PLATFORM_ID" = "platform:el8" ]; then |
| 55 | + # The packaged version of bats is too old: `BATS_ERROR_SUFFIX: unbound variable`, `bats_require_minimum_version: command not found` |
| 56 | + ( |
| 57 | + cd /tmp |
| 58 | + git clone https://github.com/bats-core/bats-core |
| 59 | + ( |
| 60 | + cd bats-core |
| 61 | + git checkout "$BATS_VERSION" |
| 62 | + ./install.sh /usr/local |
| 63 | + cat >>/etc/profile.d/sh.local <<'EOF' |
| 64 | +PATH="/usr/local/bin:$PATH" |
| 65 | +export PATH |
| 66 | +EOF |
| 67 | + cat >/etc/sudoers.d/local <<'EOF' |
| 68 | +Defaults secure_path = "/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" |
| 69 | +EOF |
| 70 | + ) |
| 71 | + rm -rf bats-core |
| 72 | + ) |
| 73 | +else |
| 74 | + "${DNF[@]}" install bats |
| 75 | +fi |
| 76 | + |
| 77 | +# Clean up DNF |
| 78 | +dnf clean all |
| 79 | + |
| 80 | +# Setup rootless user. |
| 81 | +"$SCRIPTDIR"/setup_rootless.sh |
| 82 | + |
| 83 | +# Delegate all cgroup v2 controllers to rootless user via --systemd-cgroup |
| 84 | +if [ -e /sys/fs/cgroup/cgroup.controllers ]; then |
| 85 | + mkdir -p /etc/systemd/system/user@.service.d |
| 86 | + cat >/etc/systemd/system/user@.service.d/delegate.conf <<'EOF' |
| 87 | +[Service] |
| 88 | +# The default (since systemd v252) is "pids memory cpu". |
| 89 | +Delegate=yes |
| 90 | +EOF |
| 91 | + systemctl daemon-reload |
| 92 | +fi |
0 commit comments