prepare-paas-runtime.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bun
  2. import { cp, mkdir, rm, chmod, stat, rename } from "node:fs/promises"
  3. import { join, resolve, sep } from "node:path"
  4. import { $ } from "bun"
  5. import { runtimeFiles, validateRuntimeBundle } from "./runtime-bundle"
  6. // Build the PaaS standalone directory on the target OS/architecture first.
  7. // A host Python installation is never copied into a user's installer.
  8. const source = process.env.LAMBDA_AGENT_RUNTIME_DIR
  9. if (!source) throw new Error("请设置 LAMBDA_AGENT_RUNTIME_DIR,指向包含 agentpaas 原生可执行文件的发行目录。")
  10. const name = process.platform === "win32" ? "agentpaas.exe" : "agentpaas"
  11. const binary = join(resolve(source), name)
  12. if (!(await stat(binary)).isFile()) throw new Error("运行组件缺少 agentpaas 可执行文件。")
  13. const info = await $`${binary} desktop-info`.quiet().json()
  14. if (info.desktop_protocol !== 1) throw new Error("运行组件不支持当前 Desktop 配置协议。")
  15. if (info.platform !== process.platform || info.arch !== process.arch)
  16. throw new Error("运行组件架构与当前构建平台不匹配。")
  17. const target = resolve("resources/agentpaas-runtime")
  18. if (target === resolve(source) || target.startsWith(resolve(source) + sep) || resolve(source).startsWith(target + sep))
  19. throw new Error("运行组件源目录与目标目录必须分开,不能互相包含。")
  20. const staging = `${target}.staging-${crypto.randomUUID()}`
  21. const previous = `${target}.previous-${crypto.randomUUID()}`
  22. await mkdir(staging, { recursive: true })
  23. try {
  24. await cp(resolve(source), staging, { recursive: true, dereference: true })
  25. await chmod(join(staging, name), 0o755)
  26. await Bun.write(
  27. join(staging, "desktop-runtime.json"),
  28. JSON.stringify(
  29. {
  30. protocol: 1,
  31. platform: process.platform,
  32. arch: process.arch,
  33. files: await runtimeFiles(staging),
  34. },
  35. null,
  36. 2,
  37. ),
  38. )
  39. await validateRuntimeBundle(staging)
  40. const existed = await stat(target).then(
  41. () => true,
  42. () => false,
  43. )
  44. if (existed) await rename(target, previous)
  45. try {
  46. await rename(staging, target)
  47. } catch (error) {
  48. if (existed) await rename(previous, target)
  49. throw error
  50. }
  51. await rm(previous, { recursive: true, force: true })
  52. } finally {
  53. await rm(staging, { recursive: true, force: true })
  54. }
  55. console.log("Desktop 本地运行组件已准备。")