Skip to content

Commit 4cd61e1

Browse files
committed
implement dry run
1 parent 3ff2453 commit 4cd61e1

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

packages/core/src/awsService/ec2/model.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { SsmClient } from '../../shared/clients/ssmClient'
1313
import { Ec2Client } from '../../shared/clients/ec2Client'
1414
import {
1515
VscodeRemoteConnection,
16+
createBoundProcess,
1617
ensureDependencies,
1718
getDeniedSsmActions,
1819
openRemoteTerminal,
@@ -26,7 +27,6 @@ import {
2627
startVscodeRemote,
2728
testSshConnection,
2829
} from '../../shared/extensions/ssh'
29-
import { createBoundProcess } from '../../codecatalyst/model'
3030
import { getLogger } from '../../shared/logger/logger'
3131
import { CancellationError, Timeout } from '../../shared/utilities/timeoutUtils'
3232
import { showMessageWithCancel } from '../../shared/utilities/messages'
@@ -200,7 +200,7 @@ export class Ec2Connecter implements vscode.Disposable {
200200
const remoteEnv = await this.prepareEc2RemoteEnvWithProgress(selection, remoteUser)
201201

202202
try {
203-
await testSshConnection(remoteEnv.sshPath, remoteEnv.hostname)
203+
await testSshConnection(remoteEnv.SessionProcess, remoteEnv.hostname, remoteEnv.sshPath, remoteUser)
204204
await startVscodeRemote(remoteEnv.SessionProcess, remoteEnv.hostname, '/', remoteEnv.vscPath, remoteUser)
205205
} catch (err) {
206206
this.throwGeneralConnectionError(selection, err as Error)
@@ -214,6 +214,12 @@ export class Ec2Connecter implements vscode.Disposable {
214214
return remoteEnv
215215
}
216216

217+
private async startRemoteSession(instanceId: string): Promise<SSM.StartSessionResponse> {
218+
const ssmSession = await this.ssmClient.startSession(instanceId, 'AWS-StartSSHSession')
219+
await this.addActiveSession(instanceId, ssmSession.SessionId!)
220+
return ssmSession
221+
}
222+
217223
public async prepareEc2RemoteEnv(selection: Ec2Selection, remoteUser: string): Promise<Ec2RemoteEnv> {
218224
const logger = this.configureRemoteConnectionLogger(selection.instanceId)
219225
const { ssm, vsc, ssh } = (await ensureDependencies()).unwrap()
@@ -230,8 +236,7 @@ export class Ec2Connecter implements vscode.Disposable {
230236
throw err
231237
}
232238

233-
const ssmSession = await this.ssmClient.startSession(selection.instanceId, 'AWS-StartSSHSession')
234-
await this.addActiveSession(selection.instanceId, ssmSession.SessionId!)
239+
const ssmSession = await this.startRemoteSession(selection.instanceId)
235240

236241
const vars = getEc2SsmEnv(selection, ssm, ssmSession)
237242
const envProvider = async () => {

packages/core/src/codecatalyst/model.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { getLogger } from '../shared/logger'
1919
import { AsyncCollection, toCollection } from '../shared/utilities/asyncCollection'
2020
import { getCodeCatalystSpaceName, getCodeCatalystProjectName, getCodeCatalystDevEnvId } from '../shared/vscode/env'
2121
import { sshAgentSocketVariable, startSshAgent, startVscodeRemote } from '../shared/extensions/ssh'
22-
import { ChildProcess } from '../shared/utilities/processUtils'
2322
import { isDevenvVscode } from './utils'
2423
import { Timeout } from '../shared/utilities/timeoutUtils'
2524
import { Commands } from '../shared/vscode/commands2'
@@ -28,7 +27,7 @@ import { fileExists } from '../shared/filesystemUtilities'
2827
import { CodeCatalystAuthenticationProvider } from './auth'
2928
import { ToolkitError } from '../shared/errors'
3029
import { Result } from '../shared/utilities/result'
31-
import { VscodeRemoteConnection, ensureDependencies } from '../shared/remoteSession'
30+
import { EnvProvider, VscodeRemoteConnection, createBoundProcess, ensureDependencies } from '../shared/remoteSession'
3231
import { SshConfig, sshLogFileLocation } from '../shared/sshConfig'
3332
import { fs } from '../shared'
3433

@@ -111,28 +110,6 @@ export function createCodeCatalystEnvProvider(
111110
}
112111
}
113112

114-
type EnvProvider = () => Promise<NodeJS.ProcessEnv>
115-
116-
/**
117-
* Creates a new {@link ChildProcess} class bound to a specific dev environment. All instances of this
118-
* derived class will have SSM session information injected as environment variables as-needed.
119-
*/
120-
export function createBoundProcess(envProvider: EnvProvider): typeof ChildProcess {
121-
type Run = ChildProcess['run']
122-
return class SessionBoundProcess extends ChildProcess {
123-
public override async run(...args: Parameters<Run>): ReturnType<Run> {
124-
const options = args[0]
125-
const envVars = await envProvider()
126-
const spawnOptions = {
127-
...options?.spawnOptions,
128-
env: { ...envVars, ...options?.spawnOptions?.env },
129-
}
130-
131-
return super.run({ ...options, spawnOptions })
132-
}
133-
}
134-
}
135-
136113
export async function cacheBearerToken(bearerToken: string, devenvId: string): Promise<void> {
137114
await fs.writeFile(bearerTokenCacheLocation(devenvId), `${bearerToken}`, 'utf8')
138115
}

packages/core/src/shared/extensions/ssh.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as path from 'path'
88
import * as nls from 'vscode-nls'
99
import fs from '../fs/fs'
1010
import { getLogger } from '../logger'
11-
import { ChildProcess, ChildProcessOptions } from '../utilities/processUtils'
11+
import { ChildProcess } from '../utilities/processUtils'
1212
import { ArrayConstructor, NonNullObject } from '../utilities/typeConstructors'
1313
import { Settings } from '../settings'
1414
import { VSCODE_EXTENSION_ID } from '../extensions'
@@ -120,13 +120,17 @@ export class RemoteSshSettings extends Settings.define('remote.SSH', remoteSshTy
120120
}
121121

122122
export async function testSshConnection(
123-
sshPath: string,
123+
ProcessClass: typeof ChildProcess,
124124
hostname: string,
125-
options: ChildProcessOptions = {}
125+
sshPath: string,
126+
user: string
126127
): Promise<void> {
127-
const process = new ChildProcess(sshPath, ['-T', hostname], options)
128-
const result = await process.run()
129-
console.log(result)
128+
try {
129+
await new ProcessClass(sshPath, ['-T', `${user}@${hostname}`, 'echo connected && exit']).run()
130+
} catch (error) {
131+
getLogger().error('SSH connection test failed: %O', error)
132+
throw error
133+
}
130134
}
131135

132136
export async function startVscodeRemote(

packages/core/src/shared/remoteSession.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ interface DependencyPaths {
7777
readonly ssh: string
7878
}
7979

80-
type EnvProvider = () => Promise<NodeJS.ProcessEnv>
80+
export type EnvProvider = () => Promise<NodeJS.ProcessEnv>
8181

8282
export interface VscodeRemoteConnection {
8383
readonly sshPath: string
@@ -251,3 +251,23 @@ export async function getDeniedSsmActions(client: IamClient, roleArn: string): P
251251

252252
return deniedActions
253253
}
254+
255+
/**
256+
* Creates a new {@link ChildProcess} class bound to a specific dev environment. All instances of this
257+
* derived class will have SSM session information injected as environment variables as-needed.
258+
*/
259+
export function createBoundProcess(envProvider: EnvProvider): typeof ChildProcess {
260+
type Run = ChildProcess['run']
261+
return class SessionBoundProcess extends ChildProcess {
262+
public override async run(...args: Parameters<Run>): ReturnType<Run> {
263+
const options = args[0]
264+
const envVars = await envProvider()
265+
const spawnOptions = {
266+
...options?.spawnOptions,
267+
env: { ...envVars, ...options?.spawnOptions?.env },
268+
}
269+
270+
return super.run({ ...options, spawnOptions })
271+
}
272+
}
273+
}

0 commit comments

Comments
 (0)