-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzenci-shell.js
411 lines (343 loc) · 11.6 KB
/
zenci-shell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* Originaly based on ssh2shell package but adjusted for Zen.CI project.
* credit: https://github.com/cmp-202/ssh2shell
* Author: http://github.com/Gormartsen
*/
"use strict";
const EventEmitter = require( "events" ).EventEmitter;
const util = require( "util" );
const debugF = require( "debug" );
const bind = function( fn, me ) { return function() { return fn.apply( me, arguments ); }; };
/**
* Object constructor.
*/
function ZENCIShell( sshObj1 ) {
EventEmitter.call( this );
this.sshObj = sshObj1;
this._loadDefaults();
this.connection = new require( "ssh2" )();
// Use a closure to preserve `this`
var self = this;
this._notices = [];
this._buffer = '';
this.command = '';
this._timedout = bind( this._timedout, this );
this._command_timeout = bind( this._command_timeout, this );
// Attach Events.
this.on( "connect", function() {
self.debug.events( "Connected" );
} );
this.on( "ready", function() {
self.debug.events( "Ready" );
} );
this.on( "end", function( notices ) {
self.debug.events( "End" );
} );
this.on( "close", function( err ) {
self.debug.events( "Close" );
if ( err ) {
return self.emit( "error", err, "Close" );
}
} );
this.on( "error", function( err, type ) {
self.debug.events( "Error type: % message: %", type, err );
} );
}
util.inherits( ZENCIShell, EventEmitter );
ZENCIShell.prototype.sshObj = {};
ZENCIShell.prototype.command = "";
ZENCIShell.prototype._origin_command = "";
ZENCIShell.prototype._origin_command_output = "";
ZENCIShell.prototype._notices = [];
ZENCIShell.prototype._status = -1;
ZENCIShell.prototype._start_time = 0;
ZENCIShell.prototype._total_time = 0;
ZENCIShell.prototype._stream = {};
ZENCIShell.prototype._data = "";
ZENCIShell.prototype._buffer = "";
ZENCIShell.prototype._end = false;
ZENCIShell.prototype.callback = function() { return false; };
ZENCIShell.prototype.debug = {
events: debugF( "ssh:events" ),
debug: debugF( "ssh:debug" ),
raw: debugF( "ssh:raw" )
};
/**
* Timeout event handler.
* Send \C (SIGINT) to stream.
*/
ZENCIShell.prototype._timedout = function() {
var self = this;
self.debug.debug( "Timeout triggered on : %s", self.command );
this._stream.write( "\x03" + " " );
};
/**
* Keep Alive Timeout event handler.
* call _processNextCommand to process next command in queue if exists.
*/
ZENCIShell.prototype._command_timeout = function() {
var self = this;
self.debug.events( "_processNextCommand Timeout triggered", self.command );
this._processNextCommand();
};
/**
* Process output from stream.
* Emit 'commandProcessing' event if command did not finish yet.
* - _notice object with last command details.
*/
ZENCIShell.prototype._processData = function( data ) {
var self = this;
self.debug.raw( "received : %s", data );
// Update timeout timer because we received some answer.
if ( self._idleTimer ) {
clearTimeout( self._idleTimer );
}
self._idleTimer = setTimeout( self._timedout, self._idleTime );
this._buffer += data;
// Check for login prompt to know if command finished.
if ( this.standardPrompt.test( this._buffer ) ) {
self.debug.raw( "Normal prompt detected" );
// -13 is a length of hardcoded PS1 + COMMAND_PROMPT.
if ( this._buffer.length >= 13 ) {
this._buffer = this._buffer.substr( 0, this._buffer.length - 13 );
}
// Cut out cmd line from buffer.
if ( this.command.length + 2 <= this._buffer.length ) {
this._buffer = this._buffer.substr( this.command.length + 2 );
}
// Check for status command.
if ( this.command == 'echo -e "$?"' ) {
this._status = parseInt( this._buffer );
this.command = this._origin_command;
if ( this.command != "" ) {
var _notice = {
"command": self.command,
"status": self._status,
"time": self._total_time,
"output": self._origin_command_output
};
self._notices.push( _notice );
this.emit( "commandComplete", _notice );
self.debug.events( "Command %s finished in %s ms with status %s",
self.command, self._total_time, self._status );
this.callback( _notice );
}
return this._processNextCommand();
}
// If command finished (promt is here) and it is not a echo -e "$?"
// We need to start echo -e "$?" to get status.
this._origin_command = this.command;
this._origin_command_output = this._buffer;
this._total_time = new Date().getTime() - this._start_time;
this.command = 'echo -e "$?"';
this._buffer = "";
return this._runCommand();
} else {
// Command is still running.
if ( self.command != "" ) {
// Cut out cmd line from buffer.
var tmp_buf = self._buffer;
if ( self.command.length + 2 <= self._buffer.length ) {
tmp_buf = self._buffer.substr( self.command.length + 2 );
}
var _notice = {
"command": self.command,
"status": self._status,
"time": new Date().getTime() - this._start_time,
"output": tmp_buf
};
this.emit( "commandProcessing", _notice );
self.debug.events( "Command %s is still runnung for %s ms", _notice.command, _notice.time );
this.callback( _notice );
}
}
};
/**
* Prepare to start next Command.
* Emit 'commandComplete' event if command finished,
* - _notice object with last command details.
* - sshObj - settings for this class.
* or start 'echo -e "$?"' to get latest comman status.
*/
ZENCIShell.prototype._processNextCommand = function() {
var self = this;
this._buffer = "";
if ( this.sshObj.commands.length > 0 ) {
var nextCommand = this.sshObj.commands.shift();
if ( !nextCommand.callback ) {
this.command = nextCommand;
this.callback = function() { return false;};
} else {
this.command = nextCommand.command;
this.callback = nextCommand.callback;
}
this._status = -1;
this._start_time = new Date().getTime();
if ( this.command ) {
return this._runCommand();
}
}
// We get to the end of command list.
//clear default timeout for command
if ( this._idleTimer ) {
clearTimeout( this._idleTimer );
}
// Restart Keep Alive command triger if required
if ( this._idleCommandTimer ) {
clearTimeout( this._idleCommandTimer );
}
if ( this.sshObj.keep_alive ) {
return this._idleCommandTimer = setTimeout( this._command_timeout, this._idleCommandTime );
}
// If this is not keep alive mode, close connection.
this.end();
};
/**
* Write command to stream.
*/
ZENCIShell.prototype._runCommand = function() {
var self = this;
self.debug.events( "Next command %s ", self.command );
return this._stream.write( this.command + "\n" );
return false;
};
/**
* Add command into commands pull.
* @return bool TRUE if put in queue, FALSE if connection is not alive
*/
ZENCIShell.prototype.exec = function( command, callback ) {
var self = this;
self.debug.events( "Next command %s ", command );
if ( this.connection._sshstream.writable ) {
if ( callback ) {
this.sshObj.commands.push( {
command: command,
callback: callback
} );
} else {
this.sshObj.commands.push( command );
}
return true;
}else {
callback( {
"command": command,
"status": 1,
"time": 0,
"output": "SSH is already closed"
} );
}
return false;
};
/**
* Close SSH connection.
*/
ZENCIShell.prototype.end = function() {
var self = this;
self.debug.events( "Exit and close connection" );
self.connection.end();
};
/**
* Set default values for settings.
*/
ZENCIShell.prototype._loadDefaults = function() {
var ref;
if ( !this.sshObj.keep_alive ) {
this.sshObj.keep_alive = false;
}
// Command timeout timer.
this._idleTime = ( ref = this.sshObj.idleTimeOut ) != null ? ref : 5000;
// Command queue timer for keep alive mode.
this._idleCommandTime = ( ref = this.sshObj.idleCommandTimeOut ) != null ? ref : 100;
return this.standardPrompt = new RegExp( "_zencishell: $" );
};
/**
* Init ssh2 connection.
* by using export we setup our own prompt to detect when command finished.
* Emit 'onEnd' event if command finished,
* - _notices array of notice objects with command status.
* - sshObj - settings for this class.
*/
ZENCIShell.prototype.connect = function() {
var e, error, ref, ref1;
var self = this;
if ( this.sshObj.server && this.sshObj.commands ) {
this.connection.on( "connect", function() {
self.emit( "connect" );
} );
this.connection.on( "ready", function() {
self.emit( "ready" );
// We set really wide window to avoid ANSI ESC characters to brake cmd cut.
return self.connection.shell( { "cols": 1000, "modes": { "ECHO": 53 } },
function( err, _stream ) {
self._stream = _stream;
if ( err ) {
self.emit( "error", err, "Shell" );
}
self._stream.write( "export PS1='zencishell: ';" +
"export PROMPT_COMMAND='echo -n \"_\"' ; sleep 1\n" );
self._stream.on( "error", function( err ) {
return self.emit( "error", err, "Stream" );
} );
self._stream.on( "data", function( data ) {
self._processData( "" + data );
} );
self._stream.on( "end", function() {
if ( self._status == -1 ) {
var _notice = {
"command": self.command,
"status": 1,
"time": new Date().getTime() - self._start_time,
"output": self._buffer + "\nRemote connection closed.\n"
};
if ( self._notices.length == 0 ) {
self._notices.push( _notice );
} else {
var lastCmd = self._notices[ self._notices.length - 1 ];
if ( lastCmd.command == _notice.command ) {
self._notices[ self._notices.length - 1 ] = _notice;
} else {
self._notices.push( _notice );
}
}
self.emit( "commandComplete", _notice );
self.debug.events( "Command %s finished in %s ms with status %s",
_notice.command, _notice.time, _notice._status );
self.callback( _notice );
self.emit( "error", new Error( "Connection closed due error" ), "Connection" );
}
return self.emit( "end", self._notices );
} );
return self._stream.on( "close", function( code, signal ) {
if ( self._idleTimer ) {
clearTimeout( self._idleTimer );
}
if ( self._idleCommandTimer ) {
clearTimeout( self._idleCommandTimer );
}
return self.connection.end();
} );
} );
} );
this.connection.on( "error", function( err ) {
self.emit( "error", err, "Connection" );
} );
this.connection.on( "close", function( had_error ) {
var err = false;
if ( had_error ) {
err = new Error( "Connection closed due error" );
}
self.emit( "close", err );
} );
return this.connection.connect( {
host: this.sshObj.server.host,
port: this.sshObj.server.port,
username: this.sshObj.server.userName,
password: this.sshObj.server.password,
privateKey: ( ref = this.sshObj.server.privateKey ) != null ? ref : "",
passphrase: ( ref1 = this.sshObj.server.passPhrase ) != null ? ref1 : ""
} );
} else {
return this.emit( "error", new Error( "Missing connection parameters" ), "Parameters" );
}
};
module.exports = ZENCIShell;