History

feat(connect): keep macOS hosts reachable after reboot

Theo Browne committed 3 months ago

Commit ec3c5f61 parent 90f391312 files · +690 −91Browse files

Showing the changes to apps/server/src/cloud/selfUpdate.test.ts (1 of 12 files). Show all

Unified · Split

apps/server/src/cloud/selfUpdate.test.ts+133−16
@@ -13,13 +13,17 @@ import { · 12 unmodified lines
1313 HostProcessEnvironment,
1414 HostProcessExecutablePath,
1515 HostProcessPlatform,
16+ HostProcessUserId,
1617 } from "@t3tools/shared/hostProcess";
1718
1819 import * as ServerConfig from "../config.ts";
1920 import * as ProcessRunner from "../processRunner.ts";
2021 import {
22+ BOOT_SERVICE_LAUNCH_AGENT_LABEL,
2123 BOOT_SERVICE_UNIT_ENV,
2224 BOOT_SERVICE_UNIT_FILE,
25+ launchAgentPaths,
26+ renderBootServiceLaunchAgent,
2327 renderBootServiceUnit,
2428 } from "./bootService.ts";
2529 import * as SelfUpdate from "./selfUpdate.ts";
@@ -76,6 +80,7 @@ const provideHostRefs = (input: { · 50 unmodified lines
7680 Layer.succeed(HostProcessEnvironment, input.env),
7781 Layer.succeed(HostProcessExecutablePath, NODE_PATH),
7882 Layer.succeed(HostProcessArguments, [NODE_PATH, input.entryPath, "serve"]),
83+ Layer.succeed(HostProcessUserId, 501),
7984 ),
8085 );
8186
@@ -192,6 +197,41 @@ it.layer(NodeServices.layer)("resolveServerSelfUpdateCapability", (it) => { · 110 unmodified lines
192197 }),
193198 );
194199
200+ it.effect("reports boot-service for the T3-managed macOS LaunchAgent", () =>
201+ Effect.gen(function* () {
202+ const { fs, home, path } = yield* makeHome();
203+ const baseDir = path.join(home, ".t3");
204+ const launchAgent = launchAgentPaths(path, home, baseDir, 501);
205+ yield* fs.makeDirectory(path.dirname(launchAgent.unitPath), { recursive: true });
206+ yield* fs.writeFileString(
207+ launchAgent.unitPath,
208+ renderBootServiceLaunchAgent({
209+ nodePath: NODE_PATH,
210+ t3EntryPath: launchAgent.activeEntryPath,
211+ baseDir,
212+ logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"),
213+ unitPath: launchAgent.unitPath,
214+ }),
215+ );
216+
217+ const method = yield* SelfUpdate.resolveServerSelfUpdateCapability({
218+ desktopManaged: false,
219+ }).pipe(
220+ provideHostRefs({
221+ platform: "darwin",
222+ env: {
223+ HOME: home,
224+ T3CODE_HOME: baseDir,
225+ [BOOT_SERVICE_UNIT_ENV]: BOOT_SERVICE_LAUNCH_AGENT_LABEL,
226+ },
227+ entryPath: launchAgent.activeEntryPath,
228+ }),
229+ );
230+
231+ assert.equal(method, "boot-service");
232+ }),
233+ );
234+
195235 it.effect("reports desktop-managed for desktop-supervised backends", () =>
196236 Effect.gen(function* () {
197237 const { home, path } = yield* makeHome();
@@ -262,11 +302,21 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 64 unmodified lines
262302 const path = yield* Path.Path;
263303 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" });
264304 const baseDir = path.join(home, ".t3");
305+ const platform = options?.platform ?? "linux";
306+ const launchdBootService = options?.bootService === true && platform === "darwin";
307+ const launchAgent = launchAgentPaths(path, home, baseDir, 501);
265308 const entryPath =
266309 options?.entryPath ??
267− path.join(home, ".t3/runtime/versions/0.0.28/node_modules/t3/dist/bin.mjs");
268− const env: NodeJS.ProcessEnv =
269− options?.bootService === true
310+ (launchdBootService
311+ ? launchAgent.activeEntryPath
312+ : path.join(home, ".t3/runtime/versions/0.0.28/node_modules/t3/dist/bin.mjs"));
313+ const env: NodeJS.ProcessEnv = launchdBootService
314+ ? {
315+ HOME: home,
316+ T3CODE_HOME: baseDir,
317+ [BOOT_SERVICE_UNIT_ENV]: BOOT_SERVICE_LAUNCH_AGENT_LABEL,
318+ }
319+ : options?.bootService === true
270320 ? {
271321 HOME: home,
272322 INVOCATION_ID: "abc123",
@@ -274,18 +324,37 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 1 unmodified line
274324 }
275325 : { HOME: home };
276326 if (options?.bootService === true) {
277− const unitDir = path.join(home, ".config", "systemd", "user");
278− yield* fs.makeDirectory(unitDir, { recursive: true });
279− yield* fs.writeFileString(
280− path.join(unitDir, "t3code.service"),
281− renderBootServiceUnit({
282− nodePath: NODE_PATH,
283− t3EntryPath: entryPath,
284− baseDir,
285− logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"),
286− unitPath: path.join(unitDir, "t3code.service"),
287− }),
288− );
327+ if (launchdBootService) {
328+ const previousRuntime = path.join(baseDir, "runtime", "versions", "0.0.28");
329+ yield* fs.makeDirectory(path.dirname(launchAgent.unitPath), { recursive: true });
330+ yield* fs.makeDirectory(path.dirname(launchAgent.activeRuntimeLinkPath), {
331+ recursive: true,
332+ });
333+ yield* fs.symlink(previousRuntime, launchAgent.activeRuntimeLinkPath);
334+ yield* fs.writeFileString(
335+ launchAgent.unitPath,
336+ renderBootServiceLaunchAgent({
337+ nodePath: NODE_PATH,
338+ t3EntryPath: entryPath,
339+ baseDir,
340+ logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"),
341+ unitPath: launchAgent.unitPath,
342+ }),
343+ );
344+ } else {
345+ const unitDir = path.join(home, ".config", "systemd", "user");
346+ yield* fs.makeDirectory(unitDir, { recursive: true });
347+ yield* fs.writeFileString(
348+ path.join(unitDir, "t3code.service"),
349+ renderBootServiceUnit({
350+ nodePath: NODE_PATH,
351+ t3EntryPath: entryPath,
352+ baseDir,
353+ logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"),
354+ unitPath: path.join(unitDir, "t3code.service"),
355+ }),
356+ );
357+ }
289358 }
290359
291360 const commands: Array<RecordedCommand> = [];
@@ -333,7 +402,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 41 unmodified lines
333402 configLayer,
334403 ),
335404 ),
336− provideHostRefs({ platform: options?.platform ?? "linux", env, entryPath }),
405+ provideHostRefs({ platform, env, entryPath }),
337406 );
338407 return {
339408 fs,
@@ -341,6 +410,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 1 unmodified line
341410 home,
342411 baseDir,
343412 entryPath,
413+ launchAgent,
344414 commands,
345415 spawns,
346416 exitCount: () => exited,
@@ -520,6 +590,53 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 173 unmodified lines
520590 }).pipe(Effect.provide(TestClock.layer())),
521591 );
522592
593+ it.effect("switches the macOS LaunchAgent runtime and restarts it", () =>
594+ Effect.gen(function* () {
595+ const context = yield* makeContext({
596+ platform: "darwin",
597+ bootService: true,
598+ });
599+
600+ const result = yield* context.service.update({ targetVersion: "0.0.29" });
601+
602+ assert.deepEqual(result, { targetVersion: "0.0.29", method: "boot-service" });
603+ assert.equal(
604+ yield* context.fs.readLink(context.launchAgent.activeRuntimeLinkPath),
605+ context.path.join(context.baseDir, "runtime", "versions", "0.0.29"),
606+ );
607+ assert.deepEqual(
608+ context.commands.map((entry) => entry.command),
609+ ["npm", NODE_PATH, "/bin/launchctl"],
610+ );
611+ assert.deepEqual(context.commands[2], {
612+ command: "/bin/launchctl",
613+ args: ["kickstart", "-k", "gui/501/com.t3tools.t3code.server"],
614+ });
615+ assert.lengthOf(context.spawns, 0);
616+ assert.equal(context.exitCount(), 0);
617+ }).pipe(Effect.provide(TestClock.layer())),
618+ );
619+
620+ it.effect("restores the previous macOS runtime when launchd rejects the restart", () =>
621+ Effect.gen(function* () {
622+ const context = yield* makeContext({
623+ platform: "darwin",
624+ bootService: true,
625+ failWhen: (command) => command === "/bin/launchctl",
626+ });
627+ const previousRuntime = yield* context.fs.readLink(context.launchAgent.activeRuntimeLinkPath);
628+
629+ const error = yield* context.service.update({ targetVersion: "0.0.29" }).pipe(Effect.flip);
630+
631+ assert.include(error.reason, "Restarting the T3 Code LaunchAgent failed");
632+ assert.equal(
633+ yield* context.fs.readLink(context.launchAgent.activeRuntimeLinkPath),
634+ previousRuntime,
635+ );
636+ assert.lengthOf(context.spawns, 0);
637+ }).pipe(Effect.provide(TestClock.layer())),
638+ );
639+
523640 it.effect("restores the previous unit and permits a retry when systemd restart fails", () =>
524641 Effect.gen(function* () {
525642 let failRestart = true;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t