feat(gitea_runner): add the gitea_runner role
Copied from the maintainer's local role and reworked for the Collection: the binary swap and rollback of an update go through the internal _binary_update role, input checks move to tasks/validate.yml, the removal ends the role, commands take argv, and the molecule tooling needs an explicit collections requirements file. Adds a molecule scenario with stub Docker, runner and release files. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
3ed614e00e
commit
e296eea609
28 files changed
+1988
-3
No files matched your search
@@ -0,0 +1,92 @@
|
||||
---
|
||||
- name: Deploy runner config
|
||||
become: true
|
||||
ansible.builtin.template:
|
||||
src: config.yaml.j2
|
||||
dest: "{{ gitea_runner_work_dir }}/config.yaml"
|
||||
owner: "{{ gitea_runner_user }}"
|
||||
group: "{{ gitea_runner_group }}"
|
||||
mode: "0644"
|
||||
notify: Restart Gitea Runner
|
||||
tags: [gitea_runner, configure]
|
||||
|
||||
- name: Ensure runner workdir ownership
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_work_dir }}"
|
||||
owner: "{{ gitea_runner_user }}"
|
||||
group: "{{ gitea_runner_group }}"
|
||||
recurse: true
|
||||
tags: [gitea_runner, configure]
|
||||
|
||||
- name: Check current runner registration file state
|
||||
# become: the work dir is 0750 gitea_runner_user, unreadable when ansible_user is another
|
||||
# account.
|
||||
become: true
|
||||
ansible.builtin.stat:
|
||||
path: "{{ gitea_runner_work_dir }}/.runner"
|
||||
register: gitea_runner_file_state
|
||||
tags: [gitea_runner, configure]
|
||||
|
||||
- name: Restrict runner registration file permissions
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_work_dir }}/.runner"
|
||||
mode: "0600"
|
||||
when: gitea_runner_file_state.stat.exists
|
||||
tags: [gitea_runner, configure]
|
||||
|
||||
- name: Create systemd service
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: "{{ gitea_runner_service_unit }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
content: |
|
||||
[Unit]
|
||||
Description=Gitea Actions Runner
|
||||
After=network.target docker.service
|
||||
Requires=docker.service
|
||||
|
||||
[Service]
|
||||
User={{ gitea_runner_user }}
|
||||
Group={{ gitea_runner_group }}
|
||||
WorkingDirectory={{ gitea_runner_work_dir }}
|
||||
ExecStart={{ gitea_runner_install_dir }}/gitea-runner daemon \
|
||||
--config {{ gitea_runner_work_dir }}/config.yaml
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
TimeoutStopSec=300
|
||||
KillSignal=SIGTERM
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
tags: [gitea_runner, configure]
|
||||
|
||||
- name: Reload systemd
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
tags: [gitea_runner, configure]
|
||||
|
||||
- name: Ensure runner is enabled and running
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: gitea-runner
|
||||
enabled: true
|
||||
state: started
|
||||
tags: [gitea_runner, configure]
|
||||
|
||||
- name: Check runner service status
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: gitea-runner
|
||||
register: gitea_runner_service_status
|
||||
tags: [gitea_runner, configure, health]
|
||||
|
||||
- name: Fail if runner service is not running
|
||||
ansible.builtin.fail:
|
||||
msg: "Gitea runner service is not running"
|
||||
when: gitea_runner_service_status.status.ActiveState != "active"
|
||||
tags: [gitea_runner, configure, health]
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
- name: Install Docker dependencies
|
||||
become: true
|
||||
ansible.builtin.apt:
|
||||
name:
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg
|
||||
- lsb-release
|
||||
state: present
|
||||
update_cache: true
|
||||
tags: [gitea_runner, docker, install]
|
||||
|
||||
- name: Install Docker
|
||||
become: true
|
||||
ansible.builtin.apt:
|
||||
name:
|
||||
- docker.io
|
||||
state: present
|
||||
tags: [gitea_runner, docker, install]
|
||||
|
||||
- name: Enable and start Docker
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: docker
|
||||
enabled: true
|
||||
state: started
|
||||
tags: [gitea_runner, docker, install]
|
||||
|
||||
- name: Add the runner user to the docker group
|
||||
become: true
|
||||
ansible.builtin.user:
|
||||
name: "{{ gitea_runner_user }}"
|
||||
groups: docker
|
||||
append: true
|
||||
tags: [gitea_runner, docker, install]
|
||||
|
||||
# Pulls ubuntu:24.04 from Docker Hub. Also proves Docker runs inside a container host
|
||||
# (an LXC needs nesting enabled).
|
||||
- name: Verify Docker can run a container
|
||||
become: true
|
||||
ansible.builtin.command: docker run --rm ubuntu:24.04 echo ok
|
||||
changed_when: false
|
||||
tags: [gitea_runner, docker, install, health]
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
# The one download of the runner binary, shared by install.yml (straight onto the live path,
|
||||
# since nothing runs yet) and update.yml (into a staging path next to it, before
|
||||
# mortis.kamino._binary_update stops the daemon and swaps it in). get_url checks the published
|
||||
# sha256 before it moves the file to gitea_runner_download_dest, so a failed or tampered
|
||||
# download leaves that path as it was.
|
||||
# Tagged install and update: an include does not pass its tags on to the tasks it includes,
|
||||
# so under --tags update a task tagged install only would be skipped.
|
||||
- name: Download and verify the Gitea Runner binary
|
||||
become: true
|
||||
ansible.builtin.get_url:
|
||||
url: "{{ gitea_runner_download_url }}"
|
||||
dest: "{{ gitea_runner_download_dest }}"
|
||||
checksum: "sha256:{{ gitea_runner_checksum_url }}"
|
||||
mode: "0755"
|
||||
tags: [gitea_runner, install, update]
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
- name: Ensure runner work directory exists
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_work_dir }}"
|
||||
state: directory
|
||||
owner: "{{ gitea_runner_user }}"
|
||||
group: "{{ gitea_runner_group }}"
|
||||
mode: "0750"
|
||||
tags: [gitea_runner, install]
|
||||
|
||||
- name: Download Gitea Runner binary
|
||||
ansible.builtin.include_tasks: download.yml
|
||||
vars:
|
||||
gitea_runner_download_dest: "{{ gitea_runner_install_dir }}/gitea-runner"
|
||||
tags: [gitea_runner, install]
|
||||
@@ -0,0 +1,139 @@
|
||||
---
|
||||
- name: Validate the role inputs
|
||||
ansible.builtin.import_tasks: validate.yml
|
||||
tags: [gitea_runner, always]
|
||||
|
||||
# Re-registering mints a live token and drops the current registration, so it is an
|
||||
# operator action: refused under CI.
|
||||
- name: Refuse forced re-registration under CI
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- lookup('ansible.builtin.env', 'GITHUB_ACTIONS') | length == 0
|
||||
- lookup('ansible.builtin.env', 'GITEA_ACTIONS') | length == 0
|
||||
- lookup('ansible.builtin.env', 'CI') | length == 0
|
||||
fail_msg: >-
|
||||
gitea_runner_force_register refuses to run under CI (GITHUB_ACTIONS, GITEA_ACTIONS or CI is set):
|
||||
run the play by hand from an operator's shell.
|
||||
quiet: true
|
||||
when: gitea_runner_force_register | bool
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
# Mint before anything below backs up, stops or removes the registration, so an unknown
|
||||
# owner or an unreachable token host leaves the current runner running.
|
||||
- name: Mint runner token before forced re-registration
|
||||
ansible.builtin.include_tasks: mint_token.yml
|
||||
when: gitea_runner_force_register | bool
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
# The removal ends the role: none of the tasks below runs, so a removed runner is not
|
||||
# installed and registered again in the same run.
|
||||
- name: Remove Gitea runner (explicit only)
|
||||
ansible.builtin.include_tasks: remove.yml
|
||||
when: gitea_runner_remove | bool
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Install, register, configure and update the runner
|
||||
when: not (gitea_runner_remove | bool)
|
||||
block:
|
||||
- name: Include prerequisites
|
||||
ansible.builtin.include_tasks: prereqs.yml
|
||||
tags: [gitea_runner, prereqs, always]
|
||||
|
||||
- name: Find previous runner registration backups
|
||||
# become: the work dir is 0750 gitea_runner_user, unreadable when ansible_user is another
|
||||
# account.
|
||||
become: true
|
||||
ansible.builtin.find:
|
||||
paths: "{{ gitea_runner_work_dir }}"
|
||||
patterns: ".runner.bak.*"
|
||||
hidden: true
|
||||
register: gitea_runner_old_backups
|
||||
when:
|
||||
- gitea_runner_force_register | bool
|
||||
- gitea_runner_state.stat.exists
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Remove previous runner registration backups
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_old_backup.path }}"
|
||||
state: absent
|
||||
loop: "{{ gitea_runner_old_backups.files | default([]) }}"
|
||||
loop_control:
|
||||
loop_var: gitea_runner_old_backup
|
||||
label: "{{ gitea_runner_old_backup.path }}"
|
||||
when:
|
||||
- gitea_runner_force_register | bool
|
||||
- gitea_runner_state.stat.exists
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Back up runner registration before forced removal
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
src: "{{ gitea_runner_work_dir }}/.runner"
|
||||
dest: "{{ gitea_runner_work_dir }}/.runner.bak.{{ ansible_date_time.iso8601_basic_short }}"
|
||||
remote_src: true
|
||||
owner: "{{ gitea_runner_state.stat.pw_name }}"
|
||||
group: "{{ gitea_runner_state.stat.gr_name }}"
|
||||
mode: "0600"
|
||||
when:
|
||||
- gitea_runner_force_register | bool
|
||||
- gitea_runner_state.stat.exists
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Stop Gitea Runner service before forced re-register
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: gitea-runner
|
||||
state: stopped
|
||||
when:
|
||||
- gitea_runner_force_register | bool
|
||||
- gitea_runner_state.stat.exists
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Remove stale runner registration (force re-register)
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_work_dir }}/.runner"
|
||||
state: absent
|
||||
when: gitea_runner_force_register | bool
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Refresh registration fact after forced removal
|
||||
ansible.builtin.set_fact:
|
||||
gitea_runner_registered: false
|
||||
when: gitea_runner_force_register | bool
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Install Docker
|
||||
ansible.builtin.include_tasks: docker.yml
|
||||
when: not gitea_runner_installed
|
||||
tags: [gitea_runner, docker, install, health]
|
||||
|
||||
- name: Install Gitea runner (first install only)
|
||||
ansible.builtin.include_tasks: install.yml
|
||||
when: not gitea_runner_installed
|
||||
tags: [gitea_runner, install]
|
||||
|
||||
- name: Register runner
|
||||
ansible.builtin.include_tasks: register.yml
|
||||
when: not gitea_runner_registered
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Configure runner
|
||||
ansible.builtin.include_tasks: configure.yml
|
||||
tags: [gitea_runner, configure, health]
|
||||
|
||||
- name: Schedule the Docker prune
|
||||
ansible.builtin.include_tasks: prune.yml
|
||||
tags: [gitea_runner, prune]
|
||||
|
||||
- name: Update runner binary
|
||||
ansible.builtin.include_tasks: update.yml
|
||||
when: gitea_runner_update | bool
|
||||
tags: [gitea_runner, update]
|
||||
|
||||
- name: Install the molecule test tooling
|
||||
ansible.builtin.include_tasks: molecule.yml
|
||||
when: gitea_runner_molecule_enabled | bool
|
||||
tags: [gitea_runner, molecule]
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
# The registration inputs have empty role defaults; refuse to register without them.
|
||||
- name: Require registration inputs
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- gitea_runner_token_host | length > 0
|
||||
- gitea_runner_token_host in groups['all']
|
||||
- gitea_runner_instance_url | length > 0
|
||||
- gitea_runner_name | length > 0
|
||||
fail_msg: >-
|
||||
Registration needs gitea_runner_token_host (inventory host with Gitea that mints the token;
|
||||
it must be a host in the inventory), gitea_runner_instance_url and gitea_runner_name.
|
||||
Set them in group_vars or host_vars.
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
# Nothing here may run under --check: the token is a live credential minted on the
|
||||
# Gitea server. register.yml reports the intended registration instead.
|
||||
- name: Mint the token on the token host
|
||||
when: not ansible_check_mode
|
||||
block:
|
||||
# argv: no value passes through a command string.
|
||||
- name: Generate runner token
|
||||
delegate_to: "{{ gitea_runner_token_host }}"
|
||||
become: true
|
||||
no_log: true
|
||||
ansible.builtin.command:
|
||||
argv: >-
|
||||
{{ ['sudo', '-u', gitea_runner_gitea_user, gitea_runner_gitea_install_dir ~ '/gitea',
|
||||
'--config', gitea_runner_gitea_app_dir ~ '/app.ini', 'actions', 'generate-runner-token']
|
||||
+ (['--scope', gitea_runner_registration_scope] if gitea_runner_registration_scope | length > 0 else []) }}
|
||||
register: gitea_runner_token_cmd
|
||||
failed_when: gitea_runner_token_cmd.rc != 0 or gitea_runner_token_cmd.stdout is not search('\S+')
|
||||
changed_when: false
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Set runner token
|
||||
no_log: true
|
||||
ansible.builtin.set_fact:
|
||||
gitea_runner_token: "{{ gitea_runner_token_cmd.stdout | regex_search('([a-zA-Z0-9]+)', '\\1') | first }}"
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Fail if runner token extraction failed
|
||||
ansible.builtin.fail:
|
||||
msg: "Failed to extract Gitea runner token"
|
||||
when: gitea_runner_token is not defined or gitea_runner_token | length == 0
|
||||
tags: [gitea_runner, register]
|
||||
@@ -0,0 +1,86 @@
|
||||
---
|
||||
# Everything the molecule job needs is installed here, at provision time, so a CI
|
||||
# run only executes tests: pinned tooling in a venv and the collections the roles use.
|
||||
# The role builds no test image: the scenarios are expected to pull theirs from a registry.
|
||||
# gitea_runner_collections_requirements_file is asserted non-empty in validate.yml.
|
||||
- name: Install molecule venv prerequisites
|
||||
become: true
|
||||
ansible.builtin.apt:
|
||||
name:
|
||||
- python3-venv
|
||||
- python3-pip
|
||||
- git
|
||||
# community.docker.docker_image runs on this host via the Docker SDK.
|
||||
- python3-requests
|
||||
- python3-docker
|
||||
state: present
|
||||
cache_valid_time: 3600
|
||||
tags: [gitea_runner, molecule]
|
||||
|
||||
- name: Install pinned molecule tooling into its venv
|
||||
become: true
|
||||
ansible.builtin.pip:
|
||||
name:
|
||||
- "molecule=={{ gitea_runner_molecule_version }}"
|
||||
- "molecule-plugins[docker]=={{ gitea_runner_molecule_plugins_version }}"
|
||||
- "ansible-core=={{ gitea_runner_molecule_ansible_core_version }}"
|
||||
- "docker=={{ gitea_runner_molecule_docker_sdk_version }}"
|
||||
virtualenv: "{{ gitea_runner_molecule_venv }}"
|
||||
virtualenv_command: python3 -m venv
|
||||
tags: [gitea_runner, molecule]
|
||||
|
||||
# The collections come from gitea_runner_collections_requirements_file and are
|
||||
# reinstalled when it changes. requirements.sha256 in the collections path records the
|
||||
# checksum of the file the installed set came from; it is written only after a
|
||||
# successful install, so a failed install is retried on the next run (a guard on the
|
||||
# copied file changing would skip it forever). --force: without it ansible-galaxy skips
|
||||
# a collection it finds on any other collections path of this host and installs
|
||||
# nothing into -p, leaving the molecule job without the modules it needs.
|
||||
- name: Copy the pinned collections file to the runner host
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
src: "{{ gitea_runner_collections_requirements_file }}"
|
||||
dest: "{{ gitea_runner_home }}/molecule-requirements.yml"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
tags: [gitea_runner, molecule]
|
||||
|
||||
- name: Read the requirements checksum of the installed collections
|
||||
become: true
|
||||
ansible.builtin.slurp:
|
||||
src: "{{ gitea_runner_molecule_collections_path }}/requirements.sha256"
|
||||
register: gitea_runner_molecule_collections_stamp
|
||||
failed_when: false
|
||||
tags: [gitea_runner, molecule]
|
||||
|
||||
- name: Install the collections the tested roles use
|
||||
when: gitea_runner_molecule_collections_stamp.content | default('') | b64decode | trim != gitea_runner_molecule_requirements_sha256
|
||||
vars:
|
||||
gitea_runner_molecule_requirements_sha256: "{{ lookup('ansible.builtin.file', gitea_runner_collections_requirements_file) | hash('sha256') }}"
|
||||
tags: [gitea_runner, molecule]
|
||||
block:
|
||||
- name: Install the collections of the requirements file
|
||||
become: true
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- "{{ gitea_runner_molecule_venv }}/bin/ansible-galaxy"
|
||||
- collection
|
||||
- install
|
||||
- --force
|
||||
- -r
|
||||
- "{{ gitea_runner_home }}/molecule-requirements.yml"
|
||||
- -p
|
||||
- "{{ gitea_runner_molecule_collections_path }}"
|
||||
changed_when: true
|
||||
tags: [gitea_runner, molecule]
|
||||
|
||||
- name: Record the checksum of the installed collections
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
content: "{{ gitea_runner_molecule_requirements_sha256 }}\n"
|
||||
dest: "{{ gitea_runner_molecule_collections_path }}/requirements.sha256"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
tags: [gitea_runner, molecule]
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
# The user module does not create a named primary group; on a fresh host it would fail with
|
||||
# "Group <gitea_runner_group> does not exist".
|
||||
- name: Ensure runner group exists
|
||||
become: true
|
||||
ansible.builtin.group:
|
||||
name: "{{ gitea_runner_group }}"
|
||||
system: true
|
||||
tags: [gitea_runner, prereqs, always]
|
||||
|
||||
- name: Ensure runner user exists
|
||||
become: true
|
||||
ansible.builtin.user:
|
||||
name: "{{ gitea_runner_user }}"
|
||||
group: "{{ gitea_runner_group }}"
|
||||
home: "{{ gitea_runner_home }}"
|
||||
create_home: true
|
||||
system: true
|
||||
tags: [gitea_runner, prereqs, always]
|
||||
|
||||
- name: Check if runner is installed
|
||||
ansible.builtin.stat:
|
||||
path: "{{ gitea_runner_install_dir }}/gitea-runner"
|
||||
register: gitea_runner_bin
|
||||
tags: [gitea_runner, prereqs, always]
|
||||
|
||||
- name: Check if runner is registered
|
||||
# become: the work dir is 0750 gitea_runner_user, unreadable when ansible_user is another
|
||||
# account.
|
||||
become: true
|
||||
ansible.builtin.stat:
|
||||
path: "{{ gitea_runner_work_dir }}/.runner"
|
||||
register: gitea_runner_state
|
||||
tags: [gitea_runner, prereqs, always]
|
||||
|
||||
- name: Set runner facts
|
||||
ansible.builtin.set_fact:
|
||||
gitea_runner_installed: "{{ gitea_runner_bin.stat.exists }}"
|
||||
gitea_runner_registered: "{{ gitea_runner_state.stat.exists }}"
|
||||
tags: [gitea_runner, prereqs, always]
|
||||
@@ -0,0 +1,114 @@
|
||||
---
|
||||
# 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]
|
||||
|
||||
- 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
|
||||
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]
|
||||
|
||||
- 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
|
||||
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]
|
||||
@@ -0,0 +1,47 @@
|
||||
---
|
||||
# A forced re-registration minted the token in main.yml before dropping .runner.
|
||||
- name: Mint runner token
|
||||
ansible.builtin.include_tasks: mint_token.yml
|
||||
when: not gitea_runner_force_register | bool
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
# In check mode no token was minted and the register command can't be templated
|
||||
# without one, so report the intended registration instead.
|
||||
- name: Report runner registration (check mode)
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
Check mode: would generate a runner token on {{ gitea_runner_token_host }} and register
|
||||
{{ gitea_runner_name }} against {{ gitea_runner_instance_url }}
|
||||
with scope {{ gitea_runner_registration_scope if gitea_runner_registration_scope | length > 0 else '(instance-wide)' }}
|
||||
changed_when: true
|
||||
when: ansible_check_mode
|
||||
tags: [gitea_runner, register]
|
||||
|
||||
- name: Register Gitea runner
|
||||
when: not ansible_check_mode
|
||||
become: true
|
||||
tags: [gitea_runner, register]
|
||||
block:
|
||||
- name: Attempt runner registration
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- "{{ gitea_runner_install_dir }}/gitea-runner"
|
||||
- register
|
||||
- --no-interactive
|
||||
- --instance
|
||||
- "{{ gitea_runner_instance_url }}"
|
||||
- --name
|
||||
- "{{ gitea_runner_name }}"
|
||||
chdir: "{{ gitea_runner_work_dir }}"
|
||||
creates: "{{ gitea_runner_work_dir }}/.runner"
|
||||
environment:
|
||||
GITEA_RUNNER_REGISTRATION_TOKEN: "{{ gitea_runner_token }}"
|
||||
register: gitea_runner_register_cmd
|
||||
no_log: true
|
||||
notify: Restart Gitea Runner
|
||||
rescue:
|
||||
- name: Fail with sanitized runner registration error
|
||||
ansible.builtin.fail:
|
||||
msg: >-
|
||||
Gitea runner registration failed (rc={{ gitea_runner_register_cmd.rc | default('unknown') }}):
|
||||
{{ gitea_runner_register_cmd.stderr | default('') | regex_replace(gitea_runner_token | regex_escape, '***REDACTED***') }}
|
||||
@@ -0,0 +1,75 @@
|
||||
---
|
||||
# Run with -e gitea_runner_remove=true (and gitea_runner_remove_data=true to delete the
|
||||
# registration and config too). The role ends after these tasks.
|
||||
- name: Stop Gitea runner service
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: gitea-runner
|
||||
state: stopped
|
||||
enabled: false
|
||||
register: gitea_runner_stop
|
||||
# The unit may already be gone (systemd: "Could not find the requested service <unit>").
|
||||
# Any other stop failure must halt here, before the unit file and data are deleted.
|
||||
failed_when:
|
||||
- gitea_runner_stop.failed | default(false)
|
||||
- "'Could not find the requested service' not in (gitea_runner_stop.msg | default(''))"
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Stop the Docker prune timer
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: gitea-runner-docker-prune.timer
|
||||
state: stopped
|
||||
enabled: false
|
||||
register: gitea_runner_prune_stop
|
||||
failed_when:
|
||||
- gitea_runner_prune_stop.failed | default(false)
|
||||
- "'Could not find the requested service' not in (gitea_runner_prune_stop.msg | default(''))"
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Remove the Docker prune units
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "/etc/systemd/system/{{ gitea_runner_prune_unit }}"
|
||||
state: absent
|
||||
loop:
|
||||
- gitea-runner-docker-prune.timer
|
||||
- gitea-runner-docker-prune.service
|
||||
loop_control:
|
||||
loop_var: gitea_runner_prune_unit
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Remove the Docker prune gate script
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: /usr/local/sbin/gitea-runner-docker-prune-gate
|
||||
state: absent
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Remove systemd unit
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_service_unit }}"
|
||||
state: absent
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Reload systemd
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Remove runner binary
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_install_dir }}/gitea-runner"
|
||||
state: absent
|
||||
tags: [gitea_runner, remove]
|
||||
|
||||
- name: Remove runner work directory
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_work_dir }}"
|
||||
state: absent
|
||||
when: gitea_runner_remove_data | default(false) | bool
|
||||
tags: [gitea_runner, remove]
|
||||
@@ -0,0 +1,79 @@
|
||||
---
|
||||
- name: Get current runner version
|
||||
become: true
|
||||
ansible.builtin.command:
|
||||
argv: ["{{ gitea_runner_install_dir }}/gitea-runner", --version]
|
||||
register: gitea_runner_version_cmd
|
||||
changed_when: false
|
||||
# Failure is expected when the binary is missing (rc 2): empty stdout falls through to 0.0.0
|
||||
# below, which triggers the install path.
|
||||
failed_when: false
|
||||
# Read-only, so it also runs in the --check preview. Skipped there, stdout would be empty, the version
|
||||
# would fall back to 0.0.0 and an up-to-date binary would look like it needs an install.
|
||||
check_mode: false
|
||||
tags: [gitea_runner, update]
|
||||
|
||||
- name: Extract installed version
|
||||
ansible.builtin.set_fact:
|
||||
gitea_runner_installed_version: >-
|
||||
{{
|
||||
(gitea_runner_version_cmd.stdout
|
||||
| regex_findall('v?([0-9]+\.[0-9]+\.[0-9]+)')
|
||||
| first)
|
||||
| default('0.0.0')
|
||||
}}
|
||||
tags: [gitea_runner, update]
|
||||
|
||||
- name: Determine if update required
|
||||
ansible.builtin.set_fact:
|
||||
gitea_runner_update_required: "{{ gitea_runner_installed_version != gitea_runner_version }}"
|
||||
# Next to the live binary, so the swap is a rename on one filesystem.
|
||||
gitea_runner_staged_binary: "{{ gitea_runner_install_dir }}/gitea-runner.new"
|
||||
tags: [gitea_runner, update]
|
||||
|
||||
# Download and verify before anything is stopped: a failed download or checksum leaves the
|
||||
# running runner untouched. mortis.kamino._binary_update then stops, backs up, swaps, restarts,
|
||||
# checks the version and rolls back on failure, and removes the staged binary.
|
||||
- name: Stage and swap in the new runner binary
|
||||
when: gitea_runner_update_required
|
||||
block:
|
||||
# Its own rescue, so that only a staging failure gets this message: a failure in the swap
|
||||
# below keeps the rollback message of mortis.kamino._binary_update.
|
||||
- name: Stage the new runner binary
|
||||
block:
|
||||
- name: Download and verify into the staging path
|
||||
ansible.builtin.include_tasks: download.yml
|
||||
vars:
|
||||
gitea_runner_download_dest: "{{ gitea_runner_staged_binary }}"
|
||||
tags: [gitea_runner, update]
|
||||
rescue:
|
||||
- name: Fail the staging
|
||||
ansible.builtin.fail:
|
||||
msg: >-
|
||||
Staging gitea-runner {{ gitea_runner_version }} failed ({{ ansible_failed_result.msg | default('see the task above') }}).
|
||||
The runner was not stopped and still runs {{ gitea_runner_installed_version }}.
|
||||
tags: [gitea_runner, update]
|
||||
|
||||
# An include does not pass its tags on to the included tasks, hence apply.
|
||||
- name: Swap in the staged runner binary
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino._binary_update
|
||||
apply:
|
||||
tags: [gitea_runner, update]
|
||||
vars:
|
||||
_binary_update_service: gitea-runner
|
||||
_binary_update_binary_path: "{{ gitea_runner_install_dir }}/gitea-runner"
|
||||
_binary_update_staged_path: "{{ gitea_runner_staged_binary }}"
|
||||
_binary_update_version_command: ["{{ gitea_runner_install_dir }}/gitea-runner", --version]
|
||||
_binary_update_expected_version: "{{ gitea_runner_version }}"
|
||||
tags: [gitea_runner, update]
|
||||
|
||||
always:
|
||||
# Left behind only when the run failed before the swap; get_url would reuse a matching
|
||||
# file anyway, but a stale binary next to the live one is clutter.
|
||||
- name: Remove the staged runner binary
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ gitea_runner_staged_binary }}"
|
||||
state: absent
|
||||
tags: [gitea_runner, update]
|
||||
@@ -0,0 +1,48 @@
|
||||
---
|
||||
# Input checks. Imported first in main.yml with tags: always, so a bad input fails before any
|
||||
# change, whatever tags the run selects.
|
||||
- name: Require a plain gitea_runner_version
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- gitea_runner_version is string
|
||||
- gitea_runner_version is match('^[0-9]+\\.[0-9]+\\.[0-9]+$')
|
||||
fail_msg: >-
|
||||
gitea_runner_version must be a plain release number such as 6.0.1, with no leading v: it is
|
||||
used in the download URL and compared with what the binary reports.
|
||||
quiet: true
|
||||
|
||||
# Checks the format only. Together with minting the token first, a malformed scope or a
|
||||
# failed mint stops the play before a forced re-registration drops .runner.
|
||||
- name: Require a valid registration scope
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- gitea_runner_registration_scope is string
|
||||
- gitea_runner_registration_scope | length == 0 or gitea_runner_registration_scope is match('^[A-Za-z0-9_.-]+(/[A-Za-z0-9_.-]+)?$')
|
||||
fail_msg: >-
|
||||
gitea_runner_registration_scope must be empty (instance-wide), an owner (user or organisation)
|
||||
or owner/repo, got '{{ gitea_runner_registration_scope }}'.
|
||||
quiet: true
|
||||
|
||||
# The values land in systemd unit files: a newline or % would inject or expand directives.
|
||||
- name: Validate the Docker prune settings
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- gitea_runner_docker_prune_schedule is match('^[A-Za-z0-9 :*,./~-]+$')
|
||||
- gitea_runner_docker_prune_container_age is match('^[0-9]+[smh]$')
|
||||
- gitea_runner_docker_prune_image_age is match('^[0-9]+[smh]$')
|
||||
fail_msg: >-
|
||||
gitea_runner_docker_prune_schedule must be a plain OnCalendar value (no newline or %),
|
||||
the two ages a number with s, m or h (e.g. 168h).
|
||||
quiet: true
|
||||
when: gitea_runner_docker_prune_enabled | bool
|
||||
|
||||
- name: Require the collections requirements file for the molecule tooling
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- gitea_runner_collections_requirements_file is string
|
||||
- gitea_runner_collections_requirements_file | length > 0
|
||||
fail_msg: >-
|
||||
gitea_runner_molecule_enabled is true, so gitea_runner_collections_requirements_file must name
|
||||
the collections requirements file on the control host that the molecule install reads.
|
||||
quiet: true
|
||||
when: gitea_runner_molecule_enabled | bool
|
||||
Reference in new issue
Block a user