History

feat(devices): connect simulator hosts over SSH (#10856)

Julius Marminge committed 2 months ago

Commit d2eeacd1 parent 7734c6d30 files · +1794 −78Browse files

Showing the changes to apps/server/src/device/sshDeviceScript.ts (1 of 30 files). Show all

Unified · Split

apps/server/src/device/sshDeviceScript.ts+192−0
@@ -0,0 +1,192 @@
1+import { AGENT_DEVICE_VERSION, DEVICE_HUB_VERSION } from "./DeviceToolchain.ts";
2+
3+export const quoteRemoteArg = (value: string) => `'${value.replaceAll("'", "'\"'\"'")}'`;
4+
5+/** Resolve common non-interactive SDK and Node locations without sourcing user shell scripts. */
6+export const remoteDeviceEnvironment = `export PATH="$HOME/.local/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"
7+if [ -z "$ANDROID_HOME" ]; then
8+ if [ -d "$HOME/Library/Android/sdk" ]; then export ANDROID_HOME="$HOME/Library/Android/sdk";
9+ elif [ -d "$HOME/Android/Sdk" ]; then export ANDROID_HOME="$HOME/Android/Sdk"; fi
10+fi
11+if [ -n "$ANDROID_HOME" ]; then export PATH="$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH"; fi
12+if [ -z "$JAVA_HOME" ] && ! command -v java >/dev/null 2>&1; then
13+ for device_java_home in "$HOME/.local/opt/android-studio/jbr" /opt/android-studio/jbr /Applications/Android\\ Studio.app/Contents/jbr "$HOME/Applications/Android Studio.app/Contents/jbr"; do
14+ if [ -x "$device_java_home/bin/java" ]; then export JAVA_HOME="$device_java_home"; break; fi
15+ done
16+fi
17+if [ -n "$JAVA_HOME" ]; then export PATH="$JAVA_HOME/bin:$PATH"; fi
18+`;
19+
20+/** Node runs this on the host. All paths it returns belong to that host. */
21+export const remoteDeviceScript = (
22+ owner: string,
23+ mode: "probe" | "start" | "agent-start" | "stop-agent" | "stop",
24+) =>
25+ `
26+const owner = ${JSON.stringify(owner)};
27+const mode = ${JSON.stringify(mode)};
28+const hubVersion = ${JSON.stringify(DEVICE_HUB_VERSION)};
29+const agentVersion = ${JSON.stringify(AGENT_DEVICE_VERSION)};
30+` +
31+ String.raw`
32+const fs = require('node:fs');
33+const path = require('node:path');
34+const os = require('node:os');
35+const net = require('node:net');
36+const { spawn, spawnSync } = require('node:child_process');
37+const root = path.join(os.homedir(), '.t3', 'device');
38+const state = path.join(root, 'hosts', owner);
39+const run = (command, args, options = {}) => spawnSync(command, args, { encoding: 'utf8', timeout: 30000, ...options });
40+const read = (file) => { try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return null; } };
41+const write = (file, value) => { const tmp = file + '.' + process.pid; fs.writeFileSync(tmp, JSON.stringify(value), { mode: 0o600 }); fs.renameSync(tmp, file); };
42+const stopHub = hub => {
43+ if (!hub || hub.owner !== owner) return;
44+ const command = run('ps', ['-p', String(hub.pid), '-o', 'command=']).stdout || '';
45+ if (command.includes(hub.entryPath) && command.includes(String(hub.port))) {
46+ try { process.kill(hub.pid, 'SIGTERM'); } catch {}
47+ }
48+};
49+const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
50+const healthy = async (port, route) => { try { return (await fetch('http://127.0.0.1:' + port + route, { signal: AbortSignal.timeout(2000) })).ok; } catch { return false; } };
51+const port = () => new Promise((resolve, reject) => { const server = net.createServer(); server.once('error', reject); server.listen(0, '127.0.0.1', () => { const value = server.address().port; server.close(() => resolve(value)); }); });
52+async function acquireLock(lock, complete = () => false) {
53+ const deadline = Date.now() + 600000;
54+ const token = process.pid + ':' + require('node:crypto').randomUUID();
55+ const owner = () => { try { return fs.readlinkSync(lock); } catch (error) { if (error.code === 'ENOENT') return null; throw error; } };
56+ while (true) {
57+ try {
58+ // Publishing the PID and token is atomic; suspension cannot leave an incomplete owner.
59+ fs.symlinkSync(token, lock);
60+ return () => { if (owner() === token) fs.unlinkSync(lock); };
61+ } catch (error) {
62+ if (error.code !== 'EEXIST') throw error;
63+ if (complete()) return null;
64+ const previous = owner();
65+ if (previous === null) continue;
66+ const pid = Number(previous.split(':')[0]);
67+ if (!Number.isSafeInteger(pid) || pid <= 0) throw Error('Invalid device lock at ' + lock);
68+ try { process.kill(pid, 0); } catch (error) {
69+ if (error.code === 'ESRCH' && owner() === previous) {
70+ try { fs.unlinkSync(lock); } catch (error) { if (error.code !== 'ENOENT') throw error; }
71+ continue;
72+ }
73+ }
74+ if (Date.now() > deadline) throw Error('Device operation is locked at ' + lock + '. Check the other installer before removing the lock.');
75+ await sleep(500);
76+ }
77+ }
78+}
79+async function install(name, version, entry) {
80+ const dir = path.join(root, 'tools', name + '@' + version);
81+ const file = path.join(dir, 'node_modules', name, entry);
82+ const complete = () => fs.existsSync(file) && fs.existsSync(path.join(dir, '.install-complete')) && fs.readFileSync(path.join(dir, '.install-complete'), 'utf8').trim() === version;
83+ if (complete()) return file;
84+ fs.mkdirSync(path.dirname(dir), { recursive: true });
85+ const lock = dir + '.lock';
86+ const release = await acquireLock(lock, complete);
87+ if (!release) return file;
88+ let staging;
89+ try {
90+ if (complete()) return file;
91+ staging = fs.mkdtempSync(path.join(path.dirname(dir), '.install-'));
92+ const result = run('npm', ['install', '--prefix', staging, '--no-fund', '--no-audit', name + '@' + version], { timeout: 600000, maxBuffer: 8 * 1024 * 1024 });
93+ if (result.status !== 0) throw Error('Installing ' + name + ': ' + (result.error?.message || result.stderr?.slice(-2000)));
94+ if (!fs.existsSync(path.join(staging, 'node_modules', name, entry))) throw Error('Missing installed entry for ' + name);
95+ fs.writeFileSync(path.join(staging, '.install-complete'), version);
96+ fs.rmSync(dir, { recursive: true, force: true });
97+ fs.renameSync(staging, dir);
98+ return file;
99+ } finally {
100+ if (staging) fs.rmSync(staging, { recursive: true, force: true });
101+ release();
102+ }
103+}
104+(async () => {
105+ const ios = process.platform === 'darwin' && run('xcrun', ['simctl', 'help']).status === 0;
106+ const android = run('adb', ['version']).status === 0;
107+ const platforms = [
108+ { platform: 'ios', available: ios, ...(!ios ? { reason: 'iOS needs macOS with Xcode and working xcrun simctl.' } : {}) },
109+ { platform: 'android', available: android, ...(!android ? { reason: 'Android SDK missing. Set ANDROID_HOME or put adb on the SSH PATH.' } : {}) },
110+ ];
111+ if (mode === 'probe') {
112+ if (Number(process.versions.node.split('.')[0]) < 22) throw Error('Node 22 or newer is required on the device host.');
113+ if (run('npm', ['--version']).status !== 0) throw Error('npm is missing from the non-interactive SSH PATH.');
114+ console.log(JSON.stringify({ nodePath: process.execPath, platforms })); return;
115+ }
116+ fs.mkdirSync(state, { recursive: true, mode: 0o700 });
117+ // Serialize starts and stops for this environment/host owner, including agent startup.
118+ const hostLock = path.join(state, 'runtime.lock');
119+ const releaseHost = await acquireLock(hostLock);
120+ try {
121+ const hubFile = path.join(state, 'hub.json');
122+ const daemonFile = path.join(state, 'daemon.json');
123+ const agentFile = path.join(state, 'agent.json');
124+ if (mode === 'stop' || mode === 'stop-agent') {
125+ const hub = read(hubFile);
126+ if (mode === 'stop' && hub && hub.owner === owner) {
127+ stopHub(hub);
128+ fs.rmSync(hubFile, { force: true });
129+ }
130+ const entry = read(agentFile)?.entryPath || path.join(root, 'tools', 'agent-device@' + agentVersion, 'node_modules', 'agent-device', 'bin', 'agent-device.mjs');
131+ if (fs.existsSync(entry)) run(process.execPath, [entry, 'daemon', 'stop', '--state-dir', state]);
132+ return;
133+ }
134+ if (!ios && !android) throw Error(platforms.map(p => p.reason).join(' '));
135+ fs.mkdirSync(state, { recursive: true, mode: 0o700 });
136+ const hubEntry = await install('expo-device-hub', hubVersion, 'dist/server/cli.mjs');
137+ let hub = read(hubFile);
138+ if (!hub || hub.owner !== owner || hub.entryPath !== hubEntry || !await healthy(hub.port, '/readyz')) {
139+ stopHub(hub);
140+ for (let attempt = 0; attempt < 5; attempt++) {
141+ const hubPort = await port();
142+ const log = fs.openSync(path.join(state, 'hub.log'), 'a');
143+ const child = spawn(process.execPath, [hubEntry, '--port', String(hubPort), '--host', '127.0.0.1', '--hide-sidebar', '--hide-boot-device'], {
144+ cwd: state, detached: true, stdio: ['ignore', log, log], env: { ...process.env, FORCE_COLOR: '0', NO_COLOR: '1' },
145+ });
146+ try { await new Promise((resolve, reject) => { child.once('spawn', resolve); child.once('error', reject); }); }
147+ finally { fs.closeSync(log); }
148+ child.unref();
149+ hub = { owner, pid: child.pid, port: hubPort, entryPath: hubEntry };
150+ write(hubFile, hub);
151+ const deadline = Date.now() + 30000;
152+ let listening = false;
153+ while (child.exitCode === null && child.signalCode === null) {
154+ if (await healthy(hub.port, '/readyz')) { listening = true; break; }
155+ if (Date.now() > deadline) { stopHub(hub); throw Error('Device hub did not become ready. See ' + path.join(state, 'hub.log')); }
156+ await sleep(200);
157+ }
158+ if (listening) break;
159+ // Port reservation and binding happen in different processes. Retry an early exit with a fresh port.
160+ fs.rmSync(hubFile, { force: true });
161+ if (attempt === 4) throw Error('Device hub exited before becoming ready. See ' + path.join(state, 'hub.log'));
162+ }
163+ }
164+ let agentResult = {};
165+ if (mode === 'agent-start') {
166+ const agentEntry = await install('agent-device', agentVersion, 'bin/agent-device.mjs');
167+ const previousAgent = read(agentFile)?.entryPath;
168+ let daemon = read(daemonFile);
169+ if (daemon && (previousAgent !== agentEntry || !await healthy(daemon.httpPort, '/health'))) {
170+ const stopped = run(process.execPath, [previousAgent || agentEntry, 'daemon', 'stop', '--state-dir', state]);
171+ if (stopped.status !== 0) throw Error('Could not stop the previous agent-device version.');
172+ fs.rmSync(daemonFile, { force: true });
173+ daemon = null;
174+ }
175+ if (!daemon) {
176+ fs.rmSync(daemonFile, { force: true });
177+ const env = { ...process.env, AGENT_DEVICE_STATE_DIR: state, AGENT_DEVICE_DAEMON_SERVER_MODE: 'http', AGENT_DEVICE_DAEMON_IDLE_TIMEOUT_MS: '0', AGENT_DEVICE_NO_UPDATE_NOTIFIER: '1' };
178+ delete env.AGENT_DEVICE_DAEMON_BASE_URL; delete env.AGENT_DEVICE_DAEMON_AUTH_TOKEN; delete env.AGENT_DEVICE_CONFIG;
179+ run(process.execPath, [agentEntry, 'devices', '--json'], { env });
180+ daemon = read(daemonFile);
181+ }
182+ if (!daemon || !await healthy(daemon.httpPort, '/health')) throw Error('agent-device daemon did not become ready in ' + state);
183+ write(agentFile, { entryPath: agentEntry });
184+ agentResult = { daemonPort: daemon.httpPort, token: daemon.token, entryPath: agentEntry };
185+ }
186+ const vendor = path.resolve(path.dirname(hubEntry), '../../vendor/serve-sim/dist');
187+ const optional = file => fs.existsSync(file) ? file : null;
188+ console.log(JSON.stringify({ nodePath: process.execPath, platforms, hubPort: hub.port, ...agentResult,
189+ helpers: { serveSimAxSettings: optional(path.join(vendor, 'simax/serve-sim-ax-settings')), serveSimCli: optional(path.join(vendor, 'serve-sim.js')) } }));
190+ } finally { releaseHost(); }
191+})().catch(error => { console.error(error.message); process.exitCode = 1; });
192+`;