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 ] Fix for the task for 176 release #13680

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
15 changes: 5 additions & 10 deletions Tasks/SshV0/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,8 @@ async function run() {
} else {
// 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}`;
const windowsEncodedRemoteScriptPath: string = remoteScriptPath;
let remoteScriptPath: string = `./${path.basename(scriptFile)}`;
const isWin: boolean = (os.platform() === 'win32');
if (isWin) {
remoteScriptPath = `${remoteScript}._unix`;
}
tl.debug(`remoteScriptPath = ${remoteScriptPath}`);

//setup the scp configuration based on endpoint details
Expand All @@ -141,11 +136,11 @@ async function run() {
await sshHelper.copyScriptToRemoteMachine(scriptFile, remoteScriptPath, scpConfig);

//change the line encodings
let originalScriptPath: string = '';
if (isWin) {
tl.debug('Fixing the line endings in case the file was created in Windows');
const removeLineEndingsCmd = `tr -d \'\\015\' <${windowsEncodedRemoteScriptPath}> ${remoteScriptPath}`;
console.log(removeLineEndingsCmd);
await sshHelper.runCommandOnRemoteMachine(removeLineEndingsCmd, sshClientConnection, remoteCmdOptions);
originalScriptPath = remoteScriptPath;
remoteScriptPath = await sshHelper.clearFileFromWindowsCRLF(sshClientConnection, remoteCmdOptions, originalScriptPath);
}

//set execute permissions on the script
Expand All @@ -162,7 +157,7 @@ async function run() {
//setup command to clean up script file
cleanUpScriptCmd = `rm -f ${remoteScriptPath}`;
if (isWin) {
cleanUpScriptCmd = `rm -f ${remoteScriptPath} ${windowsEncodedRemoteScriptPath}`;
cleanUpScriptCmd = `rm -f ${remoteScriptPath} ${originalScriptPath}`;
}

console.log(runScriptCmd);
Expand Down
39 changes: 33 additions & 6 deletions Tasks/SshV0/ssh2helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ function handleStreamClose(command: string, stdErrWritten: boolean, defer: Q.Def

/**
* Uses sftp to copy a file to remote machine
* @param src
* @param dest
* @param sftpConfig
* @param {string} absolutePath - Data source for data to copy to the remote server.
* @param {string} remotePath - Path to the remote file to be created on the server.
* @param {SftpClient.ConnectOptions} sftpConfig
* @returns {Promise<string>}
*/
export async function copyScriptToRemoteMachine(src: string, dest: string, sftpConfig: SftpClient.ConnectOptions): Promise<string> {
export async function copyScriptToRemoteMachine(absolutePath: string, remotePath: 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}`);
await sftpClient.put(absolutePath, remotePath);
tl.debug(`Copied script file to remote machine at: ${remotePath}`);
defer.resolve();
} catch (err) {
defer.reject(tl.loc('RemoteCopyFailed', err));
Expand Down Expand Up @@ -203,3 +203,30 @@ export interface ScpConfig {
/** For an encrypted private key, this is the passphrase used to decrypt it. */
passphrase?: string;
}

/**
* This function generates a new file with *_unix extension on the remote host
* which contains the same file but without Windows CR LF
* @param {ssh2.Client} sshClientConnection - ssh client instance
* @param {RemoteCommandOptions} remoteCmdOptions
* @param {string} remoteInputFilePath - remote path to target file
* @throws will throw an error if command execution fails on remote host
* @return {string} - path to the generated file
*/
export async function clearFileFromWindowsCRLF(sshClientConnection: ssh2.Client, remoteCmdOptions: RemoteCommandOptions, remoteInputFilePath: string): Promise<string> {
const remoteOutputFilePath = `${remoteInputFilePath}._unix`;
const removeLineEndingsCmd = `tr -d \'\\015\' <${remoteInputFilePath}> ${remoteOutputFilePath}`;

console.log(removeLineEndingsCmd);

try {
tl.debug(`Removing Windows CR LF from ${remoteInputFilePath}`);
await runCommandOnRemoteMachine(removeLineEndingsCmd, sshClientConnection, remoteCmdOptions);
} catch (error) {
throw new Error(error);
}

tl.debug(`Path to generated file = ${remoteOutputFilePath}`);

return remoteOutputFilePath;
}
2 changes: 1 addition & 1 deletion Tasks/SshV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 0,
"Minor": 176,
"Patch": 1
"Patch": 2
},
"demands": [],
"minimumAgentVersion": "2.144.0",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/SshV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 0,
"Minor": 176,
"Patch": 1
"Patch": 2
},
"demands": [],
"minimumAgentVersion": "2.144.0",
Expand Down