Skip to content

Commit 92cd3b0

Browse files
committed
checkmate-server: init at 3.4.0
1 parent 6db4155 commit 92cd3b0

File tree

6 files changed

+8046
-0
lines changed

6 files changed

+8046
-0
lines changed

nixos/modules/module-list.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@
999999
./services/monitoring/bosun.nix
10001000
./services/monitoring/cadvisor.nix
10011001
./services/monitoring/certspotter.nix
1002+
./services/monitoring/checkmate-server.nix
10021003
./services/monitoring/cockpit.nix
10031004
./services/monitoring/collectd.nix
10041005
./services/monitoring/das_watchdog.nix
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
config,
3+
pkgs,
4+
lib,
5+
...
6+
}:
7+
let
8+
cfg = config.services.checkmate-server;
9+
10+
inherit (lib)
11+
mkEnableOption
12+
mkPackageOption
13+
mkIf
14+
mkOption
15+
types
16+
isPath
17+
;
18+
inherit (builtins) toString;
19+
20+
assertStringPath =
21+
optionName: value:
22+
if isPath value then
23+
throw ''
24+
services.checkmate-server.${optionName}:
25+
${toString value}
26+
is a Nix path, but should be a string, since Nix
27+
paths are copied into the world-readable Nix store.
28+
''
29+
else
30+
value;
31+
in
32+
{
33+
options = {
34+
services.checkmate-server = {
35+
enable = mkEnableOption "Enable Checkmate.";
36+
37+
package = mkPackageOption pkgs "checkmate-server" { };
38+
39+
vhostName = mkOption {
40+
type = types.str;
41+
default = "checkmate-server";
42+
description = "Name of the nginx vhost.";
43+
};
44+
45+
enableLocalDB = mkEnableOption "a local MongoDB instance";
46+
47+
settings = {
48+
clientHost = mkOption {
49+
type = types.str;
50+
default = "http://127.0.0.1";
51+
description = "Frontend Host URI.";
52+
};
53+
54+
origin = mkOption {
55+
type = types.str;
56+
default = "localhost";
57+
description = ''
58+
Origin where requests to server originate from, for CORS purposes.
59+
'';
60+
};
61+
62+
port = mkOption {
63+
type = types.port;
64+
default = 52345;
65+
description = "Port the Checkmate backend should listen on.";
66+
};
67+
68+
logLevel = mkOption {
69+
type = types.enum [
70+
"debug"
71+
"info"
72+
"warn"
73+
"error"
74+
];
75+
default = "info";
76+
description = "Debug level, can be one of: debug, info, warn, error.";
77+
};
78+
79+
tokenTTL = mkOption {
80+
type = types.str;
81+
default = "1h";
82+
description = ''
83+
Time for token to live in vercel/ms format https://github.com/vercel/ms.
84+
'';
85+
};
86+
87+
JWTSecretFile = mkOption {
88+
type = types.path;
89+
apply = assertStringPath "settings.JWTSecretFile";
90+
description = ''
91+
Path to a file that contains the secret to sign web requests using JSON Web Tokens.
92+
'';
93+
};
94+
};
95+
96+
mongodbUri = mkOption {
97+
type = types.str;
98+
default = "mongodb://127.0.0.1:27017/uptime_db";
99+
description = ''
100+
MongoDB connection string.
101+
See http://docs.mongodb.org/manual/reference/connection-string/ for details.
102+
'';
103+
};
104+
};
105+
};
106+
107+
config = mkIf cfg.enable {
108+
109+
services.mongodb = mkIf cfg.enableLocalDB {
110+
enable = true;
111+
};
112+
113+
systemd.services.checkmate-backend = {
114+
description = "Checkmate backend daemon";
115+
wantedBy = [ "multi-user.target" ];
116+
after = [ "network.target" ] ++ lib.optionals cfg.enableLocalDB [ "mongodb.service" ];
117+
startLimitIntervalSec = 60;
118+
startLimitBurst = 3;
119+
environment = {
120+
CLIENT_HOST = cfg.settings.clientHost;
121+
LOG_LEVEL = cfg.settings.logLevel;
122+
REFRESH_TOKEN_SECRET = cfg.settings.tokenTTL;
123+
DB_CONNECTION_STRING = cfg.mongodbUri;
124+
ORIGIN = cfg.settings.origin;
125+
TOKEN_TTL = cfg.settings.tokenTTL;
126+
};
127+
serviceConfig = {
128+
LoadCredential = [ "JWT_SECRET:${cfg.settings.JWTSecretFile}" ];
129+
PrivateDevices = true;
130+
LimitCORE = 0;
131+
KillSignal = "SIGINT";
132+
TimeoutStopSec = "30s";
133+
Restart = "on-failure";
134+
DynamicUser = true;
135+
};
136+
script = ''
137+
set -eou pipefail
138+
shopt -s inherit_errexit
139+
140+
JWT_SECRET="$(<"$CREDENTIALS_DIRECTORY/JWT_SECRET")" \
141+
${cfg.package}/startserver ${cfg.package}/backend/index.js
142+
'';
143+
};
144+
145+
services.nginx.virtualHosts.${cfg.vhostName} = {
146+
locations."/" = {
147+
root = "${cfg.package}/public";
148+
index = "index.html index.htm";
149+
tryFiles = "$uri $uri/ /index.html";
150+
};
151+
locations."/api/" = {
152+
proxyPass = "http://127.0.0.1:${toString cfg.settings.port}/api/";
153+
proxyWebsockets = true;
154+
};
155+
locations."/api-docs/" = {
156+
proxyPass = "http://127.0.0.1:${toString cfg.settings.port}/api-docs/";
157+
proxyWebsockets = true;
158+
};
159+
};
160+
161+
};
162+
}

nixos/tests/all-tests.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ in
354354
cfssl = runTestOn [ "aarch64-linux" "x86_64-linux" ] ./cfssl.nix;
355355
cgit = runTest ./cgit.nix;
356356
charliecloud = runTest ./charliecloud.nix;
357+
checkmate-server = runTest ./checkmate-server.nix;
357358
chhoto-url = runTest ./chhoto-url.nix;
358359
chromadb = runTest ./chromadb.nix;
359360
chromium = (handleTestOn [ "aarch64-linux" "x86_64-linux" ] ./chromium.nix { }).stable or { };

nixos/tests/checkmate-server.nix

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Run this test with NIXPKGS_ALLOW_UNFREE=1
2+
{ lib, ... }:
3+
{
4+
name = "checkmate-server";
5+
meta.maintainers = with lib.maintainers; [ robertjakub ];
6+
7+
nodes.machine =
8+
{ pkgs, ... }:
9+
{
10+
services.mongodb.package = pkgs.mongodb-ce;
11+
12+
services.checkmate-server = {
13+
enable = true;
14+
vhostName = "default";
15+
enableLocalDB = true;
16+
settings = {
17+
logLevel = "info";
18+
clientHost = "http://127.0.0.1";
19+
JWTSecretFile = "/run/checkmate-jwt";
20+
};
21+
};
22+
23+
};
24+
25+
testScript = ''
26+
machine.start()
27+
28+
machine.execute("echo \"JWTSecret\" > /run/checkmate-jwt && chmod 400 /run/checkmate-jwt")
29+
machine.wait_for_unit("checkmate-backend.service")
30+
machine.wait_for_open_port(52345)
31+
32+
machine.wait_until_succeeds("journalctl -o cat -u checkmate-backend.service | grep 'Server started on port:52345'")
33+
34+
machine.succeed("curl -sSfN http://127.0.0.1:52345/api/ | grep \"<title>Checkmate</title>\"")
35+
machine.succeed("curl -sSfN http://127.0.0.1:52345/ | grep \"<title>Checkmate</title>\"")
36+
37+
machine.shutdown()
38+
'';
39+
40+
}

0 commit comments

Comments
 (0)