diff --git a/CLAUDE.md b/CLAUDE.md index b9173db..613d9a2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -5,7 +5,7 @@ Guidance for Claude Code (claude.ai/code) in this repository. ## What this is `mortis.kamino`: an Ansible Collection (GPL-3.0-or-later, REUSE-compliant) of reusable, -hardened roles. Roles arrive here wave by wave; `docker` is in 1.0.0, `fail2ban` in 1.1.0, `network_interfaces` and `systemd_hardening` in 1.2.0, `system_baseline` in 1.3.0, `ufw` in 1.4.0; 1.5.0 adds no role (new `system_baseline` options); `sshd` in 1.6.0; `user_management` in 1.7.0; `traefik`, with the internal `_binary_update`, in 1.8.0; `gitea` in 1.9.0. +hardened roles. Roles arrive here wave by wave; `docker` is in 1.0.0, `fail2ban` in 1.1.0, `network_interfaces` and `systemd_hardening` in 1.2.0, `system_baseline` in 1.3.0, `ufw` in 1.4.0; 1.5.0 adds no role (new `system_baseline` options); `sshd` in 1.6.0; `user_management` in 1.7.0; `traefik`, with the internal `_binary_update`, in 1.8.0; `gitea` in 1.9.0; `gitea_runner` in the next release. **Roadmap.** The roles still to come, and the ones that will never be extracted, are listed in the README section "Roadmap". Context: the maintainer's private environment repository (a diff --git a/README.md b/README.md index cc3be0c..4e7a6d7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ An Ansible Collection of reusable, hardened roles for a self-hosted home lab: Linux baseline, firewall, SSH, user management and, in later releases, Proxmox, Gitea and Gitea Actions runner roles. -Status: roles arrive in waves. Release 1.9.0 holds the `docker`, `fail2ban`, `gitea`, `network_interfaces`, `sshd`, `system_baseline`, `systemd_hardening`, `traefik`, `ufw` and `user_management` roles (and the internal `_binary_update`, which `gitea` and `traefik` call). +Status: roles arrive in waves. Release 1.9.0 holds the `docker`, `fail2ban`, `gitea`, `network_interfaces`, `sshd`, `system_baseline`, `systemd_hardening`, `traefik`, `ufw` and `user_management` roles (and the internal `_binary_update`, which `gitea` and `traefik` call; `gitea_runner`, still unreleased, calls it too). ## Consuming @@ -40,7 +40,7 @@ the environment that uses it) are met. Still to come: -- Wave 2: `pve_vm`, `pve_lxc`, `gitea_runner`, `tailscale`, `cloudflared`, `pihole`. +- Wave 2: `pve_vm`, `pve_lxc`, `tailscale`, `cloudflared`, `pihole`. - Wave 3: `rke2`, `proxmox_host`. - Later, wave not yet assigned: `opnsense`, `proxmox_firewall`. diff --git a/changelogs/fragments/gitea-runner-role.yml b/changelogs/fragments/gitea-runner-role.yml new file mode 100644 index 0000000..4f03edc --- /dev/null +++ b/changelogs/fragments/gitea-runner-role.yml @@ -0,0 +1,10 @@ +--- +minor_changes: + - >- + gitea_runner - New role. Installs a Gitea Actions runner (``gitea-runner``) with Docker as a systemd service, with a + sha256-verified binary, registers it with a token minted on a Gitea host (optionally scoped to an owner or + repository), renders ``config.yaml``, and optionally schedules a weekly Docker prune that skips while a job runs, + installs pinned molecule test tooling behind a host-executor label, and removes the runner on request. With + ``gitea_runner_update`` it downloads and verifies a new release before stopping anything and swaps it in through + the internal ``_binary_update`` role, with rollback on failure. A forced re-registration mints the new token before + it touches the current registration and is refused under CI. diff --git a/roles/gitea_runner/README.md b/roles/gitea_runner/README.md new file mode 100644 index 0000000..6f7b766 --- /dev/null +++ b/roles/gitea_runner/README.md @@ -0,0 +1,100 @@ +# gitea_runner + +Installs a Gitea Actions runner (`gitea-runner`) with Docker as a systemd service, registers it against a Gitea instance with a token minted on the `gitea_runner_token_host`, renders its `config.yaml`, optionally schedules a Docker prune, installs molecule test tooling behind a host-executor label, and optionally updates or removes the runner. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth. + +## Supported platforms + +Ubuntu 24.04 (noble), the image the molecule scenario runs on. The role uses `ansible.builtin.apt` and installs `docker.io`, so Debian-family hosts only, and needs outbound access to dl.gitea.com and Docker Hub (the Docker check pulls `ubuntu:24.04`); with the molecule tooling enabled also PyPI and Ansible Galaxy. On an unprivileged LXC host, Docker needs nesting enabled on the container. + +## Requirements and run order + +- Use it by its fully qualified name, `mortis.kamino.gitea_runner`. +- Hard dependency on the token host: `tasks/mint_token.yml` delegates `gitea actions generate-runner-token` to the inventory host named by `gitea_runner_token_host`. That host must be in the inventory and must already have Gitea installed (for example by `mortis.kamino.gitea`), with `app.ini` in place. A first install of the runner fails when it is down or has no Gitea. Registration only runs when `/.runner` does not exist, so later runs do not need it. Under `--check` the registration is reported and not run. +- Registration inputs: `gitea_runner_token_host`, `gitea_runner_instance_url` and `gitea_runner_name` have empty role defaults and are asserted non-empty only in `tasks/mint_token.yml`, so a run that never registers is not blocked. +- Keep in step with the Gitea installation on the token host: the delegated command uses `gitea_runner_gitea_user`, `gitea_runner_gitea_install_dir` and `gitea_runner_gitea_app_dir` (defaults `git`, `/usr/local/bin`, `/etc/gitea`, the same as the defaults of `mortis.kamino.gitea`). They are separate variables from the `gitea` role's: changing the `gitea` role's values on the token host does not change these, and registration then runs the wrong path or user there. Set both. +- The registration token is generated on the fly, held in a `no_log` fact and passed to `gitea-runner register` through the `GITEA_RUNNER_REGISTRATION_TOKEN` environment variable, not argv. It is never written to a file by the role. +- The role creates `gitea_runner_group` and `gitea_runner_user` when they are missing; an existing account (for example the SSH login user) is reused. +- Docker is installed by this role (`tasks/docker.yml`) on first install, not by `mortis.kamino.docker`. +- Fact gathering must be on for `gitea_runner_force_register`: the backup name uses `ansible_date_time`. +- Updates: the update path stages the verified binary and hands the swap to `mortis.kamino._binary_update`, an internal role of this Collection. +- `gitea_runner_version` is asserted at role start to be a plain `X.Y.Z` release number, with no leading `v`. Pin it in inventory to move the runner independently of Collection releases; the role default changes only with a release. +- The executor and job image of a job come from its label's suffix (`:host` or `docker://`); a label with no suffix runs its jobs on the host. `config.yaml` has no keys for them. + +## Role variables + +| Variable | Default | Description | +|---|---|---| +| `gitea_runner_remove` | `false` | Stop and disable the service, remove the unit, the binary and the Docker prune units. The role ends there: nothing is installed or registered in the same run, and the runner is not deregistered in Gitea. | +| `gitea_runner_remove_data` | `false` | With `gitea_runner_remove`, also delete the work directory, including the registration. | +| `gitea_runner_force_register` | `false` | Mint the token first (a failed mint stops before anything is touched), then back up `.runner`, stop the service, remove `.runner` and register again with the current `gitea_runner_instance_url` and `gitea_runner_registration_scope`; configure starts the service. Only the newest backup is kept. Refused when `GITHUB_ACTIONS`, `GITEA_ACTIONS` or `CI` is set on the controller; under `--check` nothing is removed or registered. | +| `gitea_runner_update` | `false` | Run the update tasks when the installed version differs from `gitea_runner_version`: download and verify to a staging path, then `mortis.kamino._binary_update` stops, backs up, swaps, restarts, checks the version and rolls back on failure. | +| `gitea_runner_version` | pinned in `defaults/main.yml` | Release to install, `X.Y.Z`. | +| `gitea_runner_download_url` | dl.gitea.com, linux amd64 (templated) | Binary URL. | +| `gitea_runner_checksum_url` | dl.gitea.com `.sha256` (templated) | Checksum file the runner binary is verified against (sha256) on install and update; it must list the file name of `gitea_runner_download_url`. | +| `gitea_runner_user` / `gitea_runner_group` | `gitea-runner` / `gitea-runner` | Daemon account; the user joins the `docker` group. | +| `gitea_runner_home` | `/home/gitea-runner` | Home of the daemon account. | +| `gitea_runner_install_dir` | `/usr/local/bin` | Binary directory. | +| `gitea_runner_work_dir` | `/var/lib/gitea-runner` | Registration file and `config.yaml`. Not passed to the runner as a job work directory: jobs use the upstream defaults. | +| `gitea_runner_service_unit` | `/etc/systemd/system/gitea-runner.service` | Unit path. | +| `gitea_runner_name` | empty (asserted when registering) | Registered runner name. | +| `gitea_runner_instance_url` | empty (asserted when registering) | Instance to register against. | +| `gitea_runner_token_host` | empty (asserted when registering) | Inventory host that mints the registration token. | +| `gitea_runner_registration_scope` | empty (instance-wide) | `--scope` of the token: an owner (user or organisation) or `owner/repo`. Format asserted at role start; read only when registering. | +| `gitea_runner_gitea_install_dir` | `/usr/local/bin` | Gitea binary directory on the token host. | +| `gitea_runner_gitea_user` | `git` | User of the delegated command on the token host. | +| `gitea_runner_gitea_app_dir` | `/etc/gitea` | `app.ini` directory on the token host. | +| `gitea_runner_log_level` | `info` | `log.level`. | +| `gitea_runner_capacity` | `1` | `runner.capacity`, concurrent jobs. | +| `gitea_runner_labels` | `docker`, `ubuntu-lxc` | Workflow labels; the molecule label is appended when enabled. | +| `gitea_runner_container_privileged` | `false` | `container.privileged`. | +| `gitea_runner_container_docker_host` | `unix:///var/run/docker.sock` | `container.docker_host`; `-` keeps the socket out of job containers. | +| `gitea_runner_container_volumes` | none (commented example in defaults) | Extra job container mounts; the key is omitted when undefined. | +| `gitea_runner_docker_prune_enabled` | `false` | Weekly systemd timer that prunes Docker data; skipped while a job container runs. | +| `gitea_runner_docker_prune_schedule` | `Sun *-*-* 04:30:00` | `OnCalendar` of the prune timer. | +| `gitea_runner_docker_prune_container_age`, `_image_age` | `24h`, `168h` | `until` filters (creation time) for containers, and for images and build cache. | +| `gitea_runner_docker_prune_job_max_age` | `21600` | Seconds; the prune skips while a younger container runs and ignores older ones as leaked job containers. | +| `gitea_runner_molecule_enabled` | `false` | Install molecule tooling and add the host label. | +| `gitea_runner_molecule_label` | `molecule` | Host-executor label name, registered as `