Add e2e script to the repo, Dockerfile, and a Nix flake with a NixOS module
- scripts/e2e.sh (+ scripts/e2e/*.ts): the two-node end-to-end test, now self-contained and cleaning up after itself; `make e2e`, part of `make check` - Dockerfile/.dockerignore: static Go build in a non-root alpine image, data in /data, defaults to region u1j; `make docker` - flake.nix, nix/: package (buildGoModule), dev shell, NixOS module services.allmende (hardened systemd unit that waits for `allmende-admin init`), and a NixOS VM test run by `nix flake check` - README: running a node with Docker and NixOS Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+160
@@ -0,0 +1,160 @@
|
||||
self:
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
cfg = config.services.allmende;
|
||||
exe = lib.getExe cfg.package;
|
||||
|
||||
# `allmende init` and `recover` run as the service's user and need a terminal
|
||||
# (they show and check the recovery phrase), so they are not part of the unit.
|
||||
admin = pkgs.writeShellScriptBin "allmende-admin" ''
|
||||
exec sudo -u allmende env ALLMENDE_DATA=${lib.escapeShellArg cfg.dataDir} ${exe} "$@"
|
||||
'';
|
||||
|
||||
args =
|
||||
[
|
||||
"run"
|
||||
"--http"
|
||||
cfg.httpAddress
|
||||
"--listen"
|
||||
"/ip4/0.0.0.0/tcp/${toString cfg.p2pPort}"
|
||||
]
|
||||
++ lib.concatMap (r: [
|
||||
"--region"
|
||||
r
|
||||
]) cfg.regions
|
||||
++ lib.concatMap (b: [
|
||||
"--bootstrap"
|
||||
b
|
||||
]) cfg.bootstrap
|
||||
++ cfg.extraArgs;
|
||||
in
|
||||
{
|
||||
options.services.allmende = {
|
||||
enable = lib.mkEnableOption "an Allmende node (a free, distributed marketplace for homes)";
|
||||
|
||||
package = lib.mkOption {
|
||||
type = lib.types.package;
|
||||
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
|
||||
defaultText = lib.literalExpression "allmende.packages.\${system}.default";
|
||||
description = "The allmende package to run.";
|
||||
};
|
||||
|
||||
regions = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
example = [ "u1j" ];
|
||||
description = ''
|
||||
Regions to replicate: 3-character geohashes, each about 150 km wide.
|
||||
`u1j` is Bonn, `u1h` Cologne, `u1x` Hamburg.
|
||||
'';
|
||||
};
|
||||
|
||||
bootstrap = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
example = [ "/ip4/203.0.113.5/tcp/4001/p2p/12D3KooW..." ];
|
||||
description = "Known peers to connect to, as multiaddrs ending in /p2p/<peer id>.";
|
||||
};
|
||||
|
||||
httpAddress = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "127.0.0.1:8080";
|
||||
description = ''
|
||||
Where the web UI and API listen. The same port serves the owner API,
|
||||
which the admin token protects, so keep it on loopback and put a TLS
|
||||
reverse proxy in front for public use.
|
||||
'';
|
||||
};
|
||||
|
||||
p2pPort = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
default = 4001;
|
||||
description = "TCP port other nodes connect to.";
|
||||
};
|
||||
|
||||
openFirewall = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Open the peer-to-peer port ({option}`p2pPort`) in the firewall.";
|
||||
};
|
||||
|
||||
dataDir = lib.mkOption {
|
||||
type = lib.types.path;
|
||||
default = "/var/lib/allmende";
|
||||
description = "Where keys, the admin token and the database live.";
|
||||
};
|
||||
|
||||
extraArgs = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
example = [ "--no-mdns" ];
|
||||
description = "Further arguments for `allmende run`.";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
assertions = [
|
||||
{
|
||||
assertion = cfg.regions != [ ];
|
||||
message = "services.allmende.regions needs at least one region, for example [ \"u1j\" ].";
|
||||
}
|
||||
];
|
||||
|
||||
users.users.allmende = {
|
||||
isSystemUser = true;
|
||||
group = "allmende";
|
||||
home = cfg.dataDir;
|
||||
description = "Allmende node";
|
||||
};
|
||||
users.groups.allmende = { };
|
||||
|
||||
systemd.tmpfiles.rules = [ "d ${cfg.dataDir} 0700 allmende allmende -" ];
|
||||
|
||||
# One-time setup, because it shows the recovery phrase:
|
||||
# sudo allmende-admin init && sudo systemctl start allmende
|
||||
# The unit is skipped (not failed) until that has happened.
|
||||
systemd.services.allmende = {
|
||||
description = "Allmende node";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
unitConfig.ConditionPathExists = "${cfg.dataDir}/admin.token";
|
||||
environment.ALLMENDE_DATA = cfg.dataDir;
|
||||
serviceConfig = {
|
||||
ExecStart = "${exe} ${lib.escapeShellArgs args}";
|
||||
User = "allmende";
|
||||
Group = "allmende";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
|
||||
NoNewPrivileges = true;
|
||||
PrivateTmp = true;
|
||||
PrivateDevices = true;
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
ReadWritePaths = [ cfg.dataDir ];
|
||||
ProtectKernelTunables = true;
|
||||
ProtectKernelModules = true;
|
||||
ProtectControlGroups = true;
|
||||
RestrictSUIDSGID = true;
|
||||
LockPersonality = true;
|
||||
# libp2p reads the network interfaces over netlink to find its addresses.
|
||||
RestrictAddressFamilies = [
|
||||
"AF_INET"
|
||||
"AF_INET6"
|
||||
"AF_UNIX"
|
||||
"AF_NETLINK"
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.p2pPort ];
|
||||
|
||||
environment.systemPackages = [ admin ];
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
{
|
||||
lib,
|
||||
buildGoModule,
|
||||
version,
|
||||
}:
|
||||
|
||||
buildGoModule {
|
||||
pname = "allmende";
|
||||
inherit version;
|
||||
|
||||
# Only what the Go build reads, so editing docs or tests does not rebuild.
|
||||
# web/dist is the pre-built UI that the binary embeds.
|
||||
src = lib.fileset.toSource {
|
||||
root = ../.;
|
||||
fileset = lib.fileset.unions [
|
||||
../go.mod
|
||||
../go.sum
|
||||
../cmd
|
||||
../internal
|
||||
../web/embed.go
|
||||
../web/dist
|
||||
];
|
||||
};
|
||||
|
||||
# Changes whenever go.mod or go.sum do: set it to lib.fakeHash, build, and
|
||||
# copy the hash from the error.
|
||||
vendorHash = "sha256-vUBmsqyS+/qXo3UDYzZmZQYik7rMhOrX7csy56UJbLU=";
|
||||
|
||||
subPackages = [ "cmd/allmende" ];
|
||||
env.CGO_ENABLED = "0";
|
||||
ldflags = [
|
||||
"-s"
|
||||
"-w"
|
||||
];
|
||||
|
||||
# The tests start libp2p nodes and need loopback networking and mDNS, which
|
||||
# the build sandbox does not give. `make test` runs them.
|
||||
doCheck = false;
|
||||
|
||||
meta = {
|
||||
description = "A free, distributed marketplace for buying, selling and renting";
|
||||
license = lib.licenses.agpl3Only;
|
||||
mainProgram = "allmende";
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
{ pkgs, module }:
|
||||
|
||||
pkgs.testers.runNixOSTest {
|
||||
name = "allmende";
|
||||
|
||||
nodes.machine = {
|
||||
imports = [ module ];
|
||||
services.allmende = {
|
||||
enable = true;
|
||||
regions = [ "u1j" ];
|
||||
openFirewall = true;
|
||||
};
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
machine.wait_for_unit("multi-user.target")
|
||||
|
||||
with subtest("the service waits for setup instead of failing"):
|
||||
machine.succeed("systemctl show allmende -p ActiveState | grep -q inactive")
|
||||
machine.fail("systemctl is-failed allmende")
|
||||
|
||||
with subtest("init creates the identity, as the service's own user"):
|
||||
# The VM's shell is a terminal, where init would wait for the phrase to be
|
||||
# typed back; empty input makes it skip that check, as in a script.
|
||||
out = machine.succeed("allmende-admin init </dev/null")
|
||||
assert "recovery phrase" in out.lower(), out
|
||||
machine.succeed("test $(stat -c %U /var/lib/allmende/admin.token) = allmende")
|
||||
machine.succeed("test $(stat -c %a /var/lib/allmende/admin.token) = 600")
|
||||
machine.fail("allmende-admin init </dev/null") # a second init must refuse
|
||||
|
||||
with subtest("the node starts and serves the UI and API"):
|
||||
machine.succeed("systemctl start allmende")
|
||||
machine.wait_for_open_port(8080)
|
||||
machine.wait_for_open_port(4001)
|
||||
status = machine.succeed("curl -sf http://127.0.0.1:8080/api/v1/status")
|
||||
assert '"regions":["u1j"]' in status, status
|
||||
machine.succeed("curl -sf http://127.0.0.1:8080/ | grep -qi '<html'")
|
||||
|
||||
with subtest("the owner API needs the admin token"):
|
||||
machine.succeed("test $(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8080/api/v1/mine) = 401")
|
||||
machine.succeed(
|
||||
"curl -sf -H \"Authorization: Bearer $(cat /var/lib/allmende/admin.token)\" http://127.0.0.1:8080/api/v1/mine"
|
||||
)
|
||||
|
||||
with subtest("the web port stays on loopback, the peer port is open"):
|
||||
machine.succeed("ss -ltn | grep -q '127.0.0.1:8080'")
|
||||
machine.fail("ss -ltn | grep -q '0.0.0.0:8080'")
|
||||
|
||||
with subtest("it survives a restart"):
|
||||
machine.succeed("systemctl restart allmende")
|
||||
machine.wait_for_open_port(8080)
|
||||
'';
|
||||
}
|
||||
Reference in New Issue
Block a user