clavarium 0.2.190-beta.1 → 0.2.191-beta.1
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.
- package/deploy/clavarium-host +212 -11
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/server/api.mjs +14 -0
- package/server/capacity.mjs +23 -8
- package/server/conversations.mjs +294 -29
- package/server/main.mjs +8 -1
- package/server/settingswords.mjs +21 -0
- package/server/vm.mjs +39 -7
- package/web/app.js +178 -11
- package/web/index.html +16 -2
- package/web/style.css +28 -0
package/deploy/clavarium-host
CHANGED
|
@@ -61,6 +61,23 @@ set -euo pipefail
|
|
|
61
61
|
export LC_ALL=C
|
|
62
62
|
|
|
63
63
|
MULTIPASS="multipass"
|
|
64
|
+
# THE WAY DOWN (vm halt, vm hardstop): how long each step is given before the next, in seconds --
|
|
65
|
+
# a polite stop, a stop by force asked of the VM manager, SIGTERM to the machine's own qemu, SIGKILL
|
|
66
|
+
# to it; then how long multipass may take to answer a list, and to see the machine stopped after.
|
|
67
|
+
HALT_POLITE="60"
|
|
68
|
+
HALT_FORCE="30"
|
|
69
|
+
HALT_TERM="5"
|
|
70
|
+
HALT_KILL="5"
|
|
71
|
+
HALT_LIST="20"
|
|
72
|
+
HALT_SETTLE="10"
|
|
73
|
+
# Where the qemu processes seen for each machine are noted (root's, in memory): a qemu ended but not
|
|
74
|
+
# reaped by a VM manager that hangs (a zombie) has no command line left to tell whose it was.
|
|
75
|
+
QEMU_SEEN="/run/clavarium/qemu"
|
|
76
|
+
# The bounds of the plain verbs (a VM manager that hangs must not hold a call for ever).
|
|
77
|
+
START_SECONDS="180"
|
|
78
|
+
STOP_SECONDS="120"
|
|
79
|
+
KILL_SECONDS="60"
|
|
80
|
+
SUSPEND_SECONDS="120"
|
|
64
81
|
IMAGE="24.04"
|
|
65
82
|
DISK="8G"
|
|
66
83
|
LAUNCH_SECONDS="900"
|
|
@@ -178,7 +195,8 @@ while [ $# -gt 0 ]; do
|
|
|
178
195
|
TEST="${2:-}"; case "$TEST" in /*) : ;; *) refuse "--test takes an absolute path" ;; esac
|
|
179
196
|
DATA="$TEST"; MNT="$TEST"; WIFI_STATE="$TEST/wifi"; NM_KEYFILES="$TEST/system-connections"; WIFI_WAIT="3"; WIFI_UP_WAIT="3"; WIFI_STEP="0.2"
|
|
180
197
|
HAND_STATE="$TEST/network"; NETPLAN_DIR="$TEST/netplan"; HAND_WAIT="3"; SYS_NET="$TEST/sys-net"
|
|
181
|
-
MESH_DATA="$TEST/mesh"; MESH_STATE="$TEST/meshcentral"; CADDYFILE="$TEST/Caddyfile"; MESH_CADDY="$TEST/clavarium-meshcentral.caddy"
|
|
198
|
+
MESH_DATA="$TEST/mesh"; MESH_STATE="$TEST/meshcentral"; CADDYFILE="$TEST/Caddyfile"; MESH_CADDY="$TEST/clavarium-meshcentral.caddy"
|
|
199
|
+
HALT_POLITE="2"; HALT_FORCE="2"; HALT_TERM="1"; HALT_KILL="2"; HALT_LIST="2"; HALT_SETTLE="2"; QEMU_SEEN="$TEST/qemu"; shift 2 ;;
|
|
182
200
|
*) break ;;
|
|
183
201
|
esac
|
|
184
202
|
done
|
|
@@ -219,12 +237,163 @@ vm_of() { is_name "${1#clv-}" && [ "$1" != "${1#clv-}" ] || refuse "not one of o
|
|
|
219
237
|
vm_qemu_pids() {
|
|
220
238
|
local want="$1" pid vm
|
|
221
239
|
for pid in $(pgrep qemu-system 2>/dev/null || true); do
|
|
240
|
+
# A process that has ended but not yet been reaped (a zombie) holds no memory: it is gone.
|
|
241
|
+
[ "$(sed -n 's/^State:[[:space:]]*\([A-Z]\).*/\1/p' "/proc/$pid/status" 2>/dev/null)" = "Z" ] && continue
|
|
222
242
|
vm="$(tr '\0' '\n' < "/proc/$pid/cmdline" 2>/dev/null | grep -oE '/instances/clv(shelf)?-[a-z0-9-]+/' | head -1 | cut -d/ -f3)" || vm=""
|
|
223
243
|
[ "$vm" = "$want" ] && printf '%s\n' "$pid"
|
|
224
244
|
done
|
|
245
|
+
# (Its status is never that of the last process looked at: callers assign it under set -e.)
|
|
246
|
+
return 0
|
|
225
247
|
}
|
|
226
248
|
# No qemu left for this machine after a short grace: its RAM has been freed.
|
|
227
249
|
vm_freed() { local v="$1" i; for i in $(seq 1 24); do [ -z "$(vm_qemu_pids "$v")" ] && return 0; sleep 0.5; done; return 1; }
|
|
250
|
+
# THIS machine's qemu, PROVEN just before a signal: a process named qemu-system* whose command line
|
|
251
|
+
# holds a disk in this instance's own directory. Read again right before each signal, so that a
|
|
252
|
+
# process id reused meanwhile, or another machine's qemu, is never hit.
|
|
253
|
+
vm_qemu_is() {
|
|
254
|
+
local pid="$1" want="$2" comm vm
|
|
255
|
+
[[ "$pid" =~ ^[0-9]+$ ]] || return 1
|
|
256
|
+
comm="$(cat "/proc/$pid/comm" 2>/dev/null)" || return 1
|
|
257
|
+
[[ "$comm" == qemu-system* ]] || return 1
|
|
258
|
+
vm="$(tr '\0' '\n' < "/proc/$pid/cmdline" 2>/dev/null | grep -oE '/instances/clv(shelf)?-[a-z0-9-]+/' | head -1 | cut -d/ -f3)" || return 1
|
|
259
|
+
[ "$vm" = "$want" ]
|
|
260
|
+
}
|
|
261
|
+
# The memory its qemu holds (kB, its resident set), counted before the way down: what is freed.
|
|
262
|
+
vm_rss_kb() { local pid kb total=0; for pid in "$@"; do kb="$(sed -n 's/^VmRSS:[[:space:]]*\([0-9]*\) kB$/\1/p' "/proc/$pid/status" 2>/dev/null)" || kb=""; total=$((total + ${kb:-0})); done; echo "$total"; }
|
|
263
|
+
gb_of_kb() { awk -v k="$1" 'BEGIN { printf "%.1fG", k / 1048576 }'; }
|
|
264
|
+
# What multipass says of it, bounded: its state, "absent" (listed, not there), or "unknown" (mute).
|
|
265
|
+
vm_state_of() {
|
|
266
|
+
local out
|
|
267
|
+
out="$(timeout "$HALT_LIST" "$MULTIPASS" list --format csv 2>/dev/null)" || { echo "unknown"; return 0; }
|
|
268
|
+
out="$(printf '%s\n' "$out" | awk -F, -v n="$1" '$1 == n { print $2; exit }')"
|
|
269
|
+
echo "${out:-absent}"
|
|
270
|
+
}
|
|
271
|
+
# A process's state letter (R, S, D, T stopped, t traced, Z ended but not reaped), or nothing when gone.
|
|
272
|
+
proc_state() { sed -n 's/^State:[[:space:]]*\([A-Za-z]\).*/\1/p' "/proc/$1/status" 2>/dev/null || true; }
|
|
273
|
+
# Still running (frozen included): its /proc entry is there and it is not a zombie.
|
|
274
|
+
pid_alive() { local st; [ -d "/proc/$1" ] || return 1; st="$(proc_state "$1")"; [ -n "$st" ] && [ "$st" != "Z" ]; }
|
|
275
|
+
# Its qemu, noted as seen (vm_qemu_unreaped reads it back).
|
|
276
|
+
vm_qemu_note() {
|
|
277
|
+
local v="$1" pids
|
|
278
|
+
pids="$(vm_qemu_pids "$v")"
|
|
279
|
+
[ -n "$pids" ] || return 0
|
|
280
|
+
install -d -m 0700 "$QEMU_SEEN" 2>/dev/null || return 0
|
|
281
|
+
printf '%s\n' $pids > "$QEMU_SEEN/$v" 2>/dev/null || true
|
|
282
|
+
}
|
|
283
|
+
# A qemu of it that has ended but that the VM manager has not reaped (a zombie: it holds no memory,
|
|
284
|
+
# and no signal can do more to it). The VM manager is the one stuck.
|
|
285
|
+
vm_qemu_unreaped() {
|
|
286
|
+
local v="$1" pid
|
|
287
|
+
[ -r "$QEMU_SEEN/$v" ] || return 0
|
|
288
|
+
while read -r pid; do
|
|
289
|
+
[[ "$pid" =~ ^[0-9]+$ ]] || continue
|
|
290
|
+
[ "$(proc_state "$pid")" = "Z" ] && [[ "$(cat "/proc/$pid/comm" 2>/dev/null)" == qemu-system* ]] && printf '%s\n' "$pid"
|
|
291
|
+
done < "$QEMU_SEEN/$v"
|
|
292
|
+
return 0
|
|
293
|
+
}
|
|
294
|
+
# A qemu of it that is FROZEN (stopped, or traced): it answers nothing, a suspend asked of it hangs the VM manager.
|
|
295
|
+
vm_qemu_frozen() { local pid; for pid in $(vm_qemu_pids "$1"); do [[ "$(proc_state "$pid")" == [Tt] ]] && return 0; done; return 1; }
|
|
296
|
+
# NOTHING LEFT TO STOP -- the same proof as the signals: no live qemu of it (after a grace of $2
|
|
297
|
+
# seconds), none ended and left unreaped, and the VM manager saying Stopped (or not there, or not
|
|
298
|
+
# answering: then there is nothing more it can do). Asleep, or anything else (Suspending, Running,
|
|
299
|
+
# Unknown...) with no qemu: not gone -- the steps go on, and the check says what is wrong.
|
|
300
|
+
halt_gone() {
|
|
301
|
+
local v="$1" i state
|
|
302
|
+
for i in $(seq 0 "$2"); do [ -z "$(vm_qemu_pids "$v")" ] && break; [ "$i" -lt "$2" ] && sleep 1; done
|
|
303
|
+
[ -z "$(vm_qemu_pids "$v")" ] || return 1
|
|
304
|
+
[ -z "$(vm_qemu_unreaped "$v")" ] || return 1
|
|
305
|
+
state="$(vm_state_of "$v")"
|
|
306
|
+
[ "$state" = "Stopped" ] || [ "$state" = "absent" ] || [ "$state" = "unknown" ]
|
|
307
|
+
}
|
|
308
|
+
# One step that asks the VM manager, in the background, so that a TERM of this program (the server
|
|
309
|
+
# moving on: "Force now") ends it at once instead of leaving it to run its whole bound.
|
|
310
|
+
HALT_CHILD=""
|
|
311
|
+
halt_run() { local rc=0; "$@" >/dev/null 2>&1 & HALT_CHILD=$!; wait "$HALT_CHILD" || rc=$?; HALT_CHILD=""; return "$rc"; }
|
|
312
|
+
# A signal to its qemu, each process proven first; then waited for, up to $3 seconds, by each
|
|
313
|
+
# process's own /proc entry (gone, or ended) and by its name again. A FROZEN qemu (stopped: SIGSTOP,
|
|
314
|
+
# or traced) keeps a TERM pending until it runs: it is let run (SIGCONT) right after. A signal
|
|
315
|
+
# refused is said on the error output.
|
|
316
|
+
halt_signal() {
|
|
317
|
+
local v="$1" sig="$2" wait="$3" pid i st pids left
|
|
318
|
+
pids="$(vm_qemu_pids "$v")"
|
|
319
|
+
for pid in $pids; do
|
|
320
|
+
vm_qemu_is "$pid" "$v" || continue
|
|
321
|
+
st="$(proc_state "$pid")"
|
|
322
|
+
kill "-$sig" "$pid" 2>/dev/null || echo "signal $sig to $pid refused" >&2
|
|
323
|
+
if [[ "$st" == [Tt] ]]; then kill -CONT "$pid" 2>/dev/null || echo "signal CONT to $pid refused" >&2; fi
|
|
324
|
+
done
|
|
325
|
+
for i in $(seq 1 $((wait * 4))); do
|
|
326
|
+
left=""; for pid in $pids; do pid_alive "$pid" && left="$left $pid"; done
|
|
327
|
+
[ -z "$left" ] && [ -z "$(vm_qemu_pids "$v")" ] && return 0
|
|
328
|
+
sleep 0.25
|
|
329
|
+
done
|
|
330
|
+
return 1
|
|
331
|
+
}
|
|
332
|
+
# THE WAY DOWN, from step $2 to step $3 (polite, force, term, kill), then the check. One line per
|
|
333
|
+
# step on the output, for the server and its page:
|
|
334
|
+
# step <id> start
|
|
335
|
+
# step <id> <ok|fail|timeout|skip> <seconds>s [why]
|
|
336
|
+
# step check <ok|fail> <seconds>s [qemu-left|mute|state <S>] freed <G>
|
|
337
|
+
# Before each step: its qemu already gone (and the machine not asleep), the rest is skipped.
|
|
338
|
+
# Exit 0 only when no qemu is left for it and multipass says Stopped.
|
|
339
|
+
halt_steps() {
|
|
340
|
+
local v="$1" from="$2" until="$3" kb s on="no" t rc res grace=0 state
|
|
341
|
+
trap '[ -n "$HALT_CHILD" ] && kill "$HALT_CHILD" 2>/dev/null; exit 143' TERM INT
|
|
342
|
+
vm_qemu_note "$v"
|
|
343
|
+
# shellcheck disable=SC2046
|
|
344
|
+
kb="$(vm_rss_kb $(vm_qemu_pids "$v"))"
|
|
345
|
+
for s in polite force term kill; do
|
|
346
|
+
[ "$s" = "$from" ] && on="yes"
|
|
347
|
+
[ "$on" = "yes" ] || continue
|
|
348
|
+
if halt_gone "$v" "$grace"; then echo "step $s skip 0s gone"; break; fi
|
|
349
|
+
grace=0
|
|
350
|
+
# Nothing alive to signal: said why (a qemu ended but left unreaped: the VM manager is stuck).
|
|
351
|
+
if { [ "$s" = "term" ] || [ "$s" = "kill" ]; } && [ -z "$(vm_qemu_pids "$v")" ]; then
|
|
352
|
+
if [ -n "$(vm_qemu_unreaped "$v")" ]; then echo "step $s skip 0s unreaped"; else echo "step $s skip 0s none"; fi
|
|
353
|
+
[ "$s" = "$until" ] && break; continue
|
|
354
|
+
fi
|
|
355
|
+
echo "step $s start"; t=$SECONDS; rc=0
|
|
356
|
+
case "$s" in
|
|
357
|
+
polite) if [ -z "$(vm_qemu_pids "$v")" ] && [ "$(vm_state_of "$v")" = "Suspended" ]; then echo "step polite skip 0s asleep"; [ "$s" = "$until" ] && break; continue; fi
|
|
358
|
+
halt_run timeout "$HALT_POLITE" "$MULTIPASS" stop "$v" || rc=$? ;;
|
|
359
|
+
force) halt_run timeout "$HALT_FORCE" "$MULTIPASS" stop --force "$v" || rc=$? ;;
|
|
360
|
+
term|kill)
|
|
361
|
+
if [ "$s" = "term" ]; then halt_signal "$v" TERM "$HALT_TERM" || rc=124; else halt_signal "$v" KILL "$HALT_KILL" || rc=124; fi ;;
|
|
362
|
+
esac
|
|
363
|
+
case "$rc" in 0) res="ok"; grace=3 ;; 124) res="timeout" ;; *) res="fail" ;; esac
|
|
364
|
+
echo "step $s $res $((SECONDS - t))s"
|
|
365
|
+
[ "$s" = "$until" ] && break
|
|
366
|
+
done
|
|
367
|
+
echo "step check start"; t=$SECONDS
|
|
368
|
+
for _ in $(seq 0 "$HALT_SETTLE"); do
|
|
369
|
+
state="$(vm_state_of "$v")"
|
|
370
|
+
[ -z "$(vm_qemu_pids "$v")" ] && [ -z "$(vm_qemu_unreaped "$v")" ] && [ "$state" = "Stopped" ] && { echo "step check ok $((SECONDS - t))s freed $(gb_of_kb "$kb")"; exit 0; }
|
|
371
|
+
# A VM manager that does not answer is not asked again and again.
|
|
372
|
+
[ "$state" = "unknown" ] && break
|
|
373
|
+
sleep 1
|
|
374
|
+
done
|
|
375
|
+
if [ -n "$(vm_qemu_pids "$v")" ]; then echo "step check fail $((SECONDS - t))s qemu-left freed 0.0G"
|
|
376
|
+
elif [ -n "$(vm_qemu_unreaped "$v")" ]; then echo "step check fail $((SECONDS - t))s qemu-unreaped freed $(gb_of_kb "$kb")"
|
|
377
|
+
elif [ "$state" = "unknown" ]; then echo "step check fail $((SECONDS - t))s mute freed $(gb_of_kb "$kb")"
|
|
378
|
+
else echo "step check fail $((SECONDS - t))s state $state freed $(gb_of_kb "$kb")"; fi
|
|
379
|
+
exit 1
|
|
380
|
+
}
|
|
381
|
+
halt_plan() {
|
|
382
|
+
local v="$1" from="$2" until="$3" s on="no"
|
|
383
|
+
echo "before each step: if no qemu is left for $v (and it is not asleep), straight to the check"
|
|
384
|
+
for s in polite force term kill; do
|
|
385
|
+
[ "$s" = "$from" ] && on="yes"
|
|
386
|
+
[ "$on" = "yes" ] || continue
|
|
387
|
+
case "$s" in
|
|
388
|
+
polite) echo "step polite: timeout $HALT_POLITE $MULTIPASS stop $v" ;;
|
|
389
|
+
force) echo "step force: timeout $HALT_FORCE $MULTIPASS stop --force $v" ;;
|
|
390
|
+
term) echo "step term: kill -TERM <the qemu of $v, proven by its disk>, waited ${HALT_TERM}s" ;;
|
|
391
|
+
kill) echo "step kill: kill -KILL <the qemu of $v, proven by its disk>, waited ${HALT_KILL}s" ;;
|
|
392
|
+
esac
|
|
393
|
+
[ "$s" = "$until" ] && break
|
|
394
|
+
done
|
|
395
|
+
echo "step check: no qemu left for $v, and $MULTIPASS says Stopped (within ${HALT_SETTLE}s)"
|
|
396
|
+
}
|
|
228
397
|
number() { [[ "$1" =~ ^[0-9]{1,6}$ ]] || refuse "not a number: ${1:-<empty>}"; }
|
|
229
398
|
# A disk or partition named by its device: /dev/sdX, /dev/vdX, /dev/nvmeNnN(pN),
|
|
230
399
|
# /dev/xvdX, /dev/mmcblkN(pN), with a digit or two at the end; a loop device only
|
|
@@ -794,9 +963,31 @@ case "$AREA $VERB" in
|
|
|
794
963
|
"vm info")
|
|
795
964
|
[ $# -eq 1 ] || usage; vm_of "$1"
|
|
796
965
|
run "$MULTIPASS" info "$1" --format json ;;
|
|
797
|
-
"vm start"
|
|
966
|
+
"vm start")
|
|
967
|
+
[ $# -eq 1 ] || usage; vm_of "$1"
|
|
968
|
+
run timeout "$START_SECONDS" "$MULTIPASS" start "$1" ;;
|
|
969
|
+
"vm stop")
|
|
970
|
+
[ $# -eq 1 ] || usage; vm_of "$1"
|
|
971
|
+
run timeout "$STOP_SECONDS" "$MULTIPASS" stop "$1" ;;
|
|
972
|
+
"vm halt")
|
|
973
|
+
# <vm> [<from> [<until>]]: STOPPED FOR SURE, step by step (halt_steps): a polite stop, a stop
|
|
974
|
+
# by force, then its own qemu ended -- SIGTERM, SIGKILL -- without the VM manager, which may be
|
|
975
|
+
# the thing that hangs. <from>: where to begin (the server moves on with it, "Force now");
|
|
976
|
+
# <until>: the last step tried before the check (force: the VM manager's own ways only).
|
|
977
|
+
[ $# -ge 1 ] && [ $# -le 3 ] || usage; vm_of "$1"
|
|
978
|
+
from="${2:-polite}"; until="${3:-kill}"
|
|
979
|
+
[[ "$from" =~ ^(polite|force|term|kill)$ ]] || refuse "not a step: $from"
|
|
980
|
+
[[ "$until" =~ ^(force|kill)$ ]] || refuse "not a last step: $until"
|
|
981
|
+
[ "$from" = "kill" ] && [ "$until" = "force" ] && refuse "kill comes after force"
|
|
982
|
+
[ "$from" = "term" ] && [ "$until" = "force" ] && refuse "term comes after force"
|
|
983
|
+
if [ "$SAY" = "yes" ]; then halt_plan "$1" "$from" "$until"; exit 0; fi
|
|
984
|
+
halt_steps "$1" "$from" "$until" ;;
|
|
985
|
+
"vm hardstop")
|
|
986
|
+
# <vm>: "Force stop", as a power cut: nothing is asked of the VM manager. Its own qemu,
|
|
987
|
+
# proven by its disk, is sent SIGTERM, then SIGKILL; then the check, as for vm halt.
|
|
798
988
|
[ $# -eq 1 ] || usage; vm_of "$1"
|
|
799
|
-
|
|
989
|
+
if [ "$SAY" = "yes" ]; then halt_plan "$1" term kill; exit 0; fi
|
|
990
|
+
halt_steps "$1" term kill ;;
|
|
800
991
|
"vm suspend")
|
|
801
992
|
# Veille: suspend must save the machine's state AND free its RAM by ending
|
|
802
993
|
# its qemu process. Multipass saves the state reliably but at times leaves
|
|
@@ -808,17 +999,25 @@ case "$AREA $VERB" in
|
|
|
808
999
|
# gone after the first suspend and none of this runs.
|
|
809
1000
|
[ $# -eq 1 ] || usage; vm_of "$1"
|
|
810
1001
|
if [ "$SAY" = "yes" ]; then
|
|
811
|
-
printf '%s\n' "$MULTIPASS suspend $1" \
|
|
1002
|
+
printf '%s\n' "timeout $SUSPEND_SECONDS $MULTIPASS suspend $1" \
|
|
812
1003
|
"if its qemu still holds the RAM: $MULTIPASS start $1 && $MULTIPASS suspend $1 (wake+resleep)" \
|
|
813
1004
|
"if it still does: $MULTIPASS stop $1 (free it; it wakes by a boot)"
|
|
814
1005
|
exit 0
|
|
815
1006
|
fi
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
"
|
|
821
|
-
|
|
1007
|
+
# FROZEN (its qemu stopped): a suspend would hang the VM manager ("Suspending" for good). Refused
|
|
1008
|
+
# at once: what frees it is Force stop.
|
|
1009
|
+
if vm_qemu_frozen "$1"; then echo "step suspend start"; echo "step suspend fail 0s frozen"; refuse "its qemu is frozen: a suspend would hang the VM manager; use Force stop"; fi
|
|
1010
|
+
# One line per step, as vm halt says them (the server shows them to the page).
|
|
1011
|
+
echo "step suspend start"; t=$SECONDS
|
|
1012
|
+
timeout "$SUSPEND_SECONDS" "$MULTIPASS" suspend "$1" || { echo "step suspend fail $((SECONDS - t))s"; refuse "suspend of $1 failed"; }
|
|
1013
|
+
vm_freed "$1" && { echo "step suspend ok $((SECONDS - t))s"; exit 0; }
|
|
1014
|
+
echo "step suspend ok $((SECONDS - t))s qemu-left"; echo "step resleep start"; t=$SECONDS
|
|
1015
|
+
timeout "$START_SECONDS" "$MULTIPASS" start "$1" >/dev/null 2>&1 && timeout "$SUSPEND_SECONDS" "$MULTIPASS" suspend "$1" >/dev/null 2>&1
|
|
1016
|
+
vm_freed "$1" && { echo "step resleep ok $((SECONDS - t))s"; exit 0; }
|
|
1017
|
+
echo "step resleep fail $((SECONDS - t))s"; echo "step stop start"; t=$SECONDS
|
|
1018
|
+
timeout "$STOP_SECONDS" "$MULTIPASS" stop "$1" >/dev/null 2>&1 || true
|
|
1019
|
+
vm_freed "$1" && { echo "step stop ok $((SECONDS - t))s"; exit 0; }
|
|
1020
|
+
echo "step stop fail $((SECONDS - t))s"
|
|
822
1021
|
refuse "suspended $1 but its memory was not freed (its qemu stayed); the VM manager likely needs a restart" ;;
|
|
823
1022
|
"vm reclaim")
|
|
824
1023
|
# Free a machine that is ASLEEP but whose qemu still holds the RAM -- a
|
|
@@ -834,6 +1033,8 @@ case "$AREA $VERB" in
|
|
|
834
1033
|
fi
|
|
835
1034
|
# Each step that frees the RAM says how, so the server can tell the owner.
|
|
836
1035
|
vm_freed "$1" && { echo "freed-already"; exit 0; }
|
|
1036
|
+
# Frozen: waking it and putting it back to sleep would hang the VM manager.
|
|
1037
|
+
vm_qemu_frozen "$1" && refuse "its qemu is frozen: use Force stop"
|
|
837
1038
|
"$MULTIPASS" start "$1" >/dev/null 2>&1 && "$MULTIPASS" suspend "$1" >/dev/null 2>&1
|
|
838
1039
|
vm_freed "$1" && { echo "freed-resleep"; exit 0; }
|
|
839
1040
|
"$MULTIPASS" stop "$1" >/dev/null 2>&1 || true
|
|
@@ -843,7 +1044,7 @@ case "$AREA $VERB" in
|
|
|
843
1044
|
# A stop that does not wait for the system inside: asked only after a
|
|
844
1045
|
# clean stop hung or failed (a restart the owner asked for).
|
|
845
1046
|
[ $# -eq 1 ] || usage; vm_of "$1"
|
|
846
|
-
run "$MULTIPASS" stop --force "$1" ;;
|
|
1047
|
+
run timeout "$KILL_SECONDS" "$MULTIPASS" stop --force "$1" ;;
|
|
847
1048
|
"vm set")
|
|
848
1049
|
# <vm> memory <megabytes> | <vm> disk <gigabytes> | <vm> cpus <n>. The VM manager
|
|
849
1050
|
# takes it only from a stopped machine, and a disk only ever grows:
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.191-beta.1",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "clavarium",
|
|
9
|
-
"version": "0.2.
|
|
9
|
+
"version": "0.2.191-beta.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/api.mjs
CHANGED
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
* POST /api/conversations/<name>/restart a clean stop (by force if it hangs), then a start
|
|
9
9
|
* POST /api/conversations/<name>/machine {memoryMb?, diskGb?, cpus?}: stopped, set, started again (202);
|
|
10
10
|
* asleep: checked and kept for its wake, as `sizeAtWake` (200)
|
|
11
|
+
* POST /api/conversations/<name>/machine {action: start|stop|restart|sleep|force|force-now}: done on its machine,
|
|
12
|
+
* one at a time (force takes the place of any other; force-now: the next
|
|
13
|
+
* step of the one running) -> its state (202), as below
|
|
14
|
+
* POST /api/conversations/<name>/machine {escalate: true|false}: a stop that hangs goes on to its qemu by itself (default) or asks
|
|
15
|
+
* GET /api/conversations/<name>/machine {action: {kind, since, until, steps: [{id, state, since, s}]} | null, report, limits, escalate}
|
|
11
16
|
* POST /api/conversations/<name>/machine/cancel the size kept for its wake, given up
|
|
12
17
|
* GET /api/conversations/<name>/health what was last seen of its machine
|
|
13
18
|
* POST /api/conversations/<name>/health look now (never wakes a machine)
|
|
@@ -118,6 +123,7 @@
|
|
|
118
123
|
* GET /api/history?of=server|<name>&span=1h|24h|7d the curves of memory, CPU and disk (and the reserve, for the server)
|
|
119
124
|
* GET /api/history/all?span=1h|24h|7d Settings, Usage: the server and every machine (asleep too) on one time axis,
|
|
120
125
|
* in GB and cores: {t, capacity, server, itself, machines: [{name, alloc, asleep, ...}]}
|
|
126
|
+
* POST /api/server/frozen {way: force|alert}: a machine to put to sleep for room that is frozen: forced off (default) or only said
|
|
121
127
|
* GET /api/server/logs?source=&span= what the server's programs said POST /api/server/restart POST /api/server/reboot (the whole server)
|
|
122
128
|
* GET /api/operators who may sign in POST /api/operators {email} -> {email, password}: shown once
|
|
123
129
|
* POST /api/operators/<email>/password -> {email, password} POST /api/operators/<email>/disable|enable|remove
|
|
@@ -472,6 +478,9 @@ export async function handleApi(request, response, conversations, headers, resou
|
|
|
472
478
|
const items = settings.configured ? await signin.recent(conversation).catch((e) => ({ failure: e.message })) : [];
|
|
473
479
|
return send(200, { ...settings, items: Array.isArray(items) ? items : [], failure: items.failure ?? null }), true;
|
|
474
480
|
}
|
|
481
|
+
/* Its machine: the action running, step by step, and the last report (conversations.mjs, machineState). */
|
|
482
|
+
const machineOf = /^\/api\/conversations\/([^/]+)\/machine$/.exec(pathname);
|
|
483
|
+
if (machineOf) return send(200, conversations.machineState(decodeURIComponent(machineOf[1]))), true;
|
|
475
484
|
const paired = /^\/api\/conversations\/([^/]+)\/devices$/.exec(pathname);
|
|
476
485
|
if (paired && sharing) return send(200, sharing.devices.of(decodeURIComponent(paired[1]))), true;
|
|
477
486
|
const sending = /^\/api\/conversations\/([^/]+)\/git\/([a-z0-9-]{1,32})\/auto$/.exec(pathname);
|
|
@@ -582,6 +591,8 @@ export async function handleApi(request, response, conversations, headers, resou
|
|
|
582
591
|
if (pathname === '/api/updates/settings' && updates) return send(200, await updates.settings({ mode: body.mode, channel: body.channel, window: body.window })), true;
|
|
583
592
|
if (pathname === '/api/updates/apply' && updates) return send(202, await updates.apply(body.version)), true;
|
|
584
593
|
if (pathname === '/api/updates/back' && updates) return send(202, await updates.back(body.version)), true;
|
|
594
|
+
/* A machine to put to sleep for room that is frozen: forced off (default) or only said. */
|
|
595
|
+
if (pathname === '/api/server/frozen' && capacity) { capacity.setFrozenWay(body.way); return send(200, await capacity.about()), true; }
|
|
585
596
|
if (pathname === '/api/server/reserve' && capacity) { capacity.setReserve({ diskMb: body.diskMb, diskPercent: body.diskPercent, memoryMb: body.memoryMb, memoryPercent: body.memoryPercent }); return send(200, await capacity.about()), true; }
|
|
586
597
|
if (pathname === '/api/git-hosts/forget' && gitAccess) return send(200, gitAccess.forgetHost(body.host)), true;
|
|
587
598
|
const unkeyed = /^\/api\/git-keys\/([a-z0-9-]{1,32})\/remove$/.exec(pathname);
|
|
@@ -910,6 +921,9 @@ export async function handleApi(request, response, conversations, headers, resou
|
|
|
910
921
|
await health.check(conversation, { asked: true }); return send(200, health.of(conversation)), true;
|
|
911
922
|
}
|
|
912
923
|
if (machine[2] === 'restart') return send(202, await conversations.restart(conversation)), true;
|
|
924
|
+
/* One action on its machine, or the owner's choice for its way down (conversations.mjs, machineAction). */
|
|
925
|
+
if (typeof body?.action === 'string') return send(202, await conversations.machineAction(conversation, body.action)), true;
|
|
926
|
+
if (typeof body?.escalate === 'boolean') return send(200, conversations.setStopEscalate(conversation, body.escalate)), true;
|
|
913
927
|
const resized = await conversations.resize(conversation, { memoryMb: body.memoryMb, diskGb: body.diskGb, cpus: body.cpus });
|
|
914
928
|
/* Asleep, nothing was started: the choice is only kept, for its wake. */
|
|
915
929
|
return send(resized.phase === 'restarting' ? 202 : 200, resized), true;
|
package/server/capacity.mjs
CHANGED
|
@@ -99,7 +99,7 @@ const percent = (part, all) => (all > 0 ? Math.round((part / all) * 100) : 0);
|
|
|
99
99
|
export class Capacity {
|
|
100
100
|
/* What the machines take on their disk, as last measured (MB), or undefined. */
|
|
101
101
|
machinesUsedMb() { const u = this.#usage; if (!u) return undefined; return Object.values(u.machines ?? {}).reduce((a, b) => a + b, 0) + (u.shelf ?? 0) + (u.images ?? 0); }
|
|
102
|
-
#run; #store; #conversations; #shelf; #now; #roomMs; #sleep; #alert; #say; #reclaim; #reclaimEveryMs; #sweeping = false; #short = { disk: false, memory: false }; #room = null; #roomAt = -Infinity; #usage = null; #usageAt = -Infinity; #going = null; #measuring = null;
|
|
102
|
+
#run; #store; #conversations; #shelf; #now; #roomMs; #sleep; #force; #alert; #say; #reclaim; #reclaimEveryMs; #sweeping = false; #short = { disk: false, memory: false }; #room = null; #roomAt = -Infinity; #usage = null; #usageAt = -Infinity; #going = null; #measuring = null;
|
|
103
103
|
|
|
104
104
|
/* `run(argv)`: the host program. `store`: where the reserve is kept.
|
|
105
105
|
`conversations`: what each machine was given. `shelf`: how large a
|
|
@@ -107,8 +107,8 @@ export class Capacity {
|
|
|
107
107
|
/* `roomMs`: how long what is free is taken as known (ROOM_MS). */
|
|
108
108
|
/* `sleep(name)`, `alert(payload)`, `say(name, text)`: what watch() does
|
|
109
109
|
when what is free falls under the reserve. */
|
|
110
|
-
constructor({ run, store, conversations, shelf = null, now = () => Date.now(), roomMs = ROOM_MS, sleep = null, alert = null, say = null, reclaim = null, reclaimEveryMs = RECLAIM_EVERY_MS }) {
|
|
111
|
-
this.#roomMs = roomMs; this.#sleep = sleep; this.#alert = alert; this.#say = say; this.#reclaim = reclaim; this.#reclaimEveryMs = reclaimEveryMs;
|
|
110
|
+
constructor({ run, store, conversations, shelf = null, now = () => Date.now(), roomMs = ROOM_MS, sleep = null, force = null, alert = null, say = null, reclaim = null, reclaimEveryMs = RECLAIM_EVERY_MS }) {
|
|
111
|
+
this.#roomMs = roomMs; this.#sleep = sleep; this.#force = force; this.#alert = alert; this.#say = say; this.#reclaim = reclaim; this.#reclaimEveryMs = reclaimEveryMs;
|
|
112
112
|
this.#run = run; this.#store = store; this.#conversations = conversations; this.#shelf = shelf; this.#now = now;
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -135,6 +135,16 @@ export class Capacity {
|
|
|
135
135
|
return this.reserve();
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
/* A MACHINE TO PUT TO SLEEP THAT IS FROZEN (its qemu stopped: a suspend would hang the VM manager,
|
|
139
|
+
and the host program refuses it): forced off ('force', the default), or only said ('alert').
|
|
140
|
+
The owner's choice, kept here. */
|
|
141
|
+
frozenWay() { return this.#store.get('frozen')?.way === 'alert' ? 'alert' : 'force'; }
|
|
142
|
+
setFrozenWay(way) {
|
|
143
|
+
if (way !== 'force' && way !== 'alert') throw new Refusal(400, 'A frozen machine is either forced off (force) or only said (alert).');
|
|
144
|
+
this.#store.put({ name: 'frozen', way });
|
|
145
|
+
return this.frozenWay();
|
|
146
|
+
}
|
|
147
|
+
|
|
138
148
|
/* What is free now. `fresh`: asked again whatever the cache says. */
|
|
139
149
|
async room({ fresh = false } = {}) {
|
|
140
150
|
if (!fresh && this.#room && this.#now() - this.#roomAt < this.#roomMs) return this.#room;
|
|
@@ -215,7 +225,7 @@ export class Capacity {
|
|
|
215
225
|
machines: { disksMb: Object.values(u.machines).reduce((a, b) => a + b, 0), each: Object.entries(u.machines).map(([name, mb]) => ({ name, mb })).sort((a, b) => b.mb - a.mb), shelfMb: u.shelf ?? null, imagesMb: u.images ?? null, othersMb: u.others ?? null },
|
|
216
226
|
at: new Date(this.#usageAt).toISOString(),
|
|
217
227
|
} : null;
|
|
218
|
-
return { known: Boolean(figures), reserve: { diskMb: this.reserve().diskMb, memoryMb: this.reserve().memoryMb }, settings: this.reserve(), ...(figures ?? {}), usage, measuring: Boolean(this.#measuring) };
|
|
228
|
+
return { known: Boolean(figures), reserve: { diskMb: this.reserve().diskMb, memoryMb: this.reserve().memoryMb }, settings: this.reserve(), frozen: this.frozenWay(), ...(figures ?? {}), usage, measuring: Boolean(this.#measuring) };
|
|
219
229
|
}
|
|
220
230
|
|
|
221
231
|
/* For the list: only whether it is high, and how much. Cheap: what is
|
|
@@ -285,12 +295,17 @@ export class Capacity {
|
|
|
285
295
|
const running = f.machines.filter((m) => m.name && awake.has(m.name));
|
|
286
296
|
const takes = (m) => (what === 'disk' ? this.#usage?.machines?.[m.name] ?? -1 : m.rssMb);
|
|
287
297
|
const most = running.sort((a, b) => takes(b) - takes(a) || b.rssMb - a.rssMb)[0] ?? null;
|
|
288
|
-
let slept = null;
|
|
289
|
-
if (most && this.#sleep) slept = await Promise.resolve(this.#sleep(most.name)).then(() => most.name, () => null);
|
|
298
|
+
let slept = null; let frozen = null; let forced = null;
|
|
299
|
+
if (most && this.#sleep) slept = await Promise.resolve(this.#sleep(most.name)).then(() => most.name, (error) => { if (/frozen/.test(String(error?.message ?? ''))) frozen = most.name; return null; });
|
|
300
|
+
/* Frozen: never suspended (it would hang the VM manager); forced off, as its owner chose by default. */
|
|
301
|
+
if (frozen && this.frozenWay() === 'force' && this.#force) forced = await Promise.resolve(this.#force(frozen)).then((ok) => (ok === false ? null : frozen), () => null);
|
|
290
302
|
const figures = `${gb(free)} free, ${gb(f.reserve[`${what}Mb`])} kept in reserve`;
|
|
291
|
-
|
|
303
|
+
const did = slept ? ` ${slept} was put to sleep.` : forced ? ` ${forced} was frozen: it was forced off.` : frozen ? ` ${frozen} takes the most but is frozen: Force stop frees it.` : '';
|
|
304
|
+
this.#alert?.({ title: `This server is short of ${what}`, body: `${figures}.${did}`, url: '/?open=settings&at=resources' });
|
|
292
305
|
if (slept) this.#say?.(slept, `This server ran short of ${what} (${figures}). This machine took the most, so it was put to sleep. Wake it once there is room again.`);
|
|
293
|
-
|
|
306
|
+
if (forced) this.#say?.(forced, `This server ran short of ${what} (${figures}). This machine took the most and was frozen (it could not be put to sleep), so it was forced off, as after a power cut. Start it again once there is room.`);
|
|
307
|
+
else if (frozen) this.#say?.(frozen, `This server ran short of ${what} (${figures}). This machine took the most but is frozen: it could not be put to sleep. Force stop frees it.`);
|
|
308
|
+
done.push({ what, slept, ...(frozen ? { frozen, forced } : {}) });
|
|
294
309
|
}
|
|
295
310
|
return done;
|
|
296
311
|
}
|