Skip to content

Commit 77cff38

Browse files
feat: try_lock/unlock hil commands (#1209)
Co-authored-by: chrisgalanis <christos.galanis@toolsforhumanity.com>
1 parent f8bdea2 commit 77cff38

3 files changed

Lines changed: 127 additions & 1 deletion

File tree

nix/machines/home-hil.nix

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,31 @@ let
1414
user = "worldcoin";
1515
};
1616
};
17+
mkHilOrchestratorCmd =
18+
{ name, source }:
19+
pkgs.writeShellScriptBin name ''
20+
export HIL_ORCHESTRATOR_URL='${osConfig.worldcoin.hilOrchestratorUrl}'
21+
export HIL_ORB_ID='${osConfig.worldcoin.orbId}'
22+
exec ${pkgs.python3.withPackages (ps: [ ps.requests ])}/bin/python3 ${source} "$@"
23+
'';
24+
hilOrchestratorCmds =
25+
lib.optionals (osConfig.worldcoin.orbId != null && osConfig.worldcoin.hilOrchestratorUrl != null)
26+
[
27+
(mkHilOrchestratorCmd {
28+
name = "try_lock";
29+
source = ../scripts/hil_try_lock.py;
30+
})
31+
(mkHilOrchestratorCmd {
32+
name = "unlock";
33+
source = ../scripts/hil_unlock.py;
34+
})
35+
];
1736
in
1837
{
1938
home = {
2039
username = "worldcoin";
2140
homeDirectory = "/home/worldcoin";
22-
packages = import "${packages}/hil.nix" { inherit pkgs; };
41+
packages = (import "${packages}/hil.nix" { inherit pkgs; }) ++ hilOrchestratorCmds;
2342
sessionVariables = {
2443
EDITOR = "nvim";
2544
VISUAL = "nvim";

nix/scripts/hil_try_lock.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
"""try_lock: lock this HIL on the orb-hil-orchestrator.
3+
4+
Reads `HIL_ORCHESTRATOR_URL` and `HIL_ORB_ID` from the environment (set by the
5+
NixOS wrapper). Optional positional args become a note shown on the dashboard
6+
so other engineers know why this runner is locked.
7+
8+
The server enforces the only real safety constraint: it rejects the request
9+
"""
10+
11+
from __future__ import annotations
12+
13+
import os
14+
import sys
15+
16+
import requests
17+
18+
19+
TIMEOUT = 10
20+
21+
22+
def _error_msg(resp: requests.Response) -> str:
23+
try:
24+
return resp.json().get("error") or resp.text
25+
except ValueError:
26+
return resp.text
27+
28+
29+
def main() -> int:
30+
orchestrator = os.environ["HIL_ORCHESTRATOR_URL"]
31+
orb_id = os.environ["HIL_ORB_ID"]
32+
note = " ".join(sys.argv[1:]).strip() or None
33+
34+
resp = requests.post(f"{orchestrator}/runners/{orb_id}/lock", timeout=TIMEOUT)
35+
if resp.status_code != 200:
36+
print(
37+
f"try_lock failed (HTTP {resp.status_code}): {_error_msg(resp)}",
38+
file=sys.stderr,
39+
)
40+
return 1
41+
print(f"lock queued for {orb_id}")
42+
43+
if note:
44+
resp = requests.put(
45+
f"{orchestrator}/runners/{orb_id}/note",
46+
json={"note": note},
47+
timeout=TIMEOUT,
48+
)
49+
if resp.status_code == 204:
50+
print(f"note set: {note}")
51+
else:
52+
print(
53+
f"try_lock: lock queued but note PUT failed "
54+
f"(HTTP {resp.status_code})",
55+
file=sys.stderr,
56+
)
57+
58+
return 0
59+
60+
61+
if __name__ == "__main__":
62+
sys.exit(main())

nix/scripts/hil_unlock.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
"""unlock: queue an unlock on the orb-hil-orchestrator and clear the note.
3+
4+
Reads `HIL_ORCHESTRATOR_URL` and `HIL_ORB_ID` from the environment (set by the
5+
NixOS wrapper).
6+
"""
7+
8+
from __future__ import annotations
9+
10+
import os
11+
import sys
12+
13+
import requests
14+
15+
16+
TIMEOUT = 10
17+
18+
19+
def _error_msg(resp: requests.Response) -> str:
20+
try:
21+
return resp.json().get("error") or resp.text
22+
except ValueError:
23+
return resp.text
24+
25+
26+
def main() -> int:
27+
orchestrator = os.environ["HIL_ORCHESTRATOR_URL"]
28+
orb_id = os.environ["HIL_ORB_ID"]
29+
30+
resp = requests.post(f"{orchestrator}/runners/{orb_id}/unlock", timeout=TIMEOUT)
31+
if resp.status_code != 200:
32+
print(
33+
f"unlock failed (HTTP {resp.status_code}): {_error_msg(resp)}",
34+
file=sys.stderr,
35+
)
36+
return 1
37+
print(f"unlock queued for {orb_id}")
38+
39+
requests.delete(f"{orchestrator}/runners/{orb_id}/note", timeout=TIMEOUT)
40+
41+
return 0
42+
43+
44+
if __name__ == "__main__":
45+
sys.exit(main())

0 commit comments

Comments
 (0)