1
- import Q = require( 'q' ) ;
2
1
import tl = require( 'azure-pipelines-task-lib/task' ) ;
3
- const path = require ( 'path' ) ;
4
2
var Ssh2Client = require ( 'ssh2' ) . Client ;
5
3
var SftpClient = require ( 'ssh2-sftp-client' ) ;
6
4
@@ -30,27 +28,27 @@ export class SshHelper {
30
28
}
31
29
32
30
private async setupSshClientConnection ( ) : Promise < void > {
33
- const defer = Q . defer < void > ( ) ;
34
- this . sshClient = new Ssh2Client ( ) ;
35
- this . sshClient . once ( 'ready' , ( ) => {
36
- defer . resolve ( ) ;
37
- } ) . once ( 'error' , ( err ) => {
38
- defer . reject ( tl . loc ( 'ConnectionFailed' , err ) ) ;
39
- } ) . connect ( this . sshConfig ) ;
40
- await defer . promise ;
31
+ return new Promise ( ( resolve , reject ) => {
32
+ this . sshClient = new Ssh2Client ( ) ;
33
+ this . sshClient . once ( 'ready' , ( ) => {
34
+ resolve ( ) ;
35
+ } ) . once ( 'error' , ( err ) => {
36
+ reject ( tl . loc ( 'ConnectionFailed' , err ) ) ;
37
+ } ) . connect ( this . sshConfig ) ;
38
+ } ) ;
41
39
}
42
40
43
41
private async setupSftpConnection ( ) : Promise < void > {
44
- const defer = Q . defer < void > ( ) ;
45
- try {
46
- this . sftpClient = new SftpClient ( ) ;
47
- await this . sftpClient . connect ( this . sshConfig ) ;
48
- defer . resolve ( ) ;
49
- } catch ( err ) {
50
- this . sftpClient = null ;
51
- defer . reject ( tl . loc ( 'ConnectionFailed' , err ) ) ;
52
- }
53
- await defer . promise ;
42
+ return new Promise ( async ( resolve , reject ) => {
43
+ try {
44
+ this . sftpClient = new SftpClient ( ) ;
45
+ await this . sftpClient . connect ( this . sshConfig ) ;
46
+ resolve ( ) ;
47
+ } catch ( err ) {
48
+ this . sftpClient = null ;
49
+ reject ( tl . loc ( 'ConnectionFailed' , err ) ) ;
50
+ }
51
+ } ) ;
54
52
}
55
53
56
54
/**
@@ -106,16 +104,7 @@ export class SshHelper {
106
104
return new Promise ( async ( resolve , reject ) => {
107
105
if ( ! this . sftpClient ) {
108
106
reject ( tl . loc ( 'ConnectionNotSetup' ) ) ;
109
- }
110
-
111
- const remotePath = path . dirname ( dest ) ;
112
-
113
- try {
114
- if ( ! await this . sftpClient . exists ( remotePath ) ) {
115
- await this . sftpClient . mkdir ( remotePath , true ) ;
116
- }
117
- } catch ( error ) {
118
- reject ( tl . loc ( 'TargetNotCreated' , remotePath ) ) ;
107
+ return ;
119
108
}
120
109
121
110
try {
@@ -124,36 +113,55 @@ export class SshHelper {
124
113
} else {
125
114
await this . sftpClient . put ( sourceFile , dest ) ;
126
115
}
116
+
127
117
resolve ( dest ) ;
128
118
} catch ( err ) {
129
119
reject ( tl . loc ( 'UploadFileFailed' , sourceFile , dest , err ) ) ;
130
120
}
131
121
} ) ;
132
122
}
133
123
124
+ async uploadFolder ( sourceFolder : string , destFolder : string ) : Promise < string > {
125
+ tl . debug ( 'Upload ' + sourceFolder + ' to ' + destFolder + ' on remote machine.' ) ;
126
+
127
+ return new Promise ( async ( resolve , reject ) => {
128
+ if ( ! this . sftpClient ) {
129
+ reject ( tl . loc ( 'ConnectionNotSetup' ) ) ;
130
+ return ;
131
+ }
132
+
133
+ try {
134
+ await this . sftpClient . uploadDir ( sourceFolder , destFolder ) ;
135
+ } catch ( err ) {
136
+ reject ( tl . loc ( 'UploadFolderFailed' , sourceFolder , destFolder , err ) ) ;
137
+ }
138
+ } ) ;
139
+ }
140
+
134
141
/**
135
142
* Returns true if the path exists on remote machine, false if it does not exist
136
143
* @param path
137
144
* @returns {Promise<boolean> }
138
145
*/
139
146
async checkRemotePathExists ( path : string ) : Promise < boolean > {
140
- var defer = Q . defer < boolean > ( ) ;
147
+ return new Promise ( async ( resolve , reject ) => {
148
+ tl . debug ( tl . loc ( 'CheckingPathExistance' , path ) ) ;
141
149
142
- tl . debug ( tl . loc ( 'CheckingPathExistance' , path ) ) ;
143
- if ( ! this . sftpClient ) {
144
- defer . reject ( tl . loc ( 'ConnectionNotSetup' ) ) ;
145
- }
146
- if ( await this . sftpClient . exists ( path ) ) {
147
- //path exists
148
- tl . debug ( tl . loc ( 'PathExists' , path ) ) ;
149
- defer . resolve ( true ) ;
150
- } else {
151
- //path does not exist
152
- tl . debug ( tl . loc ( 'PathNotExists' , path ) ) ;
153
- defer . resolve ( false ) ;
154
- }
150
+ if ( ! this . sftpClient ) {
151
+ reject ( tl . loc ( 'ConnectionNotSetup' ) ) ;
152
+ return ;
153
+ }
155
154
156
- return defer . promise ;
155
+ if ( await this . sftpClient . exists ( path ) ) {
156
+ //path exists
157
+ tl . debug ( tl . loc ( 'PathExists' , path ) ) ;
158
+ resolve ( true ) ;
159
+ } else {
160
+ //path does not exist
161
+ tl . debug ( tl . loc ( 'PathNotExists' , path ) ) ;
162
+ resolve ( false ) ;
163
+ }
164
+ } ) ;
157
165
}
158
166
159
167
/**
@@ -162,58 +170,64 @@ export class SshHelper {
162
170
* @param options
163
171
* @returns {Promise<string> }
164
172
*/
165
- runCommandOnRemoteMachine ( command : string , options : RemoteCommandOptions ) : Q . Promise < string > {
166
- var defer = Q . defer < string > ( ) ;
167
- var stdErrWritten : boolean = false ;
173
+ runCommandOnRemoteMachine ( command : string , options : RemoteCommandOptions ) : Promise < string > {
174
+ return new Promise ( ( resolve , reject ) => {
175
+ let stdErrWritten = false ;
168
176
169
- if ( ! this . sshClient ) {
170
- defer . reject ( tl . loc ( 'ConnectionNotSetup' ) ) ;
171
- }
177
+ if ( ! this . sshClient ) {
178
+ reject ( tl . loc ( 'ConnectionNotSetup' ) ) ;
179
+ return ;
180
+ }
172
181
173
- if ( ! options ) {
174
- tl . debug ( 'Options not passed to runCommandOnRemoteMachine, setting defaults.' ) ;
175
- var options = new RemoteCommandOptions ( ) ;
176
- options . failOnStdErr = true ;
177
- }
182
+ if ( ! options ) {
183
+ tl . debug ( 'Options not passed to runCommandOnRemoteMachine, setting defaults.' ) ;
184
+ const options = new RemoteCommandOptions ( ) ;
185
+ options . failOnStdErr = true ;
186
+ }
178
187
179
- var cmdToRun = command ;
180
- if ( cmdToRun . indexOf ( ';' ) > 0 ) {
181
- //multiple commands were passed separated by ;
182
- cmdToRun = cmdToRun . replace ( / ; / g, '\n' ) ;
183
- }
184
- tl . debug ( 'cmdToRun = ' + cmdToRun ) ;
188
+ let cmdToRun = command ;
185
189
186
- this . sshClient . exec ( cmdToRun , ( err , stream ) => {
187
- if ( err ) {
188
- defer . reject ( tl . loc ( 'RemoteCmdExecutionErr' , cmdToRun , err ) )
190
+ if ( cmdToRun . indexOf ( ';' ) > 0 ) {
191
+ //multiple commands were passed separated by ;
192
+ cmdToRun = cmdToRun . replace ( / ; / g , '\n' ) ;
189
193
}
190
- stream . on ( 'close' , ( code , signal ) => {
191
- tl . debug ( 'code = ' + code + ', signal = ' + signal ) ;
192
- if ( code && code != 0 ) {
193
- //non zero exit code - fail
194
- defer . reject ( tl . loc ( 'RemoteCmdNonZeroExitCode' , cmdToRun , code ) ) ;
195
- } else {
196
- //no exit code or exit code of 0
197
194
198
- //based on the options decide whether to fail the build or not if data was written to STDERR
199
- if ( stdErrWritten === true && options . failOnStdErr === true ) {
200
- //stderr written - fail the build
201
- defer . reject ( tl . loc ( 'RemoteCmdExecutionErr' , cmdToRun , tl . loc ( 'CheckLogForStdErr' ) ) ) ;
195
+ tl . debug ( 'cmdToRun = ' + cmdToRun ) ;
196
+
197
+ this . sshClient . exec ( cmdToRun , ( err , stream ) => {
198
+ if ( err ) {
199
+ reject ( tl . loc ( 'RemoteCmdExecutionErr' , cmdToRun , err ) ) ;
200
+ return ;
201
+ }
202
+
203
+ stream . on ( 'close' , ( code , signal ) => {
204
+ tl . debug ( 'code = ' + code + ', signal = ' + signal ) ;
205
+
206
+ if ( code && code != 0 ) {
207
+ //non zero exit code - fail
208
+ reject ( tl . loc ( 'RemoteCmdNonZeroExitCode' , cmdToRun , code ) ) ;
202
209
} else {
203
- //success
204
- defer . resolve ( '0' ) ;
210
+ //no exit code or exit code of 0
211
+
212
+ //based on the options decide whether to fail the build or not if data was written to STDERR
213
+ if ( stdErrWritten === true && options . failOnStdErr === true ) {
214
+ //stderr written - fail the build
215
+ reject ( tl . loc ( 'RemoteCmdExecutionErr' , cmdToRun , tl . loc ( 'CheckLogForStdErr' ) ) ) ;
216
+ } else {
217
+ //success
218
+ resolve ( '0' ) ;
219
+ }
205
220
}
206
- }
207
- } ) . on ( 'data' , ( data ) => {
208
- console . log ( data ) ;
209
- } ) . stderr . on ( 'data' , ( data ) => {
221
+ } ) . on ( 'data' , ( data ) => {
222
+ console . log ( data ) ;
223
+ } ) . stderr . on ( 'data' , ( data ) => {
210
224
stdErrWritten = true ;
211
225
tl . debug ( 'stderr = ' + data ) ;
212
- if ( data && data . toString ( ) . trim ( ) !== '' ) {
226
+ if ( data && data . toString ( ) . trim ( ) !== '' ) {
213
227
tl . error ( data ) ;
214
228
}
215
229
} ) ;
230
+ } ) ;
216
231
} ) ;
217
- return defer . promise ;
218
232
}
219
233
}
0 commit comments