122 lines
5.1 KiB
JavaScript
122 lines
5.1 KiB
JavaScript
#!/usr/bin/env node
|
|
import { createHash } from 'node:crypto'
|
|
import { spawnSync } from 'node:child_process'
|
|
import { cp, lstat, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { dirname, join, resolve } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
const platformRoot = resolve(here, '../..')
|
|
const workspaceRoot = resolve(platformRoot, '..')
|
|
const engineRoot = resolve(workspaceRoot, 'NODEDC_ENGINE_INFRA')
|
|
const artifactRoot = resolve(process.env.NODEDC_DEPLOY_ARTIFACT_DIR || join(workspaceRoot, 'deploy-artifacts'))
|
|
const [patchId = '', ...extra] = process.argv.slice(2)
|
|
const storeRelativePath = 'nodedc-source/server/engineAgents/store.js'
|
|
const predecessorSha256 = '52daa43499d6d9a97fe7ffa891edb9212b7791e733f91dd3ca686d42739b7e9a'
|
|
const targetSha256 = '2e62654c2dc12905efcc83a9dff45a818dd7b47924a10600160835c4416540e9'
|
|
const previouslyIssuedPatchIds = new Set([
|
|
'engine-agent-full-grant-migration-20260717-001',
|
|
])
|
|
|
|
if (extra.length || !/^engine-agent-full-grant-migration-\d{8}-\d{3}$/.test(patchId)) {
|
|
throw new Error('usage: build-engine-agent-full-grant-migration-artifact.mjs <fresh-patch-id>')
|
|
}
|
|
if (previouslyIssuedPatchIds.has(patchId)) {
|
|
throw new Error('engine_agent_full_grant_migration_patch_id_already_issued')
|
|
}
|
|
|
|
const source = join(engineRoot, storeRelativePath)
|
|
const sourceInfo = await lstat(source)
|
|
if (sourceInfo.isSymbolicLink() || !sourceInfo.isFile()) throw new Error('engine_agent_store_source_unsafe')
|
|
const sourceBytes = await readFile(source)
|
|
if (digest(sourceBytes) !== targetSha256) throw new Error('engine_agent_store_target_sha256_mismatch')
|
|
const sourceText = sourceBytes.toString('utf8')
|
|
for (const required of [
|
|
'const STORE_VERSION = 2',
|
|
"export const ENGINE_AGENT_FULL_DEVELOPER_PROFILE = 'full-developer'",
|
|
"export const ENGINE_AGENT_CUSTOM_PROFILE = 'custom'",
|
|
'const LEGACY_FULL_DEVELOPER_SCOPES = Object.freeze([',
|
|
"'engine:l2:data-product-publish-grant:plan'",
|
|
"'engine:l2:data-product-publish-grant:write'",
|
|
'const migrateLegacyFullDeveloper = sourceVersion === 1',
|
|
'LEGACY_FULL_DEVELOPER_SCOPES.every((scope) => scopes.includes(scope))',
|
|
'profile === ENGINE_AGENT_FULL_DEVELOPER_PROFILE ? [...ENGINE_AGENT_SCOPES] : scopes',
|
|
"throw new Error('engine_agent_store_version_unsupported')",
|
|
]) {
|
|
if (!sourceText.includes(required)) throw new Error(`engine_agent_store_contract_missing:${required}`)
|
|
}
|
|
if (/gelios|robot2b/i.test(sourceText)) throw new Error('engine_agent_store_provider_logic_forbidden')
|
|
run('node', ['--check', source])
|
|
|
|
await mkdir(artifactRoot, { recursive: true })
|
|
const artifact = join(artifactRoot, `nodedc-${patchId}.tgz`)
|
|
await assertFresh(artifact)
|
|
const stage = await mkdtemp(join(tmpdir(), 'nodedc-engine-agent-grant-migration-'))
|
|
try {
|
|
const destination = join(stage, 'payload', storeRelativePath)
|
|
await mkdir(dirname(destination), { recursive: true })
|
|
await cp(source, destination, { force: false, verbatimSymlinks: true })
|
|
await writeFile(
|
|
join(stage, 'manifest.env'),
|
|
`id=${patchId}\ncomponent=engine\ntype=app-overlay\n`,
|
|
'utf8',
|
|
)
|
|
await writeFile(join(stage, 'files.txt'), `${storeRelativePath}\n`, 'utf8')
|
|
run('python3', ['-c', canonicalTarScript(), artifact, stage])
|
|
const artifactBytes = await readFile(artifact)
|
|
console.log(JSON.stringify({
|
|
ok: true,
|
|
patchId,
|
|
artifact,
|
|
sha256: digest(artifactBytes),
|
|
entries: [storeRelativePath],
|
|
predecessorSha256,
|
|
targetSha256,
|
|
services: ['nodedc-backend'],
|
|
excluded: ['n8n', 'app', 'databases', 'credentials', 'runtime-data'],
|
|
}, null, 2))
|
|
} finally {
|
|
await rm(stage, { recursive: true, force: true })
|
|
}
|
|
|
|
async function assertFresh(target) {
|
|
try {
|
|
await lstat(target)
|
|
} catch (error) {
|
|
if (error?.code === 'ENOENT') return
|
|
throw error
|
|
}
|
|
throw new Error('engine_agent_full_grant_migration_artifact_already_exists')
|
|
}
|
|
|
|
function canonicalTarScript() {
|
|
return [
|
|
'import gzip, io, pathlib, sys, tarfile',
|
|
'root=pathlib.Path(sys.argv[2])',
|
|
"with open(sys.argv[1], 'wb') as out:",
|
|
" with gzip.GzipFile(filename='', mode='wb', fileobj=out, compresslevel=9, mtime=0) as gz:",
|
|
" with tarfile.open(fileobj=gz, mode='w', format=tarfile.PAX_FORMAT) as tar:",
|
|
" for top in ('manifest.env','files.txt','payload'):",
|
|
" p=root/top; paths=[p] + (sorted(p.rglob('*')) if p.is_dir() else [])",
|
|
' for x in paths:',
|
|
" info=tar.gettarinfo(str(x), arcname=x.relative_to(root).as_posix())",
|
|
" info.uid=info.gid=0; info.uname=info.gname='root'; info.mtime=0; info.mode=0o755 if info.isdir() else 0o644",
|
|
" with (open(x,'rb') if info.isfile() else io.BytesIO()) as src: tar.addfile(info, src if info.isfile() else None)",
|
|
].join('\n')
|
|
}
|
|
|
|
function run(command, args) {
|
|
const result = spawnSync(command, args, {
|
|
encoding: 'utf8',
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
maxBuffer: 16 * 1024 * 1024,
|
|
})
|
|
if (result.status !== 0) throw new Error(`${command}_failed:${result.stderr || result.stdout}`)
|
|
return result
|
|
}
|
|
|
|
function digest(bytes) {
|
|
return createHash('sha256').update(bytes).digest('hex')
|
|
}
|