@inkeep/impulse-compute 0.0.0-stage → 0.2.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.
@@ -0,0 +1,158 @@
1
+ #!/bin/bash
2
+ set -u
3
+ export LC_ALL=C
4
+ phase=$1
5
+ user=c-team
6
+ lume_version=0.6.1
7
+ lume_url=https://github.com/trycua/cua/releases/download/lume-v$lume_version/lume.tar.gz
8
+ lume_sha256=2675b7981b7d3eb525cd27f40a385c986052eb71b3360c0b2579644725f1bad8
9
+ lume_team=YCK386LBJ7
10
+ image_repo=trycua/macos-tahoe-vanilla
11
+ image_tag=latest
12
+ image_digest=sha256:10a8c1c95c5c2367c633d99be2aa91abf1e7db9cf80c7829061b57c997674b67
13
+ base=ot-base-tahoe
14
+ work=/var/tmp/ot-team-mac
15
+ steps=""
16
+
17
+ set_paths() {
18
+ home=$(eval "printf '%s' ~$user")
19
+ lume=$home/.local/bin/lume
20
+ probe=$home/.ot-team-mac/bin/vz-start-probe
21
+ }
22
+ set_paths
23
+
24
+ note() { steps="${steps}${steps:+$'\n'}$1"$'\t'"$2"$'\t'"$3"; printf '%s %s: %s\n' "$1" "$2" "$3" >&2; }
25
+
26
+ emit() {
27
+ STEPS=$steps perl -MJSON::PP -e '
28
+ my %r = @ARGV;
29
+ for my $k (qw(rebootScheduled)) { $r{$k} = $r{$k} eq "true" ? JSON::PP::true : JSON::PP::false if defined $r{$k} }
30
+ $r{steps} = [ map { my ($n, $s, $d) = split /\t/, $_, 3; { name => $n, status => $s, detail => $d } } grep { length } split /\n/, $ENV{STEPS} ];
31
+ print JSON::PP->new->canonical->encode(\%r), "\n";
32
+ ' -- "$@"
33
+ }
34
+
35
+ as_user() { /usr/bin/sudo -u "$user" -H env HOME="$home" LUME_TELEMETRY_ENABLED=false PATH=/usr/bin:/bin:/usr/sbin:/sbin "$@"; }
36
+
37
+ boot_time() { sysctl -n kern.boottime | sed 's/.*{ sec = \([0-9]*\),.*/\1/'; }
38
+
39
+ host_key() { cat /etc/ssh/ssh_host_ed25519_key.pub 2>/dev/null; }
40
+
41
+ aqua_state() {
42
+ local uid console gui finder dock
43
+ uid=$(id -u "$user" 2>/dev/null) || { echo "absent"; return; }
44
+ console=$(stat -f%Su /dev/console)
45
+ launchctl print "gui/$uid" >/dev/null 2>&1 && gui=yes || gui=no
46
+ pgrep -u "$uid" -x Finder >/dev/null && finder=yes || finder=no
47
+ pgrep -u "$uid" -x Dock >/dev/null && dock=yes || dock=no
48
+ if [ "$console" = "$user" ] && [ "$gui" = yes ] && [ "$finder" = yes ] && [ "$dock" = yes ]; then echo ready; else echo "not-ready console=$console gui=$gui finder=$finder dock=$dock"; fi
49
+ }
50
+
51
+ fail() { note "$1" failed "$2"; emit phase "$phase" outcome failed failedStep "$1" bootTime "$(boot_time)"; exit 1; }
52
+
53
+ [ "$(id -u)" = 0 ] || { emit phase "$phase" outcome failed failedStep root; exit 64; }
54
+
55
+ case "$phase" in
56
+ setup)
57
+ if ssh-keygen -A >/dev/null 2>&1 && [ -n "$(host_key)" ]; then note host-keys ok "ssh-keygen -A"; else fail host-keys "ssh-keygen -A left no ed25519 host key"; fi
58
+
59
+ autologin=$(defaults read /Library/Preferences/com.apple.loginwindow autoLoginUser 2>/dev/null)
60
+ if id "$user" >/dev/null 2>&1; then
61
+ if [ "$autologin" = "$user" ] && [ -s /etc/kcpassword ]; then
62
+ note user ok "$user exists with auto-login"
63
+ else
64
+ fail user "$user exists but auto-login is ${autologin:-unset}; preparing it again needs its password"
65
+ fi
66
+ else
67
+ pw=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 24)
68
+ [ ${#pw} -eq 24 ] || fail user "password generation failed"
69
+ sysadminctl -addUser "$user" -fullName "openteammate team" -password "$pw" -shell /bin/zsh >/dev/null 2>&1
70
+ id "$user" >/dev/null 2>&1 || fail user "sysadminctl did not create $user"
71
+ set_paths
72
+ [ -d "$home" ] || createhomedir -c -u "$user" >/dev/null 2>&1
73
+ [ -d "$home" ] || fail user "no home directory for $user"
74
+ PW=$pw perl -e '
75
+ my $p = $ENV{PW}; my @m = (0x7d,0x89,0x52,0x23,0xd2,0xbc,0xdd,0xea,0xa3,0xb9,0x1f);
76
+ my $n = int((length($p) + 1 + 11) / 12) * 12; my $b = $p . ("\0" x ($n - length($p))); my $o = "";
77
+ for my $i (0 .. $n - 1) { $o .= chr(ord(substr($b, $i, 1)) ^ $m[$i % 11]); }
78
+ open(my $f, ">", "/etc/kcpassword") or die "open: $!"; binmode $f; print $f $o; close $f or die "close: $!";
79
+ ' || fail user "writing /etc/kcpassword failed"
80
+ unset pw
81
+ chown root:wheel /etc/kcpassword && chmod 600 /etc/kcpassword
82
+ defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser -string "$user"
83
+ [ "$(defaults read /Library/Preferences/com.apple.loginwindow autoLoginUser 2>/dev/null)" = "$user" ] || fail user "autoLoginUser not set"
84
+ note user ok "created $user with auto-login; replaced auto-login ${autologin:-none}"
85
+ fi
86
+ if dscl . -read /Groups/com.apple.access_ssh >/dev/null 2>&1; then
87
+ dseditgroup -o edit -a "$user" -t user com.apple.access_ssh >/dev/null 2>&1 || fail ssh-access "adding $user to com.apple.access_ssh failed"
88
+ note ssh-access ok "$user added to com.apple.access_ssh"
89
+ else
90
+ note ssh-access ok "no com.apple.access_ssh group: every user may log in"
91
+ fi
92
+
93
+ if as_user "$lume" --version 2>/dev/null | grep -q "$lume_version"; then
94
+ note lume ok "Lume $lume_version already installed for $user"
95
+ else
96
+ rm -rf "$work/lume" && mkdir -p "$work/lume" || fail lume "cannot create $work/lume"
97
+ curl -fsSL -o "$work/lume/lume.tar.gz" "$lume_url" || fail lume "download failed: $lume_url"
98
+ printf '%s %s\n' "$lume_sha256" "$work/lume/lume.tar.gz" | shasum -a 256 -c - >/dev/null 2>&1 || fail lume "sha256 mismatch for $lume_url"
99
+ tar -xzf "$work/lume/lume.tar.gz" -C "$work/lume" || fail lume "extract failed"
100
+ codesign --verify --deep --strict "$work/lume/lume.app" >/dev/null 2>&1 || fail lume "codesign verification failed"
101
+ codesign -dv "$work/lume/lume.app" 2>&1 | grep -q "TeamIdentifier=$lume_team" || fail lume "signing team is not $lume_team"
102
+ mkdir -p "$home/.local/share/lume" "$home/.local/bin"
103
+ rm -rf "$home/.local/share/lume/lume.app"
104
+ mv "$work/lume/lume.app" "$home/.local/share/lume/" || fail lume "install failed"
105
+ printf '#!/bin/sh\nexec "%s/lume.app/Contents/MacOS/lume" "$@"\n' "$home/.local/share/lume" > "$lume"
106
+ chmod 755 "$lume"
107
+ chown -R "$user":staff "$home/.local"
108
+ as_user "$lume" --version 2>/dev/null | grep -q "$lume_version" || fail lume "installed Lume does not report $lume_version"
109
+ note lume ok "installed Lume $lume_version (sha256 and signing team checked)"
110
+ fi
111
+
112
+ if as_user "$lume" get "$base" --format json >/dev/null 2>&1; then
113
+ note base ok "$base already present"
114
+ else
115
+ token=$(curl -fsS "https://ghcr.io/token?scope=repository:$image_repo:pull" | perl -MJSON::PP -e 'local $/; print decode_json(<STDIN>)->{token}') || fail base "registry token request failed"
116
+ digest=$(curl -fsS -o /dev/null -D - -H "Authorization: Bearer $token" -H 'Accept: application/vnd.oci.image.manifest.v1+json' "https://ghcr.io/v2/$image_repo/manifests/$image_tag" | tr -d '\r' | awk 'tolower($1) == "docker-content-digest:" { print $2 }')
117
+ [ "$digest" = "$image_digest" ] || fail base "$image_repo:$image_tag is ${digest:-unreadable}, not the pinned $image_digest"
118
+ as_user "$lume" pull "${image_repo#trycua/}:$image_tag" "$base" > "$work/pull.log" 2>&1 || fail base "lume pull failed: $(tail -n 2 "$work/pull.log" | tr '\n' ' ')"
119
+ as_user "$lume" get "$base" --format json >/dev/null 2>&1 || fail base "$base absent after pull"
120
+ note base ok "pulled $image_repo:$image_tag ($image_digest) as $base"
121
+ fi
122
+
123
+ if [ -x "$probe" ] && cmp -s "$work/vz-start-probe.swift" "$home/.ot-team-mac/src/vz-start-probe.swift"; then
124
+ note probe ok "probe already built"
125
+ elif [ ! -x /Library/Developer/CommandLineTools/usr/bin/swiftc ]; then
126
+ note probe skipped "no Command Line Tools swiftc; refusals will carry no framework code"
127
+ else
128
+ mkdir -p "$home/.ot-team-mac/src" "$home/.ot-team-mac/bin"
129
+ cp "$work/vz-start-probe.swift" "$home/.ot-team-mac/src/vz-start-probe.swift"
130
+ printf '%s\n' '<?xml version="1.0" encoding="UTF-8"?>' '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' \
131
+ '<plist version="1.0"><dict><key>com.apple.security.virtualization</key><true/></dict></plist>' > "$home/.ot-team-mac/src/vz.entitlements"
132
+ chown -R "$user":staff "$home/.ot-team-mac"
133
+ as_user /Library/Developer/CommandLineTools/usr/bin/swiftc -O -sdk /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk \
134
+ -target arm64-apple-macos14.0 -o "$probe" "$home/.ot-team-mac/src/vz-start-probe.swift" > "$work/probe-build.log" 2>&1 \
135
+ || fail probe "swiftc failed: $(tail -n 2 "$work/probe-build.log" | tr '\n' ' ')"
136
+ as_user codesign -s - -f --entitlements "$home/.ot-team-mac/src/vz.entitlements" "$probe" >/dev/null 2>&1 || fail probe "codesign failed"
137
+ note probe ok "built and signed with the virtualization entitlement"
138
+ fi
139
+
140
+ reboot=false
141
+ if [ "$(aqua_state)" != ready ]; then
142
+ reboot=true
143
+ ( sleep 5; /sbin/shutdown -r now ) >/dev/null 2>&1 &
144
+ note reboot scheduled "auto-login takes effect at the next boot"
145
+ else
146
+ note reboot skipped "$user already has an Aqua session"
147
+ fi
148
+ emit phase setup outcome ok rebootScheduled "$reboot" bootTime "$(boot_time)"
149
+ ;;
150
+ check)
151
+ ssh-keygen -A >/dev/null 2>&1
152
+ emit phase check outcome ok bootTime "$(boot_time)" aqua "$(aqua_state)" hostKey "$(host_key)" \
153
+ lume "$(as_user "$lume" --version 2>/dev/null | tr -d '\n')" \
154
+ base "$(as_user "$lume" get "$base" --format json >/dev/null 2>&1 && echo present || echo absent)" \
155
+ probe "$([ -x "$probe" ] && echo present || echo absent)"
156
+ ;;
157
+ *) emit phase "$phase" outcome failed failedStep usage; exit 64 ;;
158
+ esac
@@ -0,0 +1,38 @@
1
+ #!/bin/bash
2
+ set -uo pipefail
3
+ export LUME_TELEMETRY_ENABLED=false LC_ALL=C
4
+ lume=${OT_LUME:-$HOME/.local/bin/lume}
5
+
6
+ rows=$("$lume" ls --format json 2>/dev/null) || { printf "%s\n" "Lume inventory failed" >&2; exit 1; }
7
+ printf "%s\n" "$rows" | perl -MJSON::PP -e '
8
+ local $/;
9
+ my $rows = eval { decode_json(<STDIN>) };
10
+ die "Lume did not return an array\n" unless ref($rows) eq "ARRAY";
11
+ my @out;
12
+ for my $row (@$rows) {
13
+ die "Invalid VM inventory row\n" unless ref($row) eq "HASH"
14
+ && defined($row->{name}) && !ref($row->{name})
15
+ && defined($row->{status}) && !ref($row->{status});
16
+ my $name = $row->{name};
17
+ my $created;
18
+ if ($name ne "." && $name ne ".." && $name !~ m{[/\x00]}
19
+ && ($row->{locationName} // "home") eq "home") {
20
+ my $path = "$ENV{HOME}/.lume/$name";
21
+ my @stat = $^O eq "darwin"
22
+ ? ("/usr/bin/stat", "-f", "%B", $path)
23
+ : ("/usr/bin/stat", "-c", "%W", $path);
24
+ if (open my $stat, "-|", @stat) {
25
+ my $value = <$stat> // "";
26
+ my $ok = close $stat;
27
+ chomp $value;
28
+ $created = 0 + $value if $ok && $value =~ /^\d+$/ && $value > 0;
29
+ }
30
+ }
31
+ push @out, {
32
+ name => $name,
33
+ status => $row->{status},
34
+ createdAtEpochSeconds => $created,
35
+ };
36
+ }
37
+ print JSON::PP->new->canonical->encode(\@out), "\n";
38
+ '
@@ -0,0 +1,278 @@
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, $pid_file) = splice @ARGV, 0, 2;
122
+ pipe(my $read_start, my $send_start) or die "pipe: $!\n";
123
+ defined(my $pid = fork()) or die "fork: $!\n";
124
+ if ($pid) {
125
+ close $read_start;
126
+ if (length $pid_file) {
127
+ unless (print("$pid\n") && close(STDOUT) && system("mv", "$pid_file.tmp", $pid_file) == 0) {
128
+ close $send_start;
129
+ waitpid($pid, 0);
130
+ exit 1;
131
+ }
132
+ }
133
+ print {$send_start} "start\n" or die "release runner: $!\n";
134
+ close $send_start or die "release runner: $!\n";
135
+ exit 0;
136
+ }
137
+ close $send_start;
138
+ <$read_start> or exit 1;
139
+ close $read_start;
140
+ setsid() or die "setsid: $!\n";
141
+ open(STDIN, "<", "/dev/null") or die "stdin: $!\n";
142
+ open(STDOUT, ">>", $log) or die "log: $!\n";
143
+ open(STDERR, ">&", \*STDOUT) or die "stderr: $!\n";
144
+ exec @ARGV or die "exec: $!\n";
145
+ ' "$@" 8>&- 9>&-
146
+ }
147
+
148
+ case "$vm" in
149
+ "" | *[!a-z0-9-]* | -*) emit outcome refused-input reason "the VM name must be lowercase letters, digits and hyphens"; exit 2 ;;
150
+ esac
151
+ case "$pubkey" in
152
+ "ssh-ed25519 "*) ;;
153
+ *) emit outcome refused-input reason "the public key must be one ssh-ed25519 line"; exit 2 ;;
154
+ esac
155
+ case "$expires" in
156
+ "" | *[!0-9]*) emit outcome refused-input reason "the VM deadline must be UTC epoch seconds"; exit 2 ;;
157
+ esac
158
+ if [ "$expires" -le "$started" ]; then
159
+ emit outcome refused-input reason "the VM deadline has already passed"
160
+ exit 2
161
+ fi
162
+ team_mac_host_lock || { emit outcome refused-input reason "cannot acquire the host claim"; exit 2; }
163
+ hold_reason=$(team_mac_hold_check "$pubkey" 2>&1)
164
+ hold_status=$?
165
+ if [ "$hold_status" -ne 0 ]; then
166
+ if [ "$hold_status" -eq 3 ]; then
167
+ emit outcome host-held reason "$hold_reason"
168
+ else
169
+ emit outcome refused-input reason "$hold_reason"
170
+ fi
171
+ exit 2
172
+ fi
173
+ case "$pubkey" in
174
+ *"'"* | *"\\"*) emit outcome refused-input reason "the public key line must not contain quotes or backslashes"; exit 2 ;;
175
+ esac
176
+ control=$HOME/.ot-team-mac/starts/$vm
177
+ mkdir -p "$control" || { emit outcome refused-input reason "cannot create the start claim"; exit 2; }
178
+ exec 9>"$control/lock" || { emit outcome refused-input reason "cannot open the start claim"; exit 2; }
179
+ perl -e 'flock STDIN, 2 or die "start claim: $!\n"' <&9 || { emit outcome refused-input reason "cannot acquire the start claim"; exit 2; }
180
+ owner_hash=$(printf '%s' "$pubkey" | shasum -a 256 | cut -d' ' -f1)
181
+ cancel=$control/cancel-$owner_hash
182
+ if [ -f "$cancel" ]; then
183
+ emit outcome refused-input reason "this start was cancelled by its run's stop"
184
+ exit 2
185
+ fi
186
+ perl -MJSON::PP -e 'print encode_json({ ownerHash => $ARGV[0], pid => 0 + $ARGV[1], startedAtEpochSeconds => 0 + $ARGV[2] }), "\n"' \
187
+ "$owner_hash" "$$" "$started" > "$control/active.tmp" && mv "$control/active.tmp" "$control/active" || { emit outcome refused-input reason "cannot publish the start claim"; exit 2; }
188
+ lease=$control/deadline-$owner_hash
189
+ trap 'rm -f "$control/active"' EXIT
190
+ if present; then
191
+ emit outcome name-taken vm "$vm" reason "a VM with this name already exists; it is not this run's to touch"
192
+ exit 3
193
+ fi
194
+ intent=$control/clone-$owner_hash
195
+ : > "$intent" || { emit outcome refused-input reason "cannot record the clone intent"; exit 2; }
196
+ if ! clone_out=$("$lume" clone "$base" "$vm" 2>&1); then
197
+ if present; then absent=false; else absent=true; fi
198
+ emit outcome clone-failed vm "$vm" base "$base" reason "$(printf '%s' "$clone_out" | tail -n 3)" absent "$absent"
199
+ exit 4
200
+ fi
201
+ if [ -f "$cancel" ]; then
202
+ cancel_start
203
+ fi
204
+ if ! printf '%s\n' "$pubkey" > "$HOME/.lume/$vm/.ot-team-mac-owner"; then
205
+ remove_clone
206
+ emit outcome clone-failed vm "$vm" base "$base" reason "cannot record the clone owner; the VM was not started" absent "$absent"
207
+ exit 4
208
+ fi
209
+ rm -f "$intent"
210
+ mkdir -p "$dir"
211
+ 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 \
212
+ "$vm" "$expires" "$pubkey" "$lume" "$control" "$dir" "$poll" "$owner_hash" >/dev/null
213
+ while [ ! -f "$lease" ]; do
214
+ [ ! -f "$cancel" ] || cancel_start
215
+ if [ "$(date -u +%s)" -ge "$deadline" ]; then
216
+ remove_clone
217
+ emit outcome clone-failed vm "$vm" base "$base" reason "the VM deadline reaper did not arm" absent "$absent"
218
+ exit 4
219
+ fi
220
+ sleep "$poll"
221
+ done
222
+ if ! detach "$dir/runner.log" "$dir/pid" /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.tmp"; then
223
+ remove_clone
224
+ emit outcome not-established vm "$vm" boundSeconds "$bound" reason "could not publish the VM runner PID" \
225
+ presentBeforeDelete "$before" absent "$absent" log ""
226
+ exit 4
227
+ fi
228
+
229
+ while :; do
230
+ if [ -f "$cancel" ]; then
231
+ cancel_start
232
+ fi
233
+ if [ -f "$dir/exit" ]; then
234
+ code=$(cat "$dir/exit")
235
+ framework_domain="" framework_code="" framework_started=false
236
+ if [ -x "$probe" ]; then
237
+ probe_out=$("$probe" "$HOME/.lume/$vm" 2>&1)
238
+ framework_domain=$(printf '%s\n' "$probe_out" | perl -ne 'if (/start failed .* domain=(\S+) code=(-?\d+)/) { print $1; exit }')
239
+ framework_code=$(printf '%s\n' "$probe_out" | perl -ne 'if (/start failed .* domain=(\S+) code=(-?\d+)/) { print $2; exit }')
240
+ case "$probe_out" in *"PROBE: started"*) framework_started=true ;; esac
241
+ fi
242
+ log=$(redacted_tail "$dir/runner.log")
243
+ remove_clone
244
+ emit outcome runner-exited vm "$vm" runnerExit "$code" frameworkDomain "$framework_domain" \
245
+ frameworkCode "$framework_code" frameworkStarted "$framework_started" probe "$([ -x "$probe" ] && echo present || echo absent)" \
246
+ presentBeforeDelete "$before" absent "$absent" log "$log"
247
+ exit 0
248
+ fi
249
+ row=$(details) || row=""
250
+ ip=$(printf '%s' "$row" | cut -f2)
251
+ mac=$(printf '%s' "$row" | cut -f3)
252
+ if [ -n "$ip" ] && [ -n "$mac" ]; then
253
+ 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=""
254
+ if printf '%s\n' "$guest" | MAC="$mac" perl -e '
255
+ my $want = join ":", map { sprintf "%02x", hex } split /:/, lc $ENV{MAC};
256
+ 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 } }
257
+ exit 1;
258
+ '; then
259
+ version=$(printf '%s\n' "$guest" | sed -n 's/^OT-VERSION //p' | head -n 1)
260
+ hostkey=$(printf '%s\n' "$guest" | sed -n 's/^OT-HOSTKEY //p' | head -n 1)
261
+ 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
262
+ [ ! -f "$cancel" ] || cancel_start
263
+ emit outcome booted vm "$vm" ip "$ip" mac "$mac" guestMacos "$version" guestHostKey "$hostkey" \
264
+ bootSeconds "$(($(date -u +%s) - started))" boundSeconds "$bound"
265
+ exit 0
266
+ fi
267
+ fi
268
+ fi
269
+ if [ "$(date -u +%s)" -ge "$deadline" ]; then
270
+ "$lume" stop --force "$vm" >/dev/null 2>&1
271
+ wait_runner_exit 60
272
+ log=$(redacted_tail "$dir/runner.log")
273
+ remove_clone
274
+ emit outcome not-established vm "$vm" boundSeconds "$bound" presentBeforeDelete "$before" absent "$absent" log "$log"
275
+ exit 0
276
+ fi
277
+ sleep "$poll"
278
+ 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"