#!/usr/bin/env bash # # This script is intended to be run during container-image build. Any # other usage outside this context is likely to cause harm. # # Setup script for rootless podman container image to run rootless GitLab Runner service. # Copyright (C) 2023 Christopher C. Evich # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program set -eo pipefail function die() { echo -n '!! ERROR:' >&2 printf " %s\n" "$@" >&2 exit 1 } function check_vars() { for varname in PRUNE_INTERVAL RUNNER_VERSION TARGETARCH; do if [[ -z "${!varname}" ]]; then die "Env. variable '$varname' must be non-empty." fi done } function main() { # Show what's happening to make debugging easier set -x # Make image smaller by not installing docs. dnf=(dnf --setopt=tsflags=nodocs -y) install_packages setup_user setup_service_podman setup_service_runner setup_gitlab_config finalize_ownership } function install_packages() { readarray xpackages < <(grep -vE '^(# )+' :65539` echo -e "podman:1:999\npodman:1002:64536" | tee /etc/subuid >/etc/subgid } function setup_volumes() { # Host volume mount necessary for nested-podman to use overlayfs2 for container & volume storage. mkdir -p /home/podman/.local/share/containers # Nested-container's local container-cache volume mount, recommended by gitlab-runner docs. mkdir -p /cache # Both the gitlab-runner and podman need access to the cache directory / volume mount. chown podman:runner /cache } function setup_service_podman() { # Setup persistent 'podman' user services to start & run without a login. mkdir -p /var/lib/systemd/linger touch /var/lib/systemd/linger/podman # Setup 'podman' socket and a container-storage pruning service for 'podman' user. mkdir -p /home/podman/.config/systemd/user/{sockets.target.wants,default.target.wants} cd /home/podman/.config/systemd/user/ ln -s "$(pwd)/podman.socket" ./sockets.target.wants/ # Added from Containerfile ln -s "$(pwd)/prune.timer" ./default.target.wants/ # also from Containerfile # Substitute value from --build-arg if specified, otherwise use default from Containerfile. sed -i -e "s/@@@PRUNE_INTERVAL@@@/$PRUNE_INTERVAL/" ./prune.timer } function setup_service_runner() { # Setup persistent 'runner' user services to start & run without a login. touch /var/lib/systemd/linger/runner # Setup persistent 'runner' user services to start & run without a login. mkdir -p /home/runner/.config/systemd/user/default.target.wants cd /home/runner/.config/systemd/user/ # Does not depend on podman.socket file availability, will retry if not present. ln -s "$(pwd)/runner.service" ./default.target.wants/ } function setup_gitlab_config() { # gitlab-runner will create side-car '.runner_system_id' file next to 'config.toml' # on first startup. Ensure access is allowed. Also link to future config file # presented as a container-secret. mkdir -p /home/runner/.gitlab-runner ln -s /var/run/secrets/config.toml /home/runner/.gitlab-runner/config.toml chmod -R 700 /home/runner/.gitlab-runner } function finalize_ownership() { # Adjust ownership to all files created after `setup_user`. # and also to the `ADD` instruction in the `Containerfile`. chown -R runner:runner /home/runner chown -R podman:podman /home/podman # Ensure correct permissions of system configuration files. # Somehow these can be set incorrectly during Containerfile # ADD instruction. local path for path in "/etc/systemd/system.conf.d" "/etc/systemd/system/user-.slice.d"; do chown root:root ${path}/* chmod 0644 ${path}/* done } check_vars main