cluster/services/storage: externalize garage layout implementation

This commit is contained in:
Max Headroom 2023-09-03 21:21:31 +02:00
parent 4cf87bac0e
commit 7eb3eea599
3 changed files with 82 additions and 29 deletions

View file

@ -18,6 +18,7 @@ in
heresy = [ ./heresy.nix ];
garage = [
./garage.nix
./garage-options.nix
./garage-layout.nix
];
garageInternal = [ ./garage-internal.nix ];

View file

@ -1,13 +1,3 @@
{ config, ... }:
let
initialLayout = ''
garage layout assign -z eu-central -c 1000 28071b8673ad14c2 # checkmate
garage layout assign -z eu-central -c 1000 124d6acad43e5f70 # prophet
garage layout assign -z eu-central -c 1000 e354a1a70adc45c9 # VEGAS
'';
in
{
system.ascensions.garage-layout = {
distributed = true;
@ -16,24 +6,9 @@ in
incantations = i: [ ];
};
systemd.services.garage-layout-init = {
distributed.enable = true;
wantedBy = [ "garage.service" ];
after = [ "garage.service" ];
path = [ config.services.garage.package ];
script = ''
while ! garage status >/dev/null 2>/dev/null; do
sleep 1
done
if [[ "$(garage layout show | grep -m1 '^Current cluster layout version:' | cut -d: -f2 | tr -d ' ')" != "0" ]]; then
exit 0
fi
${initialLayout}
garage layout apply --version 1
'';
services.garage.layout.initial = {
checkmate = { zone = "eu-central"; capacity = 1000; };
prophet = { zone = "eu-central"; capacity = 1000; };
VEGAS = { zone = "eu-central"; capacity = 1000; };
};
}

View file

@ -0,0 +1,77 @@
{ config, lib, ... }:
let
cfg = config.services.garage;
garageShellLibrary = /*bash*/ ''
getNodeId() {
nodeId=""
while [[ -z "$nodeId" ]]; do
nodeId="$(garage status | grep 'NO ROLE ASSIGNED' | grep -wm1 "$1" | cut -d' ' -f1)"
if [[ $? -ne 0 ]]; then
echo "Waiting for node $1 to appear..." 2>/dev/null
sleep 1
fi
done
echo "$nodeId"
}
waitForGarage() {
while ! garage status >/dev/null 2>/dev/null; do
sleep 1
done
}
'';
in
{
options.services.garage.layout = {
initial = lib.mkOption {
default = {};
type = with lib.types; attrsOf (submodule {
options = {
zone = lib.mkOption {
type = lib.types.str;
};
capacity = lib.mkOption {
type = lib.types.ints.positive;
};
};
});
};
};
config = {
system.extraIncantations = {
runGarage = i: script: i.execShellWith [ config.services.garage.package ] ''
${garageShellLibrary}
waitForGarage
${script}
'';
};
systemd.services.garage-layout-init = {
distributed.enable = true;
wantedBy = [ "garage.service" ];
after = [ "garage.service" ];
path = [ config.services.garage.package ];
serviceConfig = {
TimeoutStartSec = "1800s";
};
script = ''
${garageShellLibrary}
waitForGarage
if [[ "$(garage layout show | grep -m1 '^Current cluster layout version:' | cut -d: -f2 | tr -d ' ')" != "0" ]]; then
exit 0
fi
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: layout: ''
garage layout assign -z '${layout.zone}' -c '${toString layout.capacity}' "$(getNodeId '${name}')"
'') cfg.layout.initial)}
garage layout apply --version 1
'';
};
};
}