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
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 18
18import * as ServerConfig from "../config.ts";19import * as ServerConfig from "../config.ts";
19import * as ProcessRunner from "../processRunner.ts";20import * as ProcessRunner from "../processRunner.ts";
20import {21import {
22 BOOT_SERVICE_LAUNCH_AGENT_LABEL,
21 BOOT_SERVICE_UNIT_ENV,23 BOOT_SERVICE_UNIT_ENV,
22 BOOT_SERVICE_UNIT_FILE,24 BOOT_SERVICE_UNIT_FILE,
25 launchAgentPaths,
26 renderBootServiceLaunchAgent,
23 renderBootServiceUnit,27 renderBootServiceUnit,
24} from "./bootService.ts";28} from "./bootService.ts";
25import * as SelfUpdate from "./selfUpdate.ts";29import * as SelfUpdate from "./selfUpdate.ts";
@@ -76,6 +80,7 @@ const provideHostRefs = (input: { · 50 unmodified lines
76 Layer.succeed(HostProcessEnvironment, input.env),80 Layer.succeed(HostProcessEnvironment, input.env),
77 Layer.succeed(HostProcessExecutablePath, NODE_PATH),81 Layer.succeed(HostProcessExecutablePath, NODE_PATH),
78 Layer.succeed(HostProcessArguments, [NODE_PATH, input.entryPath, "serve"]),82 Layer.succeed(HostProcessArguments, [NODE_PATH, input.entryPath, "serve"]),
83 Layer.succeed(HostProcessUserId, 501),
79 ),84 ),
80 );85 );
81 86
@@ -192,6 +197,41 @@ it.layer(NodeServices.layer)("resolveServerSelfUpdateCapability", (it) => { · 110 unmodified lines
192 }),197 }),
193 );198 );
194 199
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
195 it.effect("reports desktop-managed for desktop-supervised backends", () =>235 it.effect("reports desktop-managed for desktop-supervised backends", () =>
196 Effect.gen(function* () {236 Effect.gen(function* () {
197 const { home, path } = yield* makeHome();237 const { home, path } = yield* makeHome();
@@ -262,11 +302,21 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 64 unmodified lines
262 const path = yield* Path.Path;302 const path = yield* Path.Path;
263 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" });303 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" });
264 const baseDir = path.join(home, ".t3");304 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);
265 const entryPath =308 const entryPath =
266 options?.entryPath ??309 options?.entryPath ??
267 path.join(home, ".t3/runtime/versions/0.0.28/node_modules/t3/dist/bin.mjs");310 (launchdBootService
268 const env: NodeJS.ProcessEnv =311 ? launchAgent.activeEntryPath
269 options?.bootService === true312 : 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
270 ? {320 ? {
271 HOME: home,321 HOME: home,
272 INVOCATION_ID: "abc123",322 INVOCATION_ID: "abc123",
@@ -274,18 +324,37 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 1 unmodified line
274 }324 }
275 : { HOME: home };325 : { HOME: home };
276 if (options?.bootService === true) {326 if (options?.bootService === true) {
277 const unitDir = path.join(home, ".config", "systemd", "user");327 if (launchdBootService) {
278 yield* fs.makeDirectory(unitDir, { recursive: true });328 const previousRuntime = path.join(baseDir, "runtime", "versions", "0.0.28");
279 yield* fs.writeFileString(329 yield* fs.makeDirectory(path.dirname(launchAgent.unitPath), { recursive: true });
280 path.join(unitDir, "t3code.service"),330 yield* fs.makeDirectory(path.dirname(launchAgent.activeRuntimeLinkPath), {
281 renderBootServiceUnit({331 recursive: true,
282 nodePath: NODE_PATH,332 });
283 t3EntryPath: entryPath,333 yield* fs.symlink(previousRuntime, launchAgent.activeRuntimeLinkPath);
284 baseDir,334 yield* fs.writeFileString(
285 logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"),335 launchAgent.unitPath,
286 unitPath: path.join(unitDir, "t3code.service"),336 renderBootServiceLaunchAgent({
287 }),337 nodePath: NODE_PATH,
288 );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 }
289 }358 }
290 359
291 const commands: Array<RecordedCommand> = [];360 const commands: Array<RecordedCommand> = [];
@@ -333,7 +402,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 41 unmodified lines
333 configLayer,402 configLayer,
334 ),403 ),
335 ),404 ),
336 provideHostRefs({ platform: options?.platform ?? "linux", env, entryPath }),405 provideHostRefs({ platform, env, entryPath }),
337 );406 );
338 return {407 return {
339 fs,408 fs,
@@ -341,6 +410,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 1 unmodified line
341 home,410 home,
342 baseDir,411 baseDir,
343 entryPath,412 entryPath,
413 launchAgent,
344 commands,414 commands,
345 spawns,415 spawns,
346 exitCount: () => exited,416 exitCount: () => exited,
@@ -520,6 +590,53 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 173 unmodified lines
520 }).pipe(Effect.provide(TestClock.layer())),590 }).pipe(Effect.provide(TestClock.layer())),
521 );591 );
522 592
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
523 it.effect("restores the previous unit and permits a retry when systemd restart fails", () =>640 it.effect("restores the previous unit and permits a retry when systemd restart fails", () =>
524 Effect.gen(function* () {641 Effect.gen(function* () {
525 let failRestart = true;642 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