clavarium 0.2.20-next.2 → 0.2.20-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -113,6 +113,19 @@ WIFI_STEP="3"
113
113
  # Where NetworkManager keeps its connections as files (keyfiles): a new network is written
114
114
  # there by this program, root's and 0600 -- the one place its password is ever written.
115
115
  NM_KEYFILES="/etc/NetworkManager/system-connections"
116
+ # THE NETWORK HANDED OVER TO NETWORKMANAGER (verbs "wifi handover", "wifi hand"): a server set up
117
+ # by the Ubuntu installer has its network in netplan, rendered by systemd-networkd, and no
118
+ # NetworkManager: its Wi-Fi cannot be chosen from Clavarium, and a cable plugged in later is not
119
+ # taken. NetworkManager is installed, one file of netplan's makes it the renderer, and if the
120
+ # networks in use before are not working again within $HAND_WAIT seconds, netplan's files are put
121
+ # back as they were. Its record; netplan's folder; the file added; the unit it runs in.
122
+ HAND_STATE="/var/lib/clavarium/network"
123
+ NETPLAN_DIR="/etc/netplan"
124
+ HAND_FILE="90-clavarium-network.yaml"
125
+ HAND_UNIT="clavarium-network-handover"
126
+ HAND_WAIT="90"
127
+ # Where the kernel lists the network devices (a physical one has a "device" link there).
128
+ SYS_NET="/sys/class/net"
116
129
 
117
130
  refuse() { echo "REFUSED: $*" >&2; exit 78; }
118
131
  usage() { echo "usage: clavarium-host [--say-only [--data DIR] [--mnt DIR]] <vm|shelf|folder|work|view|tools|mounts|operator|machines|disks|net|git|server|update|wifi> <verb> [arguments]" >&2; exit 64; }
@@ -125,7 +138,8 @@ while [ $# -gt 0 ]; do
125
138
  --mnt) [ "$SAY" = "yes" ] || refuse "--mnt is only for --say-only"; MNT="${2:-}"; shift 2 ;;
126
139
  --test) [ "$(id -u)" != "0" ] || refuse "--test is never for root"; [ "$SAY" = "no" ] || refuse "--test runs for real: not with --say-only"
127
140
  TEST="${2:-}"; case "$TEST" in /*) : ;; *) refuse "--test takes an absolute path" ;; esac
128
- DATA="$TEST"; MNT="$TEST"; WIFI_STATE="$TEST/wifi"; NM_KEYFILES="$TEST/system-connections"; WIFI_WAIT="3"; WIFI_UP_WAIT="3"; WIFI_STEP="0.2"; shift 2 ;;
141
+ DATA="$TEST"; MNT="$TEST"; WIFI_STATE="$TEST/wifi"; NM_KEYFILES="$TEST/system-connections"; WIFI_WAIT="3"; WIFI_UP_WAIT="3"; WIFI_STEP="0.2"
142
+ HAND_STATE="$TEST/network"; NETPLAN_DIR="$TEST/netplan"; HAND_WAIT="3"; SYS_NET="$TEST/sys-net"; shift 2 ;;
129
143
  *) break ;;
130
144
  esac
131
145
  done
@@ -458,6 +472,45 @@ wifi_keyfile() {
458
472
  [ -n "$TEST" ] || chown root:root -- "$tmp"
459
473
  mv -f -- "$tmp" "$file"
460
474
  }
475
+ # THE HANDOVER. Possible where netplan renders the network with systemd-networkd (its default)
476
+ # and this program has not handed it over already; apt-get to install NetworkManager.
477
+ hand_possible() {
478
+ local r
479
+ type -P netplan >/dev/null 2>&1 && type -P apt-get >/dev/null 2>&1 || return 1
480
+ [ ! -e "$NETPLAN_DIR/$HAND_FILE" ] || return 1
481
+ compgen -G "$NETPLAN_DIR/*.yaml" >/dev/null || return 1
482
+ r="$(netplan get renderer 2>/dev/null | head -1 || true)"
483
+ case "$r" in ""|null|networkd) return 0 ;; *) return 1 ;; esac
484
+ }
485
+ # Its record: "hand <state> <seconds since 1970>" and "handwhy <why>".
486
+ hand_record() {
487
+ install -d -m 0700 "$HAND_STATE"
488
+ { printf 'hand %s %s\n' "$1" "$(date +%s)"; [ -z "${2:-}" ] || printf 'handwhy %s\n' "$2"; } > "$HAND_STATE/last.new"
489
+ mv -f -- "$HAND_STATE/last.new" "$HAND_STATE/last"
490
+ }
491
+ hand_word() { sed -n 's/^hand \([a-z-]*\) [0-9]*$/\1/p' "$HAND_STATE/last" 2>/dev/null | head -1 || true; }
492
+ # Going on: recorded as such, and not for longer than an install and a wait could take.
493
+ hand_busy() {
494
+ local at
495
+ case "$(hand_word)" in installing|switching) : ;; *) return 1 ;; esac
496
+ at="$(sed -n 's/^hand [a-z]* \([0-9]*\)$/\1/p' "$HAND_STATE/last" | head -1)"
497
+ [ $(( $(date +%s) - ${at:-0} )) -lt 1800 ]
498
+ }
499
+ # The networks in use: "<device> <gateway>", one per default route.
500
+ hand_routes() { ip -4 route show default 2>/dev/null | sed -n 's/^default via \([0-9.]*\) dev \([A-Za-z0-9._-]*\).*$/\2 \1/p' | sort -u || true; }
501
+ # Each of them working again, through NetworkManager: connected, with its way out (a default
502
+ # route through it). Not its gateway answering a ping: many a box does not answer one.
503
+ hand_back_up() {
504
+ local dev gw st
505
+ while read -r dev gw; do
506
+ [ -n "$dev" ] || continue
507
+ st="$(nmcli -g GENERAL.STATE device show "$dev" 2>/dev/null | head -1 || true)"
508
+ case "$st" in 100*) : ;; *) return 1 ;; esac
509
+ [ -n "$(ip -4 route show default dev "$dev" 2>/dev/null || true)" ] || return 1
510
+ done < "$HAND_STATE/routes"
511
+ [ "$(nmcli -t -f RUNNING general 2>/dev/null || true)" = "running" ]
512
+ }
513
+
461
514
  # The outcome of a switch, recorded for "wifi status": <state> [<why>]. Uses $ssid and $back.
462
515
  wifi_record() {
463
516
  install -d -m 0700 "$WIFI_STATE"
@@ -1158,6 +1211,7 @@ case "$AREA $VERB" in
1158
1211
  # device <name>
1159
1212
  # using <signal 0-100> <ssid> (the SSID is the rest of the line: it may hold spaces)
1160
1213
  # saved <ssid> (one per network NetworkManager keeps)
1214
+ # link <cable|wifi> <device> <IPv4 address, or none> (one per physical device)
1161
1215
  # last <joining|joined|went-back|back-failed|interrupted> <seconds since 1970>
1162
1216
  # lastssid <ssid> lastback <ssid it went back to, or nothing> lastwhy <password|no-connection|no-internet|stopped>
1163
1217
  [ $# -eq 0 ] || usage
@@ -1170,6 +1224,23 @@ case "$AREA $VERB" in
1170
1224
  [[ "$line" =~ ^(last\ (joining|joined|went-back|back-failed|interrupted)\ [0-9]{1,12}|lastwhy\ [a-z-]{1,20}|last(ssid|back)\ .{0,32})$ ]] && printf '%s\n' "$line"
1171
1225
  done < "$WIFI_STATE/last"
1172
1226
  fi
1227
+ # The handover: how the last one went; whether it can be asked.
1228
+ if [ -f "$HAND_STATE/last" ]; then
1229
+ while IFS= read -r line; do
1230
+ if ! hand_busy && [[ "$line" =~ ^hand\ (installing|switching)\ ([0-9]{1,12})$ ]]; then line="hand interrupted ${BASH_REMATCH[2]}"; fi
1231
+ [[ "$line" =~ ^(hand\ (installing|switching|handed|went-back|failed|interrupted)\ [0-9]{1,12}|handwhy\ [a-z-]{1,20})$ ]] && printf '%s\n' "$line"
1232
+ done < "$HAND_STATE/last"
1233
+ fi
1234
+ hand_busy || ! hand_possible || echo "handover possible"
1235
+ # Its connections: each physical device, a cable or a Wi-Fi, and its address (or none).
1236
+ for d in "$SYS_NET"/*; do
1237
+ n="${d##*/}"
1238
+ [ -e "$d/device" ] && [[ "$n" =~ ^[A-Za-z0-9._-]{1,15}$ ]] || continue
1239
+ kind="cable"; [ ! -d "$d/wireless" ] || kind="wifi"
1240
+ a="$(ip -4 -o addr show dev "$n" scope global 2>/dev/null | sed -n 's/^.* inet \([0-9.]*\)\/[0-9]* .*$/\1/p' | head -1 || true)"
1241
+ [[ "$a" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]] || a="none"
1242
+ printf 'link %s %s %s\n' "$kind" "$n" "$a"
1243
+ done
1173
1244
  type -P nmcli >/dev/null 2>&1 || { printf '%s\n' "nmcli no" "wifiwhy no-nmcli"; exit 0; }
1174
1245
  echo "nmcli yes"
1175
1246
  [ "$(nmcli -t -f RUNNING general 2>/dev/null || true)" = "running" ] || { echo "wifiwhy not-running"; exit 0; }
@@ -1317,6 +1388,85 @@ case "$AREA $VERB" in
1317
1388
  done
1318
1389
  go_back no-internet
1319
1390
  exit 0 ;;
1391
+ "wifi handover")
1392
+ # THE NETWORK TO NETWORKMANAGER, asked from Settings: started in a unit of its own ("wifi
1393
+ # hand"), and this returns at once: "handing". Refused where it is not possible.
1394
+ [ $# -eq 0 ] || usage
1395
+ if [ "$SAY" = "yes" ]; then
1396
+ printf '%s\n' "systemd-run --unit=$HAND_UNIT --collect clavarium-host wifi hand:" \
1397
+ " apt-get install network-manager (when it is not installed)" \
1398
+ " copy $NETPLAN_DIR aside; write $NETPLAN_DIR/$HAND_FILE (root, 0600: renderer NetworkManager); netplan apply" \
1399
+ " ${HAND_WAIT} s at most for the networks in use before to work again: then systemctl disable systemd-networkd-wait-online;" \
1400
+ " else netplan's files put back, netplan apply"
1401
+ exit 0
1402
+ fi
1403
+ ! hand_busy || refuse "the network is being handed over already: wait for its end (a few minutes)"
1404
+ ! wifi_busy || refuse "a Wi-Fi switch is going on: wait for its end"
1405
+ hand_possible || refuse "this server's network is not one netplan renders with systemd-networkd: nothing to hand over"
1406
+ hand_record installing
1407
+ self=(/bin/bash "$(readlink -f -- "$0")"); [ -z "$TEST" ] || self+=(--test "$TEST")
1408
+ if ! systemd-run --quiet --unit="$HAND_UNIT" --collect --description="Clavarium - the network handed over to NetworkManager, back as it was if it does not work" \
1409
+ --property=TimeoutStopSec=180 "${self[@]}" wifi hand; then
1410
+ hand_record failed stopped
1411
+ refuse "the handover could not be started (another one still running?)"
1412
+ fi
1413
+ echo "handing"
1414
+ exit 0 ;;
1415
+ "wifi hand")
1416
+ # THE HANDOVER ITSELF, run by "wifi handover" in its unit, as root. NetworkManager
1417
+ # installed if it is not (nothing else changed yet: a failure there leaves all as it was);
1418
+ # netplan's folder copied aside, root's; one file of netplan's makes NetworkManager the
1419
+ # renderer (the Wi-Fi it knew comes with it, and a cable is taken when plugged in); then
1420
+ # up to $HAND_WAIT seconds for the networks in use before to work again. If they do not,
1421
+ # netplan's files are put back as they were and applied. The outcome is recorded for "wifi status".
1422
+ [ $# -eq 0 ] || usage
1423
+ if [ "$SAY" = "yes" ]; then echo "see: wifi handover"; exit 0; fi
1424
+ [ "$(hand_word)" = "installing" ] || refuse "no handover was asked"
1425
+ install -d -m 0700 "$HAND_STATE"
1426
+ hand_routes > "$HAND_STATE/routes"
1427
+ if ! type -P nmcli >/dev/null 2>&1; then
1428
+ if ! DEBIAN_FRONTEND=noninteractive apt-get install -y -q network-manager > "$HAND_STATE/install.log" 2>&1; then hand_record failed install; exit 0; fi
1429
+ fi
1430
+ hand_record switching
1431
+ aside="$HAND_STATE/netplan-before"
1432
+ rm -rf -- "$aside"; install -d -m 0700 "$aside"
1433
+ cp -a -- "$NETPLAN_DIR/." "$aside/" || { hand_record failed copy; exit 0; }
1434
+ # Back as it was: netplan's files exactly as copied (theirs only), applied.
1435
+ put_back() {
1436
+ rm -f -- "$NETPLAN_DIR/$HAND_FILE"
1437
+ find "$NETPLAN_DIR" -mindepth 1 -maxdepth 1 -name '*.yaml' -exec rm -f -- {} + 2>/dev/null || true
1438
+ cp -a -- "$aside/." "$NETPLAN_DIR/"
1439
+ netplan apply >/dev/null 2>&1 || true
1440
+ hand_record went-back "$1"
1441
+ }
1442
+ trap 'put_back stopped; exit 0' TERM INT HUP
1443
+ tmp="$(mktemp -p "$NETPLAN_DIR" .clavarium-network.XXXXXX)" || { hand_record failed write; exit 0; }
1444
+ chmod 0600 -- "$tmp"
1445
+ printf '# Written by Clavarium (Settings, Network): NetworkManager drives the network.\nnetwork:\n version: 2\n renderer: NetworkManager\n' > "$tmp"
1446
+ [ -n "$TEST" ] || chown root:root -- "$tmp"
1447
+ mv -f -- "$tmp" "$NETPLAN_DIR/$HAND_FILE"
1448
+ if ! netplan generate >/dev/null 2>&1; then rm -f -- "$NETPLAN_DIR/$HAND_FILE"; trap - TERM INT HUP; hand_record failed invalid; exit 0; fi
1449
+ netplan apply >/dev/null 2>&1 || true
1450
+ start="$SECONDS"
1451
+ looks=$(awk -v w="$HAND_WAIT" -v s="$WIFI_STEP" 'BEGIN { print int(w / s) }')
1452
+ for ((i = 0; i < looks && SECONDS - start < HAND_WAIT; i++)); do
1453
+ if hand_back_up; then
1454
+ trap - TERM INT HUP
1455
+ # Its copy held the Wi-Fi password: not kept once it is no longer needed.
1456
+ rm -rf -- "$aside"
1457
+ # systemd-networkd drives nothing now: its wait for a network would hold each start
1458
+ # two minutes, then fail. NetworkManager's own wait takes its place.
1459
+ if [ "$(systemctl is-enabled systemd-networkd-wait-online.service 2>/dev/null || true)" = "enabled" ]; then
1460
+ systemctl disable systemd-networkd-wait-online.service >/dev/null 2>&1 && echo "networkd-wait-online disabled" > "$HAND_STATE/undone"
1461
+ fi
1462
+ hand_record handed
1463
+ exit 0
1464
+ fi
1465
+ sleep "$WIFI_STEP"
1466
+ done
1467
+ trap - TERM INT HUP
1468
+ put_back no-network
1469
+ exit 0 ;;
1320
1470
  "wifi forget")
1321
1471
  # <ssid>: every network NetworkManager keeps under that name, deleted. Never the one
1322
1472
  # in use (the server would lose its network), never during a switch.
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.20-next.2",
3
+ "version": "0.2.20-next.4",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.20-next.2",
9
+ "version": "0.2.20-next.4",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
13
13
  "@xterm/xterm": "^6.0.0",
14
- "clavarium-parley": "0.13.1-next.1",
14
+ "clavarium-parley": "0.13.1-next.2",
15
15
  "mermaid": "11.17.2",
16
16
  "node-pty": "^1.1.0",
17
17
  "pdfjs-dist": "6.4.299",
@@ -686,9 +686,9 @@
686
686
  }
687
687
  },
688
688
  "node_modules/clavarium-parley": {
689
- "version": "0.13.1-next.1",
690
- "resolved": "https://registry.npmjs.org/clavarium-parley/-/clavarium-parley-0.13.1-next.1.tgz",
691
- "integrity": "sha512-PMjSH3i2NiYYLsQCqrfPp/kgJsOPXgFJOFHcYbuZuqMXgy5oaWVcI4h0p3XCduS5ffYgV5B/iMwRnQCX+8E36Q==",
689
+ "version": "0.13.1-next.2",
690
+ "resolved": "https://registry.npmjs.org/clavarium-parley/-/clavarium-parley-0.13.1-next.2.tgz",
691
+ "integrity": "sha512-xy+6BzlSg1aHPbVvW4WXRd5HLFb75tMyUwVXXgspow+QdlJ6Sk1K3nx2u2M8tncbNC+gh2+Pv0wiMbgcghY7Vg==",
692
692
  "license": "MIT",
693
693
  "bin": {
694
694
  "parley": "bin/parley.mjs",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.20-next.2",
3
+ "version": "0.2.20-next.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
@@ -44,7 +44,7 @@
44
44
  "dependencies": {
45
45
  "@xterm/addon-fit": "^0.11.0",
46
46
  "@xterm/xterm": "^6.0.0",
47
- "clavarium-parley": "0.13.1-next.1",
47
+ "clavarium-parley": "0.13.1-next.2",
48
48
  "mermaid": "11.17.2",
49
49
  "node-pty": "^1.1.0",
50
50
  "pdfjs-dist": "6.4.299",
package/server/api.mjs CHANGED
@@ -77,6 +77,7 @@
77
77
  * GET /api/wifi: this server's Wi-Fi GET /api/wifi/scan: the networks it sees (looked for again)
78
78
  * POST /api/wifi/join {ssid, password}: the switch started, back by itself if it does not work
79
79
  * POST /api/wifi/forget {ssid}: a kept network deleted (never the one in use)
80
+ * POST /api/wifi/handover: the network handed over to NetworkManager (back as it was if it does not work)
80
81
  * GET /api/events?kind=&conversation=&q=&before=&limit= what happened, newest first
81
82
  * GET /api/history?of=server|<name>&span=1h|24h|7d the curves of memory, CPU and disk (and the reserve, for the server)
82
83
  * GET /api/server/logs?source=&span= what the server's programs said POST /api/server/restart
@@ -400,6 +401,7 @@ export async function handleApi(request, response, conversations, headers, resou
400
401
  /* The password goes on to the host program and nowhere else: not in an answer, not in What happened. */
401
402
  if (pathname === '/api/wifi/join' && wifi) return send(202, await wifi.join(body.ssid, body.password ?? '')), true;
402
403
  if (pathname === '/api/wifi/forget' && wifi) return send(200, await wifi.forget(body.ssid)), true;
404
+ if (pathname === '/api/wifi/handover' && wifi) return send(202, await wifi.handover()), true;
403
405
  if (pathname === '/api/operators' && team) return send(201, await team.add(body.email)), true;
404
406
  const op = /^\/api\/operators\/([^/]{3,200})\/(password|disable|enable|remove)$/.exec(pathname);
405
407
  if (op && team) {
package/server/events.mjs CHANGED
@@ -57,6 +57,7 @@ const SAID = [
57
57
  [/^\/api\/machines\/forget-old$/, 'server', () => 'The old copy of the machines removed'],
58
58
  /* Its name only: the password of a network is never said. */
59
59
  [/^\/api\/wifi\/join$/, 'server', (m, b) => `Wi-Fi: joining "${clip(b.ssid, 40)}" asked`],
60
+ [/^\/api\/wifi\/handover$/, 'server', () => 'Network: management by Clavarium asked'],
60
61
  [/^\/api\/wifi\/forget$/, 'server', (m, b) => `Wi-Fi: "${clip(b.ssid, 40)}" forgotten`],
61
62
  [/^\/api\/instance$/, 'server', (m, b) => (String(b.name ?? '').trim() ? `This Clavarium named "${clip(b.name, 40)}"` : 'This Clavarium given back the name drawn from its address')],
62
63
  [/^\/api\/operators$/, 'sign-in', (m, b) => `Administrator ${clip(b.email, 80)} added`],
package/server/wifi.mjs CHANGED
@@ -9,7 +9,7 @@
9
9
  * before by itself -- whether or not this server, the page or the request are
10
10
  * still there to see it. The outcome is read back from `status()`.
11
11
  *
12
- * status() { available, why, device, using: {ssid, signal} | null, saved: [ssid],
12
+ * status() { available, why, device, using: {ssid, signal} | null, saved: [ssid], links: [{kind: cable|wifi, device, address | null}],
13
13
  * last: {state, at, ssid, back, why} | null }
14
14
  * why (not available): no-nmcli, not-running, no-device, unmanaged, unreadable
15
15
  * last.state: joining, joined, went-back, back-failed, interrupted
@@ -18,6 +18,15 @@
18
18
  * join(ssid, password) the switch started: { started, ssid, back } (back: the network it
19
19
  * goes back to, or null); the password goes to the host on standard input only
20
20
  * forget(ssid) a kept network deleted (never the one in use)
21
+ *
22
+ * THE NETWORK HANDED OVER TO NETWORKMANAGER: a server set up by the Ubuntu installer has
23
+ * its network in netplan with systemd-networkd and no NetworkManager -- its Wi-Fi is not
24
+ * chosen from here, a cable plugged in later is not taken. `status()` says when it can be
25
+ * handed over (`handover: true`) and how the last one went (`hand: {state, at, why}`):
26
+ * handover() started in a unit of the host's own: NetworkManager installed, netplan
27
+ * made to render through it; back as it was if the networks in use before do
28
+ * not work again within a minute and a half. { started: true }
29
+ * hand.state: installing, switching, handed, went-back, failed, interrupted
21
30
  */
22
31
  import { Refusal } from './conversations.mjs';
23
32
 
@@ -38,13 +47,16 @@ export function passwordWhy(password) {
38
47
  return '';
39
48
  }
40
49
  const STATES = ['joining', 'joined', 'went-back', 'back-failed', 'interrupted'];
50
+ const HAND_STATES = ['installing', 'switching', 'handed', 'went-back', 'failed', 'interrupted'];
51
+ /* A handover is looked at this long: an install, then a minute and a half. */
52
+ const HAND_WATCH_FOR_MS = 20 * 60_000;
41
53
  /* After a join, the outcome is looked for this often and this long, to be said in What happened. */
42
54
  const WATCH_EVERY_MS = 5000;
43
55
  const WATCH_FOR_MS = 6 * 60_000;
44
56
 
45
57
  /* "wifi status", line by line: only the lines of the expected shapes. */
46
58
  export function readStatus(text) {
47
- const out = { available: false, why: null, device: null, using: null, saved: [], last: null };
59
+ const out = { available: false, why: null, device: null, using: null, saved: [], last: null, handover: false, hand: null, links: [] };
48
60
  let nmcli = false;
49
61
  for (const line of String(text ?? '').split('\n').slice(0, 300)) {
50
62
  const word = line.split(' ', 1)[0]; const rest = line.slice(word.length + 1);
@@ -54,11 +66,16 @@ export function readStatus(text) {
54
66
  else if (word === 'using') { const m = /^(\d{1,3}) (.+)$/.exec(rest); if (m) out.using = { signal: Math.min(100, Number(m[1])), ssid: m[2] }; }
55
67
  else if (word === 'saved' && rest) out.saved.push(rest);
56
68
  else if (word === 'last') { const m = /^([a-z-]+) (\d{1,12})$/.exec(rest); if (m && STATES.includes(m[1])) out.last = { ...(out.last ?? {}), state: m[1], at: new Date(Number(m[2]) * 1000).toISOString() }; }
69
+ else if (word === 'handover') out.handover = rest === 'possible';
70
+ else if (word === 'link') { const m = /^(cable|wifi) ([A-Za-z0-9._-]{1,15}) (none|\d{1,3}(?:\.\d{1,3}){3})$/.exec(rest); if (m) out.links.push({ kind: m[1], device: m[2], address: m[3] === 'none' ? null : m[3] }); }
71
+ else if (word === 'hand') { const m = /^([a-z-]+) (\d{1,12})$/.exec(rest); if (m && HAND_STATES.includes(m[1])) out.hand = { ...(out.hand ?? {}), state: m[1], at: new Date(Number(m[2]) * 1000).toISOString() }; }
72
+ else if (word === 'handwhy' && /^[a-z-]{1,20}$/.test(rest)) out.hand = { ...(out.hand ?? {}), why: rest };
57
73
  else if (['lastssid', 'lastback', 'lastwhy'].includes(word) && line.startsWith(`${word} `)) out.last = { ...(out.last ?? {}), [word.slice(4)]: rest || null };
58
74
  }
59
75
  out.available = nmcli && !out.why && Boolean(out.device);
60
76
  if (!nmcli && !out.why) out.why = 'unreadable';
61
77
  if (out.last && !out.last.state) out.last = null;
78
+ if (out.hand && !out.hand.state) out.hand = null;
62
79
  return out;
63
80
  }
64
81
  /* "wifi scan": network <signal> <security> <now|other> <ssid>. */
@@ -72,7 +89,7 @@ export function readScan(text) {
72
89
  }
73
90
 
74
91
  export class Wifi {
75
- #host; #said; #watching = null; #everyMs; #forMs;
92
+ #host; #said; #watching = null; #handing = null; #everyMs; #forMs;
76
93
  /* `said(text)`: an outcome, in What happened (events.mjs). */
77
94
  constructor({ host, said = () => {}, watchEveryMs = WATCH_EVERY_MS, watchForMs = WATCH_FOR_MS }) {
78
95
  this.#host = host; this.#said = said; this.#everyMs = watchEveryMs; this.#forMs = watchForMs;
@@ -98,6 +115,29 @@ export class Wifi {
98
115
  this.#watch(ssid);
99
116
  return { started: true, ssid, back };
100
117
  }
118
+ async handover() {
119
+ const r = await this.#host(['wifi', 'handover'], { timeoutMs: 30_000 });
120
+ if (r.code !== 0) throw new Refusal(r.code === 78 ? 409 : 502, firstLine(r.stderr) || `exit ${r.code}`);
121
+ this.#watchHand();
122
+ return { started: true };
123
+ }
124
+ /* Its outcome, said once in What happened, as a switch's is. */
125
+ #watchHand() {
126
+ clearTimeout(this.#handing);
127
+ const until = Date.now() + HAND_WATCH_FOR_MS;
128
+ const look = async () => {
129
+ const h = (await this.status().catch(() => null))?.hand;
130
+ if (h && !['installing', 'switching'].includes(h.state)) {
131
+ this.#handing = null;
132
+ const why = { install: 'could not install NetworkManager', invalid: 'netplan refused', 'no-network': 'no network', stopped: 'stopped', copy: 'could not copy', write: 'could not write' }[h.why] ?? h.why ?? 'no reason given';
133
+ const words = { handed: 'Network: now managed by Clavarium', 'went-back': `Network: did not work (${why}), back as before`, failed: `Network: did not work (${why}), nothing changed`, interrupted: 'Network: interrupted' };
134
+ try { this.#said(words[h.state] ?? `Network: ${h.state}`); } catch { /* never in the way */ }
135
+ return;
136
+ }
137
+ if (Date.now() < until) { this.#handing = setTimeout(look, this.#everyMs); this.#handing.unref?.(); }
138
+ };
139
+ this.#handing = setTimeout(look, this.#everyMs); this.#handing.unref?.();
140
+ }
101
141
  async forget(ssid) {
102
142
  if (!nameOk(ssid)) throw new Refusal(400, 'Not a network name.');
103
143
  const r = await this.#host(['wifi', 'forget', ssid], { timeoutMs: 30_000 });
package/web/app.js CHANGED
@@ -4186,11 +4186,11 @@
4186
4186
  to the network before by itself when the new one does not work within a minute -- this page may
4187
4187
  lose the server meanwhile, so it says so before, and reads the outcome when it can again. */
4188
4188
  const WIFI_OUTSIDE = {
4189
- 'no-nmcli': 'Wi-Fi is managed outside Clavarium on this server (NetworkManager is not installed here).',
4190
- 'not-running': 'Wi-Fi is managed outside Clavarium on this server (NetworkManager does not run here).',
4191
- 'no-device': 'This server has no Wi-Fi device that NetworkManager sees: its network is managed outside Clavarium.',
4192
- unmanaged: 'Wi-Fi is managed outside Clavarium on this server (its Wi-Fi device is not NetworkManager\'s to drive).',
4193
- unreadable: 'This server\'s network cannot be read just now.',
4189
+ 'no-nmcli': 'Wi-Fi is managed outside Clavarium on this server.',
4190
+ 'not-running': 'Wi-Fi is managed outside Clavarium on this server.',
4191
+ 'no-device': 'This server has no Wi-Fi.',
4192
+ unmanaged: 'Wi-Fi is managed outside Clavarium on this server.',
4193
+ unreadable: 'The network cannot be read just now.',
4194
4194
  };
4195
4195
  const WIFI_WHY = { password: 'the password was not accepted', 'no-connection': 'it could not connect', 'no-internet': 'it gave no working network within a minute', stopped: 'the switch was stopped' };
4196
4196
  const SECURITY_WORDS = { open: 'open', wpa: 'password', wpa3: 'password (WPA3)', wep: 'old protection (WEP): not joined here', enterprise: 'user and certificate (802.1X): not joined here', other: 'not joined here' };
@@ -4208,6 +4208,35 @@
4208
4208
  const arch = document.createElementNS(ns, 'path'); arch.setAttribute('d', 'M8 11V7a4 4 0 0 1 8 0v4');
4209
4209
  svg.append(body, arch); return svg;
4210
4210
  };
4211
+ /* THE NETWORK HANDED OVER TO NETWORKMANAGER (the host program, "wifi handover"): offered where
4212
+ netplan drives it with systemd-networkd; its progress and outcome, as the host recorded them. */
4213
+ const HAND_WHY = { install: 'could not install', invalid: 'refused', 'no-network': 'no network', stopped: 'stopped', copy: 'could not copy', write: 'could not write' };
4214
+ const HAND_DAY_MS = 24 * 3600_000;
4215
+ function paintHand(s) {
4216
+ const h = s.hand; const busy = ['installing', 'switching'].includes(h?.state);
4217
+ const words = !h ? '' : {
4218
+ installing: 'Setting it up (a minute or two)...',
4219
+ switching: 'Switching (a few seconds)...',
4220
+ handed: 'Done: Clavarium manages the network.',
4221
+ 'went-back': `It did not work (${HAND_WHY[h.why] ?? h.why ?? 'no reason given'}): all is back as before.`,
4222
+ failed: `It did not work (${HAND_WHY[h.why] ?? h.why ?? 'no reason given'}): nothing changed.`,
4223
+ interrupted: 'Interrupted (the server restarted?).',
4224
+ }[h.state] ?? '';
4225
+ const old = h?.state === 'handed' && Date.now() - Date.parse(h.at) > HAND_DAY_MS;
4226
+ $('wifi-hand-said').textContent = words; $('wifi-hand-said').hidden = !words || old;
4227
+ $('wifi-hand-said').className = h && ['went-back', 'failed', 'interrupted'].includes(h.state) ? 'error' : 'hint';
4228
+ $('wifi-hand').hidden = !s.handover || busy;
4229
+ if (busy && $('settings-wifi-top').open && !$('settings').hidden) wifiTimer = setTimeout(() => { void paintWifi(); }, 3000);
4230
+ }
4231
+ const handReset = () => { const go = $('wifi-hand-go'); go.dataset.sure = ''; go.textContent = 'Manage the network'; go.disabled = false; $('wifi-hand-cancel').hidden = true; };
4232
+ $('wifi-hand-go').addEventListener('click', () => {
4233
+ const go = $('wifi-hand-go');
4234
+ /* Two clicks: the network may be cut for a few seconds. */
4235
+ if (go.dataset.sure !== 'yes') { go.dataset.sure = 'yes'; go.textContent = 'Confirm (undone by itself if it fails)'; $('wifi-hand-cancel').hidden = false; return; }
4236
+ go.disabled = true;
4237
+ void post(`${API}/wifi/handover`, {}).then(() => { handReset(); void paintWifi(); }).catch((error) => { handReset(); $('wifi-hand-said').textContent = error.message; $('wifi-hand-said').className = 'error'; $('wifi-hand-said').hidden = false; });
4238
+ });
4239
+ $('wifi-hand-cancel').addEventListener('click', handReset);
4211
4240
  async function paintWifi() {
4212
4241
  clearTimeout(wifiTimer);
4213
4242
  const s = await fetch(`${API}/wifi`, { cache: 'no-store' }).then((r) => (r.ok ? r.json() : null)).catch(() => null);
@@ -4218,8 +4247,20 @@
4218
4247
  return;
4219
4248
  }
4220
4249
  wifiNow = s;
4250
+ paintHand(s);
4251
+ /* Its connections, cable and Wi-Fi, each with its address. */
4252
+ const links = $('net-links'); links.replaceChildren();
4253
+ for (const l of s.links ?? []) {
4254
+ const li = el('li', l.address ? '' : 'hint');
4255
+ li.append(el('b', '', l.kind === 'wifi' ? 'Wi-Fi' : 'Cable'), ` (${l.device}): `, l.address ? el('code', '', l.address) : 'not connected');
4256
+ links.append(li);
4257
+ }
4258
+ links.hidden = !(s.links ?? []).length;
4221
4259
  $('wifi-controls').hidden = !s.available;
4222
- if (!s.available) { $('wifi-now').textContent = WIFI_OUTSIDE[s.why] ?? WIFI_OUTSIDE.unreadable; $('wifi-last').hidden = true; return; }
4260
+ /* While it is handed over, its progress alone is said. */
4261
+ const handing = ['installing', 'switching'].includes(s.hand?.state);
4262
+ if (!s.available) { $('wifi-now').textContent = handing ? '' : s.handover ? 'Clavarium does not manage this server\'s network yet.' : WIFI_OUTSIDE[s.why] ?? WIFI_OUTSIDE.unreadable; $('wifi-now').hidden = handing; $('wifi-last').hidden = true; return; }
4263
+ $('wifi-now').hidden = false;
4223
4264
  $('wifi-now').replaceChildren();
4224
4265
  if (s.using) $('wifi-now').append(el('span', '', 'On '), el('b', '', s.using.ssid), el('span', '', ` (Wi-Fi, ${s.device}) `), signalBars(s.using.signal));
4225
4266
  else $('wifi-now').textContent = `Not on a Wi-Fi network (${s.device}).`;
package/web/index.html CHANGED
@@ -712,11 +712,18 @@
712
712
  <!-- NETWORK: this server's Wi-Fi (server/wifi.mjs; done as root by the host program,
713
713
  through NetworkManager). A switch goes back by itself when the new network does not work. -->
714
714
  <details class="chapter" id="settings-wifi-top" data-chapter="s-network">
715
- <summary><span class="chapter-title">Network</span><span class="chapter-about">The Wi-Fi this server is on; another one joined.</span></summary>
715
+ <summary><span class="chapter-title">Network</span><span class="chapter-about">Its cable and its Wi-Fi.</span></summary>
716
716
  <div class="chapter-body">
717
717
  <div id="wifi" tabindex="-1">
718
718
  <p id="wifi-now" role="status"></p>
719
+ <ul id="net-links" class="net-links" hidden></ul>
719
720
  <p id="wifi-last" class="hint" role="status" hidden></p>
721
+ <!-- ITS NETWORK SET UP OUTSIDE CLAVARIUM (netplan and systemd-networkd, as the Ubuntu installer leaves it): handed over to NetworkManager, back as it was if it does not work. -->
722
+ <div id="wifi-hand" hidden>
723
+ <p id="wifi-hand-about">Let Clavarium manage it: Wi-Fi chosen here, a cable taken by itself. Your Wi-Fi stays as it is.</p>
724
+ <div class="row"><button type="button" class="primary" id="wifi-hand-go">Manage the network</button><button type="button" id="wifi-hand-cancel" hidden>Cancel</button></div>
725
+ </div>
726
+ <p id="wifi-hand-said" role="status" hidden></p>
720
727
  <div id="wifi-controls" hidden>
721
728
  <div class="row"><button type="button" id="wifi-scan">Look for networks</button><span class="hint" id="wifi-said" role="status"></span></div>
722
729
  <ul id="wifi-list" class="wifi-list" hidden></ul>
package/web/style.css CHANGED
@@ -1425,3 +1425,7 @@ button.inline.more-recent { text-decoration: none; }
1425
1425
  .ts-way { grid-column: 2 / -1; }
1426
1426
  .ts-size { grid-column: 2; } .ts-state { grid-column: 3; text-align: right; }
1427
1427
  }
1428
+
1429
+ /* Network: its connections, one a line. */
1430
+ .net-links { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 2px; }
1431
+ .net-links code { font-size: .875rem; }