|
| 1 | +import { formatDuration, intervalToDuration } from "date-fns"; |
| 2 | +import * as jsonc from "jsonc-parser"; |
| 3 | +import * as fs from "node:fs/promises"; |
| 4 | + |
| 5 | +import type { WorkspaceConfiguration } from "vscode"; |
| 6 | + |
| 7 | +import type { Logger } from "../logging/logger"; |
| 8 | + |
| 9 | +export interface SettingOverride { |
| 10 | + key: string; |
| 11 | + value: unknown; |
| 12 | +} |
| 13 | + |
| 14 | +interface RecommendedSetting { |
| 15 | + readonly value: number | null; |
| 16 | + readonly label: string; |
| 17 | +} |
| 18 | + |
| 19 | +function recommended( |
| 20 | + shortName: string, |
| 21 | + value: number | null, |
| 22 | +): RecommendedSetting { |
| 23 | + if (value === null) { |
| 24 | + return { value, label: `${shortName}: max allowed` }; |
| 25 | + } |
| 26 | + const humanized = formatDuration( |
| 27 | + intervalToDuration({ start: 0, end: value * 1000 }), |
| 28 | + ); |
| 29 | + return { value, label: `${shortName}: ${humanized}` }; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Applied by the "Apply Recommended SSH Settings" command. |
| 34 | + * These are more aggressive (24h) than AUTO_SETUP_DEFAULTS (8h) because the |
| 35 | + * user is explicitly opting in via the command palette. |
| 36 | + */ |
| 37 | +export const RECOMMENDED_SSH_SETTINGS = { |
| 38 | + "remote.SSH.connectTimeout": recommended("Connect Timeout", 1800), |
| 39 | + "remote.SSH.reconnectionGraceTime": recommended( |
| 40 | + "Reconnection Grace Time", |
| 41 | + 86400, |
| 42 | + ), |
| 43 | + "remote.SSH.serverShutdownTimeout": recommended( |
| 44 | + "Server Shutdown Timeout", |
| 45 | + 86400, |
| 46 | + ), |
| 47 | + "remote.SSH.maxReconnectionAttempts": recommended( |
| 48 | + "Max Reconnection Attempts", |
| 49 | + null, |
| 50 | + ), |
| 51 | +} as const satisfies Record<string, RecommendedSetting>; |
| 52 | + |
| 53 | +type SshSettingKey = keyof typeof RECOMMENDED_SSH_SETTINGS; |
| 54 | + |
| 55 | +/** Defaults set during connection when the user hasn't configured a value. */ |
| 56 | +const AUTO_SETUP_DEFAULTS = { |
| 57 | + "remote.SSH.reconnectionGraceTime": 28800, // 8h |
| 58 | + "remote.SSH.serverShutdownTimeout": 28800, // 8h |
| 59 | + "remote.SSH.maxReconnectionAttempts": null, // max allowed |
| 60 | +} as const satisfies Partial<Record<SshSettingKey, number | null>>; |
| 61 | + |
| 62 | +/** |
| 63 | + * Build the list of VS Code setting overrides needed for a remote SSH |
| 64 | + * connection to a Coder workspace. |
| 65 | + */ |
| 66 | +export function buildSshOverrides( |
| 67 | + config: Pick<WorkspaceConfiguration, "get">, |
| 68 | + sshHost: string, |
| 69 | + agentOS: string, |
| 70 | +): SettingOverride[] { |
| 71 | + const overrides: SettingOverride[] = []; |
| 72 | + |
| 73 | + // Set the remote platform for this host to bypass the platform prompt. |
| 74 | + const remotePlatforms = config.get<Record<string, string>>( |
| 75 | + "remote.SSH.remotePlatform", |
| 76 | + {}, |
| 77 | + ); |
| 78 | + if (remotePlatforms[sshHost] !== agentOS) { |
| 79 | + overrides.push({ |
| 80 | + key: "remote.SSH.remotePlatform", |
| 81 | + value: { ...remotePlatforms, [sshHost]: agentOS }, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + // Default 15s is too short for startup scripts; enforce a minimum. |
| 86 | + const connTimeoutKey: SshSettingKey = "remote.SSH.connectTimeout"; |
| 87 | + const { value: minConnTimeout } = RECOMMENDED_SSH_SETTINGS[connTimeoutKey]; |
| 88 | + const connTimeout = config.get<number>(connTimeoutKey); |
| 89 | + if (minConnTimeout && (!connTimeout || connTimeout < minConnTimeout)) { |
| 90 | + overrides.push({ key: connTimeoutKey, value: minConnTimeout }); |
| 91 | + } |
| 92 | + |
| 93 | + // Set conservative defaults for settings the user hasn't configured. |
| 94 | + for (const [key, value] of Object.entries(AUTO_SETUP_DEFAULTS)) { |
| 95 | + if (config.get(key) === undefined) { |
| 96 | + overrides.push({ key, value }); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return overrides; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Apply setting overrides to the user's settings.json file. |
| 105 | + * |
| 106 | + * We munge the file directly with jsonc instead of using the VS Code API |
| 107 | + * because the API hangs indefinitely during remote connection setup (likely |
| 108 | + * a deadlock from trying to update config on the not-yet-connected remote). |
| 109 | + */ |
| 110 | +export async function applySettingOverrides( |
| 111 | + settingsFilePath: string, |
| 112 | + overrides: SettingOverride[], |
| 113 | + logger: Logger, |
| 114 | +): Promise<boolean> { |
| 115 | + if (overrides.length === 0) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + let settingsContent = "{}"; |
| 120 | + try { |
| 121 | + settingsContent = await fs.readFile(settingsFilePath, "utf8"); |
| 122 | + } catch { |
| 123 | + // File probably doesn't exist yet. |
| 124 | + } |
| 125 | + |
| 126 | + for (const { key, value } of overrides) { |
| 127 | + settingsContent = jsonc.applyEdits( |
| 128 | + settingsContent, |
| 129 | + jsonc.modify(settingsContent, [key], value, {}), |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + await fs.writeFile(settingsFilePath, settingsContent); |
| 135 | + return true; |
| 136 | + } catch (ex) { |
| 137 | + // Could be read-only (e.g. home-manager on NixOS). Not catastrophic. |
| 138 | + logger.warn("Failed to configure settings", ex); |
| 139 | + return false; |
| 140 | + } |
| 141 | +} |
0 commit comments