Files
Kamino/roles/gitea_runner/tasks/prune.yml
T
Defame1297 e91c074373 fix(gitea_runner): address review of the prune count and threshold
Give each new input its own assert message so every refused-input case
matches only its own input, and cover a negative count and a bad disk
check schedule. Fire the pressure prune only above the threshold, as
the inputs describe it. Name the disk check script apart from the
pressure service, prefix the new registers as internal, and keep the
new comments short.

Refs #319

Assisted-by: Claude Code
2026-11-23 18:20:32 +00:00

237 lines
9.6 KiB
YAML

---
# Weekly Docker cleanup. Every pinned image version stays on disk after a bump, so without
# this the disk of a busy runner fills and every job fails at container setup.
# The settings are validated in validate.yml.
# ExecCondition runs the gate script below: it skips the run (not a failure) while a container
# younger than gitea_runner_docker_prune_job_max_age runs, so a prune never removes an image
# or container a job needs. An older container is a leaked job container (a job cannot run
# past its timeout): it is logged and ignored, or one leak would block the prune for good.
# The window between that check and the prune is a few seconds; a job started inside it can
# fail once and is re-run. A skipped run is not retried before the next timer elapse: on a busy
# week set a shorter gitea_runner_docker_prune_schedule (e.g. daily).
# The ExecStart= lines start with '-' so a failing step does not skip the later ones.
# Named volumes are never pruned (docker volume prune without -a removes anonymous ones only).
- name: Install Docker prune gate script
become: true
ansible.builtin.copy:
dest: /usr/local/sbin/gitea-runner-docker-prune-gate
owner: root
group: root
mode: "0755"
# raw: the Go template braces are not Jinja. The tag shares the shebang line: Jinja keeps the
# newline after {% raw %}, and a file that does not start with "#!" cannot be exec'd by systemd.
content: |
{% raw %}#!/bin/sh
# Exit 0 when no container younger than $1 seconds runs (prune may proceed), 1 to skip.
# Every failure skips: the prune must not run when the state is unknown.
max_age="$1"
case "$max_age" in
'' | *[!0-9]*) echo "invalid max age '$max_age'"; exit 1 ;;
esac
now=$(date +%s)
ids=$(/usr/bin/docker ps -q) || { echo "docker ps failed"; exit 1; }
for id in $ids; do
started=$(/usr/bin/docker inspect --format '{{.State.StartedAt}}' "$id") || { echo "cannot inspect $id"; exit 1; }
started=$(date -d "$started" +%s) || { echo "cannot parse start time of $id"; exit 1; }
age=$((now - started))
if [ "$age" -lt "$max_age" ]; then
echo "container $id is $age s old: a job may be running, skipping the prune"
exit 1
fi
echo "ignoring container $id, running for $age s (leaked job container)"
done
exit 0
{% endraw %}
when: gitea_runner_docker_prune_enabled | bool
tags: [gitea_runner, prune]
# Used by the weekly and pressure services when gitea_runner_docker_prune_keep_per_repository > 0.
- name: Install Docker prune count script
become: true
ansible.builtin.copy:
src: gitea-runner-docker-prune-keep
dest: /usr/local/sbin/gitea-runner-docker-prune-keep
owner: root
group: root
mode: "0755"
when: gitea_runner_docker_prune_enabled | bool
tags: [gitea_runner, prune]
- name: Install Docker prune service
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/gitea-runner-docker-prune.service
owner: root
group: root
mode: "0644"
content: |
[Unit]
Description=Prune unused Docker data on the Gitea runner host
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
ExecCondition=/usr/local/sbin/gitea-runner-docker-prune-gate {{ gitea_runner_docker_prune_job_max_age }}
ExecStart=-/usr/bin/docker container prune --force --filter until={{ gitea_runner_docker_prune_container_age }}
ExecStart=-/usr/bin/docker image prune --all --force --filter until={{ gitea_runner_docker_prune_image_age }}
ExecStart=-/usr/bin/docker builder prune --force --filter until={{ gitea_runner_docker_prune_image_age }}
ExecStart=-/usr/bin/docker volume prune --force
{% if gitea_runner_docker_prune_keep_per_repository | int > 0 %}
ExecStart=-/usr/local/sbin/gitea-runner-docker-prune-keep {{ gitea_runner_docker_prune_keep_per_repository }}
{% endif %}
register: gitea_runner_prune_service
when: gitea_runner_docker_prune_enabled | bool
tags: [gitea_runner, prune]
- name: Install Docker prune timer
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/gitea-runner-docker-prune.timer
owner: root
group: root
mode: "0644"
content: |
[Unit]
Description=Scheduled Docker prune on the Gitea runner host
[Timer]
OnCalendar={{ gitea_runner_docker_prune_schedule }}
Persistent=true
RandomizedDelaySec=10m
[Install]
WantedBy=timers.target
register: gitea_runner_prune_timer
when: gitea_runner_docker_prune_enabled | bool
tags: [gitea_runner, prune]
# Pressure path: above the disk threshold, the count prune and a capped builder prune run behind
# the same gate. There is no low watermark: if usage stays high, the count or the disk needs a human.
# The builder cache is capped because job containers share the host's docker.sock by default.
- name: Install the pressure prune when a disk threshold is set
when:
- gitea_runner_docker_prune_enabled | bool
- gitea_runner_docker_prune_disk_threshold_percent | int > 0
tags: [gitea_runner, prune]
block:
- name: Install Docker disk check script
become: true
ansible.builtin.copy:
src: gitea-runner-docker-prune-disk-check
dest: /usr/local/sbin/gitea-runner-docker-prune-disk-check
owner: root
group: root
mode: "0755"
- name: Install Docker pressure prune service
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/gitea-runner-docker-prune-pressure.service
owner: root
group: root
mode: "0644"
content: |
[Unit]
Description=Prune Docker images when the disk of the Gitea runner host is filling up
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
ExecCondition=/usr/local/sbin/gitea-runner-docker-prune-gate {{ gitea_runner_docker_prune_job_max_age }}
ExecCondition=/usr/local/sbin/gitea-runner-docker-prune-disk-check {{ gitea_runner_docker_prune_disk_threshold_percent }}
ExecStart=-/usr/local/sbin/gitea-runner-docker-prune-keep {{ gitea_runner_docker_prune_keep_per_repository }}
ExecStart=-/usr/bin/docker builder prune --force --keep-storage {{ gitea_runner_docker_prune_builder_keep_storage }}
register: __gitea_runner_prune_pressure_service
- name: Install Docker pressure prune timer
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/gitea-runner-docker-prune-pressure.timer
owner: root
group: root
mode: "0644"
content: |
[Unit]
Description=Disk check for the Docker pressure prune on the Gitea runner host
[Timer]
OnCalendar={{ gitea_runner_docker_prune_disk_check_schedule }}
Persistent=true
RandomizedDelaySec=2m
[Install]
WantedBy=timers.target
register: __gitea_runner_prune_pressure_timer
# A threshold set back to 0 turns the pressure path off: the units go, so nothing is left armed.
- name: Stop the Docker pressure prune timer when the disk threshold is 0
become: true
ansible.builtin.systemd:
name: gitea-runner-docker-prune-pressure.timer
state: stopped
enabled: false
register: __gitea_runner_prune_pressure_stop
failed_when:
- __gitea_runner_prune_pressure_stop.failed | default(false)
- "'Could not find the requested service' not in (__gitea_runner_prune_pressure_stop.msg | default(''))"
when:
- gitea_runner_docker_prune_enabled | bool
- gitea_runner_docker_prune_disk_threshold_percent | int == 0
tags: [gitea_runner, prune]
- name: Remove the Docker pressure prune units when the disk threshold is 0
become: true
ansible.builtin.file:
path: "{{ gitea_runner_prune_pressure_file }}"
state: absent
loop:
- /etc/systemd/system/gitea-runner-docker-prune-pressure.timer
- /etc/systemd/system/gitea-runner-docker-prune-pressure.service
- /usr/local/sbin/gitea-runner-docker-prune-disk-check
loop_control:
loop_var: gitea_runner_prune_pressure_file
register: __gitea_runner_prune_pressure_removed
when:
- gitea_runner_docker_prune_enabled | bool
- gitea_runner_docker_prune_disk_threshold_percent | int == 0
tags: [gitea_runner, prune]
- name: Reload systemd for the prune units
become: true
ansible.builtin.systemd:
daemon_reload: true
when:
- gitea_runner_docker_prune_enabled | bool
- >-
gitea_runner_prune_service is changed or gitea_runner_prune_timer is changed
or __gitea_runner_prune_pressure_service is changed or __gitea_runner_prune_pressure_timer is changed
or __gitea_runner_prune_pressure_removed is changed
tags: [gitea_runner, prune]
# Not in check mode: on the first run the unit files above are only simulated, so systemd
# would not find the timer. A changed timer is restarted to re-arm a new schedule.
- name: Enable the Docker prune timer
become: true
ansible.builtin.systemd:
name: gitea-runner-docker-prune.timer
enabled: true
state: "{{ 'restarted' if gitea_runner_prune_timer is changed else 'started' }}"
when:
- gitea_runner_docker_prune_enabled | bool
- not ansible_check_mode
tags: [gitea_runner, prune]
- name: Enable the Docker pressure prune timer
become: true
ansible.builtin.systemd:
name: gitea-runner-docker-prune-pressure.timer
enabled: true
state: "{{ 'restarted' if __gitea_runner_prune_pressure_timer is changed else 'started' }}"
when:
- gitea_runner_docker_prune_enabled | bool
- gitea_runner_docker_prune_disk_threshold_percent | int > 0
- not ansible_check_mode
tags: [gitea_runner, prune]