nixos: Traefik and the CI runners as native services
Traefik keeps its role and its acme.json. Routes for the native services (Forgejo, Garage, the dashboard, the XMPP certificate) are written from Nix; the app containers keep declaring theirs as labels, which Traefik reads through Podman's socket as it did through Docker's. The asxpio runner uses the NixOS module and starts jobs through Podman; the interop runner is its own unit because it connects with a UUID and token instead of registering. Tried on the rehearsal pair: Forgejo and Garage through Traefik, a label-routed container, anonymous reads of the public bucket only, and a workflow with a nix-latest and an ubuntu-latest job. A restored Forgejo database still holds production's queued jobs, schedules and Actions secrets. The test runner tried to take a queued production job and was stopped only by the different secret key. migration/rehearsal-sanitize-forgejo.sh removes all of that and must run before a runner meets restored data. Assisted-by: Claude:fable-5-2
This commit is contained in:
parent
933b086bb1
commit
e95d97869f
6 changed files with 581 additions and 120 deletions
|
|
@ -7,6 +7,7 @@
|
|||
./forgejo.nix
|
||||
./garage.nix
|
||||
./postgres.nix
|
||||
./traefik.nix
|
||||
];
|
||||
|
||||
networking.hostName = lib.mkDefault "asxpio-ops";
|
||||
|
|
|
|||
245
nixos/hosts/asxpio-ops/traefik.nix
Normal file
245
nixos/hosts/asxpio-ops/traefik.nix
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
# Traefik: the only thing listening on 80 and 443. Certificates from
|
||||
# Let's Encrypt over the HTTP challenge, kept in /var/lib/traefik/acme.json.
|
||||
#
|
||||
# Routes come from two places: for the native services, from here
|
||||
# (`asxpio.routes` below and in the services' own files); for the app
|
||||
# containers, from their labels, read through Podman's socket exactly as
|
||||
# they were read from Docker's.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
internal = [
|
||||
"10.10.0.0/20"
|
||||
"10.20.0.0/24"
|
||||
];
|
||||
in
|
||||
{
|
||||
options.asxpio.letsencryptCA = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "https://acme-v02.api.letsencrypt.org/directory";
|
||||
};
|
||||
|
||||
# Shorthand for the common case: these hosts, over TLS, to that URL
|
||||
options.asxpio.routes = lib.mkOption {
|
||||
default = { };
|
||||
type = lib.types.attrsOf (
|
||||
lib.types.submodule {
|
||||
options = {
|
||||
hosts = lib.mkOption { type = lib.types.listOf lib.types.str; };
|
||||
url = lib.mkOption { type = lib.types.str; };
|
||||
middlewares = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
config = {
|
||||
sops.secrets."traefik/letsencrypt_email" = { };
|
||||
sops.templates."traefik.env".content = ''
|
||||
TRAEFIK_CERTIFICATESRESOLVERS_LETSENCRYPT_ACME_EMAIL=${config.sops.placeholder."traefik/letsencrypt_email"}
|
||||
'';
|
||||
|
||||
services.traefik = {
|
||||
enable = true;
|
||||
package = pkgs.traefik;
|
||||
environmentFiles = [ config.sops.templates."traefik.env".path ];
|
||||
|
||||
staticConfigOptions = {
|
||||
api = {
|
||||
dashboard = true;
|
||||
insecure = true; # on the "traefik" entry point below: WireGuard only
|
||||
};
|
||||
entryPoints = {
|
||||
web = {
|
||||
address = ":80";
|
||||
http.redirections.entryPoint = {
|
||||
to = "websecure";
|
||||
scheme = "https";
|
||||
};
|
||||
};
|
||||
websecure.address = ":443";
|
||||
traefik.address = "10.20.0.10:8080";
|
||||
metrics.address = "127.0.0.1:8082";
|
||||
};
|
||||
metrics.prometheus = {
|
||||
entryPoint = "metrics";
|
||||
addRoutersLabels = true;
|
||||
addServicesLabels = true;
|
||||
};
|
||||
providers.docker = {
|
||||
endpoint = "unix:///run/podman/podman.sock";
|
||||
exposedByDefault = false;
|
||||
network = "traefik";
|
||||
};
|
||||
certificatesResolvers.letsencrypt.acme = {
|
||||
caServer = config.asxpio.letsencryptCA;
|
||||
storage = "${config.services.traefik.dataDir}/acme.json";
|
||||
httpChallenge.entryPoint = "web";
|
||||
};
|
||||
log = {
|
||||
level = "INFO";
|
||||
filePath = "/var/log/traefik/traefik.log";
|
||||
};
|
||||
accessLog = {
|
||||
filePath = "/var/log/traefik/access.log";
|
||||
bufferingSize = 100;
|
||||
fields.headers = {
|
||||
defaultMode = "drop";
|
||||
names = {
|
||||
User-Agent = "keep";
|
||||
Referer = "keep";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
dynamicConfigOptions.http = {
|
||||
middlewares = {
|
||||
secure-headers.headers = {
|
||||
sslRedirect = true;
|
||||
stsSeconds = 31536000;
|
||||
stsIncludeSubdomains = true;
|
||||
stsPreload = true;
|
||||
forceSTSHeader = true;
|
||||
frameDeny = true;
|
||||
contentTypeNosniff = true;
|
||||
browserXssFilter = true;
|
||||
};
|
||||
rate-limit.rateLimit = {
|
||||
average = 100;
|
||||
burst = 50;
|
||||
period = "1s";
|
||||
};
|
||||
internal-only.ipAllowList.sourceRange = internal;
|
||||
wireguard-only.ipAllowList.sourceRange = [ "10.20.0.0/24" ];
|
||||
|
||||
# Anonymous reads of Garage's public bucket (see garage-public)
|
||||
garage-public-strip.stripPrefix.prefixes = [ "/public" ];
|
||||
garage-public-host.headers.customRequestHeaders.Host = "public.web.garage";
|
||||
};
|
||||
|
||||
routers = {
|
||||
traefik-dashboard = {
|
||||
rule = "Host(`traefik.asxp.io`)";
|
||||
entryPoints = [ "websecure" ];
|
||||
service = "api@internal";
|
||||
middlewares = [ "internal-only" ];
|
||||
tls.certResolver = "letsencrypt";
|
||||
};
|
||||
|
||||
# Exists only to obtain the XMPP certificate (ejabberd on
|
||||
# asxpio-services); xmpp-cert-sync pushes it there on every
|
||||
# renewal. No traffic reaches it: these names point at
|
||||
# services. asxp.io is pushed from the site's own certificate.
|
||||
xmpp-cert = {
|
||||
rule = "Host(`xmpp.asxp.io`)";
|
||||
entryPoints = [ "websecure" ];
|
||||
priority = 1;
|
||||
service = "noop@internal";
|
||||
tls = {
|
||||
certResolver = "letsencrypt";
|
||||
domains = [
|
||||
{
|
||||
main = "xmpp.asxp.io";
|
||||
sans = [ "conference.asxp.io" ];
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
# Unsigned GET/HEAD of s3.asxp.io/public/<key> goes to Garage's
|
||||
# web endpoint as Host public.web.garage, path /<key>. Signed
|
||||
# requests still reach the S3 API (route garage-s3).
|
||||
garage-public = {
|
||||
rule = "Host(`s3.asxp.io`) && PathPrefix(`/public/`) && (Method(`GET`) || Method(`HEAD`)) && !HeaderRegexp(`Authorization`, `.+`) && !QueryRegexp(`X-Amz-Signature`, `.+`)";
|
||||
priority = 1000;
|
||||
entryPoints = [ "websecure" ];
|
||||
service = "garage-web";
|
||||
middlewares = [
|
||||
"garage-public-strip"
|
||||
"garage-public-host"
|
||||
];
|
||||
tls.certResolver = "letsencrypt";
|
||||
};
|
||||
}
|
||||
// lib.mapAttrs (name: r: {
|
||||
rule = lib.concatMapStringsSep " || " (h: "Host(`${h}`)") r.hosts;
|
||||
entryPoints = [ "websecure" ];
|
||||
service = name;
|
||||
inherit (r) middlewares;
|
||||
tls.certResolver = "letsencrypt";
|
||||
}) config.asxpio.routes;
|
||||
|
||||
services = {
|
||||
garage-web.loadBalancer.servers = [ { url = "http://127.0.0.1:3902"; } ];
|
||||
}
|
||||
// lib.mapAttrs (_: r: { loadBalancer.servers = [ { inherit (r) url; } ]; }) config.asxpio.routes;
|
||||
};
|
||||
};
|
||||
|
||||
asxpio.routes = {
|
||||
forgejo = {
|
||||
hosts = [
|
||||
"git.asxp.io"
|
||||
"git.czsk.it"
|
||||
];
|
||||
url = "http://127.0.0.1:3000";
|
||||
};
|
||||
garage-s3 = {
|
||||
hosts = [ "s3.asxp.io" ];
|
||||
url = "http://127.0.0.1:3900";
|
||||
};
|
||||
};
|
||||
|
||||
users.users.traefik.extraGroups = [ "podman" ];
|
||||
systemd.services.traefik = {
|
||||
after = [
|
||||
"podman.socket"
|
||||
"wireguard-wg0.service"
|
||||
];
|
||||
wants = [
|
||||
"podman.socket"
|
||||
"wireguard-wg0.service"
|
||||
];
|
||||
serviceConfig.LogsDirectory = "traefik";
|
||||
};
|
||||
services.logrotate.settings.traefik = {
|
||||
files = "/var/log/traefik/*.log";
|
||||
frequency = "weekly";
|
||||
rotate = 8;
|
||||
compress = true;
|
||||
delaycompress = true;
|
||||
# Traefik reopens its log files on USR1
|
||||
postrotate = "${config.systemd.package}/bin/systemctl kill -s USR1 traefik.service";
|
||||
};
|
||||
|
||||
# App containers that Traefik routes to join this network
|
||||
systemd.services.podman-network-traefik = {
|
||||
description = "Podman network traefik";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
before = [ "traefik.service" ];
|
||||
path = [ config.virtualisation.podman.package ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
};
|
||||
script = "podman network exists traefik || podman network create traefik";
|
||||
};
|
||||
|
||||
networking.firewall.allowedTCPPorts = [
|
||||
80
|
||||
443
|
||||
];
|
||||
|
||||
# acme.json holds the account key and every certificate
|
||||
asxpio.waitForData = [ "traefik" ];
|
||||
asxpio.backup.paths = [ "/var/lib/traefik" ];
|
||||
};
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
imports = [
|
||||
./acme-forward.nix
|
||||
./minecraft.nix
|
||||
./runner.nix
|
||||
./sillytavern.nix
|
||||
./xmpp.nix
|
||||
./xray.nix
|
||||
|
|
|
|||
158
nixos/hosts/asxpio-services/runner.nix
Normal file
158
nixos/hosts/asxpio-services/runner.nix
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
# Forgejo Actions runners. Jobs are containers started through Podman's
|
||||
# Docker-compatible socket.
|
||||
#
|
||||
# asxpio for git.asxp.io, reached over the private network
|
||||
# interop for another-im/interop on dev.another.im
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
# Image behind the nix labels. Jobs mount a nix-store volume over /nix,
|
||||
# seeded from this image on first use; the image's /bin/sh and
|
||||
# /etc/nix/nix.conf link into that store, so a volume from another
|
||||
# version breaks every job. Bumping this switches to a fresh volume;
|
||||
# remove the old one afterwards (podman volume rm nix-store-<old>).
|
||||
# renovate: datasource=docker depName=nixos/nix
|
||||
nixVersion = "2.35.6";
|
||||
|
||||
yaml = pkgs.formats.yaml { };
|
||||
interopConfig = yaml.generate "runner-interop.yaml" {
|
||||
log = {
|
||||
level = "info";
|
||||
job_level = "info";
|
||||
};
|
||||
runner = {
|
||||
capacity = 3;
|
||||
timeout = "5h";
|
||||
shutdown_timeout = "5h";
|
||||
fetch_interval = "5s";
|
||||
envs.NIX_CONFIG = ''
|
||||
experimental-features = nix-command flakes
|
||||
build-users-group =
|
||||
sandbox = false
|
||||
'';
|
||||
labels = [
|
||||
"nix:docker://nixos/nix:${nixVersion}"
|
||||
"docker:docker://node:22.23.5-bookworm"
|
||||
"nix-host:docker://nixos/nix:${nixVersion}"
|
||||
];
|
||||
};
|
||||
container = {
|
||||
network = "bridge";
|
||||
privileged = false;
|
||||
docker_host = "-"; # its jobs get no container socket
|
||||
valid_volumes = [ ];
|
||||
force_pull = false;
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
options.asxpio.runner.forgejoUrl = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "http://10.10.0.10:3000"; # over the private network, not through Traefik
|
||||
};
|
||||
|
||||
config = {
|
||||
sops.secrets."runner/token" = { };
|
||||
sops.secrets."runner/interop_token" = { };
|
||||
sops.templates."runner.env".content = ''
|
||||
TOKEN=${config.sops.placeholder."runner/token"}
|
||||
'';
|
||||
# The connection (URL, UUID, token) is merged into the config at
|
||||
# start; the UUID is not secret (interop's infra/runner.json)
|
||||
sops.templates."runner-interop-server.yaml".content = ''
|
||||
server:
|
||||
connections:
|
||||
interop:
|
||||
url: https://dev.another.im/
|
||||
uuid: dc326085-8420-4c33-8b88-a9ef46b3359b
|
||||
token: ${config.sops.placeholder."runner/interop_token"}
|
||||
'';
|
||||
|
||||
services.gitea-actions-runner = {
|
||||
package = pkgs.forgejo-runner;
|
||||
instances.asxpio = {
|
||||
enable = true;
|
||||
name = config.networking.hostName;
|
||||
url = config.asxpio.runner.forgejoUrl;
|
||||
tokenFile = config.sops.templates."runner.env".path;
|
||||
labels = [
|
||||
"ubuntu-latest:docker://node:20.20.2-bookworm"
|
||||
"ubuntu-22.04:docker://node:20.20.2-bookworm"
|
||||
"ubuntu-24.04:docker://node:22.23.5-bookworm"
|
||||
"debian-latest:docker://debian:12.15"
|
||||
"debian-12:docker://debian:12.15"
|
||||
"debian-11:docker://debian:11.11"
|
||||
"arch-latest:docker://archlinux:base-20261108.0.623074"
|
||||
"nginx-latest:docker://nginx:1.31.8"
|
||||
"nginx-alpine:docker://nginx:1.31.8-alpine"
|
||||
"node-20:docker://node:20.20.2"
|
||||
"node-22:docker://node:22.23.5"
|
||||
"python-3.11:docker://python:3.11.18"
|
||||
"python-3.12:docker://python:3.12.16"
|
||||
"alpine-latest:docker://alpine:3.24.4"
|
||||
"nix-latest:docker://nixos/nix:${nixVersion}"
|
||||
];
|
||||
settings = {
|
||||
log = {
|
||||
level = "info";
|
||||
job_level = "info";
|
||||
};
|
||||
runner = {
|
||||
capacity = 4;
|
||||
timeout = "3h";
|
||||
shutdown_timeout = "3h";
|
||||
fetch_timeout = "5s";
|
||||
fetch_interval = "2s";
|
||||
report_interval = "1s";
|
||||
};
|
||||
cache.enabled = true;
|
||||
container = {
|
||||
privileged = false;
|
||||
options = "-v nix-store-${nixVersion}:/nix";
|
||||
valid_volumes = [ "**" ];
|
||||
docker_host = "automount"; # deploy jobs build and push images
|
||||
force_pull = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services.forgejo-runner-interop = {
|
||||
description = "Forgejo Actions runner for another-im/interop";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [
|
||||
"network-online.target"
|
||||
"podman.socket"
|
||||
];
|
||||
wants = [
|
||||
"network-online.target"
|
||||
"podman.socket"
|
||||
];
|
||||
environment.DOCKER_HOST = "unix:///run/podman/podman.sock";
|
||||
path = [
|
||||
pkgs.forgejo-runner
|
||||
pkgs.yq-go
|
||||
];
|
||||
serviceConfig = {
|
||||
DynamicUser = true;
|
||||
SupplementaryGroups = [ "podman" ];
|
||||
StateDirectory = "forgejo-runner-interop";
|
||||
WorkingDirectory = "/var/lib/forgejo-runner-interop";
|
||||
RuntimeDirectory = "forgejo-runner-interop";
|
||||
LoadCredential = "server.yaml:${config.sops.templates."runner-interop-server.yaml".path}";
|
||||
Restart = "always";
|
||||
RestartSec = 5;
|
||||
};
|
||||
script = ''
|
||||
umask 077
|
||||
yq eval-all '. as $i ireduce ({}; . * $i)' ${interopConfig} "$CREDENTIALS_DIRECTORY/server.yaml" \
|
||||
> "$RUNTIME_DIRECTORY/config.yaml"
|
||||
exec forgejo-runner daemon --config "$RUNTIME_DIRECTORY/config.yaml"
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -24,6 +24,10 @@
|
|||
"cron.update_mirrors".ENABLED = false;
|
||||
};
|
||||
|
||||
# The names point at production, so no challenge can succeed here:
|
||||
# ask the staging CA, and expect Traefik's self-signed certificate
|
||||
asxpio.letsencryptCA = "https://acme-staging-v02.api.letsencrypt.org/directory";
|
||||
|
||||
asxpio.wireguard.peers = [
|
||||
{
|
||||
name = "asxpio-services";
|
||||
|
|
|
|||
|
|
@ -37,6 +37,9 @@
|
|||
autoStart = false;
|
||||
});
|
||||
|
||||
# This runner would take real jobs of another project
|
||||
systemd.services.forgejo-runner-interop.wantedBy = lib.mkForce [ ];
|
||||
|
||||
asxpio.wireguard.peers = [
|
||||
{
|
||||
name = "asxpio-ops";
|
||||
|
|
|
|||
Loading…
Reference in a new issue