service-update.mjs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import { execFileSync } from 'node:child_process'
  2. import { randomUUID, createHash } from 'node:crypto'
  3. import { chmodSync, cpSync, existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
  4. import { homedir } from 'node:os'
  5. import { dirname, join, relative, resolve } from 'node:path'
  6. import { request as httpRequest } from 'node:http'
  7. import { request as httpsRequest } from 'node:https'
  8. import { DatabaseSync } from 'node:sqlite'
  9. import { readJSON, readRuntime, syncDecision, verifyRuntimePackage } from './runtime-release.mjs'
  10. import { backupData, databaseSchema, legacyDataIdle, restoreData } from './service-data.mjs'
  11. import { normalizeReleaseSource } from './release-source.mjs'
  12. const sleep = ms => new Promise(done => setTimeout(done, ms))
  13. export const alive = pid => { try { process.kill(pid, 0); return true } catch (error) { return error.code !== 'ESRCH' } }
  14. export function writeJSON(path, value) {
  15. mkdirSync(dirname(path), { recursive: true, mode: 0o700 })
  16. const temporary = `${path}.${randomUUID()}`
  17. writeFileSync(temporary, JSON.stringify(value, null, 2) + '\n', { mode: 0o600 }); renameSync(temporary, path)
  18. }
  19. export function updateMutex(home) {
  20. mkdirSync(join(home, 'updates'), { recursive: true, mode: 0o700 })
  21. const db = new DatabaseSync(join(home, 'updates', 'mutex.sqlite3'))
  22. chmodSync(join(home, 'updates', 'mutex.sqlite3'), 0o600)
  23. try { db.exec('PRAGMA busy_timeout=0; BEGIN EXCLUSIVE') }
  24. catch { db.close(); throw new Error('已有系统更新正在执行,请稍后重试') }
  25. return () => db.close()
  26. }
  27. export function reserveUpdate(home, jobID) {
  28. const path = join(home, 'updates', 'active.lock')
  29. if (existsSync(path)) {
  30. const lock = readJSON(path)
  31. if (Number.isSafeInteger(lock.pid) && alive(lock.pid)) throw new Error('已有系统更新正在执行,请稍后重试')
  32. // Called only while the common SQLite update mutex is held.
  33. rmSync(path)
  34. }
  35. writeFileSync(path, JSON.stringify({ jobID, pid: process.pid }), { flag: 'wx', mode: 0o600 })
  36. return () => { if (existsSync(path) && readJSON(path).jobID === jobID) rmSync(path) }
  37. }
  38. export function localRequest(url, token, method = 'GET') {
  39. const target = new URL(url)
  40. if (!['http:', 'https:'].includes(target.protocol) || !['127.0.0.1', '[::1]', 'localhost'].includes(target.hostname) || target.username || target.password)
  41. throw new Error('升级只支持已核对的本机服务')
  42. return new Promise((done, reject) => {
  43. const req = (target.protocol === 'https:' ? httpsRequest : httpRequest)(target, {
  44. method, agent: false, headers: token ? { 'x-yaoyao-desktop-token': token } : {},
  45. }, res => {
  46. let data = ''
  47. res.on('data', bytes => { data += bytes; if (data.length > 128 * 1024) req.destroy(new Error('服务响应过大')) })
  48. res.on('end', () => { try { done({ status: res.statusCode, body: JSON.parse(data) }) } catch { reject(new Error('本地服务响应无效')) } })
  49. })
  50. req.on('error', reject); req.setTimeout(3000, () => req.destroy(new Error('本地服务请求超时'))); req.end()
  51. })
  52. }
  53. function command(bin, args, input) {
  54. try { return execFileSync(bin, args, { encoding: 'utf8', input, timeout: 30000, stdio: ['pipe', 'pipe', 'pipe'] }) }
  55. catch { throw new Error(`${bin.split('/').at(-1)} ${args[0]} 未完成`) }
  56. }
  57. export function currentRelease(releaseRoot) {
  58. const path = join(releaseRoot, 'current')
  59. if (!existsSync(path)) return undefined
  60. if (!lstatSync(path).isSymbolicLink()) throw new Error('发布目录 current 必须是符号链接')
  61. return realpathSync(path)
  62. }
  63. export function switchRelease(releaseRoot, target) {
  64. mkdirSync(releaseRoot, { recursive: true, mode: 0o700 })
  65. releaseRoot = realpathSync(releaseRoot)
  66. if (target) target = realpathSync(target)
  67. const path = join(releaseRoot, 'current')
  68. if (existsSync(path) && !lstatSync(path).isSymbolicLink()) throw new Error('拒绝替换非符号链接发布目录')
  69. if (!target) { rmSync(path, { force: true }); return }
  70. const temp = join(releaseRoot, `.current-${randomUUID()}`)
  71. symlinkSync(relative(releaseRoot, target), temp); renameSync(temp, path)
  72. }
  73. export class LaunchAgentService {
  74. constructor({ home, port = 15300, releaseRoot, label = 'com.samien.hermes-yaoyao', plistPath, environment = {} }) {
  75. this.home = realpathSync(home); this.port = port; this.releaseRoot = resolve(releaseRoot)
  76. this.label = label; this.domain = `gui/${process.getuid()}`
  77. this.plistPath = plistPath ?? join(homedir(), 'Library', 'LaunchAgents', `${label}.plist`)
  78. this.environment = environment
  79. }
  80. snapshot() {
  81. if (!existsSync(this.plistPath)) {
  82. // Never install a second service over an unregistered listener or owner.
  83. if (this.listeners().size || this.record()) throw new Error('已有服务未注册为此 App 的本机后台服务,暂不接管')
  84. return undefined
  85. }
  86. const plist = JSON.parse(command('/usr/bin/plutil', ['-convert', 'json', '-o', '-', this.plistPath]))
  87. const env = plist.EnvironmentVariables ?? {}, root = realpathSync(plist.WorkingDirectory)
  88. this.tls = Boolean(env.HERMES_YAOYAO_TLS_CERT)
  89. if (plist.Label !== this.label || realpathSync(env.HERMES_YAOYAO_HOME ?? join(homedir(), '.hermes-yaoyao')) !== this.home || Number(env.HERMES_YAOYAO_PORT ?? 15300) !== this.port)
  90. throw new Error('已有服务的数据目录或端口与 App 不一致,暂不接管')
  91. const args = plist.ProgramArguments
  92. if (!Array.isArray(args) || args.length !== 2 || !['server.mjs', 'dist-server/server/index.js'].some(entry => realpathSync(args[1]) === join(root, entry)))
  93. throw new Error('已有服务的启动入口无法核对')
  94. readRuntime(root)
  95. const pid = this.pid(), listeners = this.listeners()
  96. if (listeners.size && (listeners.size !== 1 || !listeners.has(pid))) throw new Error('端口由另一个进程占用,暂不接管')
  97. if (pid) {
  98. const holders = this.holders(join(this.home, 'workspace.sqlite3'))
  99. if (!holders.has(pid)) throw new Error('无法确认现有服务的数据归属')
  100. }
  101. return { plist, root, wasRunning: Boolean(pid) }
  102. }
  103. pid() {
  104. try { return Number(execFileSync('launchctl', ['print', `${this.domain}/${this.label}`], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).match(/^\s*pid = (\d+)$/m)?.[1]) || undefined }
  105. catch { return undefined }
  106. }
  107. holders(path) {
  108. try { return new Set(execFileSync('/usr/sbin/lsof', ['-t', path], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim().split(/\s+/).map(Number)) }
  109. catch { return new Set() }
  110. }
  111. listeners(established = false) {
  112. const pid = established ? this.pid() : undefined
  113. if (established && !pid) return new Set()
  114. try { return new Set(execFileSync('/usr/sbin/lsof', [...(pid ? ['-a', '-p', String(pid)] : []), '-nP', `-iTCP:${this.port}`, '-sTCP:' + (established ? 'ESTABLISHED' : 'LISTEN'), '-t'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim().split(/\s+/).map(Number)) }
  115. catch { return new Set() }
  116. }
  117. record() {
  118. try {
  119. const value = readJSON(join(this.home, 'service-instance.json'))
  120. return value.protocol === 1 && value.dataKey === createHash('sha256').update(this.home).digest('hex') && Number.isSafeInteger(value.pid) && alive(value.pid) ? value : undefined
  121. } catch { return undefined }
  122. }
  123. url(record = this.record()) {
  124. if (!record?.url) return `${this.tls ? 'https' : 'http'}://127.0.0.1:${this.port}`
  125. const url = new URL(record.url)
  126. if (Number(url.port) !== this.port) throw new Error('本机服务端口发生变化')
  127. if (url.hostname === '0.0.0.0') url.hostname = '127.0.0.1'
  128. return url.origin
  129. }
  130. async quiesce(onProgress, timeout = 60000) {
  131. if (!this.pid()) return
  132. const deadline = Date.now() + timeout
  133. do {
  134. const record = this.record()
  135. if (record?.maintenanceProtocol === 1) {
  136. if (record.pid !== this.pid()) throw new Error('服务进程归属发生变化')
  137. const response = await localRequest(`${this.url(record)}/desktop/service/quiesce`, record.token, 'POST')
  138. if (response.status === 200 && response.body.quiesced === true) return
  139. if (response.status !== 409) throw new Error('服务未接受更新准备请求')
  140. } else if (legacyDataIdle(this.home) && !this.listeners(true).size) {
  141. return
  142. }
  143. onProgress('正在等待聊天和后台任务结束后同步 Web…')
  144. if (Date.now() >= deadline) break
  145. await sleep(1000)
  146. } while (Date.now() < deadline)
  147. throw Object.assign(new Error('当前仍有任务或旧版客户端连接,已延后同步;任务结束并关闭其他客户端后可重试'), { code: 'service_busy' })
  148. }
  149. async resume() {
  150. const record = this.record()
  151. if (record?.maintenanceProtocol === 1) await localRequest(`${this.url(record)}/desktop/service/quiesce`, record.token, 'DELETE').catch(() => {})
  152. }
  153. async stop() {
  154. const pid = this.pid()
  155. try { execFileSync('launchctl', ['print', `${this.domain}/${this.label}`], { stdio: 'ignore' }); command('launchctl', ['bootout', `${this.domain}/${this.label}`]) }
  156. catch (error) { if (this.pid()) throw error }
  157. const deadline = Date.now() + 15000
  158. while (pid && alive(pid) && Date.now() < deadline) await sleep(100)
  159. if ((pid && alive(pid)) || this.listeners().size || this.holders(join(this.home, 'workspace.sqlite3')).size)
  160. throw new Error('后台服务尚未完全停止,操作未完成')
  161. }
  162. writePlist(plist) {
  163. plist.EnvironmentVariables ??= {}
  164. plist.EnvironmentVariables.HERMES_YAOYAO_RELEASE_SOURCE = normalizeReleaseSource(plist.EnvironmentVariables.HERMES_YAOYAO_RELEASE_SOURCE)
  165. mkdirSync(dirname(this.plistPath), { recursive: true })
  166. const xml = command('/usr/bin/plutil', ['-convert', 'xml1', '-o', '-', '--', '-'], JSON.stringify(plist))
  167. const temporary = `${this.plistPath}.${randomUUID()}`
  168. writeFileSync(temporary, xml, { mode: 0o600 }); renameSync(temporary, this.plistPath)
  169. }
  170. async boot() {
  171. let loaded = false
  172. try { execFileSync('launchctl', ['print', `${this.domain}/${this.label}`], { stdio: 'ignore' }); loaded = true } catch { /* Not registered yet. */ }
  173. if (loaded) { command('launchctl', ['kickstart', `${this.domain}/${this.label}`]); return }
  174. const deadline = Date.now() + 20000
  175. for (;;) {
  176. try { command('launchctl', ['bootstrap', this.domain, this.plistPath]); return }
  177. catch (error) { if (Date.now() >= deadline) throw error; await sleep(500) }
  178. }
  179. }
  180. async start(root, previous) {
  181. const stable = join(this.releaseRoot, 'current')
  182. const node = existsSync(join(root, 'node')) ? join(root, 'node') : previous?.plist.ProgramArguments[0] ?? process.execPath
  183. const plist = previous ? structuredClone(previous.plist) : {
  184. Label: this.label, RunAtLoad: true, KeepAlive: true, ThrottleInterval: 10, ProcessType: 'Interactive', Umask: 63,
  185. StandardOutPath: join(this.home, 'service.log'), StandardErrorPath: join(this.home, 'service.log'),
  186. EnvironmentVariables: { PATH: `${dirname(node)}:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`, HERMES_YAOYAO_HOST: '127.0.0.1', ...this.environment },
  187. }
  188. plist.ProgramArguments = [node, join(stable, existsSync(join(root, 'server.mjs')) ? 'server.mjs' : 'dist-server/server/index.js')]
  189. plist.WorkingDirectory = stable
  190. Object.assign(plist.EnvironmentVariables, { NODE_ENV: 'production', HERMES_YAOYAO_HOME: this.home, HERMES_YAOYAO_PORT: String(this.port),
  191. HERMES_YAOYAO_SERVICE_ROOT: stable, HERMES_YAOYAO_RELEASE_ROOT: this.releaseRoot, HERMES_YAOYAO_DESKTOP: '0',
  192. HERMES_YAOYAO_STATIC_DIR: join(stable, existsSync(join(root, 'ui')) ? 'ui' : 'dist') })
  193. delete plist.EnvironmentVariables.ELECTRON_RUN_AS_NODE
  194. this.writePlist(plist); await this.boot()
  195. }
  196. async restore(previous) {
  197. if (!previous) { rmSync(this.plistPath, { force: true }); return }
  198. this.writePlist(previous.plist)
  199. if (previous.wasRunning && !this.pid()) await this.boot()
  200. }
  201. async verify(expected, timeout = 45000) {
  202. const deadline = Date.now() + timeout
  203. do {
  204. try {
  205. const record = this.record(), url = this.url(record)
  206. if (record?.build && record.pid === this.pid()) {
  207. const identity = await localRequest(`${url}/desktop/service`, record.token)
  208. const actual = identity.body
  209. if (identity.status !== 200 || actual.instanceId !== record.instanceId || actual.pid !== record.pid || actual.dataKey !== record.dataKey) throw new Error('实例不匹配')
  210. if (expected && (actual.version !== expected.version || actual.build.commit !== expected.commit || (expected.artifactDigest && actual.build.artifactDigest !== expected.artifactDigest))) throw new Error('运行构建不匹配')
  211. } else if (expected?.hasBuildIdentity || expected?.artifactDigest || !this.pid()) throw new Error('新构建尚未就绪')
  212. const health = await localRequest(`${url}/healthz`)
  213. const status = await localRequest(`${url}/api/status`)
  214. if (health.status === 200 && health.body.ok && status.status === 200 && status.body.server_kind === 'yaoyao-web') return
  215. } catch { /* Startup and launchd recovery can take a moment. */ }
  216. await sleep(300)
  217. } while (Date.now() < deadline)
  218. throw new Error('新服务的构建、健康或接口核验失败')
  219. }
  220. }
  221. export async function recoverTransition({ home, releaseRoot, driver, onProgress = () => {} }) {
  222. const path = join(home, 'updates', 'transition.json')
  223. if (!existsSync(path)) return
  224. const journal = readJSON(path)
  225. if (journal.home !== realpathSync(home) || journal.releaseRoot !== resolve(releaseRoot)) throw new Error('未完成更新的归属不一致')
  226. onProgress('正在恢复上一次未完成的更新…')
  227. if (journal.phase !== 'restored') {
  228. await driver.stop()
  229. if (journal.snapshot) restoreData(home, journal.snapshot)
  230. switchRelease(releaseRoot, journal.previousCurrent)
  231. const success = join(home, 'updates', 'last-success.json')
  232. if (journal.previousSuccess) writeJSON(success, journal.previousSuccess)
  233. else rmSync(success, { force: true })
  234. journal.phase = 'restored'; writeJSON(path, journal)
  235. }
  236. await driver.restore(journal.previous)
  237. if (journal.previous?.wasRunning) await driver.verify(undefined)
  238. rmSync(path)
  239. await driver.resume()
  240. }
  241. export async function transitionService({ home, releaseRoot, driver, finalRoot, onProgress = () => {}, rollback = false, waitForIdleMs }) {
  242. const previous = driver.snapshot(), previousCurrent = currentRelease(releaseRoot)
  243. const path = join(home, 'updates', 'transition.json'), id = randomUUID()
  244. const success = join(home, 'updates', 'last-success.json')
  245. const journal = { id, home: realpathSync(home), releaseRoot: resolve(releaseRoot), previous, previousCurrent, finalRoot,
  246. previousSuccess: existsSync(success) ? readJSON(success) : undefined }
  247. const expected = readRuntime(finalRoot)
  248. try {
  249. if (previous?.wasRunning) await driver.verify(undefined, 5000)
  250. await driver.quiesce(onProgress, waitForIdleMs)
  251. onProgress('正在备份数据并切换 Web 服务…')
  252. // Persist before stopping; recovery never mistakes a partial backup for a complete snapshot.
  253. writeJSON(path, journal)
  254. await driver.stop()
  255. journal.snapshot = join(home, 'updates', 'backups', id)
  256. let beforeSchema
  257. try { beforeSchema = backupData(home, journal.snapshot) }
  258. catch (error) { rmSync(journal.snapshot, { recursive: true, force: true }); throw error }
  259. writeJSON(path, journal)
  260. switchRelease(releaseRoot, finalRoot)
  261. await driver.start(finalRoot, previous)
  262. onProgress('正在核对 Web 构建及服务状态…')
  263. await driver.verify(expected)
  264. const afterSchema = databaseSchema(home)
  265. // The new process rejects application traffic until the durable journal is removed.
  266. const result = { previousCurrentTarget: previousCurrent, previousServiceRoot: previous?.root, previous,
  267. finalRoot, commit: expected.commit, target: expected.release, snapshot: journal.snapshot, beforeSchema, afterSchema }
  268. if (!rollback) writeJSON(join(home, 'updates', 'last-success.json'), result)
  269. rmSync(path)
  270. await driver.resume()
  271. return result
  272. } catch (error) {
  273. if (existsSync(path)) {
  274. try { await recoverTransition({ home, releaseRoot, driver, onProgress }) }
  275. catch (recovery) { throw new Error(`${error.message};恢复未完成:${recovery.message}`) }
  276. } else await driver.resume()
  277. throw error
  278. }
  279. }
  280. export async function synchronizeDesktop({ home, runtimeRoot, releaseRoot, driver, onProgress = () => {} }) {
  281. mkdirSync(home, { recursive: true, mode: 0o700 })
  282. const unlock = updateMutex(home), id = randomUUID()
  283. let release
  284. try {
  285. release = reserveUpdate(home, id)
  286. await recoverTransition({ home, releaseRoot, driver, onProgress })
  287. onProgress('正在核对 App 配套 Web 服务…')
  288. const target = verifyRuntimePackage(runtimeRoot)
  289. const marker = join(home, 'updates', 'desktop-sync.json')
  290. const previous = driver.snapshot(), current = previous ? readRuntime(previous.root) : undefined
  291. if (existsSync(marker) && readJSON(marker).artifactDigest === target.artifactDigest && previous) {
  292. if (!previous.wasRunning) { driver.writePlist(previous.plist); await driver.boot() }
  293. await driver.verify(undefined)
  294. return { action: 'already-synced', current }
  295. }
  296. const decision = syncDecision(target, current)
  297. if (decision === 'unknown') throw new Error('App 与 Web 的构建先后关系无法确定,已保留当前服务;请使用明确的新发布版本')
  298. if (decision === 'same' || decision === 'newer') {
  299. if (!previous.wasRunning) { driver.writePlist(previous.plist); await driver.boot() }
  300. await driver.verify(undefined)
  301. writeJSON(marker, { artifactDigest: target.artifactDigest, action: decision, completedAt: new Date().toISOString() })
  302. return { action: decision, current }
  303. }
  304. const finalRoot = join(releaseRoot, 'releases', `${target.version}-${target.artifactDigest.slice(0, 16)}`)
  305. if (!existsSync(finalRoot)) {
  306. const staging = join(releaseRoot, 'releases', `.staging-${id}`)
  307. mkdirSync(dirname(staging), { recursive: true, mode: 0o700 })
  308. try { cpSync(runtimeRoot, staging, { recursive: true }); verifyRuntimePackage(staging); chmodSync(join(staging, 'node'), 0o755); renameSync(staging, finalRoot) }
  309. finally { rmSync(staging, { recursive: true, force: true }) }
  310. }
  311. verifyRuntimePackage(finalRoot)
  312. await transitionService({ home, releaseRoot, driver, finalRoot, onProgress, waitForIdleMs: 0 })
  313. writeJSON(marker, { artifactDigest: target.artifactDigest, action: decision, completedAt: new Date().toISOString() })
  314. return { action: decision, current: target }
  315. } finally { release?.(); unlock() }
  316. }
  317. /** Explicit native Stop command. It never changes the release or user data,
  318. * and unloads the verified LaunchAgent so KeepAlive cannot restart it. */
  319. export async function stopDesktopService({home,driver,onProgress=()=>{}}) {
  320. const unlock=updateMutex(home)
  321. let release
  322. try {
  323. release=reserveUpdate(home,randomUUID())
  324. if(existsSync(join(home,'updates','transition.json')))throw new Error('系统更新尚未完成,请完成更新后再停止后台服务')
  325. const previous=driver.snapshot()
  326. if(!previous)return {action:'stopped'}
  327. if(previous.wasRunning)await driver.verify(undefined,5000)
  328. onProgress('正在停止后台任务与虚拟机…')
  329. await driver.stop()
  330. onProgress('后台服务已停止')
  331. return {action:'stopped'}
  332. } finally {release?.();unlock()}
  333. }