From 5109c3f4fa3f70b5dc650019a6ed79edfa5f1b10 Mon Sep 17 00:00:00 2001 From: kurogeek Date: Thu, 31 Jul 2025 10:40:31 +0700 Subject: [PATCH] rework stirling-pdf to be more generic with test --- inventories/default.nix | 1 - machines/b4l/services/stirling-pdf.nix | 26 ++++++++++++ modules/clan/stirling-pdf/default.nix | 41 ++++--------------- modules/clan/stirling-pdf/flake-module.nix | 19 +++++++++ .../clan/stirling-pdf/tests/vm/default.nix | 34 +++++++++++++++ .../b4l/b4l-stirling-pdf/subdomain/value | 1 + 6 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 machines/b4l/services/stirling-pdf.nix create mode 100644 modules/clan/stirling-pdf/flake-module.nix create mode 100644 modules/clan/stirling-pdf/tests/vm/default.nix create mode 100644 vars/per-machine/b4l/b4l-stirling-pdf/subdomain/value diff --git a/inventories/default.nix b/inventories/default.nix index 9474901..d41db8b 100644 --- a/inventories/default.nix +++ b/inventories/default.nix @@ -8,7 +8,6 @@ { clan = { modules = { - stirling-pdf = ../modules/clan/stirling-pdf; victoria-metrics = ../modules/clan/victoria-metrics; }; inventory = { diff --git a/machines/b4l/services/stirling-pdf.nix b/machines/b4l/services/stirling-pdf.nix new file mode 100644 index 0000000..37e64fd --- /dev/null +++ b/machines/b4l/services/stirling-pdf.nix @@ -0,0 +1,26 @@ +{ config, ... }: +let + stDomain = "${config.clan.core.vars.generators.b4l-stirling-pdf.files.subdomain.value}.${config.networking.fqdn}"; +in +{ + clan.core.vars.generators.b4l-stirling-pdf = { + files.subdomain.secret = false; + + prompts = { + subdomain = { + persist = true; + type = "line"; + description = "Sub-domain for Stirling PDF app. Default:(pdf)"; + }; + }; + script = ''cat $prompts/subdomain || echo -n "pdf" > $out/subdomain''; + }; + + services.nginx.virtualHosts."${stDomain}" = { + forceSSL = true; + useACMEHost = "${config.networking.fqdn}"; + locations."/" = { + proxyPass = "http://localhost:${builtins.toString config.services.stirling-pdf.environment.SERVER_PORT}"; + }; + }; +} diff --git a/modules/clan/stirling-pdf/default.nix b/modules/clan/stirling-pdf/default.nix index 93427c8..6f87c3f 100644 --- a/modules/clan/stirling-pdf/default.nix +++ b/modules/clan/stirling-pdf/default.nix @@ -1,4 +1,4 @@ -{ lib, ... }: +{ ... }: { _class = "clan.service"; manifest.name = "stirling-pdf"; @@ -6,43 +6,18 @@ manifest.categories = [ "System" ]; roles.default = { - interface.options = { - domain = lib.mkOption { - type = lib.types.str; - default = "pdf"; - description = "Sub domain or Stirling PDF service"; - }; - }; - - perInstance = + perInstance.nixosModule = { - settings, + lib, ... }: { - nixosModule = - { - config, - ... - }: - let - domain = "${settings.domain}.${config.networking.fqdn}"; - in - { - services.stirling-pdf = { - enable = true; - environment = { - SERVER_PORT = 8080; - }; - }; - services.nginx.virtualHosts."${domain}" = { - forceSSL = true; - useACMEHost = "${config.networking.fqdn}"; - locations."/" = { - proxyPass = "http://localhost:${builtins.toString config.services.stirling-pdf.environment.SERVER_PORT}"; - }; - }; + services.stirling-pdf = { + enable = lib.mkDefault true; + environment = { + SERVER_PORT = lib.mkDefault 8080; }; + }; }; }; } diff --git a/modules/clan/stirling-pdf/flake-module.nix b/modules/clan/stirling-pdf/flake-module.nix new file mode 100644 index 0000000..02daf01 --- /dev/null +++ b/modules/clan/stirling-pdf/flake-module.nix @@ -0,0 +1,19 @@ +{ lib, ... }: +let + module = lib.modules.importApply ./default.nix { }; +in +{ + clan.modules = { + stirling-pdf = module; + }; + + perSystem = + { ... }: + { + clan.nixosTests.stirling-pdf = { + imports = [ ./tests/vm/default.nix ]; + + clan.modules."@clan/stirling-pdf" = module; + }; + }; +} diff --git a/modules/clan/stirling-pdf/tests/vm/default.nix b/modules/clan/stirling-pdf/tests/vm/default.nix new file mode 100644 index 0000000..dfa8170 --- /dev/null +++ b/modules/clan/stirling-pdf/tests/vm/default.nix @@ -0,0 +1,34 @@ +{ + ... +}: +{ + name = "service-stirling-pdf"; + + clan = { + directory = ./.; + inventory = { + machines.server = { }; + + instances = { + actual-budget-test = { + module.name = "@clan/stirling-pdf"; + module.input = "self"; + roles.default.machines."server".settings = { }; + }; + }; + }; + }; + + nodes = { + server = { }; + }; + + testScript = '' + start_all() + + server.wait_for_unit("stirling-pdf") + + server.succeed("systemctl status stirling-pdf") + + ''; +} diff --git a/vars/per-machine/b4l/b4l-stirling-pdf/subdomain/value b/vars/per-machine/b4l/b4l-stirling-pdf/subdomain/value new file mode 100644 index 0000000..6089659 --- /dev/null +++ b/vars/per-machine/b4l/b4l-stirling-pdf/subdomain/value @@ -0,0 +1 @@ +pdf \ No newline at end of file