oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1#!/bin/sh
2# The first program in Equipo's CI box (scripts/ci-box.py run). It starts
3# as root, makes the box look like a runner host, and runs the command as
4# the job's own user:
5#
6# - /etc on a filesystem of its own. Containment lays an overlay over
7# /etc, and the kernel refuses one whose lower layer is the
8# container's own overlay root;
9# - a machine id, which a real host has and the image does not;
10# - a home directory, with the pinned programs where tests look for them,
11# and a git identity, without which git refuses to commit;
12# - a cgroup v2 subtree the user may write, at the path systemd gives a
13# user session. The cgroup move test moves a contained process between
14# two cgroups there, as a person on a runner host can.
15#
16# Tests never run as root: bubblewrap and file permissions behave
17# differently for root, and no runner runs as root. The one exception is a
18# rootless engine on a developer's machine, whose root is the caller.
19set -eu
20
21uid=${EQUIPO_BOX_UID:-0}
22gid=${EQUIPO_BOX_GID:-0}
23home=/home/ci
24
25if [ "$(id -u)" != 0 ]; then
26 exec "$@"
27fi
28
29mkdir -p /run/equipo-etc
30mount -t tmpfs -o mode=0755 tmpfs /run/equipo-etc
31cp -a /etc/. /run/equipo-etc/
32mount --bind /run/equipo-etc /etc
33[ -s /etc/machine-id ] || echo 0ec1b0c1e9015c1b0c5e9015c1b0c1e9 >/etc/machine-id
34
35grep -q '^ci:' /etc/passwd || [ "$uid" = 0 ] || echo "ci:x:$uid:$gid:Equipo CI:$home:/bin/sh" >>/etc/passwd
36grep -q '^ci:' /etc/group || [ "$gid" = 0 ] || echo "ci:x:$gid:" >>/etc/group
37mkdir -p "$home/.local/share/equipo/try"
38chown "$uid:$gid" "$home" "$home/.local" "$home/.local/share" "$home/.local/share/equipo" "$home/.local/share/equipo/try"
39
40base=/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service
41if mkdir -p "$base/ci.scope" 2>/dev/null && echo $$ >"$base/ci.scope/cgroup.procs" 2>/dev/null; then
42 for file in "$base" "$base/cgroup.procs" "$base/cgroup.threads" "$base/cgroup.subtree_control" \
43 "$base/ci.scope" "$base/ci.scope/cgroup.procs" "$base/ci.scope/cgroup.threads" "$base/ci.scope/cgroup.subtree_control"; do
44 chown "$uid:$gid" "$file"
45 done
46else
47 echo "ci-box: no writable cgroup v2 tree here; the cgroup move test will fail" >&2
48fi
49
50printf '[user]\n\tname = Equipo CI\n\temail = ci@equipo.invalid\n' >"$home/.gitconfig"
51[ "$uid" != 0 ] || printf '[safe]\n\tdirectory = *\n' >>"$home/.gitconfig"
52chown "$uid:$gid" "$home/.gitconfig"
53export HOME="$home"
54if [ "$uid" = 0 ]; then
55 exec "$@"
56fi
57export USER=ci LOGNAME=ci
58exec setpriv --reuid "$uid" --regid "$gid" --clear-groups -- "$@"
59