nixos: bucket replica and XMPP certificate sync

s3-replicate is the same rclone sync with the same delete limit, run by
the rclone package instead of its image.

The certificate push keeps its shape: ops reads Traefik's acme.json and
sends each certificate over SSH with a key that may run one command on
services, from ops only. The key is now a sops secret with its public
half in the config, instead of being generated on the host, so a
rebuilt pair works without a manual step. Tried on the rehearsal pair
with a throwaway certificate; any other command is refused.

Assisted-by: Claude:fable-5-2
This commit is contained in:
Sergei Poljanski 2026-11-16 23:27:47 +04:00
commit 422eb3bc6e
Signed by: asxpi
GPG key ID: 4F8851660FA4121B

View file

@ -0,0 +1,86 @@
# Daily one-way replica of the hel1 buckets into their fsn1 twins.
# restic/ is left out: restic writes its own, independent fsn1 repository.
{
config,
lib,
pkgs,
...
}:
let
pairs = [
{
src = "asxpio-backups-main";
dst = "asxpio-backups-repl";
exclude = [ "restic/**" ];
}
{
src = "asxpio-terraform-main";
dst = "asxpio-terraform-repl";
}
{
src = "k8s-asxpio-main";
dst = "k8s-asxpio-repl";
}
];
# A run that would delete more than this stops and fails (and mails):
# a mistake on the hel1 side must not empty the replica the next morning
maxDelete = 1000;
textfileDir = "/var/lib/node_exporter/textfile";
in
{
sops.templates."s3-replicate.env".content =
let
remote = name: region: ''
RCLONE_CONFIG_${name}_TYPE=s3
RCLONE_CONFIG_${name}_PROVIDER=Other
RCLONE_CONFIG_${name}_ENDPOINT=https://${region}.your-objectstorage.com
RCLONE_CONFIG_${name}_REGION=${region}
RCLONE_CONFIG_${name}_ACCESS_KEY_ID=${config.sops.placeholder."s3/access_key"}
RCLONE_CONFIG_${name}_SECRET_ACCESS_KEY=${config.sops.placeholder."s3/secret_key"}
'';
in
remote "HEL1" "hel1"
+ remote "FSN1" "fsn1"
+ ''
RCLONE_CONFIG_FSN1_NO_CHECK_BUCKET=true
'';
systemd.services.s3-replicate = {
description = "Replicate the hel1 buckets to fsn1";
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
onFailure = [ "alert-mail@%n.service" ];
path = [ pkgs.rclone ];
serviceConfig = {
Type = "oneshot";
EnvironmentFile = config.sops.templates."s3-replicate.env".path;
TimeoutStartSec = "12h";
};
# One bucket at a time; a failing pair does not stop the others, but
# fails the run
script = ''
failed=0
${lib.concatMapStrings (p: ''
echo "s3-replicate: ${p.src} -> ${p.dst}"
rclone sync hel1:${p.src} fsn1:${p.dst} \
--fast-list --checkers 16 --transfers 8 --max-delete ${toString maxDelete} \
${lib.concatMapStringsSep " " (e: "--exclude ${lib.escapeShellArg e}") (p.exclude or [ ])} \
--stats-one-line -v || failed=1
'') pairs}
if [ $failed = 0 ] && [ -d ${textfileDir} ]; then
f=${textfileDir}/s3_replicate.prom
printf '# HELP s3_replicate_last_success_timestamp_seconds Last fully successful hel1 to fsn1 replica.\n# TYPE s3_replicate_last_success_timestamp_seconds gauge\ns3_replicate_last_success_timestamp_seconds %s\n' "$(date +%s)" > "$f.tmp" && mv "$f.tmp" "$f"
fi
exit $failed
'';
};
systemd.timers.s3-replicate = {
description = "Daily hel1 -> fsn1 bucket replica";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "*-*-* 06:00:00";
RandomizedDelaySec = "15m";
Persistent = true;
};
};
}