Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ansibleUtils from './ansibleUtils';
import { RemoteCommandOptions } from './ansibleUtils'
import { ansibleTaskParameters } from './ansibleTaskParameters';
import { ansibleCommandLineInterface } from './ansibleCommandLineInterface';
import * as SftpClient from 'ssh2-sftp-client';

var os = require('os');
var shell = require('shelljs');
Expand Down Expand Up @@ -39,16 +40,16 @@ export class ansibleRemoteMachineInterface extends ansibleCommandLineInterface {
if (!ansibleUtils.testIfFileExist(path.join(playbookRoot,playbookFile))) {
throw tl.loc('PlaybookNotPresent', playbookFile, playbookRoot);
}

var remotePlaybookRoot = '/tmp/' + path.basename(playbookRoot);
var playbookFullPath = playbookRoot + '/' + playbookFile;
var remotePlaybookRoot = '/tmp/' + path.basename(playbookFile);
tl.debug('ansiblePlaybookRootPath = ' + '"' + remotePlaybookRoot + '"');

let scpConfig = this._sshConfig || {};
scpConfig.path = remotePlaybookRoot;
tl.debug('Copying playbook to ansible machine.');
this._playbookPath = remotePlaybookRoot + "/" + playbookFile;
this._playbookPath = remotePlaybookRoot;
this._cleanupCmd.push('rm -rf ' + remotePlaybookRoot);
await ansibleUtils.copyFileToRemoteMachine(playbookRoot, scpConfig);
await ansibleUtils.copyFileToRemoteMachine(playbookFullPath,remotePlaybookRoot, scpConfig);
}

private async copyInventoryAndSetPathForAgentAsSource() {
Expand All @@ -64,7 +65,7 @@ export class ansibleRemoteMachineInterface extends ansibleCommandLineInterface {
tl.debug('Copying Inventory file to ansible machine.');
this._inventoryPath = remoteInventory;
this._cleanupCmd.push('rm -f ' + remoteInventory);
await ansibleUtils.copyFileToRemoteMachine(inventoryFile, scpConfig);
await ansibleUtils.copyFileToRemoteMachine(inventoryFile, remoteInventory, scpConfig);
}

protected async executeCommand(cmd: string): Promise<string> {
Expand Down
38 changes: 25 additions & 13 deletions Extensions/Ansible/Src/Tasks/Ansible/ansibleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import tl = require("azure-pipelines-task-lib/task");
import Q = require("q");
import util = require("util");
import querystring = require('querystring');
import * as SftpClient from 'ssh2-sftp-client';

var uuid = require('uuid/v4');
var httpClient = require('vso-node-api/HttpClient');
var httpObj = new httpClient.HttpCallbackClient(tl.getVariable("AZURE_HTTP_USER_AGENT"));

var os = require('os');
var Ssh2Client = require('ssh2').Client;
var Scp2Client = require('scp2');
var shell = require('shelljs');

var _outStream = process.stdout;
Expand All @@ -22,22 +22,34 @@ export class RemoteCommandOptions {
}

/**
* 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>|Promise<T>}
*/
export function copyFileToRemoteMachine(scriptFile: string, scpConfig: any): Q.Promise<string> {
export async function copyFileToRemoteMachine(src: string, dest: string, sftpConfig: SftpClient.ConnectOptions): Promise<string> {
var defer = Q.defer<string>();

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

try {
await sftpClient.connect(sftpConfig);
await sftpClient.put(src, dest);
tl.debug('Copied script file to remote machine at: ${dest}');
defer.resolve('0');
} catch (err) {
defer.reject(tl.loc('RemoteCopyFailed', err));
}

try {
sftpClient.on('error', (err) => {
tl.debug(`sftpClient: Ignoring error diconnecting: ${err}`);
}); // ignore logout errors - since there could be spontaneous ECONNRESET errors after logout; see: https://github.com/mscdex/node-imap/issues/695
await sftpClient.end();
} catch(err) {
tl.debug('Failed to close SFTP client: ${err}');
}

return defer.promise;
}
Expand Down
Loading