Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 105 additions & 5 deletions ansible/ci/playbooks/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@
# with the vault's CLI, `op`, which the second play installs from the
# vendor's repository where the host has none (plexus.ci.actions_runners,
# vault_cli). The playbook reads every other secret from there itself (vars/vault.yml names the items
# and fields), and none is passed in. A runner host sends no mail: no mail
# relay is mounted, and a failed unit shows in the journal alone.
# and fields), and none is passed in.
#
# The host mails the tenant through the tenant's relay, as an app host does
# (plexus.host.mail): what it needs, the relay, its credentials, the sender
# and the recipient, is the vault's item mail-relay. It mails once a day at
# most per unit: when a unit fails (BuildKit, a cache server, a slot's
# container, the housekeeping), when the disk is fuller than
# actions_runners_disk_alert_percent, and when a slot has failed to mint its
# runner config for longer than actions_runners_slot_alert_after, saying for
# how long; the last two are units of their own, so that no other failure of
# the day takes their mail, and one failed minting mails nobody
# (plexus.ci.actions_runners). The host takes no mail over SMTP, since its
# slots share its loopback. An item that is missing, lacks a field, leaves one
# empty or names a port that is no number or a mode Postfix does not know
# fails no run: the host is set up, sends no mail, and says so and why in its
# journal; so does a mail role that fails. A relay that refuses fails no run
# either; Postfix says so in the journal at each try.
#
# The first play locks the host down: the values are checked before
# anything changes, then the jobs' user takes the runner image's uid, the
Expand All @@ -65,7 +80,8 @@
# for a run that logged in as root over SSH, which this one never did, so it
# is told the first admin's name instead. The handlers of the first play run
# even when a task of it fails. The second play reads the vault and sets up
# the rest: the baseline, the overlay, Docker, the cache servers, the slots.
# the rest: the mail, the baseline, the overlay, Docker, the cache servers,
# the slots.
#
# Tags: every play carries its layer (`host`, `ci`) and every role its own
# name.
Expand Down Expand Up @@ -196,7 +212,7 @@
vars:
unread: >-
{%- set unread = [] -%}
{%- for read in ci_vault_read.results if read.rc is defined and read.rc != 0 -%}
{%- for read in ci_vault_read.results if read.rc is defined and read.rc != 0 and not read.item.optional | default(false) -%}
{%- set _ = unread.append(read.item.item ~ '/' ~ read.item.field ~ ' (' ~ (read.stderr | trim) ~ ')') -%}
{%- endfor -%}
{{ unread }}
Expand All @@ -207,9 +223,93 @@
ansible.builtin.set_fact:
"{{ ci_vault[item].var }}": "{{ ci_vault_read.results[item].stdout }}"
loop: "{{ range(ci_vault | length) | list }}"
when: ci_vault_read.results[item].rc is defined
when: ci_vault_read.results[item].rc | default(1) == 0
no_log: true
tags: [always]

# From the variables, not from the reads, so that one given on the command
# line counts. A field that is empty counts as missing; a port is digits,
# and a mode one the mail role knows. Anything wrong leaves the host
# without mail.
- name: What is wrong with the mail relay
ansible.builtin.set_fact:
ci_mail_wrong: >-
{%- set fields = ci_vault | selectattr('item', 'eq', 'mail-relay') | list -%}
{%- set lacks = [] -%}
{%- for entry in fields if lookup('ansible.builtin.vars', entry.var, default='') | string | trim | length == 0 -%}
{%- set _ = lacks.append(entry.field) -%}
{%- endfor -%}
{%- set wrong = [] -%}
{%- if lacks | length == fields | length -%}
{%- set _ = wrong.append('the vault ' ~ ci_owner ~ '-ci-host has no item mail-relay') -%}
{%- elif lacks | length > 0 -%}
{%- set _ = wrong.append('the item mail-relay of the vault ' ~ ci_owner ~ '-ci-host lacks ' ~ lacks | join(', ')) -%}
{%- endif -%}
{%- set port = lookup('ansible.builtin.vars', 'ci_mail_port', default='') | string | trim -%}
{%- if port | length > 0 and port is not match('^[0-9]+$') -%}
{%- set _ = wrong.append('the item mail-relay names the port ' ~ port ~ ', which is no number') -%}
{%- endif -%}
{%- set mode = lookup('ansible.builtin.vars', 'ci_mail_mode', default='') | string | trim -%}
{%- if mode | length > 0 and mode not in ['starttls', 'tls'] -%}
{%- set _ = wrong.append('the item mail-relay names the mode ' ~ mode ~ ', which is neither starttls nor tls') -%}
{%- endif -%}
{{ wrong }}
tags: [always]

- name: Hand the mail relay to the mail role
ansible.builtin.set_fact:
mail_smtp:
server: "{{ ci_mail_server }}"
port: "{{ ci_mail_port }}"
mode: "{{ ci_mail_mode }}"
username: "{{ ci_mail_username }}"
password: "{{ ci_mail_password }}"
from_address: "{{ ci_mail_from_address }}"
mailto: "{{ ci_mail_mailto }}"
when: ci_mail_wrong | length == 0
no_log: true
tags: [always]

# Before the roles, so that a unit of theirs that fails is mailed about
# from its first start, a slot whose first run cannot register among them,
# and rescued, so that a mail role that fails (a Postfix that cannot be
# installed) fails no run: the host is set up all the same and says so
# below. Its port on loopback closed, since the slots share the host's
# network.
- name: The host's mail
when: ci_mail_wrong | length == 0
tags: [mail]
block:
- name: The mail role
ansible.builtin.import_role:
name: plexus.host.mail
vars:
mail_loopback_smtp: false
rescue:
- name: What failed of the mail role
ansible.builtin.set_fact:
ci_mail_wrong:
- >-
the role plexus.host.mail failed at '{{ ansible_failed_task.name | default('?') }}':
{{ ansible_failed_result.msg | default('') | truncate(400) }}

# Under cloud-init the run's output is a log nobody reads; the journal is
# where an admin looks first.
- name: Say in the journal that the host sends no mail
ansible.builtin.command:
argv:
- logger
- --tag
- plexus-ci
- --priority
- user.warning
- >-
This host sends no mail: {{ ci_mail_wrong | join('; ') }}. A failed
unit, a disk over its threshold and a slot that cannot register
show in this journal alone.
when: ci_mail_wrong | length > 0
changed_when: false
tags: [mail]
roles:
- { role: plexus.host.base, tags: [base] }
- { role: plexus.host.upgrades, tags: [upgrades] }
Expand Down
18 changes: 16 additions & 2 deletions ansible/ci/playbooks/vars/vault.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
# What the playbook reads from the host's vault, <ci_owner>-ci-host, and
# into which variable: the one place the items and fields are named. An
# entry with `when` is read only where that holds; a variable given on the
# command line wins over what is read.
# entry with `when` is read only where that holds; an `optional` one fails
# no run when the vault lacks it; a variable given on the command line wins
# over what is read.
ci_vault:
- { var: actions_runners_app_client_id, item: github-app, field: client-id }
- { var: actions_runners_app_private_key, item: github-app, field: private-key }
Expand All @@ -17,3 +18,16 @@ ci_vault:
- { var: actions_runners_connect_credentials, item: 1password-connect, field: credentials, when: actions_runners_connect }
- { var: actions_runners_connect_token, item: 1password-connect, field: token, when: actions_runners_connect }
- { var: netbird_setup_key, item: netbird, field: setup-key, when: netbird_enabled }
# The tenant's mail relay, which the host mails through (plexus.host.mail,
# its mail_smtp): the relay's name or address, its port, `starttls` or
# `tls` (implicit TLS), the relay's user and password, the sender every
# mail goes from and the one mailbox it goes to. An item that is missing,
# lacks a field, leaves one empty, or names a port that is no number or
# another mode leaves the host without mail, which it says in its journal.
- { var: ci_mail_server, item: mail-relay, field: server, optional: true }
- { var: ci_mail_port, item: mail-relay, field: port, optional: true }
- { var: ci_mail_mode, item: mail-relay, field: mode, optional: true }
- { var: ci_mail_username, item: mail-relay, field: username, optional: true }
- { var: ci_mail_password, item: mail-relay, field: password, optional: true }
- { var: ci_mail_from_address, item: mail-relay, field: from-address, optional: true }
- { var: ci_mail_mailto, item: mail-relay, field: mailto, optional: true }
22 changes: 18 additions & 4 deletions ansible/ci/roles/actions_runners/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,17 @@
# file a repo slot reads (turbo_cache). The role opens no port: the runner
# polls the forge.
#
# A runner host sends no mail: the playbook mounts no mail relay, so a failed
# unit, the housekeeping's disk alert among them, shows in the journal alone.
# Where the playbook sets up the host's mail (plexus.host.mail), a unit that
# fails is a mail, at most once a day per unit: a slot's container, BuildKit,
# the housekeeping. Two units exist to fail, so that a mail says so, each with
# a budget of its own: ci-runner-disk, hourly, on a disk fuller than
# actions_runners_disk_alert_percent, and ci-runner-cannot-register@<slot>,
# on a slot whose minting has failed at every attempt for
# actions_runners_slot_alert_after, which says for how long. A slot fails at
# every attempt that fails, and is started again; its own mail is held back
# for a failed minting (files/ci-runner-mail-hold), so that one refusal of the
# forge mails nobody. Without the host's mail every failure shows in the
# journal alone.

# Where the slots register: `org` at the organisation, in a runner group of
# it, `repo` on one repository (they are then that repository's alone,
Expand Down Expand Up @@ -208,10 +217,15 @@ actions_runners_connect_uid: 999
actions_runners_connect_port: 8080

# Housekeeping, hourly: the registry pair onto this week's buckets, the
# pinned image pulled, stopped containers, unused images and volumes removed;
# fails when the root file system is fuller than this afterwards.
# pinned image pulled, stopped containers, unused images and volumes removed.
# The disk check, hourly too, a unit of its own: fails when the root file
# system is fuller than this, in percent.
actions_runners_disk_alert_percent: 85

# How long, in seconds, a slot's minting fails at every attempt before the
# host mails about it; an attempt takes about 50 s with the wait after it.
actions_runners_slot_alert_after: 300

# The BuildKit service (§ 8 PLX-Delivery): one buildkitd on the host, the
# systemd unit ci-buildkit, through which the publish step of a job builds
# and pushes its image without a Docker daemon of its own, with buildx's
Expand Down
48 changes: 48 additions & 0 deletions ansible/ci/roles/actions_runners/files/ci-runner-cannot-register
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh
# Says that a slot cannot register, as a unit that fails, so that the host's
# mail on a failed unit (plexus.host.mail) mails it with a daily budget of its
# own (ci-runner-cannot-register@<slot>.service): a container of the slot that
# failed earlier the same day takes nothing from it. Managed by Ansible
# (actions_runners).
#
# The slot's ExecStopPost starts it after every attempt that failed. It fails
# when the slot's row of failed mintings, /run/ci-runner/<slot>.failing (the
# host's uptime in seconds as the row's first attempt started, which
# ci-runner-jit writes), is older than ALERT_AFTER seconds, and says for how
# long; the mail carries what it says and the slot's last lines. Without a
# row, or with a younger one, it exits 0 and nobody is mailed. It fails
# towards mailing: a row or a threshold that is no number fails at once and
# says what it holds. Hand-runnable as root:
#
# ALERT_AFTER=300 ci-runner-cannot-register repo-1; echo $?
slot=${1:?the slot, e.g. repo-1}
row=/run/ci-runner/$slot.failing
[ -e "$row" ] || exit 0
since=$(cat "$row" 2>/dev/null)
after=${ALERT_AFTER:-}

fail() {
echo "ci-runner-cannot-register: $*"
echo "The last lines of ci-runner@$slot.service:"
journalctl --no-pager -o cat -n 20 -u "ci-runner@$slot.service" || true
exit 1
}

case $since in
'' | *[!0-9]*) fail "the slot $slot has failed to mint its runner config, and its row $row holds no number but '$since'" ;;
esac
case $after in
'' | *[!0-9]*) fail "the slot $slot has failed to mint its runner config, and the threshold ALERT_AFTER is no number but '$after'" ;;
esac
read -r up _ </proc/uptime
took=$((${up%.*} - since))
[ "$took" -ge 0 ] || fail "the slot $slot has failed to mint its runner config, and its row $row holds a time ahead of the uptime, $since"
[ "$took" -ge "$after" ] || exit 0
if [ "$took" -ge 3600 ]; then
took="$((took / 3600)) h $((took % 3600 / 60)) min"
elif [ "$took" -ge 60 ]; then
took="$((took / 60)) min $((took % 60)) s"
else
took="$took s"
fi
fail "the slot $slot has failed to mint its runner config at every attempt for $took; its jobs wait for another slot"
18 changes: 18 additions & 0 deletions ansible/ci/roles/actions_runners/files/ci-runner-disk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
# Fails when the root file system is fuller than DISK_ALERT_PERCENT, from the
# unit's environment (ci-runner-disk.service, hourly), so that the host's mail
# on a failed unit (plexus.host.mail) says how full. A unit apart from the
# housekeeping, since that mail goes at most once a day per unit: a pull that
# failed earlier the same day takes nothing from it, and a pull that fails
# because the disk is full does not keep it from running. Managed by Ansible
# (actions_runners). Hand-runnable as root:
# DISK_ALERT_PERCENT=85 ci-runner-disk
set -eu
: "${DISK_ALERT_PERCENT:?}"

used=$(df --output=pcent / | tail -1 | tr -dc '0-9')
echo "root file system ${used}% used"
if [ "$used" -gt "$DISK_ALERT_PERCENT" ]; then
echo "above ${DISK_ALERT_PERCENT}%: look at /var/cache/ci-runner (the repo slots' caches: the pnpm store, mise, corepack, Playwright), docker system df, and du -sh /var/lib/buildkit against maxUsedSpace in /etc/buildkit/buildkitd.toml" >&2
exit 1
fi
16 changes: 5 additions & 11 deletions ansible/ci/roles/actions_runners/files/ci-runner-housekeeping
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/sh
# Hourly housekeeping of a CI runner host. Managed by Ansible
# (actions_runners); IMAGE, PULL (false for an image built on the host),
# REGISTRY (true | false), REGISTRY_IMAGE and DISK_ALERT_PERCENT come from
# the unit's environment. Hand-runnable as root:
# REGISTRY (true | false) and REGISTRY_IMAGE come from the unit's
# environment. How full the disk is, ci-runner-disk says, a unit of its own.
# Hand-runnable as root:
# IMAGE=ghcr.io/plexus-ms/ci-runner:<version> PULL=true REGISTRY=true \
# REGISTRY_IMAGE=registry:2 DISK_ALERT_PERCENT=85 ci-runner-housekeeping
# REGISTRY_IMAGE=registry:2 ci-runner-housekeeping
set -eu
: "${IMAGE:?}" "${PULL:?}" "${REGISTRY:?}" "${REGISTRY_IMAGE:?}" "${DISK_ALERT_PERCENT:?}"
: "${IMAGE:?}" "${PULL:?}" "${REGISTRY:?}" "${REGISTRY_IMAGE:?}"

# The registry pair onto this week's buckets, first: a failure here is the
# one that costs every run of the week a cold cache.
Expand All @@ -31,10 +32,3 @@ done
# The BuildKit service (ci-buildkit) keeps its cache in /var/lib/buildkit,
# outside this daemon, and bounds it by its own garbage collection; nothing
# here touches it, since a build may be using it.

used=$(df --output=pcent / | tail -1 | tr -dc '0-9')
echo "root file system ${used}% used"
if [ "$used" -gt "$DISK_ALERT_PERCENT" ]; then
echo "above ${DISK_ALERT_PERCENT}%: look at /var/cache/ci-runner (the repo slots' caches: the pnpm store, mise, corepack, Playwright), docker system df, and du -sh /var/lib/buildkit against maxUsedSpace in /etc/buildkit/buildkitd.toml" >&2
exit 1
fi
27 changes: 26 additions & 1 deletion ansible/ci/roles/actions_runners/files/ci-runner-jit
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,33 @@ esac
name="$HOST-$instance"
out=/run/ci-runner/$instance.env

# The row of failed attempts, $failing: the host's uptime in seconds as the
# row's first attempt started, a clock no time step moves and that starts
# again with /run at a boot. Each attempt starts the row where there is none,
# so that an attempt systemd ends for its time counts too, and a minting that
# succeeds ends it. A start of the slot that is not one of systemd's restarts
# (NRestarts 0) begins a new row, since a stop between two attempts runs no
# ExecStopPost and leaves the old one. After an attempt that failed the
# slot's ExecStopPost starts ci-runner-cannot-register@<slot>, which mails
# once the row is older than a few minutes; ci-runner-mail-hold keeps the
# slot's own mail for a container that failed after it minted.
failing=/run/ci-runner/$instance.failing
mkdir -p /run/ci-runner
if [ "$(systemctl show -P NRestarts "ci-runner@$instance.service")" = 0 ]; then
rm -f "$failing"
fi
if [ ! -e "$failing" ]; then
read -r up _ </proc/uptime
echo "${up%.*}" >"$failing"
fi

# A failure waits before it fails, so that a forge outage costs one attempt
# per slot every half minute rather than every restart interval.
trap 'echo "ci-runner-jit: minting the config of $name failed" >&2; sleep 30' ERR
minting_failed() {
echo "ci-runner-jit: minting the config of $name failed" >&2
sleep 30
}
trap minting_failed ERR

forge_login

Expand Down Expand Up @@ -92,4 +116,5 @@ done
umask 077
mkdir -p /run/ci-runner
printf 'ACTIONS_RUNNER_INPUT_JITCONFIG=%s\n' "$jit" >"$out"
rm -f "$failing"
echo "$name: registered on $base with $labels"
31 changes: 31 additions & 0 deletions ansible/ci/roles/actions_runners/files/ci-runner-mail-hold
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
# Holds back the host's mail on a slot that failed in its minting
# (ExecCondition of failure-mail@.service, plexus.host.mail, by a drop-in of
# this role): ci-runner-cannot-register@<slot> mails that, once the minting
# has failed for a few minutes, with a daily budget of its own. Managed by
# Ansible (actions_runners).
#
# A slot fails as a unit at every attempt that fails, and is started again.
# The slot's row of failed mintings, /run/ci-runner/<slot>.failing, which
# ci-runner-jit writes as an attempt starts and removes once it has minted,
# tells a minting that failed from a container that failed after it: the
# second is mailed at once, as every other unit is. It fails towards mailing:
# a row that is missing or holds no number lets the slot's mail go. A failed
# minting it holds back is mailed only if the slot's ExecStopPost can start
# ci-runner-cannot-register@<slot>. Exits 0 to mail, 1 to hold back, which
# systemd counts as no failure of failure-mail@.
#
# ci-runner-mail-hold ci-runner@repo-1.service; echo $?
unit=${1:?the unit that failed, e.g. ci-runner@repo-1.service}
case $unit in
ci-runner@*.service) ;;
*) exit 0 ;;
esac
slot=${unit#ci-runner@}
slot=${slot%.service}
since=$(cat "/run/ci-runner/$slot.failing" 2>/dev/null) || exit 0
case $since in
'' | *[!0-9]*) exit 0 ;;
esac
echo "ci-runner-mail-hold: $slot failed in its minting; ci-runner-cannot-register@$slot.service mails that"
exit 1
3 changes: 2 additions & 1 deletion ansible/ci/roles/actions_runners/files/ci-runner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#
# the runner GitHub's Actions runner and the libraries it needs, run
# as the user runner (1001), with sudo where the engine
# allows it (a slot's no-new-privileges does not)
# allows it (a slot's container, which ci-runner-slot
# starts with no capability, does not)
# node, pnpm node from the base; pnpm and yarn through corepack, at
# the version a repo's packageManager names, downloaded
# into ~/.cache/node/corepack
Expand Down
Loading