mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 19:34:29 +00:00
The generator now emits VM_SCRIPTS_HASH and a monotonic VM_SCRIPTS_STAMP (kept while the scripts are unchanged; max(previous + 1, now) on a change). Provisioning records the stamp in /opt/orca/bin/.scripts-stamp (source builds use the fetched commit's stamp), installEntryPoints writes the Worker's, and ensureEntryPoints probes stamp + marker in one command and skips images whose stamp is newer than the Worker's. Golden refreshes use ensureEntryPoints too, so a lagging Worker cannot bake older scripts into a refreshed golden.
62 lines
3.7 KiB
TypeScript
62 lines
3.7 KiB
TypeScript
// Embeds the image scripts + manifest as strings so the Worker can run a golden build itself.
|
|
// Regenerate after editing image/: `pnpm --filter @orca-cloud/sandbox-control exec tsx image/generate-vm-scripts.ts`
|
|
// (image/golden-image.test.ts fails while the generated file is stale).
|
|
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { VM_SCRIPTS, vmScriptsHash, type BuildInputs } from './golden-image'
|
|
|
|
const here = (p: string): string => fileURLToPath(new URL(p, import.meta.url).href)
|
|
const GENERATED = here('./vm-scripts.generated.ts')
|
|
|
|
export type VmScriptsStamp = { hash: string; stamp: number }
|
|
|
|
/**
|
|
* VM_SCRIPTS_STAMP only moves forward: unchanged scripts keep the previous stamp (so regenerating is
|
|
* deterministic); changed scripts get max(previous + 1, now in seconds). Images record it in
|
|
* /opt/orca/bin/.scripts-stamp, and ensureEntryPoints never installs a lower-stamped set over it.
|
|
*/
|
|
export function nextVmScriptsStamp(previous: VmScriptsStamp | null, hash: string, nowMs: number): number {
|
|
if (previous && previous.hash === hash) {
|
|
return previous.stamp
|
|
}
|
|
return Math.max((previous?.stamp ?? 0) + 1, Math.floor(nowMs / 1000))
|
|
}
|
|
|
|
/** The hash + stamp recorded in a generated module (null when absent: first run or a pre-stamp file). */
|
|
export function parseVmScriptsStamp(generated: string): VmScriptsStamp | null {
|
|
const hash = /^export const VM_SCRIPTS_HASH: string = "([0-9a-f]{8})"$/m.exec(generated)?.[1]
|
|
const stamp = /^export const VM_SCRIPTS_STAMP: number = (\d+)$/m.exec(generated)?.[1]
|
|
return hash && stamp ? { hash, stamp: Number(stamp) } : null
|
|
}
|
|
|
|
export function renderVmScriptsModule(
|
|
previous: VmScriptsStamp | null = existsSync(GENERATED) ? parseVmScriptsStamp(readFileSync(GENERATED, 'utf8')) : null,
|
|
nowMs: number = Date.now()
|
|
): string {
|
|
const manifest = JSON.parse(readFileSync(here('./golden-image.manifest.json'), 'utf8'))
|
|
const scripts = Object.fromEntries(VM_SCRIPTS.map((name) => [name, readFileSync(here(`./vm/${name}`), 'utf8')])) as BuildInputs['vmScripts']
|
|
const hash = vmScriptsHash(scripts)
|
|
return [
|
|
'// GENERATED by image/generate-vm-scripts.ts from image/. Do not edit.',
|
|
"import type { BuildInputs, GoldenImageManifest } from './golden-image'",
|
|
'',
|
|
`export const MANIFEST: GoldenImageManifest = ${JSON.stringify(manifest)}`,
|
|
`export const PROVISION_SCRIPT: string = ${JSON.stringify(readFileSync(here('./provision-golden.sh'), 'utf8'))}`,
|
|
`export const VM_SCRIPT_SOURCES: BuildInputs['vmScripts'] = ${JSON.stringify(scripts)}`,
|
|
// Monotonic version of VM_SCRIPT_SOURCES (see nextVmScriptsStamp); the hash detects a change.
|
|
`export const VM_SCRIPTS_HASH: string = ${JSON.stringify(hash)}`,
|
|
`export const VM_SCRIPTS_STAMP: number = ${nextVmScriptsStamp(previous, hash, nowMs)}`,
|
|
// The Worker's from-source golden build (GOLDEN_IMAGE_SOURCE=git:…): builds orcad, then provisions.
|
|
`export const SOURCE_BUILD_SCRIPT: string = ${JSON.stringify(readFileSync(here('./source-build.sh'), 'utf8'))}`,
|
|
// Run by the control plane per environment build / workspace boot; not installed in the image.
|
|
`export const ENVIRONMENT_BUILD_SCRIPT: string = ${JSON.stringify(readFileSync(here('./env/environment-build.sh'), 'utf8'))}`,
|
|
`export const ENVIRONMENT_DEPS_HOOK: string = ${JSON.stringify(readFileSync(here('./env/post-checkout-deps.sh'), 'utf8'))}`,
|
|
`export const WORKSPACE_PRELUDE_SCRIPT: string = ${JSON.stringify(readFileSync(here('./env/workspace-prelude.sh'), 'utf8'))}`,
|
|
''
|
|
].join('\n')
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).href) {
|
|
writeFileSync(GENERATED, renderVmScriptsModule())
|
|
}
|