Skip to content

Commit

Permalink
fix: support remote-ssh customConfigFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mlhiter committed Nov 8, 2024
1 parent 6659db0 commit 5303496
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion extensions/ide/vscode/devbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "devbox-aio",
"displayName": "Devbox",
"description": "help code for cloud devbox in sailos/sealos",
"version": "0.9.4",
"version": "0.9.5",
"keywords": [
"devbox",
"remote development",
Expand Down
42 changes: 35 additions & 7 deletions extensions/ide/vscode/devbox/src/commands/remoteConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,51 @@ export class RemoteSSHConnector extends Disposable {
)
}
}

private replaceHomePathInConfig(content: string): string {
return content.replace(
/Include ~\/.ssh\/sealos\/devbox_config/g,
`Include ${os.homedir()}/.ssh/sealos/devbox_config`
)
}

private sshConfigPreProcess() {
// 1. ensure .ssh/config exists
ensureFileExists(defaultSSHConfigPath, '.ssh')
// 2. ensure .ssh/sealos/devbox_config exists
ensureFileExists(defaultDevboxSSHConfigPath, '.ssh/sealos')
// 3. ensure .ssh/config includes .ssh/sealos/devbox_config
const existingSSHConfig = fs.readFileSync(defaultSSHConfigPath, 'utf8')
if (!existingSSHConfig.includes('Include ~/.ssh/sealos/devbox_config')) {
let existingSSHConfig = fs.readFileSync(defaultSSHConfigPath, 'utf-8')
const newConfig =
'Include ~/.ssh/sealos/devbox_config\n' + existingSSHConfig
fs.writeFileSync(defaultSSHConfigPath, newConfig)

const customConfigFile = vscode.workspace
.getConfiguration('remote.SSH')
.get<string>('configFile', '')

if (customConfigFile) {
const resolvedPath = customConfigFile.replace(/^~/, os.homedir())
try {
const existingSSHConfig = fs.readFileSync(resolvedPath, 'utf8')
const updatedConfig = this.replaceHomePathInConfig(existingSSHConfig)
if (updatedConfig !== existingSSHConfig) {
fs.writeFileSync(resolvedPath, updatedConfig)
}
} catch (error) {
console.error(`Error reading/writing SSH config: ${error}`)
this.handleDefaultSSHConfig()
}
} else {
this.handleDefaultSSHConfig()
}
// 4. ensure sshConfig from version1 to version2
convertSSHConfigToVersion2(defaultDevboxSSHConfigPath)
}

private handleDefaultSSHConfig() {
const existingSSHConfig = fs.readFileSync(defaultSSHConfigPath, 'utf8')
const updatedConfig = this.replaceHomePathInConfig(existingSSHConfig)
if (updatedConfig !== existingSSHConfig) {
fs.writeFileSync(defaultSSHConfigPath, updatedConfig)
}
}

private async connectRemoteSSH(args: {
sshDomain: string
sshPort: string
Expand Down

0 comments on commit 5303496

Please sign in to comment.