From 7eb3eea5993984da6bec6f54b85c2876a2b7bbea Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 3 Sep 2023 21:21:31 +0200 Subject: [PATCH] cluster/services/storage: externalize garage layout implementation --- cluster/services/storage/default.nix | 1 + cluster/services/storage/garage-layout.nix | 33 ++------- cluster/services/storage/garage-options.nix | 77 +++++++++++++++++++++ 3 files changed, 82 insertions(+), 29 deletions(-) create mode 100644 cluster/services/storage/garage-options.nix diff --git a/cluster/services/storage/default.nix b/cluster/services/storage/default.nix index 3028af3..8f19915 100644 --- a/cluster/services/storage/default.nix +++ b/cluster/services/storage/default.nix @@ -18,6 +18,7 @@ in heresy = [ ./heresy.nix ]; garage = [ ./garage.nix + ./garage-options.nix ./garage-layout.nix ]; garageInternal = [ ./garage-internal.nix ]; diff --git a/cluster/services/storage/garage-layout.nix b/cluster/services/storage/garage-layout.nix index 2ad2d6e..4e707e1 100644 --- a/cluster/services/storage/garage-layout.nix +++ b/cluster/services/storage/garage-layout.nix @@ -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; }; }; } diff --git a/cluster/services/storage/garage-options.nix b/cluster/services/storage/garage-options.nix new file mode 100644 index 0000000..2c1bbc1 --- /dev/null +++ b/cluster/services/storage/garage-options.nix @@ -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 + ''; + }; + }; +}