Log SSH tunnel lifecycle and surface bootstrap failures
- Add structured logging around SSH command, tunnel, and pairing flows - Let desktop SSH bootstrap failures propagate instead of timing out locally - Update reconnect test to expect the underlying SSH timeout error
This commit is contained in:
1 parent
873158254c
commit
0f6f2035e4
4 files changed
+288
-64
No files matched your search
@@ -748,8 +748,7 @@ describe("addSavedEnvironment", () => {
|
||||
await resetEnvironmentServiceForTests();
|
||||
});
|
||||
|
||||
it("times out a saved ssh reconnect when desktop bootstrap never settles", async () => {
|
||||
vi.useFakeTimers();
|
||||
it("surfaces desktop ssh bootstrap failures during saved ssh reconnect", async () => {
|
||||
mockSavedRecords = [
|
||||
{
|
||||
environmentId: EnvironmentId.make("environment-1"),
|
||||
@@ -767,17 +766,14 @@ describe("addSavedEnvironment", () => {
|
||||
},
|
||||
];
|
||||
mockReadSavedEnvironmentBearerToken.mockResolvedValue(null);
|
||||
mockEnsureSshEnvironment.mockReturnValue(new Promise(() => undefined));
|
||||
mockEnsureSshEnvironment.mockRejectedValue(new Error("SSH command timed out after 60000ms."));
|
||||
|
||||
const { reconnectSavedEnvironment, resetEnvironmentServiceForTests } =
|
||||
await import("./service");
|
||||
|
||||
const reconnectPromise = reconnectSavedEnvironment(EnvironmentId.make("environment-1"));
|
||||
const reconnectAssertion = expect(reconnectPromise).rejects.toThrow(
|
||||
"Timed out starting SSH environment for devbox.",
|
||||
await expect(reconnectSavedEnvironment(EnvironmentId.make("environment-1"))).rejects.toThrow(
|
||||
"SSH command timed out after 60000ms.",
|
||||
);
|
||||
await vi.advanceTimersByTimeAsync(70_000);
|
||||
await reconnectAssertion;
|
||||
expect(mockPatchRuntime).toHaveBeenCalledWith(
|
||||
EnvironmentId.make("environment-1"),
|
||||
expect.objectContaining({
|
||||
@@ -788,7 +784,7 @@ describe("addSavedEnvironment", () => {
|
||||
EnvironmentId.make("environment-1"),
|
||||
expect.objectContaining({
|
||||
connectionState: "error",
|
||||
lastError: "Timed out starting SSH environment for devbox.",
|
||||
lastError: "SSH command timed out after 60000ms.",
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -114,7 +114,6 @@ const THREAD_DETAIL_SUBSCRIPTION_IDLE_EVICTION_MS = 15 * 60 * 1000;
|
||||
const MAX_CACHED_THREAD_DETAIL_SUBSCRIPTIONS = 32;
|
||||
const NOOP = () => undefined;
|
||||
const SSH_HTTP_STATUS_RE = /^\[ssh_http:(\d+)\]\s/u;
|
||||
const SAVED_ENVIRONMENT_CONNECT_TIMEOUT_MS = 70_000;
|
||||
|
||||
function compareAppliedProjectionVersion(
|
||||
left: { readonly sequence: number; readonly updatedAt: string | null },
|
||||
@@ -597,10 +596,7 @@ async function resolveDesktopSshEnvironmentBootstrap(
|
||||
throw new Error("SSH launch is only available in the desktop app.");
|
||||
}
|
||||
|
||||
return await withDesktopSshBootstrapTimeout(
|
||||
target,
|
||||
desktopBridge.ensureSshEnvironment(target, options),
|
||||
);
|
||||
return await desktopBridge.ensureSshEnvironment(target, options);
|
||||
}
|
||||
|
||||
function getDesktopSshBridge() {
|
||||
@@ -742,36 +738,6 @@ function setRuntimeError(environmentId: EnvironmentId, error: unknown) {
|
||||
});
|
||||
}
|
||||
|
||||
function getDesktopSshTargetDisplayName(target: DesktopSshEnvironmentTarget): string {
|
||||
return target.alias.trim() || target.hostname.trim() || "SSH host";
|
||||
}
|
||||
|
||||
function createDesktopSshBootstrapTimeoutError(target: DesktopSshEnvironmentTarget): Error {
|
||||
return new Error(
|
||||
`Timed out starting SSH environment for ${getDesktopSshTargetDisplayName(target)}.`,
|
||||
);
|
||||
}
|
||||
|
||||
async function withDesktopSshBootstrapTimeout<T>(
|
||||
target: DesktopSshEnvironmentTarget,
|
||||
operation: Promise<T>,
|
||||
): Promise<T> {
|
||||
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
||||
const timeout = new Promise<never>((_, reject) => {
|
||||
timeoutId = setTimeout(() => {
|
||||
reject(createDesktopSshBootstrapTimeoutError(target));
|
||||
}, SAVED_ENVIRONMENT_CONNECT_TIMEOUT_MS);
|
||||
});
|
||||
|
||||
try {
|
||||
return await Promise.race([operation, timeout]);
|
||||
} finally {
|
||||
if (timeoutId !== null) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function coalesceOrchestrationUiEvents(
|
||||
events: ReadonlyArray<OrchestrationEvent>,
|
||||
): OrchestrationEvent[] {
|
||||
|
||||
+49
-11
@@ -117,6 +117,15 @@ function normalizeSshErrorMessage(stderr: string, fallbackMessage: string): stri
|
||||
return cleaned.length > 0 ? cleaned : fallbackMessage;
|
||||
}
|
||||
|
||||
function sshTargetLogFields(target: DesktopSshEnvironmentTarget) {
|
||||
return {
|
||||
alias: target.alias,
|
||||
hostname: target.hostname,
|
||||
username: target.username,
|
||||
port: target.port,
|
||||
};
|
||||
}
|
||||
|
||||
function stdinStream(input: string | undefined) {
|
||||
return input === undefined ? Stream.empty : Stream.make(encoder.encode(input));
|
||||
}
|
||||
@@ -155,6 +164,12 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
...(input.remoteCommandArgs ?? []),
|
||||
];
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
yield* Effect.logDebug("ssh.command.start", {
|
||||
...sshTargetLogFields(target),
|
||||
command: ["ssh", ...args],
|
||||
hasStdin: input.stdin !== undefined,
|
||||
timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
|
||||
});
|
||||
const child = yield* spawner
|
||||
.spawn(
|
||||
ChildProcess.make("ssh", args, {
|
||||
@@ -205,6 +220,12 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
);
|
||||
|
||||
if (exitCode !== 0) {
|
||||
yield* Effect.logWarning("ssh.command.failed", {
|
||||
...sshTargetLogFields(target),
|
||||
command: ["ssh", ...args],
|
||||
exitCode,
|
||||
stderr,
|
||||
});
|
||||
return yield* new SshCommandError({
|
||||
command: ["ssh", ...args],
|
||||
exitCode,
|
||||
@@ -216,6 +237,10 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
});
|
||||
}
|
||||
|
||||
yield* Effect.logDebug("ssh.command.succeeded", {
|
||||
...sshTargetLogFields(target),
|
||||
command: ["ssh", ...args],
|
||||
});
|
||||
return { stdout, stderr };
|
||||
});
|
||||
|
||||
@@ -235,14 +260,21 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* (
|
||||
Option.match(result, {
|
||||
onSome: Effect.succeed,
|
||||
onNone: () =>
|
||||
Effect.fail(
|
||||
new SshCommandError({
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.logWarning("ssh.command.timedOut", {
|
||||
...sshTargetLogFields(target),
|
||||
timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
|
||||
remoteCommandArgs: input.remoteCommandArgs ?? [],
|
||||
preHostArgs: input.preHostArgs ?? [],
|
||||
hasStdin: input.stdin !== undefined,
|
||||
});
|
||||
return yield* new SshCommandError({
|
||||
command: ["ssh"],
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
message: `SSH command timed out after ${input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS}ms.`,
|
||||
}),
|
||||
),
|
||||
});
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
@@ -260,6 +292,7 @@ export const resolveSshTarget = Effect.fn("ssh/command.resolveSshTarget")(functi
|
||||
return yield* new SshInvalidTargetError({ message: "SSH host alias is required." });
|
||||
}
|
||||
|
||||
yield* Effect.logDebug("ssh.target.resolve.start", { alias: trimmedAlias });
|
||||
return yield* runSshCommand(
|
||||
{
|
||||
alias: trimmedAlias,
|
||||
@@ -270,13 +303,18 @@ export const resolveSshTarget = Effect.fn("ssh/command.resolveSshTarget")(functi
|
||||
{ preHostArgs: ["-G"] },
|
||||
).pipe(
|
||||
Effect.map((result) => parseSshResolveOutput(trimmedAlias, result.stdout)),
|
||||
Effect.catch(() =>
|
||||
Effect.succeed({
|
||||
alias: trimmedAlias,
|
||||
hostname: trimmedAlias,
|
||||
username: null,
|
||||
port: null,
|
||||
}),
|
||||
Effect.tap((target) =>
|
||||
Effect.logDebug("ssh.target.resolve.succeeded", sshTargetLogFields(target)),
|
||||
),
|
||||
Effect.catch((cause) =>
|
||||
Effect.logDebug("ssh.target.resolve.fallback", { alias: trimmedAlias, cause }).pipe(
|
||||
Effect.as({
|
||||
alias: trimmedAlias,
|
||||
hostname: trimmedAlias,
|
||||
username: null,
|
||||
port: null,
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
+233
-9
@@ -12,6 +12,7 @@ import {
|
||||
Exit,
|
||||
FileSystem,
|
||||
Layer,
|
||||
Option,
|
||||
Path,
|
||||
Schema,
|
||||
Scope,
|
||||
@@ -98,6 +99,25 @@ function makeSshTunnelCancelledError(target: DesktopSshEnvironmentTarget): SshCo
|
||||
});
|
||||
}
|
||||
|
||||
function sshTargetLogFields(target: DesktopSshEnvironmentTarget) {
|
||||
return {
|
||||
alias: target.alias,
|
||||
hostname: target.hostname,
|
||||
username: target.username,
|
||||
port: target.port,
|
||||
};
|
||||
}
|
||||
|
||||
function sshRunnerLogFields(runner: RemoteT3RunnerOptions | undefined) {
|
||||
if (runner?.nodeScriptPath?.trim()) {
|
||||
return { runner: "node-script", nodeScriptPath: runner.nodeScriptPath.trim() };
|
||||
}
|
||||
if (runner?.packageSpec?.trim()) {
|
||||
return { runner: "package", packageSpec: runner.packageSpec.trim() };
|
||||
}
|
||||
return { runner: "default" };
|
||||
}
|
||||
|
||||
interface SshAuthOperationInput<T> {
|
||||
readonly key: string;
|
||||
readonly target: DesktopSshEnvironmentTarget;
|
||||
@@ -341,6 +361,11 @@ export const launchOrReuseRemoteServer = Effect.fn("ssh/tunnel.launchOrReuseRemo
|
||||
SshCommandError | SshInvalidTargetError | SshLaunchError,
|
||||
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
|
||||
> {
|
||||
yield* Effect.logInfo("ssh.remoteServer.launch.start", {
|
||||
...sshTargetLogFields(target),
|
||||
...sshRunnerLogFields(runner),
|
||||
stateKey: remoteStateKey(target),
|
||||
});
|
||||
const result = yield* runSshCommand(target, {
|
||||
remoteCommandArgs: ["sh", "-s", "--", remoteStateKey(target)],
|
||||
stdin: buildRemoteLaunchScript(runner),
|
||||
@@ -370,6 +395,11 @@ export const launchOrReuseRemoteServer = Effect.fn("ssh/tunnel.launchOrReuseRemo
|
||||
stdout: result.stdout,
|
||||
});
|
||||
}
|
||||
yield* Effect.logInfo("ssh.remoteServer.launch.ready", {
|
||||
...sshTargetLogFields(target),
|
||||
remotePort: parsed.remotePort,
|
||||
stateKey: remoteStateKey(target),
|
||||
});
|
||||
return parsed.remotePort;
|
||||
},
|
||||
);
|
||||
@@ -383,6 +413,10 @@ export const issueRemotePairingToken = Effect.fn("ssh/tunnel.issueRemotePairingT
|
||||
SshCommandError | SshInvalidTargetError | SshPairingError,
|
||||
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
|
||||
> {
|
||||
yield* Effect.logInfo("ssh.remoteServer.pairingToken.start", {
|
||||
...sshTargetLogFields(target),
|
||||
stateKey: remoteStateKey(target),
|
||||
});
|
||||
const result = yield* runSshCommand(target, {
|
||||
remoteCommandArgs: ["sh", "-s"],
|
||||
stdin: buildRemotePairingScript(target, runner),
|
||||
@@ -412,6 +446,10 @@ export const issueRemotePairingToken = Effect.fn("ssh/tunnel.issueRemotePairingT
|
||||
stdout: result.stdout,
|
||||
});
|
||||
}
|
||||
yield* Effect.logInfo("ssh.remoteServer.pairingToken.created", {
|
||||
...sshTargetLogFields(target),
|
||||
stateKey: remoteStateKey(target),
|
||||
});
|
||||
return parsed.credential;
|
||||
});
|
||||
|
||||
@@ -423,6 +461,10 @@ export const stopRemoteServer = Effect.fn("ssh/tunnel.stopRemoteServer")(functio
|
||||
SshCommandError | SshInvalidTargetError,
|
||||
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
|
||||
> {
|
||||
yield* Effect.logInfo("ssh.remoteServer.stop.start", {
|
||||
...sshTargetLogFields(target),
|
||||
stateKey: remoteStateKey(target),
|
||||
});
|
||||
yield* runSshCommand(target, {
|
||||
remoteCommandArgs: ["sh", "-s"],
|
||||
stdin: buildRemoteStopScript(target),
|
||||
@@ -430,6 +472,10 @@ export const stopRemoteServer = Effect.fn("ssh/tunnel.stopRemoteServer")(functio
|
||||
...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }),
|
||||
...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }),
|
||||
});
|
||||
yield* Effect.logInfo("ssh.remoteServer.stop.succeeded", {
|
||||
...sshTargetLogFields(target),
|
||||
stateKey: remoteStateKey(target),
|
||||
});
|
||||
});
|
||||
|
||||
export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(function* (input: {
|
||||
@@ -442,24 +488,44 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
const intervalMs = input.intervalMs ?? 100;
|
||||
const maxRetries = Math.max(0, Math.ceil(timeoutMs / intervalMs));
|
||||
const requestUrl = new URL(input.path ?? "/", input.baseUrl).toString();
|
||||
const client = (yield* HttpClient.HttpClient).pipe(
|
||||
HttpClient.filterStatusOk,
|
||||
HttpClient.retry({
|
||||
schedule: Schedule.spaced(Duration.millis(intervalMs)),
|
||||
times: maxRetries,
|
||||
}),
|
||||
);
|
||||
const client = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
|
||||
|
||||
yield* client.execute(HttpClientRequest.get(requestUrl)).pipe(
|
||||
yield* Effect.logInfo("ssh.tunnel.httpReady.start", {
|
||||
baseUrl: input.baseUrl,
|
||||
requestUrl,
|
||||
timeoutMs,
|
||||
intervalMs,
|
||||
});
|
||||
|
||||
const result = yield* client.execute(HttpClientRequest.get(requestUrl)).pipe(
|
||||
Effect.asVoid,
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshReadinessError({
|
||||
message: `Timed out waiting for backend readiness at ${input.baseUrl}.`,
|
||||
message: `Backend readiness probe failed at ${input.baseUrl}.`,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.retry({
|
||||
schedule: Schedule.spaced(Duration.millis(intervalMs)),
|
||||
times: maxRetries,
|
||||
}),
|
||||
Effect.timeoutOption(Duration.millis(timeoutMs)),
|
||||
);
|
||||
|
||||
return yield* Option.match(result, {
|
||||
onSome: () =>
|
||||
Effect.logInfo("ssh.tunnel.httpReady.succeeded", {
|
||||
baseUrl: input.baseUrl,
|
||||
requestUrl,
|
||||
}),
|
||||
onNone: () =>
|
||||
Effect.fail(
|
||||
new SshReadinessError({
|
||||
message: `Timed out waiting ${timeoutMs}ms for backend readiness at ${input.baseUrl}.`,
|
||||
}),
|
||||
),
|
||||
});
|
||||
});
|
||||
|
||||
function isLoopbackHostname(hostname: string): boolean {
|
||||
@@ -616,6 +682,12 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
];
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
const scope = yield* Scope.Scope;
|
||||
yield* Effect.logInfo("ssh.tunnel.spawn.start", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
httpBaseUrl: input.httpBaseUrl,
|
||||
});
|
||||
const child = yield* spawner
|
||||
.spawn(
|
||||
ChildProcess.make("ssh", args, {
|
||||
@@ -638,6 +710,12 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
}),
|
||||
),
|
||||
);
|
||||
yield* Effect.logInfo("ssh.tunnel.spawn.succeeded", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
httpBaseUrl: input.httpBaseUrl,
|
||||
});
|
||||
const tunnelEntry: SshTunnelEntry = {
|
||||
key: input.key,
|
||||
target: input.resolvedTarget,
|
||||
@@ -686,6 +764,23 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
}),
|
||||
exitFailure,
|
||||
).pipe(
|
||||
Effect.tap(() =>
|
||||
Effect.logInfo("ssh.tunnel.ready", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
httpBaseUrl: input.httpBaseUrl,
|
||||
}),
|
||||
),
|
||||
Effect.tapError((cause) =>
|
||||
Effect.logWarning("ssh.tunnel.ready.failed", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
httpBaseUrl: input.httpBaseUrl,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.onExit((exit) =>
|
||||
Exit.isSuccess(exit)
|
||||
? Effect.void
|
||||
@@ -714,7 +809,19 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
const closeTunnelEntry = Effect.fn("ssh/tunnel.closeTunnelEntry")(function* (
|
||||
entry: SshTunnelEntry,
|
||||
) {
|
||||
yield* Effect.logInfo("ssh.tunnel.close.start", {
|
||||
...sshTargetLogFields(entry.target),
|
||||
key: entry.key,
|
||||
localPort: entry.localPort,
|
||||
remotePort: entry.remotePort,
|
||||
});
|
||||
yield* Scope.close(entry.scope, Exit.void).pipe(Effect.ignore);
|
||||
yield* Effect.logInfo("ssh.tunnel.close.succeeded", {
|
||||
...sshTargetLogFields(entry.target),
|
||||
key: entry.key,
|
||||
localPort: entry.localPort,
|
||||
remotePort: entry.remotePort,
|
||||
});
|
||||
});
|
||||
|
||||
yield* Scope.addFinalizer(
|
||||
@@ -734,11 +841,19 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
const promptService = yield* SshPasswordPrompt;
|
||||
const hostSpec = yield* buildSshHostSpecEffect(target);
|
||||
if (!promptService.isAvailable) {
|
||||
yield* Effect.logWarning("ssh.auth.passwordPrompt.unavailable", {
|
||||
...sshTargetLogFields(target),
|
||||
attempt,
|
||||
});
|
||||
return yield* new SshPasswordPromptError({
|
||||
message: `SSH authentication failed for ${hostSpec}.`,
|
||||
});
|
||||
}
|
||||
|
||||
yield* Effect.logInfo("ssh.auth.passwordPrompt.request", {
|
||||
...sshTargetLogFields(target),
|
||||
attempt,
|
||||
});
|
||||
const password = yield* promptService.request({
|
||||
attempt,
|
||||
destination: target.alias.trim() || target.hostname.trim(),
|
||||
@@ -746,10 +861,18 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
prompt: `Enter the SSH password for ${hostSpec}.`,
|
||||
});
|
||||
if (password === null) {
|
||||
yield* Effect.logWarning("ssh.auth.passwordPrompt.cancelled", {
|
||||
...sshTargetLogFields(target),
|
||||
attempt,
|
||||
});
|
||||
return yield* new SshPasswordPromptError({
|
||||
message: `SSH authentication cancelled for ${hostSpec}.`,
|
||||
});
|
||||
}
|
||||
yield* Effect.logInfo("ssh.auth.passwordPrompt.received", {
|
||||
...sshTargetLogFields(target),
|
||||
attempt,
|
||||
});
|
||||
return password;
|
||||
});
|
||||
|
||||
@@ -763,6 +886,12 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
return yield* input.error;
|
||||
}
|
||||
|
||||
yield* Effect.logWarning("ssh.auth.failed", {
|
||||
...sshTargetLogFields(input.target),
|
||||
key: input.key,
|
||||
promptCount: input.promptCount,
|
||||
cause: input.error,
|
||||
});
|
||||
const promptService = yield* SshPasswordPrompt;
|
||||
if (!promptService.isAvailable) {
|
||||
return yield* input.error;
|
||||
@@ -821,15 +950,31 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
readonly resolvedTarget: DesktopSshEnvironmentTarget;
|
||||
readonly runner?: RemoteT3RunnerOptions;
|
||||
}): Effect.fn.Return<SshTunnelEntry, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
|
||||
yield* Effect.logInfo("ssh.environment.tunnel.create.start", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
...sshRunnerLogFields(input.runner),
|
||||
key: input.key,
|
||||
});
|
||||
const remotePort = yield* runWithSshAuth({
|
||||
key: input.key,
|
||||
target: input.resolvedTarget,
|
||||
operation: (authOptions) =>
|
||||
launchOrReuseRemoteServer(input.resolvedTarget, authOptions, input.runner),
|
||||
});
|
||||
yield* Effect.logInfo("ssh.environment.remotePort.ready", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
key: input.key,
|
||||
remotePort,
|
||||
});
|
||||
const localPort = yield* reserveLocalTunnelPort();
|
||||
const httpBaseUrl = `http://127.0.0.1:${localPort}/`;
|
||||
const wsBaseUrl = `ws://127.0.0.1:${localPort}/`;
|
||||
yield* Effect.logInfo("ssh.environment.localPort.reserved", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
key: input.key,
|
||||
localPort,
|
||||
remotePort,
|
||||
});
|
||||
const entryScope = yield* Scope.make("sequential");
|
||||
const tunnelEntry = yield* runWithSshAuth({
|
||||
key: input.key,
|
||||
@@ -859,6 +1004,12 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
if (tunnels.get(tunnelEntry.key) !== tunnelEntry) {
|
||||
return;
|
||||
}
|
||||
yield* Effect.logInfo("ssh.environment.tunnel.finalizer.start", {
|
||||
...sshTargetLogFields(tunnelEntry.target),
|
||||
key: tunnelEntry.key,
|
||||
localPort: tunnelEntry.localPort,
|
||||
remotePort: tunnelEntry.remotePort,
|
||||
});
|
||||
tunnels.delete(tunnelEntry.key);
|
||||
const authSecret = authSecrets.get(tunnelEntry.key) ?? null;
|
||||
yield* Effect.all(
|
||||
@@ -887,8 +1038,20 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
).pipe(Effect.ignore);
|
||||
yield* Effect.logInfo("ssh.environment.tunnel.finalizer.succeeded", {
|
||||
...sshTargetLogFields(tunnelEntry.target),
|
||||
key: tunnelEntry.key,
|
||||
localPort: tunnelEntry.localPort,
|
||||
remotePort: tunnelEntry.remotePort,
|
||||
});
|
||||
}).pipe(Effect.ignore),
|
||||
);
|
||||
yield* Effect.logInfo("ssh.environment.tunnel.create.succeeded", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
key: input.key,
|
||||
localPort,
|
||||
remotePort,
|
||||
});
|
||||
return tunnelEntry;
|
||||
});
|
||||
|
||||
@@ -900,18 +1063,41 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
let entry = tunnels.get(key) ?? null;
|
||||
|
||||
if (entry !== null) {
|
||||
yield* Effect.logDebug("ssh.environment.tunnel.existing.check", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
localPort: entry.localPort,
|
||||
remotePort: entry.remotePort,
|
||||
});
|
||||
const readinessExit = yield* Effect.exit(
|
||||
waitForHttpReady({ baseUrl: entry.httpBaseUrl, timeoutMs: 2_000 }),
|
||||
);
|
||||
if (Exit.isSuccess(readinessExit)) {
|
||||
yield* Effect.logInfo("ssh.environment.tunnel.reused", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
localPort: entry.localPort,
|
||||
remotePort: entry.remotePort,
|
||||
});
|
||||
return entry;
|
||||
}
|
||||
yield* Effect.logWarning("ssh.environment.tunnel.existing.stale", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
localPort: entry.localPort,
|
||||
remotePort: entry.remotePort,
|
||||
cause: readinessExit.cause,
|
||||
});
|
||||
yield* closeTunnelEntry(entry);
|
||||
entry = null;
|
||||
}
|
||||
|
||||
const pending = pendingTunnelEntries.get(key);
|
||||
if (pending) {
|
||||
yield* Effect.logInfo("ssh.environment.tunnel.pending.await", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
});
|
||||
return yield* Deferred.await(pending);
|
||||
}
|
||||
|
||||
@@ -923,6 +1109,13 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
resolvedTarget,
|
||||
...(runner === undefined ? {} : { runner }),
|
||||
}).pipe(
|
||||
Effect.tapError((cause) =>
|
||||
Effect.logWarning("ssh.environment.tunnel.create.failed", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.onExit((exit) =>
|
||||
Effect.sync(() => {
|
||||
if (pendingTunnelEntries.get(key) === deferred) {
|
||||
@@ -941,6 +1134,10 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
SshEnvironmentEffectError,
|
||||
SshEnvironmentEffectContext
|
||||
> {
|
||||
yield* Effect.logInfo("ssh.environment.ensure.start", {
|
||||
...sshTargetLogFields(target),
|
||||
issuePairingToken: requestOptions?.issuePairingToken === true,
|
||||
});
|
||||
const baseResolved = yield* resolveSshTarget(target.alias || target.hostname);
|
||||
const resolvedTarget: DesktopSshEnvironmentTarget = {
|
||||
...baseResolved,
|
||||
@@ -948,9 +1145,18 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
...(target.port !== null ? { port: target.port } : {}),
|
||||
};
|
||||
const key = targetConnectionKey(resolvedTarget);
|
||||
yield* Effect.logInfo("ssh.environment.target.resolved", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
});
|
||||
const packageSpec = options.resolveCliPackageSpec?.();
|
||||
const runner =
|
||||
options.resolveCliRunner?.() ?? (packageSpec === undefined ? undefined : { packageSpec });
|
||||
yield* Effect.logDebug("ssh.environment.runner.resolved", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
...sshRunnerLogFields(runner),
|
||||
key,
|
||||
});
|
||||
const entry = yield* ensureTunnelEntry(key, resolvedTarget, runner);
|
||||
|
||||
const pairingToken = requestOptions?.issuePairingToken
|
||||
@@ -961,6 +1167,13 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
})
|
||||
: null;
|
||||
|
||||
yield* Effect.logInfo("ssh.environment.ensure.succeeded", {
|
||||
...sshTargetLogFields(entry.target),
|
||||
key,
|
||||
localPort: entry.localPort,
|
||||
remotePort: entry.remotePort,
|
||||
issuedPairingToken: pairingToken !== null,
|
||||
});
|
||||
return {
|
||||
target: entry.target,
|
||||
httpBaseUrl: entry.httpBaseUrl,
|
||||
@@ -972,6 +1185,7 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
const disconnectEnvironment = Effect.fn("ssh/tunnel.disconnectEnvironment")(function* (
|
||||
target: DesktopSshEnvironmentTarget,
|
||||
): Effect.fn.Return<void, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
|
||||
yield* Effect.logInfo("ssh.environment.disconnect.start", sshTargetLogFields(target));
|
||||
const baseResolved = yield* resolveSshTarget(target.alias || target.hostname);
|
||||
const resolvedTarget: DesktopSshEnvironmentTarget = {
|
||||
...baseResolved,
|
||||
@@ -980,6 +1194,12 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
};
|
||||
const key = targetConnectionKey(resolvedTarget);
|
||||
const entry = tunnels.get(key) ?? null;
|
||||
yield* Effect.logDebug("ssh.environment.disconnect.targetResolved", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
hasTunnel: entry !== null,
|
||||
hasPendingTunnel: pendingTunnelEntries.has(key),
|
||||
});
|
||||
if (entry !== null) {
|
||||
yield* closeTunnelEntry(entry);
|
||||
}
|
||||
@@ -997,6 +1217,10 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
|
||||
operation: (authOptions) => stopRemoteServer(resolvedTarget, authOptions),
|
||||
});
|
||||
}
|
||||
yield* Effect.logInfo("ssh.environment.disconnect.succeeded", {
|
||||
...sshTargetLogFields(resolvedTarget),
|
||||
key,
|
||||
});
|
||||
});
|
||||
|
||||
return SshEnvironmentManager.of({ ensureEnvironment, disconnectEnvironment });
|
||||
|
||||
Reference in new issue
Block a user