-
Notifications
You must be signed in to change notification settings - Fork 40
feat: set remote.SSH.reconnectionGraceTime and refactor settings manipulation #826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b09d2b
feat: set reconnectionGraceTime to 8h, extract settings helper
EhabY ead3b59
feat: add "Apply Recommended SSH Settings" command and set defaults f…
EhabY 4b6af7f
feat: add confirmation dialog, split auto/recommended SSH defaults
EhabY 340f560
Review comments
EhabY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { formatDuration, intervalToDuration } from "date-fns"; | ||
| import * as jsonc from "jsonc-parser"; | ||
| import * as fs from "node:fs/promises"; | ||
|
|
||
| import type { WorkspaceConfiguration } from "vscode"; | ||
|
|
||
| import type { Logger } from "../logging/logger"; | ||
|
|
||
| export interface SettingOverride { | ||
| key: string; | ||
| value: unknown; | ||
| } | ||
|
|
||
| interface RecommendedSetting { | ||
| readonly value: number | null; | ||
| readonly label: string; | ||
| } | ||
|
|
||
| function recommended( | ||
| shortName: string, | ||
| value: number | null, | ||
| ): RecommendedSetting { | ||
| if (value === null) { | ||
| return { value, label: `${shortName}: max allowed` }; | ||
| } | ||
| const humanized = formatDuration( | ||
| intervalToDuration({ start: 0, end: value * 1000 }), | ||
| ); | ||
| return { value, label: `${shortName}: ${humanized}` }; | ||
| } | ||
|
|
||
| /** | ||
| * Applied by the "Apply Recommended SSH Settings" command. | ||
| * These are more aggressive (24h) than AUTO_SETUP_DEFAULTS (8h) because the | ||
| * user is explicitly opting in via the command palette. | ||
| */ | ||
| export const RECOMMENDED_SSH_SETTINGS = { | ||
| "remote.SSH.connectTimeout": recommended("Connect Timeout", 1800), | ||
| "remote.SSH.reconnectionGraceTime": recommended( | ||
| "Reconnection Grace Time", | ||
| 86400, | ||
| ), | ||
| "remote.SSH.serverShutdownTimeout": recommended( | ||
| "Server Shutdown Timeout", | ||
| 86400, | ||
| ), | ||
| "remote.SSH.maxReconnectionAttempts": recommended( | ||
| "Max Reconnection Attempts", | ||
| null, | ||
| ), | ||
| } as const satisfies Record<string, RecommendedSetting>; | ||
|
|
||
| type SshSettingKey = keyof typeof RECOMMENDED_SSH_SETTINGS; | ||
|
|
||
| /** Defaults set during connection when the user hasn't configured a value. */ | ||
| const AUTO_SETUP_DEFAULTS = { | ||
| "remote.SSH.reconnectionGraceTime": 28800, // 8h | ||
| "remote.SSH.serverShutdownTimeout": 28800, // 8h | ||
| "remote.SSH.maxReconnectionAttempts": null, // max allowed | ||
| } as const satisfies Partial<Record<SshSettingKey, number | null>>; | ||
|
|
||
| /** | ||
| * Build the list of VS Code setting overrides needed for a remote SSH | ||
| * connection to a Coder workspace. | ||
| */ | ||
| export function buildSshOverrides( | ||
| config: Pick<WorkspaceConfiguration, "get">, | ||
| sshHost: string, | ||
| agentOS: string, | ||
| ): SettingOverride[] { | ||
| const overrides: SettingOverride[] = []; | ||
|
|
||
| // Set the remote platform for this host to bypass the platform prompt. | ||
| const remotePlatforms = config.get<Record<string, string>>( | ||
| "remote.SSH.remotePlatform", | ||
| {}, | ||
| ); | ||
| if (remotePlatforms[sshHost] !== agentOS) { | ||
| overrides.push({ | ||
| key: "remote.SSH.remotePlatform", | ||
| value: { ...remotePlatforms, [sshHost]: agentOS }, | ||
| }); | ||
| } | ||
|
|
||
| // Default 15s is too short for startup scripts; enforce a minimum. | ||
| const connTimeoutKey: SshSettingKey = "remote.SSH.connectTimeout"; | ||
| const { value: minConnTimeout } = RECOMMENDED_SSH_SETTINGS[connTimeoutKey]; | ||
| const connTimeout = config.get<number>(connTimeoutKey); | ||
| if (minConnTimeout && (!connTimeout || connTimeout < minConnTimeout)) { | ||
| overrides.push({ key: connTimeoutKey, value: minConnTimeout }); | ||
| } | ||
|
|
||
| // Set conservative defaults for settings the user hasn't configured. | ||
| for (const [key, value] of Object.entries(AUTO_SETUP_DEFAULTS)) { | ||
| if (config.get(key) === undefined) { | ||
| overrides.push({ key, value }); | ||
| } | ||
| } | ||
|
|
||
| return overrides; | ||
| } | ||
|
|
||
| /** | ||
| * Apply setting overrides to the user's settings.json file. | ||
| * | ||
| * We munge the file directly with jsonc instead of using the VS Code API | ||
| * because the API hangs indefinitely during remote connection setup (likely | ||
| * a deadlock from trying to update config on the not-yet-connected remote). | ||
| */ | ||
| export async function applySettingOverrides( | ||
| settingsFilePath: string, | ||
| overrides: SettingOverride[], | ||
| logger: Logger, | ||
| ): Promise<boolean> { | ||
| if (overrides.length === 0) { | ||
| return true; | ||
| } | ||
|
|
||
| let settingsContent = "{}"; | ||
| try { | ||
| settingsContent = await fs.readFile(settingsFilePath, "utf8"); | ||
| } catch { | ||
| // File probably doesn't exist yet. | ||
| } | ||
|
|
||
| for (const { key, value } of overrides) { | ||
| settingsContent = jsonc.applyEdits( | ||
| settingsContent, | ||
EhabY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| jsonc.modify(settingsContent, [key], value, {}), | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| await fs.writeFile(settingsFilePath, settingsContent); | ||
| return true; | ||
| } catch (ex) { | ||
| // Could be read-only (e.g. home-manager on NixOS). Not catastrophic. | ||
| logger.warn("Failed to configure settings", ex); | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.