@@ -129,15 +129,16 @@ module.exports = function(robot) {
129
129
if ( env . ST2_API ) {
130
130
robot . logger . warning ( "ST2_API is deprecated and will be removed in a future releases. Instead, please use the ST2_API_URL environment variable." ) ;
131
131
}
132
- var url = utils . parseUrl ( env . ST2_API_URL ) ;
133
-
134
- var opts = {
135
- protocol : url . protocol ,
136
- host : url . hostname ,
137
- port : url . port ,
138
- prefix : url . path ,
139
- rejectUnauthorized : false
140
- } ;
132
+ var self = this ,
133
+ authenticated = Promise . resolve ( ) ,
134
+ url = utils . parseUrl ( env . ST2_API_URL ) ,
135
+ opts = {
136
+ protocol : url . protocol ,
137
+ host : url . hostname ,
138
+ port : url . port ,
139
+ prefix : url . path ,
140
+ rejectUnauthorized : false
141
+ } ;
141
142
142
143
if ( env . ST2_STREAM_URL ) {
143
144
var stream_url = utils . parseUrl ( env . ST2_STREAM_URL ) ;
@@ -149,16 +150,10 @@ module.exports = function(robot) {
149
150
} ;
150
151
}
151
152
152
- var api = st2client ( opts ) ;
153
-
154
- if ( env . ST2_API_KEY ) {
155
- api . setKey ( { key : env . ST2_API_KEY } ) ;
156
- } else if ( env . ST2_AUTH_TOKEN ) {
157
- api . setToken ( { token : env . ST2_AUTH_TOKEN } ) ;
158
- }
153
+ var api_client = st2client ( opts ) ;
159
154
160
155
function authenticate ( ) {
161
- api . removeListener ( 'expiry' , authenticate ) ;
156
+ api_client . removeListener ( 'expiry' , authenticate ) ;
162
157
163
158
// API key gets precedence 1
164
159
if ( env . ST2_API_KEY ) {
@@ -175,7 +170,7 @@ module.exports = function(robot) {
175
170
176
171
var url = utils . parseUrl ( env . ST2_AUTH_URL ) ;
177
172
178
- var client = st2client ( {
173
+ var auth_client = st2client ( {
179
174
auth : {
180
175
protocol : url . protocol ,
181
176
host : url . hostname ,
@@ -184,11 +179,11 @@ module.exports = function(robot) {
184
179
}
185
180
} ) ;
186
181
187
- return client . authenticate ( env . ST2_AUTH_USERNAME , env . ST2_AUTH_PASSWORD )
182
+ return auth_client . authenticate ( env . ST2_AUTH_USERNAME , env . ST2_AUTH_PASSWORD )
188
183
. then ( function ( token ) {
189
184
robot . logger . info ( 'Token received. Expiring ' + token . expiry ) ;
190
- api . setToken ( token ) ;
191
- client . on ( 'expiry' , authenticate ) ;
185
+ api_client . setToken ( token ) ;
186
+ auth_client . on ( 'expiry' , authenticate ) ;
192
187
} )
193
188
. catch ( function ( err ) {
194
189
robot . logger . error ( 'Failed to authenticate: ' + err . message ) ;
@@ -197,21 +192,6 @@ module.exports = function(robot) {
197
192
} ) ;
198
193
}
199
194
200
- if ( env . ST2_API_KEY || env . ST2_AUTH_TOKEN || env . ST2_AUTH_USERNAME || env . ST2_AUTH_PASSWORD ) {
201
- // If using username and password then all are required.
202
- if ( ( env . ST2_AUTH_USERNAME || env . ST2_AUTH_PASSWORD ) &&
203
- ! ( env . ST2_AUTH_USERNAME && env . ST2_AUTH_PASSWORD && env . ST2_AUTH_URL ) ) {
204
- throw new Error ( 'Env variables ST2_AUTH_USERNAME, ST2_AUTH_PASSWORD and ST2_AUTH_URL should only be used together.' ) ;
205
- }
206
- promise = authenticate ( ) ;
207
- }
208
-
209
- // Pending 2-factor auth commands
210
- if ( env . HUBOT_2FA ) {
211
- var twofactor = { } ;
212
- robot . logger . info ( 'Two-factor auth is enabled' ) ;
213
- }
214
-
215
195
// factory to manage commands
216
196
var command_factory = new CommandFactory ( robot ) ;
217
197
@@ -224,7 +204,7 @@ module.exports = function(robot) {
224
204
var loadCommands = function ( ) {
225
205
robot . logger . info ( 'Loading commands....' ) ;
226
206
227
- api . actionAlias . list ( { limit : - 1 } )
207
+ api_client . actionAlias . list ( { limit : - 1 } )
228
208
. then ( function ( aliases ) {
229
209
// Remove all the existing commands
230
210
command_factory . removeCommands ( ) ;
@@ -288,7 +268,7 @@ module.exports = function(robot) {
288
268
var sendAliasExecutionRequest = function ( msg , payload ) {
289
269
robot . logger . debug ( 'Sending command payload:' , JSON . stringify ( payload ) ) ;
290
270
291
- api . aliasExecution . create ( payload )
271
+ api_client . aliasExecution . create ( payload )
292
272
. then ( function ( res ) { sendAck ( msg , res ) ; } )
293
273
. catch ( function ( err ) {
294
274
// Compatibility with older StackStorm versions
@@ -331,7 +311,7 @@ module.exports = function(robot) {
331
311
var twofactor_id = uuid . v4 ( ) ;
332
312
robot . logger . debug ( 'Requested an action that requires 2FA. Guid: ' + twofactor_id ) ;
333
313
msg . send ( TWOFACTOR_MESSAGE ) ;
334
- api . executions . create ( {
314
+ api_client . executions . create ( {
335
315
'action' : env . HUBOT_2FA ,
336
316
'parameters' : {
337
317
'uuid' : twofactor_id ,
@@ -350,7 +330,7 @@ module.exports = function(robot) {
350
330
} ;
351
331
352
332
robot . respond ( / ( [ \s \S ] + ?) $ / i, function ( msg ) {
353
- var command , result , command_name , format_string , action_alias ;
333
+ var command , result ;
354
334
355
335
// Normalize the command and remove special handling provided by the chat service.
356
336
// e.g. slack replace quote marks with left double quote which would break behavior.
@@ -363,9 +343,7 @@ module.exports = function(robot) {
363
343
return ;
364
344
}
365
345
366
- command_name = result [ 0 ] ;
367
- format_string = result [ 1 ] ;
368
- action_alias = result [ 2 ] ;
346
+ var [ command_name , format_string , action_alias ] = result ;
369
347
370
348
executeCommand ( msg , command_name , format_string , command , action_alias ) ;
371
349
} ) ;
@@ -392,17 +370,17 @@ module.exports = function(robot) {
392
370
var commands_load_interval ;
393
371
394
372
function start ( ) {
395
- api . stream . listen ( ) . catch ( function ( err ) {
373
+ api_client . stream . listen ( ) . catch ( function ( err ) {
396
374
robot . logger . error ( 'Unable to connect to stream:' , err ) ;
397
- } ) . then ( function ( source ) {
398
- source . onerror = function ( err ) {
375
+ } ) . then ( function ( st2stream ) {
376
+ st2stream . onerror = function ( err ) {
399
377
// TODO: squeeze a little bit more info out of evensource.js
400
- robot . logger . error ( 'Stream error:' , err ) ;
378
+ robot . logger . warning ( 'Stream error:' , err ) ;
401
379
if ( err . status === 401 ) {
402
380
throw err ;
403
381
}
404
382
} ;
405
- source . addEventListener ( 'st2.announcement__chatops' , function ( e ) {
383
+ st2stream . addEventListener ( 'st2.announcement__chatops' , function ( e ) {
406
384
var data ;
407
385
408
386
robot . logger . debug ( 'Chatops message received:' , e . data ) ;
@@ -417,7 +395,7 @@ module.exports = function(robot) {
417
395
} ) ;
418
396
419
397
if ( env . HUBOT_2FA ) {
420
- source . addEventListener ( 'st2.announcement__2fa' , function ( e ) {
398
+ st2stream . addEventListener ( 'st2.announcement__2fa' , function ( e ) {
421
399
var data ;
422
400
423
401
robot . logger . debug ( 'Successfull two-factor auth:' , e . data ) ;
@@ -447,21 +425,43 @@ module.exports = function(robot) {
447
425
448
426
function stop ( ) {
449
427
clearInterval ( commands_load_interval ) ;
450
- api . stream . listen ( ) . then ( function ( source ) {
451
- source . removeAllListeners ( ) ;
452
- source . close ( ) ;
428
+ api_client . stream . listen ( ) . then ( function ( st2stream ) {
429
+ st2stream . removeAllListeners ( ) ;
430
+ st2stream . close ( ) ;
453
431
} ) ;
454
432
}
455
433
456
434
function install_sigusr2_handler ( ) {
457
435
process . on ( 'SIGUSR2' , function ( ) {
436
+ robot . logger . debug ( "Caught SIGUSR2, reloading commands" ) ;
458
437
loadCommands ( ) ;
459
438
} ) ;
460
439
}
461
440
441
+ if ( env . ST2_API_KEY ) {
442
+ api_client . setKey ( { key : env . ST2_API_KEY } ) ;
443
+ } else if ( env . ST2_AUTH_TOKEN ) {
444
+ api_client . setToken ( { token : env . ST2_AUTH_TOKEN } ) ;
445
+ }
446
+
447
+ if ( env . ST2_API_KEY || env . ST2_AUTH_TOKEN || env . ST2_AUTH_USERNAME || env . ST2_AUTH_PASSWORD ) {
448
+ // If using username and password then all are required.
449
+ if ( ( env . ST2_AUTH_USERNAME || env . ST2_AUTH_PASSWORD ) &&
450
+ ! ( env . ST2_AUTH_USERNAME && env . ST2_AUTH_PASSWORD && env . ST2_AUTH_URL ) ) {
451
+ throw new Error ( 'Env variables ST2_AUTH_USERNAME, ST2_AUTH_PASSWORD and ST2_AUTH_URL should only be used together.' ) ;
452
+ }
453
+ authenticated = authenticate ( ) ;
454
+ }
455
+
456
+ // Pending 2-factor auth commands
457
+ if ( env . HUBOT_2FA ) {
458
+ var twofactor = { } ;
459
+ robot . logger . info ( 'Two-factor auth is enabled' ) ;
460
+ }
461
+
462
462
// Authenticate with StackStorm backend and then call start.
463
463
// On a failure to authenticate log the error but do not quit.
464
- return promise . then ( function ( ) {
464
+ return authenticated . then ( function ( ) {
465
465
start ( ) ;
466
466
return stop ;
467
467
} ) ;
0 commit comments