180 lines
7.4 KiB
Nix
180 lines
7.4 KiB
Nix
{ system, inputs, pkgs, ... }:
|
|
let
|
|
lib = inputs.nixpkgs.lib;
|
|
clock = pkgs.writeShellScriptBin "date" ''
|
|
case "$*" in
|
|
+%s) echo 1700000000 ;;
|
|
"-u +%Y%m%dT%H%M%SZ") echo 20231114T221320Z ;;
|
|
*) exit 97 ;;
|
|
esac
|
|
'';
|
|
coreutils = pkgs.symlinkJoin {
|
|
name = "minecraft-backup-test-coreutils";
|
|
paths = [ pkgs.coreutils ];
|
|
postBuild = ''
|
|
rm "$out/bin/date"
|
|
ln -s ${clock}/bin/date "$out/bin/date"
|
|
'';
|
|
};
|
|
systemctl = pkgs.writeShellScriptBin "systemctl" ''
|
|
set -eu
|
|
case "$1" in
|
|
is-active) test "$(cat /build/control/state)" = active ;;
|
|
stop)
|
|
echo inactive > /build/control/state
|
|
echo "''${STOP_RESULT:-success}" > /build/control/result
|
|
;;
|
|
start) echo active > /build/control/state ;;
|
|
show) cat /build/control/result ;;
|
|
*) exit 99 ;;
|
|
esac
|
|
'';
|
|
rsync = pkgs.writeShellScriptBin "rsync" ''
|
|
set -eu
|
|
test "$(cat /build/control/state)" = inactive
|
|
test "''${FAIL_COPY:-0}" = 0 || exit 31
|
|
exec ${pkgs.rsync}/bin/rsync "$@"
|
|
'';
|
|
aws = pkgs.writeShellScriptBin "aws" ''
|
|
set -eu
|
|
case "$1 $2" in
|
|
"s3 cp")
|
|
test "''${FAIL_UPLOAD:-0}" = 0 || exit 32
|
|
cp "$3" "/build/bucket/''${3##*/}"
|
|
;;
|
|
"s3api list-objects-v2")
|
|
cat /build/objects.json
|
|
;;
|
|
"s3api delete-objects")
|
|
shift 2
|
|
while [ "$1" != --delete ]; do shift; done
|
|
manifest="''${2#file://}"
|
|
if [ "''${FAIL_DELETE:-0}" = 1 ]; then
|
|
echo '{"Errors":[{"Code":"AccessDenied"}]}'
|
|
else
|
|
${pkgs.jq}/bin/jq -r '.Objects[].Key' "$manifest" |
|
|
while IFS= read -r key; do rm -f "/build/bucket/''${key##*/}"; done
|
|
echo '{"Errors":null}'
|
|
fi
|
|
;;
|
|
*) exit 98 ;;
|
|
esac
|
|
'';
|
|
cfg = (lib.nixosSystem {
|
|
inherit system;
|
|
inherit pkgs;
|
|
modules = [
|
|
inputs.nix-minecraft.nixosModules.minecraft-servers
|
|
({ config, lib, ... }: import ../../nixos/module/generic/minecraft-backups.nix {
|
|
inherit config lib;
|
|
pkgs = pkgs // { systemd = systemctl; inherit rsync coreutils; awscli2 = aws; };
|
|
})
|
|
{
|
|
system.stateVersion = "25.05";
|
|
services.minecraft-servers = {
|
|
enable = true;
|
|
eula = true;
|
|
servers.auto = { enable = true; package = pkgs.hello; };
|
|
servers.disabled = { enable = false; package = pkgs.hello; };
|
|
};
|
|
services.minecraft-backups = {
|
|
enable = true;
|
|
stateDir = "/build/backups";
|
|
s3 = {
|
|
bucket = "test";
|
|
endpoint = "https://s3.invalid";
|
|
region = "test";
|
|
prefix = "minecraft";
|
|
credentialsFile = "/run/test-credentials";
|
|
};
|
|
servers.test = {
|
|
directory = "/build/source";
|
|
service = "minecraft-test.service";
|
|
};
|
|
};
|
|
}
|
|
];
|
|
}).config;
|
|
service = cfg.systemd.services.minecraft-backup-test;
|
|
runCase = name: test: pkgs.runCommand "minecraft-backup-${name}" {
|
|
nativeBuildInputs = [ pkgs.jq pkgs.gnutar pkgs.zstd ];
|
|
} ''
|
|
set -eu
|
|
mkdir -p /build/source/world /build/control /build/bucket /build/backups/test
|
|
echo active > /build/control/state
|
|
echo success > /build/control/result
|
|
echo world-data > /build/source/world/level.dat
|
|
echo secret > /build/source/server.properties
|
|
touch /build/bucket/minecraft-test-20200101T000000Z.tar.zst /build/bucket/manual.tar.zst
|
|
touch /build/bucket/minecraft-test-20990101T000000Z.tar.zst /build/bucket/minecraft-other-20200101T000000Z.tar.zst
|
|
echo '{"Contents":[
|
|
{"Key":"minecraft/test/minecraft-test-20200101T000000Z.tar.zst","LastModified":"2020-01-01T00:00:00Z"},
|
|
{"Key":"minecraft/test/manual.tar.zst","LastModified":"2020-01-01T00:00:00Z"},
|
|
{"Key":"minecraft/test/minecraft-test-20990101T000000Z.tar.zst","LastModified":"2099-01-01T00:00:00Z"},
|
|
{"Key":"minecraft/other/minecraft-other-20200101T000000Z.tar.zst","LastModified":"2020-01-01T00:00:00Z"}
|
|
]}' > /build/objects.json
|
|
export AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test
|
|
${test}
|
|
touch "$out"
|
|
'';
|
|
in {
|
|
minecraft-backup-enabled-server-timers =
|
|
assert cfg.services.minecraft-backups.servers.auto.directory == "/srv/minecraft/auto";
|
|
assert cfg.services.minecraft-backups.servers.auto.service == "minecraft-server-auto.service";
|
|
assert builtins.filter (name: lib.hasPrefix "minecraft-backup-" name)
|
|
(builtins.attrNames cfg.systemd.timers) == [ "minecraft-backup-auto" "minecraft-backup-test" ];
|
|
assert cfg.systemd.timers.minecraft-backup-auto.timerConfig.OnCalendar == "*-*-* 00/6:00:00";
|
|
pkgs.runCommand "minecraft-backup-enabled-server-timers" {} ''touch "$out"'';
|
|
minecraft-backup-upload-and-retention = runCase "upload-and-retention" ''
|
|
${service.serviceConfig.ExecStart}
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/restart-needed
|
|
test ! -e /build/bucket/minecraft-test-20200101T000000Z.tar.zst
|
|
test -e /build/bucket/manual.tar.zst
|
|
test -e /build/bucket/minecraft-test-20990101T000000Z.tar.zst
|
|
test -e /build/bucket/minecraft-other-20200101T000000Z.tar.zst
|
|
test -z "$(find /build/backups/test/archive -name '*.tar.zst' -print)"
|
|
mkdir /build/restored
|
|
tar --zstd -xf /build/bucket/minecraft-test-20231114T221320Z.tar.zst -C /build/restored
|
|
test "$(cat /build/restored/world/level.dat)" = world-data
|
|
test ! -e /build/restored/server.properties
|
|
'';
|
|
minecraft-backup-copy-failure-resumes-server = runCase "copy-failure-resumes-server" ''
|
|
if FAIL_COPY=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/restart-needed
|
|
'';
|
|
minecraft-backup-upload-failure-keeps-archive = runCase "upload-failure-keeps-archive" ''
|
|
if FAIL_UPLOAD=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test "$(find /build/backups/test/archive -name '*.tar.zst' | wc -l)" -eq 1
|
|
test "$(stat -c %a /build/backups/test/archive/*.tar.zst)" = 600
|
|
# A subsequent copy failure still allows the pending archive to be uploaded first.
|
|
if FAIL_COPY=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test -z "$(find /build/backups/test/archive -name '*.tar.zst' -print)"
|
|
test -e /build/bucket/minecraft-test-20231114T221320Z.tar.zst
|
|
'';
|
|
minecraft-backup-inactive-server-stays-inactive = runCase "inactive-server-stays-inactive" ''
|
|
echo inactive > /build/control/state
|
|
${service.serviceConfig.ExecStart}
|
|
test "$(cat /build/control/state)" = inactive
|
|
'';
|
|
minecraft-backup-unclean-stop-refuses-snapshot = runCase "unclean-stop-refuses-snapshot" ''
|
|
if STOP_RESULT=timeout ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/staging/world/level.dat
|
|
'';
|
|
minecraft-backup-timeout-resumes-server = runCase "timeout-resumes-server" ''
|
|
echo inactive > /build/control/state
|
|
touch /build/backups/test/restart-needed
|
|
${service.serviceConfig.ExecStopPost}
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/restart-needed
|
|
'';
|
|
minecraft-backup-delete-errors-fail-service = runCase "delete-errors-fail-service" ''
|
|
if FAIL_DELETE=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test -e /build/bucket/minecraft-test-20200101T000000Z.tar.zst
|
|
'';
|
|
}
|