@inkeep/impulse-compute 0.0.0-stage → 0.1.0
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/README.md +137 -2
- package/dist/impulse-compute.mjs +1092 -0
- package/dist/internal/launch.js +34 -0
- package/package.json +33 -3
- package/team-mac/host-claim.sh +23 -0
- package/team-mac/host-hold-check.sh +23 -0
- package/team-mac/host-hold-read.sh +17 -0
- package/team-mac/host-hold.sh +88 -0
- package/team-mac/host-prepare.sh +158 -0
- package/team-mac/vm-list.sh +38 -0
- package/team-mac/vm-start.sh +257 -0
- package/team-mac/vm-stop.sh +137 -0
- package/team-mac/vz-start-probe.swift +60 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
set -u
|
|
3
|
+
if ! declare -F team_mac_host_lock >/dev/null; then
|
|
4
|
+
. "$(dirname "$0")/host-claim.sh" || exit 2
|
|
5
|
+
fi
|
|
6
|
+
export LUME_TELEMETRY_ENABLED=false LC_ALL=C
|
|
7
|
+
vm=$1
|
|
8
|
+
base=$2
|
|
9
|
+
bound=$3
|
|
10
|
+
pubkey=$4
|
|
11
|
+
expires=${5:-}
|
|
12
|
+
lume=${OT_LUME:-$HOME/.local/bin/lume}
|
|
13
|
+
probe=${OT_VZ_PROBE:-$HOME/.ot-team-mac/bin/vz-start-probe}
|
|
14
|
+
poll=${OT_POLL_SECONDS:-3}
|
|
15
|
+
dir=$HOME/.ot-team-mac/runs/$vm
|
|
16
|
+
started=$(date -u +%s)
|
|
17
|
+
deadline=$((started + bound))
|
|
18
|
+
|
|
19
|
+
emit() {
|
|
20
|
+
perl -MJSON::PP -e '
|
|
21
|
+
my %r = @ARGV;
|
|
22
|
+
for my $k (qw(runnerExit frameworkCode bootSeconds boundSeconds)) { $r{$k} += 0 if defined $r{$k} && $r{$k} =~ /^-?\d+$/ }
|
|
23
|
+
for my $k (qw(absent presentBeforeDelete frameworkStarted)) { $r{$k} = $r{$k} eq "true" ? JSON::PP::true : JSON::PP::false if defined $r{$k} }
|
|
24
|
+
print JSON::PP->new->canonical->encode(\%r), "\n";
|
|
25
|
+
' -- "$@"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
redacted_tail() {
|
|
29
|
+
[ -f "$1" ] || return 0
|
|
30
|
+
tail -n 15 "$1" | perl -pe 's{vnc://[^@\s]*@}{vnc://<redacted>@}g'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
details() {
|
|
34
|
+
"$lume" get "$vm" --format json 2>/dev/null | perl -MJSON::PP -e '
|
|
35
|
+
local $/; my $doc = eval { decode_json(<STDIN>) } or exit 3;
|
|
36
|
+
my @vms = ref $doc eq "ARRAY" ? @$doc : ($doc);
|
|
37
|
+
for my $v (@vms) {
|
|
38
|
+
next unless ($v->{name} // "") eq $ARGV[0];
|
|
39
|
+
printf "%s\t%s\t%s\n", $v->{status} // "", $v->{ipAddress} // "", $v->{macAddress} // "";
|
|
40
|
+
exit 0;
|
|
41
|
+
}
|
|
42
|
+
exit 4;
|
|
43
|
+
' "$vm"
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
present() {
|
|
47
|
+
[ -e "$HOME/.lume/$vm" ] || "$lume" get "$vm" --format json >/dev/null 2>&1
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
remove_clone() {
|
|
51
|
+
if present; then before=true; else before=false; fi
|
|
52
|
+
"$lume" delete "$vm" --force >/dev/null 2>&1
|
|
53
|
+
if present; then absent=false; else absent=true; fi
|
|
54
|
+
if [ "$absent" = true ]; then
|
|
55
|
+
rm -rf "$dir"
|
|
56
|
+
rm -f "$lease"
|
|
57
|
+
fi
|
|
58
|
+
tmp=$(getconf DARWIN_USER_TEMP_DIR 2>/dev/null) && [ -n "$tmp" ] && rm -rf "${tmp}lume-config-$vm"
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
wait_runner_exit() {
|
|
62
|
+
local until=$(($(date -u +%s) + $1))
|
|
63
|
+
while [ ! -f "$dir/exit" ] && [ "$(date -u +%s)" -lt "$until" ]; do sleep "${OT_POLL_SECONDS:-1}"; done
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
cancel_start() {
|
|
67
|
+
"$lume" stop --force "$vm" >/dev/null 2>&1
|
|
68
|
+
[ ! -f "$dir/pid" ] || wait_runner_exit 60
|
|
69
|
+
remove_clone
|
|
70
|
+
emit outcome cancelled vm "$vm" absent "$absent" reason "this start was cancelled by its run's stop"
|
|
71
|
+
exit 0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
owns_vm() {
|
|
75
|
+
[ -f "$marker" ] && [ "$(<"$marker")" = "$pubkey" ]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
reap_vm() {
|
|
79
|
+
vm=$1 expires=$2 pubkey=$3 lume=$4 control=$5 dir=$6 poll=$7 owner_hash=$8
|
|
80
|
+
cancel=$control/cancel-$owner_hash
|
|
81
|
+
lease=$control/deadline-$owner_hash
|
|
82
|
+
marker=$HOME/.lume/$vm/.ot-team-mac-owner
|
|
83
|
+
[ ! -f "$cancel" ] || return 0
|
|
84
|
+
printf '%s\n%s\n' "$expires" "$$" > "$lease.tmp" && mv "$lease.tmp" "$lease" || return 1
|
|
85
|
+
while [ -f "$lease" ] && [ "$(date -u +%s)" -lt "$expires" ]; do
|
|
86
|
+
if ! owns_vm; then
|
|
87
|
+
rm -f "$lease"
|
|
88
|
+
if [ -f "$cancel" ]; then outcome=deadline-cancelled; else outcome=deadline-owner-changed; fi
|
|
89
|
+
emit outcome "$outcome" vm "$vm"
|
|
90
|
+
return 0
|
|
91
|
+
fi
|
|
92
|
+
sleep "$poll"
|
|
93
|
+
done
|
|
94
|
+
if [ ! -f "$lease" ]; then
|
|
95
|
+
emit outcome deadline-cancelled vm "$vm"
|
|
96
|
+
return 0
|
|
97
|
+
fi
|
|
98
|
+
: > "$cancel" || return 1
|
|
99
|
+
exec 9>"$control/lock" || return 1
|
|
100
|
+
perl -e 'flock STDIN, 2 or die "deadline claim: $!\n"' <&9 || return 1
|
|
101
|
+
if [ ! -f "$lease" ]; then
|
|
102
|
+
emit outcome deadline-cancelled vm "$vm"
|
|
103
|
+
return 0
|
|
104
|
+
fi
|
|
105
|
+
if ! owns_vm; then
|
|
106
|
+
rm -f "$lease"
|
|
107
|
+
emit outcome deadline-owner-changed vm "$vm"
|
|
108
|
+
return 0
|
|
109
|
+
fi
|
|
110
|
+
"$lume" stop --force "$vm" >/dev/null 2>&1
|
|
111
|
+
wait_runner_exit 60
|
|
112
|
+
remove_clone
|
|
113
|
+
rm -f "$lease"
|
|
114
|
+
emit outcome deadline-expired vm "$vm" absent "$absent"
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
detach() {
|
|
118
|
+
perl -e '
|
|
119
|
+
use POSIX qw(setsid);
|
|
120
|
+
$SIG{HUP} = "IGNORE";
|
|
121
|
+
my $log = shift @ARGV;
|
|
122
|
+
defined(my $pid = fork()) or die "fork: $!\n";
|
|
123
|
+
if ($pid) { print "$pid\n"; exit 0 }
|
|
124
|
+
setsid() or die "setsid: $!\n";
|
|
125
|
+
open(STDIN, "<", "/dev/null") or die "stdin: $!\n";
|
|
126
|
+
open(STDOUT, ">>", $log) or die "log: $!\n";
|
|
127
|
+
open(STDERR, ">&", \*STDOUT) or die "stderr: $!\n";
|
|
128
|
+
exec @ARGV or die "exec: $!\n";
|
|
129
|
+
' "$@" 8>&- 9>&-
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
case "$vm" in
|
|
133
|
+
"" | *[!a-z0-9-]* | -*) emit outcome refused-input reason "the VM name must be lowercase letters, digits and hyphens"; exit 2 ;;
|
|
134
|
+
esac
|
|
135
|
+
case "$pubkey" in
|
|
136
|
+
"ssh-ed25519 "*) ;;
|
|
137
|
+
*) emit outcome refused-input reason "the public key must be one ssh-ed25519 line"; exit 2 ;;
|
|
138
|
+
esac
|
|
139
|
+
case "$expires" in
|
|
140
|
+
"" | *[!0-9]*) emit outcome refused-input reason "the VM deadline must be UTC epoch seconds"; exit 2 ;;
|
|
141
|
+
esac
|
|
142
|
+
if [ "$expires" -le "$started" ]; then
|
|
143
|
+
emit outcome refused-input reason "the VM deadline has already passed"
|
|
144
|
+
exit 2
|
|
145
|
+
fi
|
|
146
|
+
team_mac_host_lock || { emit outcome refused-input reason "cannot acquire the host claim"; exit 2; }
|
|
147
|
+
hold_reason=$(team_mac_hold_check "$pubkey" 2>&1)
|
|
148
|
+
hold_status=$?
|
|
149
|
+
if [ "$hold_status" -ne 0 ]; then
|
|
150
|
+
if [ "$hold_status" -eq 3 ]; then
|
|
151
|
+
emit outcome host-held reason "$hold_reason"
|
|
152
|
+
else
|
|
153
|
+
emit outcome refused-input reason "$hold_reason"
|
|
154
|
+
fi
|
|
155
|
+
exit 2
|
|
156
|
+
fi
|
|
157
|
+
case "$pubkey" in
|
|
158
|
+
*"'"* | *"\\"*) emit outcome refused-input reason "the public key line must not contain quotes or backslashes"; exit 2 ;;
|
|
159
|
+
esac
|
|
160
|
+
control=$HOME/.ot-team-mac/starts/$vm
|
|
161
|
+
mkdir -p "$control" || { emit outcome refused-input reason "cannot create the start claim"; exit 2; }
|
|
162
|
+
exec 9>"$control/lock" || { emit outcome refused-input reason "cannot open the start claim"; exit 2; }
|
|
163
|
+
perl -e 'flock STDIN, 2 or die "start claim: $!\n"' <&9 || { emit outcome refused-input reason "cannot acquire the start claim"; exit 2; }
|
|
164
|
+
owner_hash=$(printf '%s' "$pubkey" | shasum -a 256 | cut -d' ' -f1)
|
|
165
|
+
cancel=$control/cancel-$owner_hash
|
|
166
|
+
if [ -f "$cancel" ]; then
|
|
167
|
+
emit outcome refused-input reason "this start was cancelled by its run's stop"
|
|
168
|
+
exit 2
|
|
169
|
+
fi
|
|
170
|
+
perl -MJSON::PP -e 'print encode_json({ ownerHash => $ARGV[0], pid => 0 + $ARGV[1], startedAtEpochSeconds => 0 + $ARGV[2] }), "\n"' \
|
|
171
|
+
"$owner_hash" "$$" "$started" > "$control/active.tmp" && mv "$control/active.tmp" "$control/active" || { emit outcome refused-input reason "cannot publish the start claim"; exit 2; }
|
|
172
|
+
lease=$control/deadline-$owner_hash
|
|
173
|
+
trap 'rm -f "$control/active"' EXIT
|
|
174
|
+
if present; then
|
|
175
|
+
emit outcome name-taken vm "$vm" reason "a VM with this name already exists; it is not this run's to touch"
|
|
176
|
+
exit 3
|
|
177
|
+
fi
|
|
178
|
+
intent=$control/clone-$owner_hash
|
|
179
|
+
: > "$intent" || { emit outcome refused-input reason "cannot record the clone intent"; exit 2; }
|
|
180
|
+
if ! clone_out=$("$lume" clone "$base" "$vm" 2>&1); then
|
|
181
|
+
if present; then absent=false; else absent=true; fi
|
|
182
|
+
emit outcome clone-failed vm "$vm" base "$base" reason "$(printf '%s' "$clone_out" | tail -n 3)" absent "$absent"
|
|
183
|
+
exit 4
|
|
184
|
+
fi
|
|
185
|
+
if [ -f "$cancel" ]; then
|
|
186
|
+
cancel_start
|
|
187
|
+
fi
|
|
188
|
+
if ! printf '%s\n' "$pubkey" > "$HOME/.lume/$vm/.ot-team-mac-owner"; then
|
|
189
|
+
remove_clone
|
|
190
|
+
emit outcome clone-failed vm "$vm" base "$base" reason "cannot record the clone owner; the VM was not started" absent "$absent"
|
|
191
|
+
exit 4
|
|
192
|
+
fi
|
|
193
|
+
rm -f "$intent"
|
|
194
|
+
mkdir -p "$dir"
|
|
195
|
+
detach "$control/deadline-$owner_hash.log" /bin/bash -c "$(declare -f emit present remove_clone wait_runner_exit owns_vm reap_vm); reap_vm \"\$@\"" bash \
|
|
196
|
+
"$vm" "$expires" "$pubkey" "$lume" "$control" "$dir" "$poll" "$owner_hash" >/dev/null
|
|
197
|
+
while [ ! -f "$lease" ]; do
|
|
198
|
+
[ ! -f "$cancel" ] || cancel_start
|
|
199
|
+
if [ "$(date -u +%s)" -ge "$deadline" ]; then
|
|
200
|
+
remove_clone
|
|
201
|
+
emit outcome clone-failed vm "$vm" base "$base" reason "the VM deadline reaper did not arm" absent "$absent"
|
|
202
|
+
exit 4
|
|
203
|
+
fi
|
|
204
|
+
sleep "$poll"
|
|
205
|
+
done
|
|
206
|
+
detach "$dir/runner.log" /bin/sh -c 'NSUnbufferedIO=YES "$1" run "$2" --no-display; code=$?; printf "%s\n" "$code" > "$3/exit.tmp" && mv "$3/exit.tmp" "$3/exit"' sh "$lume" "$vm" "$dir" > "$dir/pid"
|
|
207
|
+
|
|
208
|
+
while :; do
|
|
209
|
+
if [ -f "$cancel" ]; then
|
|
210
|
+
cancel_start
|
|
211
|
+
fi
|
|
212
|
+
if [ -f "$dir/exit" ]; then
|
|
213
|
+
code=$(cat "$dir/exit")
|
|
214
|
+
framework_domain="" framework_code="" framework_started=false
|
|
215
|
+
if [ -x "$probe" ]; then
|
|
216
|
+
probe_out=$("$probe" "$HOME/.lume/$vm" 2>&1)
|
|
217
|
+
framework_domain=$(printf '%s\n' "$probe_out" | perl -ne 'if (/start failed .* domain=(\S+) code=(-?\d+)/) { print $1; exit }')
|
|
218
|
+
framework_code=$(printf '%s\n' "$probe_out" | perl -ne 'if (/start failed .* domain=(\S+) code=(-?\d+)/) { print $2; exit }')
|
|
219
|
+
case "$probe_out" in *"PROBE: started"*) framework_started=true ;; esac
|
|
220
|
+
fi
|
|
221
|
+
log=$(redacted_tail "$dir/runner.log")
|
|
222
|
+
remove_clone
|
|
223
|
+
emit outcome runner-exited vm "$vm" runnerExit "$code" frameworkDomain "$framework_domain" \
|
|
224
|
+
frameworkCode "$framework_code" frameworkStarted "$framework_started" probe "$([ -x "$probe" ] && echo present || echo absent)" \
|
|
225
|
+
presentBeforeDelete "$before" absent "$absent" log "$log"
|
|
226
|
+
exit 0
|
|
227
|
+
fi
|
|
228
|
+
row=$(details) || row=""
|
|
229
|
+
ip=$(printf '%s' "$row" | cut -f2)
|
|
230
|
+
mac=$(printf '%s' "$row" | cut -f3)
|
|
231
|
+
if [ -n "$ip" ] && [ -n "$mac" ]; then
|
|
232
|
+
guest=$("$lume" ssh --timeout 30 "$vm" "ifconfig -a; echo OT-VERSION \$(sw_vers -productVersion); echo OT-HOSTKEY \$(cat /etc/ssh/ssh_host_ed25519_key.pub)" 2>/dev/null) || guest=""
|
|
233
|
+
if printf '%s\n' "$guest" | MAC="$mac" perl -e '
|
|
234
|
+
my $want = join ":", map { sprintf "%02x", hex } split /:/, lc $ENV{MAC};
|
|
235
|
+
while (<STDIN>) { if (/\bether\s+([0-9a-fA-F:]+)/) { my $got = join ":", map { sprintf "%02x", hex } split /:/, lc $1; exit 0 if $got eq $want } }
|
|
236
|
+
exit 1;
|
|
237
|
+
'; then
|
|
238
|
+
version=$(printf '%s\n' "$guest" | sed -n 's/^OT-VERSION //p' | head -n 1)
|
|
239
|
+
hostkey=$(printf '%s\n' "$guest" | sed -n 's/^OT-HOSTKEY //p' | head -n 1)
|
|
240
|
+
if "$lume" ssh --timeout 30 "$vm" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && printf '%s\n' '$pubkey' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" >/dev/null 2>&1; then
|
|
241
|
+
[ ! -f "$cancel" ] || cancel_start
|
|
242
|
+
emit outcome booted vm "$vm" ip "$ip" mac "$mac" guestMacos "$version" guestHostKey "$hostkey" \
|
|
243
|
+
bootSeconds "$(($(date -u +%s) - started))" boundSeconds "$bound"
|
|
244
|
+
exit 0
|
|
245
|
+
fi
|
|
246
|
+
fi
|
|
247
|
+
fi
|
|
248
|
+
if [ "$(date -u +%s)" -ge "$deadline" ]; then
|
|
249
|
+
"$lume" stop --force "$vm" >/dev/null 2>&1
|
|
250
|
+
wait_runner_exit 60
|
|
251
|
+
log=$(redacted_tail "$dir/runner.log")
|
|
252
|
+
remove_clone
|
|
253
|
+
emit outcome not-established vm "$vm" boundSeconds "$bound" presentBeforeDelete "$before" absent "$absent" log "$log"
|
|
254
|
+
exit 0
|
|
255
|
+
fi
|
|
256
|
+
sleep "$poll"
|
|
257
|
+
done
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
set -u
|
|
3
|
+
export LUME_TELEMETRY_ENABLED=false LC_ALL=C
|
|
4
|
+
vm=$1
|
|
5
|
+
grace=${2:-120}
|
|
6
|
+
owner=${3:-}
|
|
7
|
+
legacy_mac=${4:-}
|
|
8
|
+
lume=${OT_LUME:-$HOME/.local/bin/lume}
|
|
9
|
+
poll=${OT_POLL_SECONDS:-1}
|
|
10
|
+
dir=$HOME/.ot-team-mac/runs/$vm
|
|
11
|
+
|
|
12
|
+
emit() {
|
|
13
|
+
perl -MJSON::PP -e '
|
|
14
|
+
my %r = @ARGV;
|
|
15
|
+
for my $k (qw(absent presentBefore runnerExited forced)) { $r{$k} = $r{$k} eq "true" ? JSON::PP::true : JSON::PP::false if defined $r{$k} }
|
|
16
|
+
print JSON::PP->new->canonical->encode(\%r), "\n";
|
|
17
|
+
' -- "$@"
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
present() {
|
|
21
|
+
[ -e "$HOME/.lume/$vm" ] || "$lume" get "$vm" --format json >/dev/null 2>&1
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
runner_alive() {
|
|
25
|
+
[ -f "$dir/pid" ] && [ ! -f "$dir/exit" ] && kill -0 "$(cat "$dir/pid")" 2>/dev/null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
wait_runner() {
|
|
29
|
+
local until=$(($(date -u +%s) + $1))
|
|
30
|
+
while runner_alive && [ "$(date -u +%s)" -lt "$until" ]; do sleep "$poll"; done
|
|
31
|
+
! runner_alive
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
case "$vm" in
|
|
35
|
+
"" | *[!a-z0-9-]* | -*) emit outcome refused-input reason "the VM name must be lowercase letters, digits and hyphens"; exit 2 ;;
|
|
36
|
+
esac
|
|
37
|
+
if [ -n "$owner" ]; then
|
|
38
|
+
case "$owner" in
|
|
39
|
+
"ssh-ed25519 "*) ;;
|
|
40
|
+
*) emit outcome refused-owner vm "$vm" absent false; exit 3 ;;
|
|
41
|
+
esac
|
|
42
|
+
fi
|
|
43
|
+
control=$HOME/.ot-team-mac/starts/$vm
|
|
44
|
+
intent=""
|
|
45
|
+
mkdir -p "$control" || { emit outcome claim-failed vm "$vm" absent false reason "cannot create the start claim"; exit 4; }
|
|
46
|
+
if [ -n "$owner" ]; then
|
|
47
|
+
owner_hash=$(printf '%s' "$owner" | shasum -a 256 | cut -d' ' -f1)
|
|
48
|
+
intent=$control/clone-$owner_hash
|
|
49
|
+
: > "$control/cancel-$owner_hash" || { emit outcome claim-failed vm "$vm" absent false reason "cannot request start cancellation"; exit 4; }
|
|
50
|
+
fi
|
|
51
|
+
exec 9>"$control/lock" || { emit outcome claim-failed vm "$vm" absent false reason "cannot open the start claim"; exit 4; }
|
|
52
|
+
stop_deadline=$(($(date -u +%s) + grace))
|
|
53
|
+
while :; do
|
|
54
|
+
perl -e 'exit(flock(STDIN, 6) ? 0 : $!{EWOULDBLOCK} ? 1 : 2)' <&9
|
|
55
|
+
locked=$?
|
|
56
|
+
[ "$locked" -eq 0 ] && break
|
|
57
|
+
[ "$locked" -eq 1 ] || { emit outcome claim-failed vm "$vm" absent false reason "cannot read the start claim"; exit 4; }
|
|
58
|
+
if [ "$(date -u +%s)" -ge "$stop_deadline" ]; then
|
|
59
|
+
requested=""
|
|
60
|
+
[ -z "$owner" ] || requested="cancellation requested; "
|
|
61
|
+
emit outcome start-in-progress vm "$vm" absent false reason "another start or stop of this VM is active; ${requested}retry stop after it exits"
|
|
62
|
+
exit 4
|
|
63
|
+
fi
|
|
64
|
+
sleep "$poll"
|
|
65
|
+
done
|
|
66
|
+
rm -f "$control/active"
|
|
67
|
+
if ! present; then
|
|
68
|
+
[ -z "$owner" ] || rm -f "$control/deadline-$owner_hash"
|
|
69
|
+
rm -rf "$dir"
|
|
70
|
+
[ -z "$intent" ] || rm -f "$intent"
|
|
71
|
+
emit outcome absent-already vm "$vm" presentBefore false absent true
|
|
72
|
+
exit 0
|
|
73
|
+
fi
|
|
74
|
+
marker=$HOME/.lume/$vm/.ot-team-mac-owner
|
|
75
|
+
if [ -n "$owner" ]; then
|
|
76
|
+
if [ -f "$marker" ]; then
|
|
77
|
+
[ "$(cat "$marker")" = "$owner" ] || { emit outcome refused-owner vm "$vm" absent false reason "the VM ownership marker belongs to another attempt"; exit 3; }
|
|
78
|
+
elif [ ! -f "$intent" ]; then
|
|
79
|
+
emit outcome refused-owner vm "$vm" absent false reason "no VM ownership marker or matching clone intent was found"
|
|
80
|
+
exit 3
|
|
81
|
+
fi
|
|
82
|
+
else
|
|
83
|
+
if [ -e "$marker" ] || [ -L "$marker" ]; then
|
|
84
|
+
emit outcome refused-owner vm "$vm" absent false reason "the legacy VM carries an ownership marker"
|
|
85
|
+
exit 3
|
|
86
|
+
fi
|
|
87
|
+
if ! perl -MJSON::PP -e '
|
|
88
|
+
sub normalized {
|
|
89
|
+
my ($value) = @_;
|
|
90
|
+
return unless defined($value) && !ref($value);
|
|
91
|
+
my @parts = split /:/, $value, -1;
|
|
92
|
+
return unless @parts == 6 && !grep { !/\A[0-9a-fA-F]{1,2}\z/ } @parts;
|
|
93
|
+
return join ":", map { sprintf "%02x", hex } @parts;
|
|
94
|
+
}
|
|
95
|
+
open my $file, "<", $ARGV[0] or exit 2;
|
|
96
|
+
local $/; my $config = eval { decode_json(<$file>) };
|
|
97
|
+
exit 2 unless ref($config) eq "HASH";
|
|
98
|
+
my $actual = normalized($config->{macAddress});
|
|
99
|
+
my $expected = normalized($ARGV[1]);
|
|
100
|
+
exit 2 unless defined($actual) && defined($expected);
|
|
101
|
+
exit($actual eq $expected ? 0 : 1);
|
|
102
|
+
' "$HOME/.lume/$vm/config.json" "$legacy_mac"; then
|
|
103
|
+
emit outcome refused-owner vm "$vm" absent false reason "the legacy VM MAC could not be matched to the run record"
|
|
104
|
+
exit 3
|
|
105
|
+
fi
|
|
106
|
+
fi
|
|
107
|
+
if [ -n "$owner" ] && [ ! -e "$marker" ] && [ -f "$intent" ] \
|
|
108
|
+
&& { [ ! -f "$HOME/.lume/$vm/config.json" ] || [ ! -f "$HOME/.lume/$vm/disk.img" ] || [ ! -f "$HOME/.lume/$vm/nvram.bin" ]; }; then
|
|
109
|
+
rm -rf "$HOME/.lume/$vm"
|
|
110
|
+
if present; then
|
|
111
|
+
emit outcome partial-clone-delete-unconfirmed vm "$vm" absent false reason "the attempt's uninitialized clone could not be removed"
|
|
112
|
+
exit 4
|
|
113
|
+
fi
|
|
114
|
+
rm -rf "$dir"
|
|
115
|
+
rm -f "$intent"
|
|
116
|
+
rm -f "$control/deadline-$owner_hash"
|
|
117
|
+
emit outcome partial-clone-deleted vm "$vm" absent true reason "removed the attempt's uninitialized clone"
|
|
118
|
+
exit 0
|
|
119
|
+
fi
|
|
120
|
+
forced=false
|
|
121
|
+
"$lume" stop "$vm" >/dev/null 2>&1
|
|
122
|
+
if ! wait_runner "$((stop_deadline - $(date -u +%s)))"; then
|
|
123
|
+
forced=true
|
|
124
|
+
"$lume" stop --force "$vm" >/dev/null 2>&1
|
|
125
|
+
wait_runner 60
|
|
126
|
+
fi
|
|
127
|
+
if runner_alive; then exited=false; else exited=true; fi
|
|
128
|
+
"$lume" delete "$vm" --force >/dev/null 2>&1
|
|
129
|
+
if present; then absent=false; else absent=true; fi
|
|
130
|
+
if [ "$absent" = true ]; then
|
|
131
|
+
[ -z "$owner" ] || rm -f "$control/deadline-$owner_hash"
|
|
132
|
+
rm -rf "$dir"
|
|
133
|
+
[ -z "$intent" ] || rm -f "$intent"
|
|
134
|
+
tmp=$(getconf DARWIN_USER_TEMP_DIR 2>/dev/null) && [ -n "$tmp" ] && rm -rf "${tmp}lume-config-$vm"
|
|
135
|
+
fi
|
|
136
|
+
emit outcome "$([ "$absent" = true ] && echo deleted || echo delete-unconfirmed)" vm "$vm" presentBefore true \
|
|
137
|
+
runnerExited "$exited" forced "$forced" absent "$absent"
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Virtualization
|
|
3
|
+
|
|
4
|
+
let dir = URL(fileURLWithPath: CommandLine.arguments[1])
|
|
5
|
+
guard let raw = try? Data(contentsOf: dir.appendingPathComponent("config.json")),
|
|
6
|
+
let cfg = try? JSONSerialization.jsonObject(with: raw) as? [String: Any],
|
|
7
|
+
let hmText = cfg["hardwareModel"] as? String, let hmData = Data(base64Encoded: hmText),
|
|
8
|
+
let midText = cfg["machineIdentifier"] as? String, let midData = Data(base64Encoded: midText),
|
|
9
|
+
let hm = VZMacHardwareModel(dataRepresentation: hmData),
|
|
10
|
+
let mid = VZMacMachineIdentifier(dataRepresentation: midData),
|
|
11
|
+
let cpus = cfg["cpuCount"] as? NSNumber, let mem = cfg["memorySize"] as? NSNumber
|
|
12
|
+
else {
|
|
13
|
+
print("PROBE: unreadable config")
|
|
14
|
+
exit(2)
|
|
15
|
+
}
|
|
16
|
+
let platform = VZMacPlatformConfiguration()
|
|
17
|
+
platform.hardwareModel = hm
|
|
18
|
+
platform.machineIdentifier = mid
|
|
19
|
+
platform.auxiliaryStorage = VZMacAuxiliaryStorage(contentsOf: dir.appendingPathComponent("nvram.bin"))
|
|
20
|
+
let config = VZVirtualMachineConfiguration()
|
|
21
|
+
config.platform = platform
|
|
22
|
+
config.bootLoader = VZMacOSBootLoader()
|
|
23
|
+
config.cpuCount = cpus.intValue
|
|
24
|
+
config.memorySize = mem.uint64Value
|
|
25
|
+
guard let disk = try? VZDiskImageStorageDeviceAttachment(url: dir.appendingPathComponent("disk.img"), readOnly: false) else {
|
|
26
|
+
print("PROBE: unreadable disk")
|
|
27
|
+
exit(2)
|
|
28
|
+
}
|
|
29
|
+
config.storageDevices = [VZVirtioBlockDeviceConfiguration(attachment: disk)]
|
|
30
|
+
let graphics = VZMacGraphicsDeviceConfiguration()
|
|
31
|
+
graphics.displays = [VZMacGraphicsDisplayConfiguration(widthInPixels: 1024, heightInPixels: 768, pixelsPerInch: 80)]
|
|
32
|
+
config.graphicsDevices = [graphics]
|
|
33
|
+
do {
|
|
34
|
+
try config.validate()
|
|
35
|
+
} catch {
|
|
36
|
+
let e = error as NSError
|
|
37
|
+
print("PROBE: validate failed domain=\(e.domain) code=\(e.code) desc=\(e.localizedDescription)")
|
|
38
|
+
exit(3)
|
|
39
|
+
}
|
|
40
|
+
let vm = VZVirtualMachine(configuration: config)
|
|
41
|
+
let began = Date()
|
|
42
|
+
vm.start { result in
|
|
43
|
+
switch result {
|
|
44
|
+
case .success:
|
|
45
|
+
print("PROBE: started after \(Date().timeIntervalSince(began)) s, state=\(vm.state.rawValue); stopping")
|
|
46
|
+
vm.stop { stopError in
|
|
47
|
+
if let s = stopError as NSError? { print("PROBE: stop error domain=\(s.domain) code=\(s.code)") } else { print("PROBE: stopped") }
|
|
48
|
+
exit(0)
|
|
49
|
+
}
|
|
50
|
+
case .failure(let error):
|
|
51
|
+
let e = error as NSError
|
|
52
|
+
print("PROBE: start failed after \(Date().timeIntervalSince(began)) s domain=\(e.domain) code=\(e.code) desc=\(e.localizedDescription)")
|
|
53
|
+
exit(1)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
|
|
57
|
+
print("PROBE: no completion within 60 s")
|
|
58
|
+
exit(4)
|
|
59
|
+
}
|
|
60
|
+
dispatchMain()
|