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

[SshV0] Migrated to ssh2-sftp-client #13549

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
192 changes: 87 additions & 105 deletions Tasks/SshV0/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Tasks/SshV0/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"@types/node": "^6.0.101",
"@types/q": "^1.0.7",
"@types/ssh2": "0.5.43",
"@types/ssh2-sftp-client": "^5.2.0",
"azure-pipelines-task-lib": "^2.9.3",
"scp2": "0.5.0",
"ssh2": "0.8.2",
"ssh2-sftp-client": "^5.1.2",
"uuid": "^3.2.1"
}
}
7 changes: 3 additions & 4 deletions Tasks/SshV0/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ async function run() {
// both other runOptions: inline and script
// setup script path on remote machine relative to user's $HOME directory
const remoteScript: string = `./${path.basename(scriptFile)}`;
let remoteScriptPath: string = `"${remoteScript}"`;
let remoteScriptPath: string = `${remoteScript}`;
const windowsEncodedRemoteScriptPath: string = remoteScriptPath;
const isWin: boolean = (os.platform() === 'win32');
if (isWin) {
remoteScriptPath = `"${remoteScript}._unix"`;
remoteScriptPath = `${remoteScript}._unix`;
}
tl.debug(`remoteScriptPath = ${remoteScriptPath}`);

Expand All @@ -127,7 +127,6 @@ async function run() {
host: hostname,
port: port,
username: username,
path: remoteScript
};

if (privateKey) {
Expand All @@ -139,7 +138,7 @@ async function run() {

//copy script file to remote machine
tl.debug('Copying script to remote machine.');
await sshHelper.copyScriptToRemoteMachine(scriptFile, scpConfig);
await sshHelper.copyScriptToRemoteMachine(scriptFile, remoteScriptPath, scpConfig);

//change the line encodings
if (isWin) {
Expand Down
40 changes: 24 additions & 16 deletions Tasks/SshV0/ssh2helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as tl from 'azure-pipelines-task-lib/task';
import * as Q from 'q';
import * as scp2 from 'scp2';
import * as ssh2 from 'ssh2';
import * as SftpClient from 'ssh2-sftp-client';

export class RemoteCommandOptions {
public failOnStdErr: boolean;
Expand Down Expand Up @@ -63,23 +63,33 @@ function handleStreamClose(command: string, stdErrWritten: boolean, defer: Q.Def
}

/**
* Uses scp2 to copy a file to remote machine
* @param scriptFile
* @param scpConfig
* Uses sftp to copy a file to remote machine
* @param src
* @param dest
* @param sftpConfig
* @returns {Promise<string>}
*/
export function copyScriptToRemoteMachine(scriptFile: string, scpConfig: any): Q.Promise<string> {
export async function copyScriptToRemoteMachine(src: string, dest: string, sftpConfig: SftpClient.ConnectOptions): Promise<string> {
const defer = Q.defer<string>();
const sftpClient = new SftpClient();

try {
await sftpClient.connect(sftpConfig);
await sftpClient.put(src, dest);
tl.debug('Copied script file to remote machine at: ' + dest);
anatolybolshakov marked this conversation as resolved.
Show resolved Hide resolved
defer.resolve();
} catch (err) {
defer.reject(tl.loc('RemoteCopyFailed', err));
}

scp2.scp(scriptFile, scpConfig, (err) => {
if (err) {
defer.reject(tl.loc('RemoteCopyFailed', err));
} else {
tl.debug('Copied script file to remote machine at: ' + scpConfig.path);
defer.resolve(scpConfig.path);
}
});

try {
sftpClient.on('error', (err) => {
tl.debug('sftpClient: Ignoring error diconnecting: ' + err);
anatolybolshakov marked this conversation as resolved.
Show resolved Hide resolved
}); // ignore logout errors; see: https://github.com/mscdex/node-imap/issues/695
anatolybolshakov marked this conversation as resolved.
Show resolved Hide resolved
await sftpClient.end();
} catch(err) {
tl.debug('Failed to close SFTP client: ' + err);
anatolybolshakov marked this conversation as resolved.
Show resolved Hide resolved
}
return defer.promise;
}

Expand Down Expand Up @@ -192,6 +202,4 @@ export interface ScpConfig {
privateKey?: string;
/** For an encrypted private key, this is the passphrase used to decrypt it. */
passphrase?: string;
/** String that contains path where the file will be placed on the server */
path: string;
}
6 changes: 3 additions & 3 deletions Tasks/SshV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"version": {
"Major": 0,
"Minor": 176,
"Patch": 0
"Patch": 1
},
"demands": [],
"minimumAgentVersion": "2.102.0",
"minimumAgentVersion": "2.144.0",
"instanceNameFormat": "Run shell $(runOptions) on remote machine",
"groups": [
{
Expand Down Expand Up @@ -138,7 +138,7 @@
}
],
"execution": {
"Node": {
"Node10": {
"target": "ssh.js",
"argumentFormat": ""
}
Expand Down
Loading