service-update.mjs 18 KB

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