#!/usr/bin/env bash set -Eeuo pipefail readonly runner_user=akyldash-runner readonly runner_home=/var/lib/akyldash-runner readonly runner_runtime_home="$runner_home/.config/act-runner" readonly runner_xdg_config_home="$runner_home/.config" readonly runner_xdg_data_home="$runner_runtime_home/.local/share" readonly runner_store="$runner_home/docker-data" readonly runner_image="$runner_home/docker-store.img" readonly runner_mount_unit_name="$(systemd-escape --path --suffix=mount /var/lib/akyldash-runner/docker-data)" readonly runner_mount_unit="/etc/systemd/system/$runner_mount_unit_name" readonly runner_image_bytes=16106127360 readonly runner_version=0.2.13 readonly runner_sha256=3acac8b506ac8cadc88a55155b5d6378f0fab0b8f62d1e0c0450f4ccd69733e2 readonly runner_url="https://dl.gitea.com/gitea-runner/$runner_version/act_runner-$runner_version-linux-amd64" readonly checkout=/opt/akyldash/current die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; } [[ $EUID -eq 0 ]] || die 'run this script as root' [[ -r /etc/os-release ]] || die 'cannot identify operating system' . /etc/os-release [[ ${ID:-} == ubuntu && ${VERSION_ID:-} == 24.04 ]] || die 'Ubuntu 24.04 is required' [[ $(uname -m) == x86_64 ]] || die 'this pinned binary is for x86_64 only' [[ $(stat -fc %T /sys/fs/cgroup) == cgroup2fs ]] || die 'rootless Docker requires cgroup v2' [[ $(findmnt -n -o FSTYPE /) == ext4 ]] || die 'the bounded runner store currently requires ext4 on /' [[ -d $checkout/.git && ! -L $checkout && $(stat -c %U "$checkout") == root ]] || die 'install the reviewed root-owned checkout at /opt/akyldash/current first' for directory in /opt /opt/akyldash; do [[ $(stat -c %U "$directory") == root ]] || die "$directory must be owned by root" mode=$(stat -c %a "$directory") (( (8#$mode & 022) == 0 )) || die "$directory must not be group/world-writable" done unsafe=$(find "$checkout" -xdev \( ! -user root -o -perm /022 \) -print -quit) [[ -z $unsafe ]] || die 'the repository checkout contains files writable by non-root users' git -C "$checkout" diff --quiet HEAD -- infra/runner || die 'runner files differ from the reviewed Git revision' source "$checkout/infra/runner/install-runner-lib.sh" if systemctl is-active --quiet akyldash-act-runner.service; then die 'stop akyldash-act-runner.service after all jobs finish before rerunning this installer' fi [[ -r /etc/subuid && -r /etc/subgid ]] || die 'subordinate ID allocation files are unavailable' if getent passwd "$runner_user" >/dev/null; then runner_ensure_account "$runner_user" "$runner_home" || die 'runner account validation failed' runner_lock_home "$runner_home" || die 'runner home protection failed' else [[ ! -e $runner_home && ! -L $runner_home ]] || die 'runner home exists without its account; inspect it before continuing' fi if [[ -e $runner_image || -L $runner_image ]]; then runner_ensure_image "$runner_image" "$runner_image_bytes" 0 711 || die 'runner image validation failed' else [[ $(df -Pk / | awk 'NR==2 {print $4}') -ge 25000000 ]] || die 'need at least 25 GiB free before reserving 15 GiB for runner data' fi for file in /etc/subuid /etc/subgid; do awk -F: '!/^[[:space:]]*#/ && !/^[[:space:]]*$/ && (NF != 3 || $2 !~ /^[0-9]+$/ || $3 !~ /^[0-9]+$/ || $3 < 1) { bad=1 } END { exit bad }' "$file" || die "$file contains an invalid subordinate ID entry" done subuid_pair=$(awk -F: -v user="$runner_user" '$1 == user { count++; pair=$2 ":" $3 } END { if (count > 1) exit 2; if (count == 1) print pair }' /etc/subuid) || die 'runner has multiple subuid allocations; inspect them before continuing' subgid_pair=$(awk -F: -v user="$runner_user" '$1 == user { count++; pair=$2 ":" $3 } END { if (count > 1) exit 2; if (count == 1) print pair }' /etc/subgid) || die 'runner has multiple subgid allocations; inspect them before continuing' for pair in "$subuid_pair" "$subgid_pair"; do [[ -z $pair || $pair =~ ^[0-9]+:65536$ ]] || die 'existing runner subordinate ID allocation is not a single 65536-ID range' done if [[ -n $subuid_pair && -n $subgid_pair && $subuid_pair != "$subgid_pair" ]]; then die 'runner subuid and subgid allocations differ; inspect them before continuing' fi existing_pair=${subuid_pair:-$subgid_pair} if [[ -n $existing_pair ]]; then subid_start=${existing_pair%%:*} [[ $subid_start =~ ^[1-9][0-9]*$ && $subid_start -ge 100000 && $subid_start -le 4294901760 ]] || die 'existing runner subordinate ID range is outside the supported range' subid_end=$((subid_start + 65535)) subid_range="$subid_start-$subid_end" else subid_range=$(awk -f "$(dirname "$0")/find-free-subid-range.awk" /etc/subuid /etc/subgid) || die 'could not find a free 65536-ID range shared by /etc/subuid and /etc/subgid' subid_start=${subid_range%-*} subid_end=${subid_range#*-} fi for file in /etc/subuid /etc/subgid; do awk -F: -v user="$runner_user" -v start="$subid_start" -v end="$subid_end" '$1 != user && $2 <= end && $2 + $3 - 1 >= start { conflict=1 } END { exit conflict }' "$file" || die "runner subordinate ID range conflicts with another entry in $file" done apt-get update DEBIAN_FRONTEND=noninteractive apt-get install -y uidmap dbus-user-session curl logrotate docker-ce-rootless-extras runner_ensure_account "$runner_user" "$runner_home" || die 'runner account creation failed' runner_lock_home "$runner_home" || die 'runner home protection failed' subid_args=() [[ -n $subuid_pair ]] || subid_args+=(--add-subuids "$subid_range") [[ -n $subgid_pair ]] || subid_args+=(--add-subgids "$subid_range") if ((${#subid_args[@]})); then usermod "${subid_args[@]}" "$runner_user" fi runner_uid=$(id -u "$runner_user") runner_ensure_private_directory "$runner_home/.config" "$runner_user" "$runner_user" || die 'runner config directory failed validation' runner_ensure_private_directory "$runner_home/.config/docker" "$runner_user" "$runner_user" || die 'runner Docker config directory failed validation' runner_ensure_private_directory "$runner_home/.config/systemd" "$runner_user" "$runner_user" || die 'runner systemd config directory failed validation' runner_ensure_private_directory "$runner_home/.config/systemd/user" "$runner_user" "$runner_user" || die 'runner user systemd directory failed validation' runner_ensure_private_directory "$runner_home/.config/systemd/user/docker.service.d" "$runner_user" "$runner_user" || die 'runner Docker override directory failed validation' runner_ensure_private_directory "$runner_runtime_home" "$runner_user" "$runner_user" || die 'runner runtime home failed directory validation' runner_ensure_private_directory "$runner_runtime_home/.local" "$runner_user" "$runner_user" || die 'runner XDG data parent failed directory validation' runner_ensure_private_directory "$runner_xdg_data_home" "$runner_user" "$runner_user" || die 'runner XDG data directory failed directory validation' if [[ -e $runner_home/.runner || -L $runner_home/.runner ]]; then [[ -f $runner_home/.runner && ! -L $runner_home/.runner ]] || die 'legacy runner registration state is not a regular file' if [[ ! -e $runner_runtime_home/.runner && ! -L $runner_runtime_home/.runner ]]; then runner_install_user_content "$runner_runtime_home/.runner" "$runner_user" "$runner_user" 0600 <"$runner_home/.runner" || die 'legacy runner registration state could not be preserved' fi fi if [[ ! -e $runner_store && ! -L $runner_store ]]; then install -d -o root -g root -m 0755 "$runner_store" fi [[ -d $runner_store && ! -L $runner_store ]] || die 'runner mount path is not a directory' install -d -o root -g "$runner_user" -m 0750 /etc/akyldash-runner install -d -o "$runner_user" -g "$runner_user" -m 0750 /var/log/akyldash-runner runner_ensure_image "$runner_image" "$runner_image_bytes" 0 711 || die 'runner image initialization failed' runner_ensure_mount "$runner_image" "$runner_store" "$runner_mount_unit_name" "$runner_mount_unit" || die 'runner mount activation failed' chown "$runner_user:$runner_user" "$runner_store" chmod 0700 "$runner_store" runner_install_file "$(dirname "$0")/act-runner-healthcheck.sh" /usr/local/sbin/akyldash-runner-healthcheck root root 0755 || die 'healthcheck command differs from reviewed configuration' runner_install_file "$(dirname "$0")/config.yaml" /etc/akyldash-runner/config.yaml root "$runner_user" 0640 || die 'runner config differs from reviewed configuration' if [[ -e /usr/local/bin/act_runner || -L /usr/local/bin/act_runner ]]; then [[ -f /usr/local/bin/act_runner && ! -L /usr/local/bin/act_runner ]] || die 'existing act_runner path is not a regular file' printf '%s %s\n' "$runner_sha256" /usr/local/bin/act_runner | sha256sum --check --status || die 'existing act_runner binary does not match the pinned release' else tmp_binary=$(mktemp) trap 'rm -f "$tmp_binary"' EXIT curl --fail --location --silent --show-error "$runner_url" -o "$tmp_binary" printf '%s %s\n' "$runner_sha256" "$tmp_binary" | sha256sum --check --status || die 'official act_runner checksum mismatch' install -o root -g root -m 0755 "$tmp_binary" /usr/local/bin/act_runner rm -f "$tmp_binary" trap - EXIT fi runner_install_user_content "$runner_home/.config/docker/daemon.json" "$runner_user" "$runner_user" 0600 </dev/null 2>&1 && exit 0; sleep 2; done; exit 1' ExecStart=/usr/local/bin/act_runner --config /etc/akyldash-runner/config.yaml daemon Restart=always RestartSec=10 CPUQuota=50% MemoryMax=768M TasksMax=256 NoNewPrivileges=yes PrivateTmp=yes ProtectSystem=strict ProtectHome=read-only ProtectKernelTunables=yes ProtectKernelModules=yes ProtectKernelLogs=yes RestrictSUIDSGID=yes ReadWritePaths=$runner_runtime_home $runner_xdg_config_home /var/log/akyldash-runner /run/user/$runner_uid StandardOutput=append:/var/log/akyldash-runner/act_runner.log StandardError=append:/var/log/akyldash-runner/act_runner.log [Install] WantedBy=multi-user.target EOF install -d -o root -g root -m 0755 "/etc/systemd/system/user-$runner_uid.slice.d" runner_install_content "/etc/systemd/system/user-$runner_uid.slice.d/limits.conf" root root 0644 <