Skip to content

Commit 395184a

Browse files
fix(ssh): detect installed services without a user manager (#88)
* fix(ssh): detect installed services without a user manager * fix(ssh): find the installed unit across login config homes
1 parent 9604540 commit 395184a

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,16 @@ const runArchiveRunner = Effect.fn("test.runArchiveRunner")(
142142
);
143143

144144
describe("ssh tunnel scripts", () => {
145-
it.live.each(["launchd", "systemd", "none"] as const)(
145+
const serviceManagers = [
146+
"launchd",
147+
"systemd",
148+
"systemd-unavailable",
149+
"systemd-unavailable-xdg",
150+
"systemd-unavailable-relative-xdg",
151+
"systemd-unavailable-login-xdg",
152+
"none",
153+
] as const;
154+
it.live.each(serviceManagers)(
146155
"respects %s service ownership when the server is stopped",
147156
(manager) =>
148157
Effect.gen(function* () {
@@ -175,12 +184,27 @@ esac
175184
yield* fs.makeDirectory(agents, { recursive: true });
176185
yield* fs.writeFileString(`${agents}/com.t3tools.t3code.service.plist`, "installed");
177186
}
187+
const xdgConfigHome =
188+
manager === "systemd-unavailable-xdg" || manager === "systemd-unavailable-login-xdg"
189+
? `${root}/custom config`
190+
: manager === "systemd-unavailable-relative-xdg"
191+
? "relative-config"
192+
: undefined;
193+
if (manager.startsWith("systemd-unavailable")) {
194+
const units = `${manager === "systemd-unavailable-xdg" ? xdgConfigHome : `${root}/.config`}/systemd/user`;
195+
yield* fs.makeDirectory(units, { recursive: true });
196+
yield* fs.writeFileString(`${units}/t3code.service`, "installed");
197+
}
178198
const ssh = ChildProcessSpawner.make((command) => {
179199
if (command._tag !== "StandardCommand") return Effect.die("Expected SSH command");
180200
return spawner.spawn(
181201
ChildProcess.make("sh", ["-s", "--", "test-peer"], {
182202
...command.options,
183-
env: { HOME: root, PATH: `${bin}:/usr/bin:/bin` },
203+
env: {
204+
HOME: root,
205+
PATH: `${bin}:/usr/bin:/bin`,
206+
...(xdgConfigHome === undefined ? {} : { XDG_CONFIG_HOME: xdgConfigHome }),
207+
},
184208
extendEnv: false,
185209
}),
186210
);

‎packages/ssh/src/tunnel.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,14 @@ if [ -z "$REMOTE_PORT" ]; then
744744
fi
745745
;;
746746
Linux)
747-
if command -v systemctl >/dev/null 2>&1 && systemctl --user cat t3code.service >/dev/null 2>&1; then
747+
SERVICE_UNIT="systemd/user/t3code.service"
748+
SERVICE_CONFIG_HOME="$HOME/.config"
749+
case "\${XDG_CONFIG_HOME:-}" in
750+
/*) SERVICE_CONFIG_HOME="$XDG_CONFIG_HOME" ;;
751+
esac
752+
# The SSH login may have a different XDG home and no connection to the user manager.
753+
if [ -f "$SERVICE_CONFIG_HOME/$SERVICE_UNIT" ] || [ -f "$HOME/.config/$SERVICE_UNIT" ] ||
754+
{ command -v systemctl >/dev/null 2>&1 && systemctl --user cat t3code.service >/dev/null 2>&1; }; then
748755
SERVICE_INSTALLED=1
749756
fi
750757
;;

0 commit comments

Comments
 (0)