Skip to content

Commit d58c0c0

Browse files
author
Alexandr Smolaykov
authored
[SSHV0 ] Fix for the task for 176 release (#13680)
* Fix problem with the naming of the script file on the remote host * Extract logic for removing CR LF to separate function * Remove extra variable * Update comments * Bump up task version
1 parent 88156ae commit d58c0c0

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

Tasks/SshV0/ssh.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,8 @@ async function run() {
113113
} else {
114114
// both other runOptions: inline and script
115115
// setup script path on remote machine relative to user's $HOME directory
116-
const remoteScript: string = `./${path.basename(scriptFile)}`;
117-
let remoteScriptPath: string = `${remoteScript}`;
118-
const windowsEncodedRemoteScriptPath: string = remoteScriptPath;
116+
let remoteScriptPath: string = `./${path.basename(scriptFile)}`;
119117
const isWin: boolean = (os.platform() === 'win32');
120-
if (isWin) {
121-
remoteScriptPath = `${remoteScript}._unix`;
122-
}
123118
tl.debug(`remoteScriptPath = ${remoteScriptPath}`);
124119

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

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

151146
//set execute permissions on the script
@@ -162,7 +157,7 @@ async function run() {
162157
//setup command to clean up script file
163158
cleanUpScriptCmd = `rm -f ${remoteScriptPath}`;
164159
if (isWin) {
165-
cleanUpScriptCmd = `rm -f ${remoteScriptPath} ${windowsEncodedRemoteScriptPath}`;
160+
cleanUpScriptCmd = `rm -f ${remoteScriptPath} ${originalScriptPath}`;
166161
}
167162

168163
console.log(runScriptCmd);

Tasks/SshV0/ssh2helpers.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ function handleStreamClose(command: string, stdErrWritten: boolean, defer: Q.Def
6464

6565
/**
6666
* Uses sftp to copy a file to remote machine
67-
* @param src
68-
* @param dest
69-
* @param sftpConfig
67+
* @param {string} absolutePath - Data source for data to copy to the remote server.
68+
* @param {string} remotePath - Path to the remote file to be created on the server.
69+
* @param {SftpClient.ConnectOptions} sftpConfig
7070
* @returns {Promise<string>}
7171
*/
72-
export async function copyScriptToRemoteMachine(src: string, dest: string, sftpConfig: SftpClient.ConnectOptions): Promise<string> {
72+
export async function copyScriptToRemoteMachine(absolutePath: string, remotePath: string, sftpConfig: SftpClient.ConnectOptions): Promise<string> {
7373
const defer = Q.defer<string>();
7474
const sftpClient = new SftpClient();
7575

7676
try {
7777
await sftpClient.connect(sftpConfig);
78-
await sftpClient.put(src, dest);
79-
tl.debug(`Copied script file to remote machine at: ${dest}`);
78+
await sftpClient.put(absolutePath, remotePath);
79+
tl.debug(`Copied script file to remote machine at: ${remotePath}`);
8080
defer.resolve();
8181
} catch (err) {
8282
defer.reject(tl.loc('RemoteCopyFailed', err));
@@ -203,3 +203,30 @@ export interface ScpConfig {
203203
/** For an encrypted private key, this is the passphrase used to decrypt it. */
204204
passphrase?: string;
205205
}
206+
207+
/**
208+
* This function generates a new file with *_unix extension on the remote host
209+
* which contains the same file but without Windows CR LF
210+
* @param {ssh2.Client} sshClientConnection - ssh client instance
211+
* @param {RemoteCommandOptions} remoteCmdOptions
212+
* @param {string} remoteInputFilePath - remote path to target file
213+
* @throws will throw an error if command execution fails on remote host
214+
* @return {string} - path to the generated file
215+
*/
216+
export async function clearFileFromWindowsCRLF(sshClientConnection: ssh2.Client, remoteCmdOptions: RemoteCommandOptions, remoteInputFilePath: string): Promise<string> {
217+
const remoteOutputFilePath = `${remoteInputFilePath}._unix`;
218+
const removeLineEndingsCmd = `tr -d \'\\015\' <${remoteInputFilePath}> ${remoteOutputFilePath}`;
219+
220+
console.log(removeLineEndingsCmd);
221+
222+
try {
223+
tl.debug(`Removing Windows CR LF from ${remoteInputFilePath}`);
224+
await runCommandOnRemoteMachine(removeLineEndingsCmd, sshClientConnection, remoteCmdOptions);
225+
} catch (error) {
226+
throw new Error(error);
227+
}
228+
229+
tl.debug(`Path to generated file = ${remoteOutputFilePath}`);
230+
231+
return remoteOutputFilePath;
232+
}

Tasks/SshV0/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"version": {
1919
"Major": 0,
2020
"Minor": 176,
21-
"Patch": 1
21+
"Patch": 2
2222
},
2323
"demands": [],
2424
"minimumAgentVersion": "2.144.0",

Tasks/SshV0/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"version": {
1919
"Major": 0,
2020
"Minor": 176,
21-
"Patch": 1
21+
"Patch": 2
2222
},
2323
"demands": [],
2424
"minimumAgentVersion": "2.144.0",

0 commit comments

Comments
 (0)