Showing the changes to apps/server/src/cloud/selfUpdate.ts (1 of 12 files). Show all
Unified · Split
| @@ -13,6 +13,7 @@ import { · 12 unmodified lines | ||
| 13 | 13 | HostProcessEnvironment, |
| 14 | 14 | HostProcessExecutablePath, |
| 15 | 15 | HostProcessPlatform, |
| 16 | + HostProcessUserId, | |
| 16 | 17 | } from "@t3tools/shared/hostProcess"; |
| 17 | 18 | import * as NodeChildProcess from "node:child_process"; |
| 18 | 19 | import * as Context from "effect/Context"; |
| @@ -24,11 +25,13 @@ import * as Path from "effect/Path"; · 5 unmodified lines | ||
| 24 | 25 | import * as Ref from "effect/Ref"; |
| 25 | 26 | |
| 26 | 27 | import * as ServerConfig from "../config.ts"; |
| 27 | −import { writeFileStringAtomically } from "../atomicWrite.ts"; | |
| 28 | +import { writeFileStringAtomically, writeSymbolicLinkAtomically } from "../atomicWrite.ts"; | |
| 28 | 29 | import * as ProcessRunner from "../processRunner.ts"; |
| 29 | 30 | import { |
| 31 | + BOOT_SERVICE_LAUNCH_AGENT_LABEL, | |
| 30 | 32 | BOOT_SERVICE_UNIT_ENV, |
| 31 | 33 | BOOT_SERVICE_UNIT_FILE, |
| 34 | + launchAgentPaths, | |
| 32 | 35 | quoteSystemdValue, |
| 33 | 36 | renderBootServiceUnit, |
| 34 | 37 | } from "./bootService.ts"; |
| @@ -82,11 +85,11 @@ export function isPublishedCliEntry(entryPath: string): boolean { · 47 unmodified lines | ||
| 82 | 85 | * The update path this process can offer, or null when only a manual |
| 83 | 86 | * relaunch works. "desktop-managed" — the T3 Code desktop app spawned this |
| 84 | 87 | * backend and owns its version; only updating the app updates it. |
| 85 | − * "boot-service" — this is the systemd-supervised process from | |
| 86 | − * bootService.ts: rewrite the unit and let systemd swap it. "respawn" — a | |
| 87 | − * foreground POSIX process running a published artifact: replace it with a | |
| 88 | − * detached child. Windows foreground runs are unsupported for now (no | |
| 89 | − * equivalent of the detach-and-exec handoff below). | |
| 88 | + * "boot-service" — this is the systemd- or launchd-supervised process from | |
| 89 | + * bootService.ts: switch the service to the verified runtime and restart it. | |
| 90 | + * "respawn" — a foreground POSIX process running a published artifact: | |
| 91 | + * replace it with a detached child. Windows foreground runs are unsupported | |
| 92 | + * for now (no equivalent of the detach-and-exec handoff below). | |
| 90 | 93 | */ |
| 91 | 94 | export const resolveServerSelfUpdateCapability = Effect.fn( |
| 92 | 95 | "cloud.server_self_update.resolve_capability", |
| @@ -101,6 +104,7 @@ export const resolveServerSelfUpdateCapability = Effect.fn( · 8 unmodified lines | ||
| 101 | 104 | const platform = yield* HostProcessPlatform; |
| 102 | 105 | const env = yield* HostProcessEnvironment; |
| 103 | 106 | const hostArguments = yield* HostProcessArguments; |
| 107 | + const userId = yield* HostProcessUserId; | |
| 104 | 108 | const fs = yield* FileSystem.FileSystem; |
| 105 | 109 | const path = yield* Path.Path; |
| 106 | 110 | |
| @@ -110,6 +114,26 @@ export const resolveServerSelfUpdateCapability = Effect.fn( · 3 unmodified lines | ||
| 110 | 114 | } |
| 111 | 115 | |
| 112 | 116 | const homeDir = env.HOME ?? ""; |
| 117 | + if (platform === "darwin" && homeDir !== "" && userId !== null) { | |
| 118 | + const launchAgent = launchAgentPaths(path, homeDir, env.T3CODE_HOME ?? "", userId); | |
| 119 | + const unitExists = yield* fs | |
| 120 | + .exists(launchAgent.unitPath) | |
| 121 | + .pipe(Effect.orElseSucceed(() => false)); | |
| 122 | + if ( | |
| 123 | + unitExists && | |
| 124 | + entryPath === launchAgent.activeEntryPath && | |
| 125 | + env[BOOT_SERVICE_UNIT_ENV] === BOOT_SERVICE_LAUNCH_AGENT_LABEL | |
| 126 | + ) { | |
| 127 | + return "boot-service" as const; | |
| 128 | + } | |
| 129 | + | |
| 130 | + // A marked LaunchAgent process with an unexpected definition must not | |
| 131 | + // detach a second server outside its supervisor. | |
| 132 | + if (env[BOOT_SERVICE_UNIT_ENV] === BOOT_SERVICE_LAUNCH_AGENT_LABEL) { | |
| 133 | + return null; | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 113 | 137 | if (platform === "linux" && homeDir !== "") { |
| 114 | 138 | const unitPath = path.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE); |
| 115 | 139 | const unitReferencesEntry = yield* fs.readFileString(unitPath).pipe( |
| @@ -159,6 +183,8 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option · 43 unmodified lines | ||
| 159 | 183 | const path = yield* Path.Path; |
| 160 | 184 | const runner = yield* ProcessRunner.ProcessRunner; |
| 161 | 185 | const env = yield* HostProcessEnvironment; |
| 186 | + const platform = yield* HostProcessPlatform; | |
| 187 | + const userId = yield* HostProcessUserId; | |
| 162 | 188 | const hostExecPath = yield* HostProcessExecutablePath; |
| 163 | 189 | const hostArguments = yield* HostProcessArguments; |
| 164 | 190 | const capability: ServerSelfUpdateCapability | null = yield* resolveServerSelfUpdateCapability({ |
| @@ -224,6 +250,11 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option · 59 unmodified lines | ||
| 224 | 250 | Effect.provideService(FileSystem.FileSystem, fs), |
| 225 | 251 | Effect.provideService(Path.Path, path), |
| 226 | 252 | ); |
| 253 | + const writeRuntimeLinkAtomically = (linkPath: string, targetPath: string) => | |
| 254 | + writeSymbolicLinkAtomically({ linkPath, targetPath }).pipe( | |
| 255 | + Effect.provideService(FileSystem.FileSystem, fs), | |
| 256 | + Effect.provideService(Path.Path, path), | |
| 257 | + ); | |
| 227 | 258 | |
| 228 | 259 | const update: ServerSelfUpdate["Service"]["update"] = Effect.fn( |
| 229 | 260 | "cloud.server_self_update.update", |
| @@ -297,7 +328,58 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option · 67 unmodified lines | ||
| 297 | 328 | ); |
| 298 | 329 | } |
| 299 | 330 | |
| 300 | − if (activeMethod === "boot-service") { | |
| 331 | + if (activeMethod === "boot-service" && platform === "darwin" && userId !== null) { | |
| 332 | + const homeDir = env.HOME ?? ""; | |
| 333 | + const launchAgent = launchAgentPaths(path, homeDir, serverConfig.baseDir, userId); | |
| 334 | + const previousRuntime = yield* fs | |
| 335 | + .readLink(launchAgent.activeRuntimeLinkPath) | |
| 336 | + .pipe( | |
| 337 | + Effect.mapError((cause) => | |
| 338 | + failWith("Could not read the active launchd runtime.", cause), | |
| 339 | + ), | |
| 340 | + ); | |
| 341 | + yield* writeRuntimeLinkAtomically( | |
| 342 | + launchAgent.activeRuntimeLinkPath, | |
| 343 | + runtimePaths.versionDir, | |
| 344 | + ).pipe( | |
| 345 | + Effect.mapError((cause) => | |
| 346 | + failWith("Could not activate the requested launchd runtime.", cause), | |
| 347 | + ), | |
| 348 | + ); | |
| 349 | + yield* Effect.logInfo("Server self-update installed; restarting LaunchAgent.", { | |
| 350 | + targetVersion, | |
| 351 | + }); | |
| 352 | + | |
| 353 | + yield* Effect.gen(function* () { | |
| 354 | + const kickstart = yield* runner | |
| 355 | + .run({ | |
| 356 | + command: "/bin/launchctl", | |
| 357 | + args: ["kickstart", "-k", launchAgent.serviceTarget], | |
| 358 | + }) | |
| 359 | + .pipe( | |
| 360 | + Effect.mapError((cause) => | |
| 361 | + failWith("Could not restart the T3 Code LaunchAgent.", cause), | |
| 362 | + ), | |
| 363 | + ); | |
| 364 | + if (kickstart.code !== 0) { | |
| 365 | + return yield* failWith( | |
| 366 | + `Restarting the T3 Code LaunchAgent failed (exit code ${String(kickstart.code)}).`, | |
| 367 | + ); | |
| 368 | + } | |
| 369 | + }).pipe( | |
| 370 | + Effect.catch((restartError) => | |
| 371 | + writeRuntimeLinkAtomically(launchAgent.activeRuntimeLinkPath, previousRuntime).pipe( | |
| 372 | + Effect.mapError((rollbackCause) => | |
| 373 | + failWith("Could not restore the previous launchd runtime.", { | |
| 374 | + restartError, | |
| 375 | + rollbackCause, | |
| 376 | + }), | |
| 377 | + ), | |
| 378 | + Effect.andThen(Effect.fail(restartError)), | |
| 379 | + ), | |
| 380 | + ), | |
| 381 | + ); | |
| 382 | + } else if (activeMethod === "boot-service") { | |
| 301 | 383 | const homeDir = env.HOME ?? ""; |
| 302 | 384 | const unitPath = path.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE); |
| 303 | 385 | const previousUnit = yield* fs |