nx_swarm_endpoint.nx source
↩ module page · 145 lines · 7415 B
1// nx_swarm_endpoint.nx -- thin CLI over nx_swarm_endpoint_lib (the resolver SSOT).
2//
3// The parser and the table live in the LIB so that consumers (nx_swarm_coord, nx_worker_dispatch,
4// nx_mesh_lb, nx_hostctl, nx_mgmt_api) call the SAME code this CLI proves. If the CLI owned its own
5// copy of the parser we would have rebuilt the very duplication this whole arc exists to delete.
6//
7// EXIT: 0 resolved / 4 REFUSE (unknown role, unreadable table) / 2 usage.
8// license_tier: ORIGINAL expect_exit: 0
9import "nx_syscalls.nx"
10import "nx_swarm_endpoint_lib.nx"
11const K_MAGIC_65536: i64 = 65536
12
13func main(argc: i64, argv: *i64) -> i64 {
14 let out: *u8 = sys_mmap(K_MAGIC_65536)
15 var o: i64 = 0
16 if argc < 2 {
17 o = se_put(out, o, "usage: nx_swarm_endpoint {addr <role> | list | selftest}\n" as *u8)
18 o = se_put(out, o, " exit 0 resolved | 4 REFUSE (unknown role / unreadable table)\n" as *u8)
19 sys_write(1, out, o)
20 return 0
21 }
22 let verb: *u8 = argv[1] as *u8
23 let ctx: *i64 = se_ctx()
24 if ctx[3] < 0 {
25 o = se_put(out, o, "{\x22v\x22:1,\x22organ\x22:\x22nx_swarm_endpoint\x22,\x22verdict\x22:\x22REFUSE\x22,\x22reason\x22:\x22endpoint table unreadable -- fail-closed, no fabricated address\x22}\n" as *u8)
26 sys_write(1, out, o)
27 sys_exit(4)
28 return 4
29 }
30 let buf: *u8 = ctx[0] as *u8
31 let n: i64 = ctx[1]
32 let rows: *i64 = ctx[2] as *i64
33 let nr: i64 = ctx[3]
34
35 if se_ceq(verb, "list" as *u8) == 1 {
36 o = se_put(out, o, "{\x22v\x22:1,\x22organ\x22:\x22nx_swarm_endpoint\x22,\x22verb\x22:\x22list\x22,\x22rows\x22:[" as *u8)
37 var i: i64 = 0
38 while i < nr {
39 if i > 0 { o = se_put(out, o, "," as *u8) }
40 o = se_put(out, o, "{\x22role\x22:\x22" as *u8)
41 o = se_putn(out, o, buf, rows[i * 8 + 0], rows[i * 8 + 1])
42 o = se_put(out, o, "\x22,\x22node\x22:\x22" as *u8)
43 o = se_putn(out, o, buf, rows[i * 8 + 2], rows[i * 8 + 3])
44 o = se_put(out, o, "\x22,\x22addr\x22:\x22" as *u8)
45 o = se_putn(out, o, buf, rows[i * 8 + 4], rows[i * 8 + 5])
46 o = se_put(out, o, "\x22,\x22desc\x22:\x22" as *u8)
47 o = se_putn(out, o, buf, rows[i * 8 + 6], rows[i * 8 + 7])
48 o = se_put(out, o, "\x22}" as *u8)
49 i = i + 1
50 }
51 o = se_put(out, o, "],\x22count\x22:" as *u8)
52 o = se_putd(out, o, nr)
53 o = se_put(out, o, "}\n" as *u8)
54 sys_write(1, out, o)
55 return 0
56 }
57
58 if se_ceq(verb, "selftest" as *u8) == 1 {
59 var pass: i64 = 0
60 var fail: i64 = 0
61 o = se_put(out, o, "{\x22v\x22:1,\x22organ\x22:\x22nx_swarm_endpoint\x22,\x22verb\x22:\x22selftest\x22,\x22teeth\x22:[" as *u8)
62
63 let a1: *u8 = se_addr("gpu-image" as *u8)
64 o = se_put(out, o, "{\x22t\x22:1,\x22name\x22:\x22gpu-image-resolves\x22,\x22ok\x22:" as *u8)
65 if (a1 as i64) != 0 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) } else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
66
67 let a2: *u8 = se_addr("gpu-video" as *u8)
68 o = se_put(out, o, ",{\x22t\x22:2,\x22name\x22:\x22gpu-video-resolves\x22,\x22ok\x22:" as *u8)
69 if (a2 as i64) != 0 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) } else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
70
71 let a3: *u8 = se_addr("gpu-nonexistent" as *u8)
72 o = se_put(out, o, ",{\x22t\x22:3,\x22name\x22:\x22unknown-role-refuses\x22,\x22ok\x22:" as *u8)
73 if (a3 as i64) == 0 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) } else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
74
75 o = se_put(out, o, ",{\x22t\x22:4,\x22name\x22:\x22stale-193-absent\x22,\x22ok\x22:" as *u8)
76 if se_has(buf, 0, n, "192.168.8.193" as *u8) == 0 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) }
77 else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
78
79 o = se_put(out, o, ",{\x22t\x22:5,\x22name\x22:\x22needle-scanner-non-vacuous\x22,\x22ok\x22:" as *u8)
80 if se_has(buf, 0, n, "gpu-image" as *u8) == 1 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) }
81 else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
82
83 o = se_put(out, o, ",{\x22t\x22:6,\x22name\x22:\x22image-addr-is-measured-192\x22,\x22ok\x22:" as *u8)
84 var t6: i64 = 0
85 if (a1 as i64) != 0 { if se_ceq(a1, "192.168.8.192:7861" as *u8) == 1 { t6 = 1 } }
86 if t6 == 1 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) } else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
87
88 // T7 the DISPLAY form the five old literals spelled by hand is reproduced exactly,
89 // so repointing a consumer at the lib is a behaviour-preserving swap, not a reformat.
90 let d7: *u8 = se_addr_desc("gpu-image" as *u8)
91 o = se_put(out, o, ",{\x22t\x22:7,\x22name\x22:\x22addr-desc-render\x22,\x22ok\x22:" as *u8)
92 var t7: i64 = 0
93 if (d7 as i64) != 0 { if se_ceq(d7, "192.168.8.192:7861 (laptop RTX 5080)" as *u8) == 1 { t7 = 1 } }
94 if t7 == 1 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) } else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
95
96 // T8 an unknown role must not render a display string either (fail-closed on BOTH paths)
97 let d8: *u8 = se_addr_desc("gpu-nonexistent" as *u8)
98 o = se_put(out, o, ",{\x22t\x22:8,\x22name\x22:\x22unknown-desc-refuses\x22,\x22ok\x22:" as *u8)
99 if (d8 as i64) == 0 { pass = pass + 1; o = se_put(out, o, "1}" as *u8) } else { fail = fail + 1; o = se_put(out, o, "0}" as *u8) }
100
101 o = se_put(out, o, "],\x22pass\x22:" as *u8)
102 o = se_putd(out, o, pass)
103 o = se_put(out, o, ",\x22fail\x22:" as *u8)
104 o = se_putd(out, o, fail)
105 o = se_put(out, o, ",\x22verdict\x22:\x22" as *u8)
106 if fail == 0 { o = se_put(out, o, "GREEN" as *u8) } else { o = se_put(out, o, "RED" as *u8) }
107 o = se_put(out, o, "\x22}\n" as *u8)
108 sys_write(1, out, o)
109 if fail != 0 { sys_exit(1) }
110 return 0
111 }
112
113 if se_ceq(verb, "addr" as *u8) == 1 {
114 if argc < 3 {
115 o = se_put(out, o, "{\x22verdict\x22:\x22REFUSE\x22,\x22reason\x22:\x22addr needs a role\x22}\n" as *u8)
116 sys_write(1, out, o)
117 sys_exit(2)
118 return 2
119 }
120 let role: *u8 = argv[2] as *u8
121 let a: *u8 = se_addr(role)
122 if (a as i64) == 0 {
123 o = se_put(out, o, "{\x22v\x22:1,\x22organ\x22:\x22nx_swarm_endpoint\x22,\x22role\x22:\x22" as *u8)
124 o = se_put(out, o, role)
125 o = se_put(out, o, "\x22,\x22verdict\x22:\x22REFUSE\x22,\x22reason\x22:\x22role not in the endpoint table -- never fabricate an address\x22}\n" as *u8)
126 sys_write(1, out, o)
127 sys_exit(4)
128 return 4
129 }
130 o = se_put(out, o, "{\x22v\x22:1,\x22organ\x22:\x22nx_swarm_endpoint\x22,\x22role\x22:\x22" as *u8)
131 o = se_put(out, o, role)
132 o = se_put(out, o, "\x22,\x22addr\x22:\x22" as *u8)
133 o = se_put(out, o, a)
134 o = se_put(out, o, "\x22,\x22display\x22:\x22" as *u8)
135 o = se_put(out, o, se_addr_desc(role))
136 o = se_put(out, o, "\x22,\x22verdict\x22:\x22RESOLVED\x22}\n" as *u8)
137 sys_write(1, out, o)
138 return 0
139 }
140
141 o = se_put(out, o, "nx_swarm_endpoint: unknown verb\n" as *u8)
142 sys_write(1, out, o)
143 sys_exit(2)
144 return 2
145}