Skip to content

Commit

Permalink
Merge pull request #46 from cspr-rad/add-cctl-module
Browse files Browse the repository at this point in the history
Add cctl module and extend e2e test to use it
  • Loading branch information
marijanp authored Apr 1, 2024
2 parents 210229f + a0d71e8 commit acc3bd1
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 // {
Expand Down
8 changes: 8 additions & 0 deletions kairos-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions kairos-test-utils/bin/cctld.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use kairos_test_utils::cctl;
use sd_notify::NotifyState;
use tokio::signal;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
10 changes: 9 additions & 1 deletion nixos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -40,6 +42,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;
};
};
};
}
62 changes: 62 additions & 0 deletions nixos/modules/cctl.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{ lib, pkgs, config, ... }:
let
inherit (lib)
types
mkOption
mkIf
mkMerge
mkEnableOption
;
cfg = config.services.cctl;
in
{
options.services.cctl = {

enable = mkEnableOption ("cctl");

package = mkOption {
type = types.package;
};

port = mkOption {
type = types.port;
default = 11101;
example = 60000;
description = ''
Port to listen on.
TODO make port configurable in cctl
'';
};

logLevel = mkOption {
type = types.str;
default = "info";
description = ''
The log-level that should be used.
'';
};
};

config = mkIf cfg.enable {

systemd.services.cctl =
{
description = "cctl";
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" ];
environment = {
RUST_LOG = cfg.logLevel;
};
serviceConfig = mkMerge [
{
ExecStart = ''${lib.getExe cfg.package}'';
Type = "notify";
Restart = "always";
DynamicUser = true;
}
];
};
};
}
9 changes: 9 additions & 0 deletions nixos/tests/end-to-end.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
, mkKairosHostConfig
, kairos
, testResources ? ../../kairos-cli/tests/fixtures
, cctlModule
, casper-client-rs
}:
nixosTest {
name = "kairos e2e test";
Expand All @@ -10,13 +12,16 @@ nixosTest {
server = { config, pkgs, lib, ... }: {
imports = [
(mkKairosHostConfig "kairos")
cctlModule
];

# modify acme for nixos-test environment
security.acme = {
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, ... }: {
Expand All @@ -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)))
Expand Down

0 comments on commit acc3bd1

Please sign in to comment.