Skip to content

Commit

Permalink
fix scp error when private key invalid, but no error out (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
yungezz authored May 4, 2018
1 parent 29543f7 commit 1990173
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Ansible",
"description": "VSCode extension for Ansible",
"license": "SEE LICENSE IN LICENSE.md",
"version": "0.1.5",
"version": "0.2.6",
"publisher": "vscoss",
"icon": "images/logo.png",
"engines": {
Expand Down Expand Up @@ -195,4 +195,4 @@
"ws": "^3.3.2",
"yamljs": "^0.3.0"
}
}
}
2 changes: 1 addition & 1 deletion src/cloudConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const OSes: Record<string, OS> = {
};

export async function openCloudConsole(api: AzureAccount, os: OS, files, outputChannel: OutputChannel, tempFile: string) {
const progress = delayedInterval(() => { outputChannel.append('.') }, 800);
const progress = delayedInterval(() => { outputChannel.append('.') }, 500);
return (async function retry(): Promise<any> {
outputChannel.append('\nConnecting to Cloud Shell.');
outputChannel.show();
Expand Down
79 changes: 50 additions & 29 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { platform } from 'os';
import { SSHServer } from './interfaces';
import * as scp from 'scp2';
import { clearInterval } from 'timers';
import * as ssh from 'ssh2';

const sshConfigFile = path.join(os.homedir(), '.ssh', 'servers.json');

Expand Down Expand Up @@ -135,7 +136,7 @@ export function parseCredentialsFile(outputChannel): string[] {
var configValue = getCredentialsFile();

if (outputChannel != null) {
outputChannel.appendLine('\nCredential file: ' + configValue);
outputChannel.appendLine('Credential file: ' + configValue);
outputChannel.show();
}
var credentials = [];
Expand Down Expand Up @@ -216,38 +217,58 @@ export function copyFilesRemote(source: string, dest: string, sshServer: SSHServ

var client: {};

try {
if (sshServer.password) {
client = {
host: sshServer.host,
port: sshServer.port,
username: sshServer.user,
password: sshServer.password,
path: dest
};
} else if (sshServer.key) {
if (!fsExtra.existsSync(sshServer.key)) {
vscode.window.showErrorMessage('File not exists: ' + sshServer.key);
}
client = {
host: sshServer.host,
port: sshServer.port,
username: sshServer.user,
privateKey: String(fsExtra.readFileSync(sshServer.key)),
path: dest
};
if (sshServer.password) {
client = {
host: sshServer.host,
port: sshServer.port,
username: sshServer.user,
password: sshServer.password,
path: dest
};

} else if (sshServer.key) {
if (!fsExtra.existsSync(sshServer.key)) {
vscode.window.showErrorMessage('File does not exist: ' + sshServer.key);
reject('File does not exist: ' + sshServer.key);
}
} catch (err) {
reject(err);

client = {
host: sshServer.host,
port: sshServer.port,
username: sshServer.user,
privateKey: String(fsExtra.readFileSync(sshServer.key)),
path: dest
};
}

scp.scp(source, client, (err) => {
if (err) {
vscode.window.showErrorMessage('Failed to copy ' + source + ' to ' + sshServer.host + ': ' + err);
try {
var conn = new ssh.Client();

conn.connect({
host: sshServer.host,
port: sshServer.port,
username: sshServer.user,
password: sshServer.password,
privateKey: (sshServer.key === null || sshServer.key === undefined) ? sshServer.key : fsExtra.readFileSync(sshServer.key)
});

conn.on('error', (err) => {
reject(err);
}
resolve();
});
});

conn.end();

scp.scp(source, client, (err) => {
if (err) {
vscode.window.showErrorMessage('Failed to copy ' + source + ' to ' + sshServer.host + ': ' + err);
return reject(err);
}
return resolve();
});

} catch (err) {
reject('scp error: ' + err);
}
});
}

Expand Down

0 comments on commit 1990173

Please sign in to comment.