mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* refactor(main): organize SSH transport and managed server owners * Remove orphan deploy readiness fixture and shrink test baselines * refactor(wsl): import lock records from their defining owner * refactor(wsl): use the canonical deferred update contract
195 lines
7.2 KiB
TypeScript
195 lines
7.2 KiB
TypeScript
// Leave the v1.4.224 relay detached with a live shell under its versioned remote directory, matching app quit.
|
||
// COMPAT(legacy-relay-bridge): revisit 2027-01-15, with src/main/ssh/legacy-relay/.
|
||
import { execFileSync } from 'node:child_process'
|
||
import { cpSync, mkdtempSync, rmSync } from 'node:fs'
|
||
import os from 'node:os'
|
||
import path from 'node:path'
|
||
import { quotePosixShell } from '../../../src/shared/shell/launch/shell-argument-quoting'
|
||
import { relaySocketNameForInstanceId } from '../../../src/main/ssh/legacy-relay/endpoints/ssh-relay-instance-id'
|
||
import { SshChannelMultiplexer } from '../../../src/main/ssh/protocol/ssh-channel-multiplexer'
|
||
import {
|
||
leaveShellInReleasedRelay,
|
||
openShellRelayTransport
|
||
} from '../cross-version-wire/released-relay-transport'
|
||
import { execDockerSshHostTargetCommand, type DockerSshHostTarget } from './docker-ssh-host-target'
|
||
|
||
export type DockerReleasedRelay = { dir: string; version: string }
|
||
|
||
export type DockerRelayInstance = { sockPath: string; relayPtyId: string }
|
||
|
||
// Bundle release sources using the orcad template’s glibc-floor N-API node-pty, which loads on the host Node.
|
||
export async function installReleasedRelayOnDockerHost(
|
||
target: DockerSshHostTarget,
|
||
orcadTemplateDir: string
|
||
): Promise<DockerReleasedRelay> {
|
||
const built: unknown = JSON.parse(
|
||
execFileSync(
|
||
path.join(process.cwd(), 'node_modules', '.bin', 'tsx'),
|
||
[path.join(process.cwd(), 'tests', 'e2e', 'helpers', 'build-released-relay-bundle.mts')],
|
||
{ encoding: 'utf8', timeout: 300_000, stdio: ['ignore', 'pipe', 'inherit'] }
|
||
)
|
||
.trim()
|
||
.split('\n')
|
||
.at(-1) ?? 'null'
|
||
)
|
||
if (
|
||
typeof built !== 'object' ||
|
||
built === null ||
|
||
!('bundleDir' in built) ||
|
||
!('version' in built) ||
|
||
typeof built.bundleDir !== 'string' ||
|
||
typeof built.version !== 'string'
|
||
) {
|
||
throw new Error('Building the released relay printed no bundle')
|
||
}
|
||
const { bundleDir, version } = built
|
||
const dir = `/root/.orca-remote/relay-${version}`
|
||
const staging = mkdtempSync(path.join(os.tmpdir(), 'orca-legacy-relay-'))
|
||
try {
|
||
// Why the host's arch: a Docker host on an arm64 machine runs arm64 containers.
|
||
const arch = execDockerSshHostTargetCommand(target, 'uname -m') === 'aarch64' ? 'arm64' : 'x64'
|
||
const pty = path.join(staging, 'node_modules', 'node-pty')
|
||
cpSync(path.join(orcadTemplateDir, 'node_modules', 'node-pty'), pty, { recursive: true })
|
||
cpSync(
|
||
path.join(orcadTemplateDir, 'targets', `linux-${arch}-glibc`, 'node_modules', 'node-pty'),
|
||
pty,
|
||
{
|
||
recursive: true
|
||
}
|
||
)
|
||
// Why dereference: the bundle is a real file here, never the checkout's symlink.
|
||
cpSync(path.join(bundleDir, 'relay.js'), path.join(staging, 'relay.js'), { dereference: true })
|
||
execDockerSshHostTargetCommand(target, `mkdir -p ${quotePosixShell(dir)}`)
|
||
execFileSync('docker', ['cp', `${staging}/.`, `${target.containerName}:${dir}`], {
|
||
stdio: 'ignore',
|
||
timeout: 60_000
|
||
})
|
||
execDockerSshHostTargetCommand(
|
||
target,
|
||
`printf '%s' ${quotePosixShell(version)} > ${quotePosixShell(`${dir}/.version`)}`
|
||
)
|
||
} finally {
|
||
rmSync(staging, { recursive: true, force: true })
|
||
}
|
||
return { dir, version }
|
||
}
|
||
|
||
function connectCommand(relay: DockerReleasedRelay, sockPath: string): string {
|
||
return (
|
||
`cd ${quotePosixShell(relay.dir)} && node relay.js --connect --sock-path ` +
|
||
`${quotePosixShell(sockPath)} --credential-file ${quotePosixShell(`${sockPath}.credential`)}`
|
||
)
|
||
}
|
||
|
||
/** The relay's own bridge, opened from the runner the way an SSH exec channel would. */
|
||
export function openDockerRelayTransport(
|
||
target: DockerSshHostTarget,
|
||
relay: DockerReleasedRelay,
|
||
sockPath: string
|
||
): ReturnType<typeof openShellRelayTransport> {
|
||
return openShellRelayTransport(
|
||
`docker exec -i ${quotePosixShell(target.containerName)} sh -c ${quotePosixShell(
|
||
connectCommand(relay, sockPath)
|
||
)}`
|
||
)
|
||
}
|
||
|
||
// Use the old client’s keep-terminals-forever grace and leave an echoing shell behind.
|
||
export async function startDockerRelayWithShell(
|
||
target: DockerSshHostTarget,
|
||
relay: DockerReleasedRelay,
|
||
targetId: string,
|
||
marker: string
|
||
): Promise<DockerRelayInstance> {
|
||
const sockPath = `${relay.dir}/${relaySocketNameForInstanceId(targetId)}`
|
||
execDockerSshHostTargetCommand(
|
||
target,
|
||
[
|
||
`cd ${quotePosixShell(relay.dir)}`,
|
||
`SHELL=/bin/sh nohup node relay.js --detached --grace-time 0 --sock-path ${quotePosixShell(
|
||
sockPath
|
||
)} --credential-file ${quotePosixShell(`${sockPath}.credential`)} ` +
|
||
`> ${quotePosixShell(`${sockPath}.log`)} 2>&1 < /dev/null &`,
|
||
`for i in $(seq 1 100); do [ -S ${quotePosixShell(sockPath)} ] && ` +
|
||
`[ -f ${quotePosixShell(`${sockPath}.credential`)} ] && exit 0; sleep 0.1; done`,
|
||
`cat ${quotePosixShell(`${sockPath}.log`)}; exit 1`
|
||
].join('\n')
|
||
)
|
||
const relayPtyId = await leaveShellInReleasedRelay(
|
||
() => openDockerRelayTransport(target, relay, sockPath),
|
||
`${targetId}-v1.4.224-desktop`,
|
||
marker
|
||
)
|
||
return { sockPath, relayPtyId }
|
||
}
|
||
|
||
/** Owner without flow control; a holder that just disconnected is waited out, as any client would. */
|
||
async function claimOwnerWaitingOutGrace(mux: SshChannelMultiplexer): Promise<void> {
|
||
for (let attempt = 0; ; attempt++) {
|
||
try {
|
||
await mux.request('pty.openClient', {
|
||
protocolVersion: 1,
|
||
clientInstanceId: 'e2e-probe',
|
||
requestedRole: 'session-owner'
|
||
})
|
||
return
|
||
} catch (error) {
|
||
const code: unknown =
|
||
typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined
|
||
if (code !== -32045 || attempt >= 60) {
|
||
throw error
|
||
}
|
||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||
}
|
||
}
|
||
}
|
||
|
||
export async function echoThroughDockerRelay(
|
||
target: DockerSshHostTarget,
|
||
relay: DockerReleasedRelay,
|
||
instance: DockerRelayInstance,
|
||
line: string
|
||
): Promise<string> {
|
||
const mux = new SshChannelMultiplexer(
|
||
await openDockerRelayTransport(target, relay, instance.sockPath)
|
||
)
|
||
try {
|
||
await claimOwnerWaitingOutGrace(mux)
|
||
let output = ''
|
||
mux.onNotification((method, params) => {
|
||
if (method === 'pty.data' && params.id === instance.relayPtyId) {
|
||
output += String(params.data)
|
||
}
|
||
})
|
||
await mux.request('pty.attach', { id: instance.relayPtyId })
|
||
mux.notify('pty.data', { id: instance.relayPtyId, data: `${line}\n` })
|
||
const deadline = Date.now() + 15_000
|
||
while (!output.includes(`GOT:${line}`) && Date.now() < deadline) {
|
||
await new Promise((resolve) => setTimeout(resolve, 100))
|
||
}
|
||
return output
|
||
} finally {
|
||
mux.dispose()
|
||
}
|
||
}
|
||
|
||
export async function listDockerRelayPtys(
|
||
target: DockerSshHostTarget,
|
||
relay: DockerReleasedRelay,
|
||
sockPath: string
|
||
): Promise<string[]> {
|
||
const mux = new SshChannelMultiplexer(await openDockerRelayTransport(target, relay, sockPath))
|
||
try {
|
||
const rows = await mux.request('pty.listProcesses', { includeForegroundProcessEvidence: false })
|
||
return Array.isArray(rows)
|
||
? rows.flatMap((row: unknown) => {
|
||
const id: unknown =
|
||
typeof row === 'object' && row !== null && 'id' in row ? row.id : undefined
|
||
return typeof id === 'string' ? [id] : []
|
||
})
|
||
: []
|
||
} finally {
|
||
mux.dispose()
|
||
}
|
||
}
|