-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstackstorm_api.js
235 lines (198 loc) · 7.16 KB
/
stackstorm_api.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
"use strict";
var _ = require('lodash');
var util = require('util');
var env = _.clone(process.env);
var Promise = require('rsvp').Promise;
var utils = require('./utils.js');
var st2client = require('st2client');
var EventEmitter = require('events').EventEmitter;
// Setup the Environment
env.ST2_API = env.ST2_API || 'http://localhost:9101';
env.ST2_ROUTE = env.ST2_ROUTE || null;
// Optional authentication info
env.ST2_AUTH_USERNAME = env.ST2_AUTH_USERNAME || null;
env.ST2_AUTH_PASSWORD = env.ST2_AUTH_PASSWORD || null;
// Optional authentication token
env.ST2_AUTH_TOKEN = env.ST2_AUTH_TOKEN || null;
// Optional API key
env.ST2_API_KEY = env.ST2_API_KEY || null;
// Optional, if not provided, we infer it from the API URL
env.ST2_AUTH_URL = env.ST2_AUTH_URL || null;
env.ST2_ALIAS_PACK_RESTRICTION = env.ST2_ALIAS_PACK_RESTRICTION || null;
var START_MESSAGES = [
"I'll take it from here! Your execution ID for reference is %s",
"Got it! Remember %s as your execution ID",
"I'm on it! Your execution ID is %s",
"Let me get right on that. Remember %s as your execution ID",
"Always something with you. :) I'll take care of that. Your ID is %s",
"I have it covered. Your execution ID is %s",
"Let me start up the machine! Your execution ID is %s",
"I'll throw that task in the oven and get cookin'! Your execution ID is %s",
"Want me to take that off your hand? You got it! Don't forget your execution ID: %s",
"River Tam will get it done with her psychic powers. Your execution ID is %s"
];
var ERROR_MESSAGES = [
"I'm sorry, Dave. I'm afraid I can't do that. {~} %s"
];
function StackStormApi(logger) {
var self = this;
self.logger = logger;
var url = utils.parseUrl(env.ST2_API);
var opts = {
protocol: url.protocol,
host: url.hostname,
port: url.port,
prefix: url.path,
rejectUnauthorized: false
};
self.api = st2client(opts);
if (env.ST2_API_KEY) {
self.api.setKey({ key: env.ST2_API_KEY });
} else if (env.ST2_AUTH_TOKEN) {
self.api.setToken({ token: env.ST2_AUTH_TOKEN });
}
if (env.ST2_API_KEY || env.ST2_AUTH_TOKEN || env.ST2_AUTH_USERNAME || env.ST2_AUTH_PASSWORD) {
// If using username and password then all are required.
if ((env.ST2_AUTH_USERNAME || env.ST2_AUTH_PASSWORD) &&
!(env.ST2_AUTH_USERNAME && env.ST2_AUTH_PASSWORD && env.ST2_AUTH_URL)) {
throw new Error('Env variables ST2_AUTH_USERNAME, ST2_AUTH_PASSWORD and ST2_AUTH_URL should only be used together.');
}
}
EventEmitter.call(this);
}
util.inherits(StackStormApi, EventEmitter);
StackStormApi.prototype.startListener = function start() {
var self = this;
return self.api.stream.listen()
.catch(function (err) {
self.logger.error('Unable to connect to stream:', err);
})
.then(function (source) {
source.onerror = function (err) {
// TODO: squeeze a little bit more info out of evensource.js
self.logger.error('Stream error:', err);
};
source.addEventListener('st2.announcement__chatops', function (e) {
var data;
self.logger.debug('Chatops message received:', e.data);
if (e.data) {
data = JSON.parse(e.data).payload;
} else {
data = e.data;
}
self.emit('st2.chatops_announcement', data);
});
return source;
})
};
StackStormApi.prototype.getAliases = function () {
var self = this;
self.logger.info('Getting Action Aliases....');
return self.api.actionAlias.list()
.then(function (aliases) {
if (env.ST2_ALIAS_PACK_RESTRICTION) {
self.logger.info('Alias Restrictions are in place, only retrieving aliases from the following pack(s): ' + env.ST2_ALIAS_PACK_RESTRICTION);
var return_aliases = [];
var restrictions = env.ST2_ALIAS_PACK_RESTRICTION.split(',');
_.each(aliases, function(alias) {
if (restrictions.indexOf(alias.pack) > -1) {
return_aliases.push(alias);
}
});
return return_aliases;
} else {
return aliases;
}
})
.catch(function (err) {
var error_msg = 'Failed to retrieve commands from "%s": %s';
self.logger.error(util.format(error_msg, env.ST2_API, err.message));
return [];
});
};
StackStormApi.prototype.sendAck = function (msg, res) {
var history_url = utils.getExecutionHistoryUrl(res.execution);
var history = history_url ? util.format(' (details available at %s)', history_url) : '';
if (res.actionalias && res.actionalias.ack) {
if (res.actionalias.ack.enabled === false) {
return;
} else if (res.actionalias.ack.append_url === false) {
history = '';
}
}
if (res.message) {
return msg.send(res.message + history);
}
var message = util.format(_.sample(START_MESSAGES), res.execution.id);
return msg.send(message + history);
};
// TODO: decouple the msg object from stackstorm api, this should use an event emitter
StackStormApi.prototype.executeCommand = function (msg, alias_name, format_string, command, addressee) {
var self = this;
var payload = {
'name': alias_name,
'format': format_string,
'command': command,
'user': addressee.name,
'source_channel': addressee.room,
'notification_route': env.ST2_ROUTE || 'hubot'
};
self.logger.debug('Sending command payload:', JSON.stringify(payload));
return self.api.aliasExecution.create(payload)
.then(function (res) { self.sendAck(msg, res); })
.catch(function (err) {
if (err.status === 200) {
return self.sendAck(msg, { execution: { id: err.message } });
}
self.logger.error('Failed to create an alias execution:', err);
var message = util.format(_.sample(ERROR_MESSAGES), err.message);
if (err.requestId) {
message = util.format(
message,
util.format('; Use request ID %s to grep st2 api logs.', err.requestId));
}
self.emit('st2.execution_error', {
name: alias_name,
format_string: format_string,
message: message,
addressee: addressee,
command: command
});
throw err;
});
};
StackStormApi.prototype.authenticate = function authenticate() {
var self = this;
self.api.removeListener('expiry', authenticate);
// API key gets precedence 1
if (env.ST2_API_KEY) {
self.logger.info('Using ST2_API_KEY as authentication. Expiry will lead to bot exit.');
return Promise.resolve();
}
// Auth token gets precedence 2
if (env.ST2_AUTH_TOKEN) {
self.logger.info('Using ST2_AUTH_TOKEN as authentication. Expiry will lead to bot exit.');
return Promise.resolve();
}
self.logger.info('Requesting a token...');
var url = utils.parseUrl(env.ST2_AUTH_URL);
var client = st2client({
auth: {
protocol: url.protocol,
host: url.hostname,
port: url.port,
prefix: url.path
}
});
return client.authenticate(env.ST2_AUTH_USERNAME, env.ST2_AUTH_PASSWORD)
.then(function (token) {
self.logger.info('Token received. Expiring ' + token.expiry);
self.api.setToken(token);
client.on('expiry', self.authenticate);
})
.catch(function (err) {
self.logger.error('Failed to authenticate: ' + err.message);
throw err;
});
};
module.exports = StackStormApi;