Skip to content
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

Merged
merged 4 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@
"default": true,
"description": "Enable/Disable ansible autocompletion"
},
"ansible.reuseSSHTerminal": {
"type": "boolean",
"default": true,
"description": "Enable/Disable resuing SSH terminal session"
},
"ansible.runPlaybookOptions": {
"type": "string",
"default": "",
Expand Down
109 changes: 77 additions & 32 deletions src/sshRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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);

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?

Copy link
Contributor Author

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

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());

Choose a reason for hiding this comment

The 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?

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down