From f04b4ebf188a22f233540f05eeb26daa0c57aa09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Wed, 20 Mar 2024 15:27:41 -0500 Subject: [PATCH 1/4] cctld: initial impl using kairos-test-utils --- Cargo.lock | 7 +++++++ flake.nix | 11 +++++++++++ kairos-test-utils/Cargo.toml | 8 ++++++++ kairos-test-utils/bin/cctld.rs | 14 ++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 kairos-test-utils/bin/cctld.rs diff --git a/Cargo.lock b/Cargo.lock index e42fd1ad..069f91a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1725,6 +1725,7 @@ dependencies = [ "backoff", "casper-client", "nom", + "sd-notify", "tempfile", "tokio", "tracing", @@ -2711,6 +2712,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "sd-notify" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "621e3680f3e07db4c9c2c3fb07c6223ab2fab2e54bd3c04c3ae037990f428c32" + [[package]] name = "sec1" version = "0.7.3" diff --git a/flake.nix b/flake.nix index 77b8f4ba..a2c18b75 100644 --- a/flake.nix +++ b/flake.nix @@ -85,6 +85,17 @@ cargoArtifacts = self'.packages.kairos-deps; }); + cctld = pkgs.runCommand "cctld-wrapped" + { + buildInputs = [ pkgs.makeWrapper ]; + meta.mainProgram = "cctld"; + } + '' + mkdir -p $out/bin + makeWrapper ${self'.packages.kairos}/bin/cctld $out/bin/cctld \ + --set PATH ${pkgs.lib.makeBinPath [inputs'.csprpkgs.packages.cctl ]} + ''; + default = self'.packages.kairos; kairos-docs = craneLib.cargoDoc (kairosNodeAttrs // { diff --git a/kairos-test-utils/Cargo.toml b/kairos-test-utils/Cargo.toml index 72766425..2e333083 100644 --- a/kairos-test-utils/Cargo.toml +++ b/kairos-test-utils/Cargo.toml @@ -4,6 +4,13 @@ version.workspace = true edition.workspace = true license.workspace = true +[[bin]] +name = "cctld" +path = "bin/cctld.rs" +version.workspace = true +test = false +bench = false + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] @@ -13,6 +20,7 @@ anyhow = "1" backoff = { version = "0.4", features = ["tokio", "futures"]} casper-client = "2" nom = "7" +sd-notify = "0.4" tokio = { version = "1", features = [ "full", "tracing", "macros" ] } tempfile = "3" tracing = "0.1" diff --git a/kairos-test-utils/bin/cctld.rs b/kairos-test-utils/bin/cctld.rs new file mode 100644 index 00000000..9e4f90bb --- /dev/null +++ b/kairos-test-utils/bin/cctld.rs @@ -0,0 +1,14 @@ +use kairos_test_utils::cctl; +use sd_notify::NotifyState; +use tokio::signal; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let _network = cctl::CCTLNetwork::run() + .await + .expect("An error occured while starting the CCTL network"); + + let _ = sd_notify::notify(true, &[NotifyState::Ready]); + signal::ctrl_c().await?; + Ok(()) +} From d9b3eedae6ce9adc7966fa42d021cca2288b98b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Wed, 20 Mar 2024 15:31:10 -0500 Subject: [PATCH 2/4] nixos/modules/cctl: initial module definition --- nixos/default.nix | 6 ++++ nixos/modules/cctl.nix | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 nixos/modules/cctl.nix diff --git a/nixos/default.nix b/nixos/default.nix index 1757047e..dee0f65b 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -40,6 +40,12 @@ in imports = [ ./modules/kairos.nix ]; services.kairos.package = self.packages.${pkgs.system}.kairos; }; + cctl = + { pkgs, lib, ... }: + { + imports = [ ./modules/cctl.nix ]; + services.cctl.package = self.packages.${pkgs.system}.cctld; + }; }; }; } diff --git a/nixos/modules/cctl.nix b/nixos/modules/cctl.nix new file mode 100644 index 00000000..f03fcc71 --- /dev/null +++ b/nixos/modules/cctl.nix @@ -0,0 +1,69 @@ +{ lib, pkgs, config, ... }: +let + inherit (lib) + types + mkOption + mkIf + mkMerge + mkEnableOption + mdDoc + ; + cfg = config.services.cctl; +in +{ + options.services.cctl = { + + enable = mkEnableOption (mdDoc "cctl"); + + package = mkOption { + type = types.package; + }; + + port = mkOption { + type = types.port; + default = 11101; + example = 60000; + description = mdDoc '' + Port to listen on. + TODO make port configurable in cctl + ''; + }; + + logLevel = mkOption { + type = types.enum [ + "error" + "warn" + "info" + "debug" + "trace" + ]; + default = "info"; + description = '' + The log-level that should be used. + ''; + }; + }; + + config = mkIf cfg.enable { + + systemd.services.cctl = + { + description = "cctl"; + documentation = [ "" ]; + wantedBy = [ "multi-user.target" ]; + after = [ "network-online.target" ]; + requires = [ "network-online.target" ]; + environment = { + RUST_LOG = cfg.logLevel; + }; + serviceConfig = mkMerge [ + { + ExecStart = ''${lib.getExe cfg.package}''; + Type = "notify"; + Restart = "always"; + DynamicUser = true; + } + ]; + }; + }; +} From ae1e56f28e87df4fa433d30b8a59645c2a5d2a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Wed, 20 Mar 2024 15:31:39 -0500 Subject: [PATCH 3/4] nixos/e2e: add cctl nixos module to e2e test --- nixos/default.nix | 4 +++- nixos/tests/end-to-end.nix | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/nixos/default.nix b/nixos/default.nix index dee0f65b..494dce20 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -30,7 +30,9 @@ in ./tests/end-to-end.nix { inherit mkKairosHostConfig; - inherit (self.packages."x86_64-linux") kairos; + inherit (self.packages.${pkgs.system}) kairos; + cctlModule = self.nixosModules.cctl; + inherit (inputs.csprpkgs.packages.${pkgs.system}) casper-client-rs; }; }; nixosModules = { diff --git a/nixos/tests/end-to-end.nix b/nixos/tests/end-to-end.nix index 335fa8b4..fb81c59b 100644 --- a/nixos/tests/end-to-end.nix +++ b/nixos/tests/end-to-end.nix @@ -2,6 +2,8 @@ , mkKairosHostConfig , kairos , testResources ? ../../kairos-cli/tests/fixtures +, cctlModule +, casper-client-rs }: nixosTest { name = "kairos e2e test"; @@ -10,6 +12,7 @@ nixosTest { server = { config, pkgs, lib, ... }: { imports = [ (mkKairosHostConfig "kairos") + cctlModule ]; # modify acme for nixos-test environment @@ -17,6 +20,8 @@ nixosTest { preliminarySelfsigned = true; defaults.server = "https://example.com"; # don't spam the acme production server }; + services.cctl.enable = true; + environment.systemPackages = [ casper-client-rs ]; }; client = { config, pkgs, nodes, ... }: { @@ -29,12 +34,16 @@ nixosTest { start_all() + kairos.wait_for_unit("cctl.service") + kairos.wait_for_unit("kairos.service") kairos.wait_for_unit("nginx.service") kairos.wait_for_open_port(80) client.wait_for_unit ("multi-user.target") + kairos.succeed("casper-client get-node-status --node-address http://localhost:11101") + deposit_request = { "public_key": "publickey", "amount": 10 } # REST API client.succeed("curl -X POST http://kairos/api/v1/deposit -H 'Content-Type: application/json' -d '{}'".format(json.dumps(deposit_request))) From a0d71e8caa93ebe04a9c156e8b1ec749478d3b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Mon, 1 Apr 2024 12:42:38 -0500 Subject: [PATCH 4/4] nixos/cctl: use string log level since casper-node uses tracing --- nixos/modules/cctl.nix | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/nixos/modules/cctl.nix b/nixos/modules/cctl.nix index f03fcc71..63b734c5 100644 --- a/nixos/modules/cctl.nix +++ b/nixos/modules/cctl.nix @@ -6,14 +6,13 @@ let mkIf mkMerge mkEnableOption - mdDoc ; cfg = config.services.cctl; in { options.services.cctl = { - enable = mkEnableOption (mdDoc "cctl"); + enable = mkEnableOption ("cctl"); package = mkOption { type = types.package; @@ -23,20 +22,14 @@ in type = types.port; default = 11101; example = 60000; - description = mdDoc '' + description = '' Port to listen on. TODO make port configurable in cctl ''; }; logLevel = mkOption { - type = types.enum [ - "error" - "warn" - "info" - "debug" - "trace" - ]; + type = types.str; default = "info"; description = '' The log-level that should be used. @@ -49,7 +42,7 @@ in systemd.services.cctl = { description = "cctl"; - documentation = [ "" ]; + documentation = [ "A collection of bash applications to work with a local casper-node network, turned to a systemd service." ]; wantedBy = [ "multi-user.target" ]; after = [ "network-online.target" ]; requires = [ "network-online.target" ];