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 | HostProcessEnvironment, | 13 | HostProcessEnvironment, |
| 14 | HostProcessExecutablePath, | 14 | HostProcessExecutablePath, |
| 15 | HostProcessPlatform, | 15 | HostProcessPlatform, |
| 16 | HostProcessUserId, | ||
| 16 | } from "@t3tools/shared/hostProcess"; | 17 | } from "@t3tools/shared/hostProcess"; |
| 17 | import * as NodeChildProcess from "node:child_process"; | 18 | import * as NodeChildProcess from "node:child_process"; |
| 18 | import * as Context from "effect/Context"; | 19 | import * as Context from "effect/Context"; |
| @@ -24,11 +25,13 @@ import * as Path from "effect/Path"; · 5 unmodified lines | |||
| 24 | import * as Ref from "effect/Ref"; | 25 | import * as Ref from "effect/Ref"; |
| 25 | 26 | ||
| 26 | import * as ServerConfig from "../config.ts"; | 27 | import * as ServerConfig from "../config.ts"; |
| 27 | import { writeFileStringAtomically } from "../atomicWrite.ts"; | 28 | import { writeFileStringAtomically, writeSymbolicLinkAtomically } from "../atomicWrite.ts"; |
| 28 | import * as ProcessRunner from "../processRunner.ts"; | 29 | import * as ProcessRunner from "../processRunner.ts"; |
| 29 | import { | 30 | import { |
| 31 | BOOT_SERVICE_LAUNCH_AGENT_LABEL, | ||
| 30 | BOOT_SERVICE_UNIT_ENV, | 32 | BOOT_SERVICE_UNIT_ENV, |
| 31 | BOOT_SERVICE_UNIT_FILE, | 33 | BOOT_SERVICE_UNIT_FILE, |
| 34 | launchAgentPaths, | ||
| 32 | quoteSystemdValue, | 35 | quoteSystemdValue, |
| 33 | renderBootServiceUnit, | 36 | renderBootServiceUnit, |
| 34 | } from "./bootService.ts"; | 37 | } from "./bootService.ts"; |
| @@ -82,11 +85,11 @@ export function isPublishedCliEntry(entryPath: string): boolean { · 47 unmodified lines | |||
| 82 | * The update path this process can offer, or null when only a manual | 85 | * The update path this process can offer, or null when only a manual |
| 83 | * relaunch works. "desktop-managed" — the T3 Code desktop app spawned this | 86 | * relaunch works. "desktop-managed" — the T3 Code desktop app spawned this |
| 84 | * backend and owns its version; only updating the app updates it. | 87 | * backend and owns its version; only updating the app updates it. |
| 85 | * "boot-service" — this is the systemd-supervised process from | 88 | * "boot-service" — this is the systemd- or launchd-supervised process from |
| 86 | * bootService.ts: rewrite the unit and let systemd swap it. "respawn" — a | 89 | * bootService.ts: switch the service to the verified runtime and restart it. |
| 87 | * foreground POSIX process running a published artifact: replace it with a | 90 | * "respawn" — a foreground POSIX process running a published artifact: |
| 88 | * detached child. Windows foreground runs are unsupported for now (no | 91 | * replace it with a detached child. Windows foreground runs are unsupported |
| 89 | * equivalent of the detach-and-exec handoff below). | 92 | * for now (no equivalent of the detach-and-exec handoff below). |
| 90 | */ | 93 | */ |
| 91 | export const resolveServerSelfUpdateCapability = Effect.fn( | 94 | export const resolveServerSelfUpdateCapability = Effect.fn( |
| 92 | "cloud.server_self_update.resolve_capability", | 95 | "cloud.server_self_update.resolve_capability", |
| @@ -101,6 +104,7 @@ export const resolveServerSelfUpdateCapability = Effect.fn( · 8 unmodified lines | |||
| 101 | const platform = yield* HostProcessPlatform; | 104 | const platform = yield* HostProcessPlatform; |
| 102 | const env = yield* HostProcessEnvironment; | 105 | const env = yield* HostProcessEnvironment; |
| 103 | const hostArguments = yield* HostProcessArguments; | 106 | const hostArguments = yield* HostProcessArguments; |
| 107 | const userId = yield* HostProcessUserId; | ||
| 104 | const fs = yield* FileSystem.FileSystem; | 108 | const fs = yield* FileSystem.FileSystem; |
| 105 | const path = yield* Path.Path; | 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 | const homeDir = env.HOME ?? ""; | 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 | if (platform === "linux" && homeDir !== "") { | 137 | if (platform === "linux" && homeDir !== "") { |
| 114 | const unitPath = path.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE); | 138 | const unitPath = path.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE); |
| 115 | const unitReferencesEntry = yield* fs.readFileString(unitPath).pipe( | 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 | const path = yield* Path.Path; | 183 | const path = yield* Path.Path; |
| 160 | const runner = yield* ProcessRunner.ProcessRunner; | 184 | const runner = yield* ProcessRunner.ProcessRunner; |
| 161 | const env = yield* HostProcessEnvironment; | 185 | const env = yield* HostProcessEnvironment; |
| 186 | const platform = yield* HostProcessPlatform; | ||
| 187 | const userId = yield* HostProcessUserId; | ||
| 162 | const hostExecPath = yield* HostProcessExecutablePath; | 188 | const hostExecPath = yield* HostProcessExecutablePath; |
| 163 | const hostArguments = yield* HostProcessArguments; | 189 | const hostArguments = yield* HostProcessArguments; |
| 164 | const capability: ServerSelfUpdateCapability | null = yield* resolveServerSelfUpdateCapability({ | 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 | Effect.provideService(FileSystem.FileSystem, fs), | 250 | Effect.provideService(FileSystem.FileSystem, fs), |
| 225 | Effect.provideService(Path.Path, path), | 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 | const update: ServerSelfUpdate["Service"]["update"] = Effect.fn( | 259 | const update: ServerSelfUpdate["Service"]["update"] = Effect.fn( |
| 229 | "cloud.server_self_update.update", | 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 | const homeDir = env.HOME ?? ""; | 383 | const homeDir = env.HOME ?? ""; |
| 302 | const unitPath = path.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE); | 384 | const unitPath = path.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE); |
| 303 | const previousUnit = yield* fs | 385 | const previousUnit = yield* fs |