-
Notifications
You must be signed in to change notification settings - Fork 48
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
reuse ssh terminal when configured #130
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,24 @@ const browseThePC = 'Browse the PC..'; | |
|
||
export class SSHRunner extends TerminalBaseRunner { | ||
private folderSyncer: FolderSyncer; | ||
private terminalList: { [key: string]: vscode.Terminal } = {}; | ||
|
||
constructor(outputChannel: vscode.OutputChannel) { | ||
super(outputChannel); | ||
|
||
this.folderSyncer = new FolderSyncer(outputChannel); | ||
|
||
vscode.window.onDidCloseTerminal((terminal) => { | ||
|
||
var terminalNames = Object.keys(this.terminalList); | ||
for (let name of terminalNames) { | ||
if (name === terminal.name) { | ||
this.terminalList[name].dispose(); | ||
delete this.terminalList[name]; | ||
break; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
protected getCmds(playbook: string, envs: string[], terminalId: string): string[] { | ||
|
@@ -93,48 +105,81 @@ export class SSHRunner extends TerminalBaseRunner { | |
} | ||
} | ||
|
||
openSSHConsole(this._outputChannel, targetServer) | ||
.then((terminal) => { | ||
if (!terminal) { | ||
this._outputChannel.appendLine('\nSSH connection failed.'); | ||
this._outputChannel.show(); | ||
return; | ||
let terminal = undefined; | ||
|
||
let reuse = utilities.getCodeConfiguration<boolean>('ansible', 'reuseSSHTerminal'); | ||
|
||
if (reuse) { | ||
let terminalNames = Object.keys(this.terminalList); | ||
for (let t of terminalNames) { | ||
if (t === this.getTerminalName(targetServer.host)) { | ||
terminal = this.terminalList[t]; | ||
break; | ||
} | ||
var count: number = 60; | ||
var _localthis = this; | ||
var connected = false; | ||
|
||
const tempFile = path.join(os.tmpdir(), 'vscodeansible-ssh-' + targetServer.host + '.log'); | ||
|
||
var interval = setInterval(function () { | ||
count--; | ||
if (count > 0) { | ||
if (fs.existsSync(tempFile)) { | ||
count = 0; | ||
fs.removeSync(tempFile); | ||
connected = true; | ||
|
||
if (utilities.isTelemetryEnabled()) { | ||
terminal.sendText('export ' + Constants.UserAgentName + '=' + utilities.getUserAgent()); | ||
} | ||
} | ||
|
||
if (terminal) { | ||
terminal.show(); | ||
|
||
cmds.push('ansible-playbook ' + targetPlaybook); | ||
this.sendCommandsToTerminal(terminal, cmds); | ||
|
||
} else { | ||
openSSHConsole(this._outputChannel, targetServer) | ||
.then((term) => { | ||
if (!term) { | ||
this._outputChannel.appendLine('\nSSH connection failed.'); | ||
this._outputChannel.show(); | ||
return; | ||
} | ||
this.terminalList[this.getTerminalName(targetServer.host)] = term; | ||
|
||
var count: number = 60; | ||
var _localthis = this; | ||
var connected = false; | ||
|
||
const tempFile = path.join(os.tmpdir(), 'vscodeansible-ssh-' + targetServer.host + '.log'); | ||
|
||
var interval = setInterval(function () { | ||
count--; | ||
if (count > 0) { | ||
if (fs.existsSync(tempFile)) { | ||
count = 0; | ||
fs.removeSync(tempFile); | ||
connected = true; | ||
|
||
cmds.push('ansible-playbook ' + targetPlaybook); | ||
_localthis.sendCommandsToTerminal(term, cmds); | ||
term.show(); | ||
} | ||
} else { | ||
clearInterval(interval); | ||
|
||
for (let cmd of cmds) { | ||
terminal.sendText(cmd); | ||
if (!connected) { | ||
_localthis._outputChannel.appendLine('\nFailed to connect to ' + targetServer.host + ' after 30 seconds'); | ||
} | ||
|
||
terminal.sendText(_localthis.getRunPlaybookCmd(targetPlaybook)); | ||
terminal.show(); | ||
} | ||
} else { | ||
clearInterval(interval); | ||
}, 500); | ||
}); | ||
} | ||
} | ||
|
||
if (!connected) { | ||
_localthis._outputChannel.appendLine('\nFailed to connect to ' + targetServer.host + ' after 30 seconds'); | ||
} | ||
} | ||
}, 500); | ||
}); | ||
private getTerminalName(host: string): string { | ||
return 'SSH ' + host; | ||
} | ||
|
||
private sendCommandsToTerminal(terminal: vscode.Terminal, cmds: string[]): void { | ||
if (utilities.isTelemetryEnabled()) { | ||
terminal.sendText('export ' + Constants.UserAgentName + '=' + utilities.getUserAgent()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so this will be sent to terminal even if terminal is reused and these variables are already set? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually i think there should be if (telemetry) sendCommands... (telemetry commands) when terminal is created. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those variables may change. |
||
} | ||
|
||
for (let cmd of cmds) { | ||
terminal.sendText(cmd); | ||
} | ||
} | ||
|
||
private getTargetFolder(workspaceRoot: string, playbook: string): string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How will this work with other PR which introduced ansible-playbook params?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge. so pls review them, then i can do merge