-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.js
444 lines (384 loc) · 11.3 KB
/
client.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
'use strict';
/*!
* Module dependencies.
*/
var assert = require('assert');
var restify = require('restify-clients');
var url = require('url');
module.exports = function(options){
var apiUrls = {};
apiUrls.ps = '/ps';
apiUrls.config = '/config';
apiUrls.status = '/status';
apiUrls.log = '/log';
apiUrls.run = '/run';
apiUrls.tasks = '/tasks';
apiUrls.createTask = apiUrls.tasks + '/new';
apiUrls.collections = '/collections';
var api = false;
var client = {};
/**
* Setup the restify client
*
* @param {object} options Restify client options like url, timeout and retries
*/
client.configure = function(options){
if (!options) { options = {}; }
var config = {};
// Cheap and dirty default values.
config.version = '*';
config.url = options.url || 'http://localhost:8888';
if (options.retry) { config.retry = { "retries":options.retry }; }
config.timeout = options.timeout || 1;
api = restify.createJsonClient(config);
if (options.username && options.password) {
api.basicAuth(options.username, options.password);
}
};
// Configure the restify client
client.configure(options);
/**
* Authenticate your client
*
* If you pass username and password keys to the constructor
* it will authenticate for you
*
* @see client.configure()
* @param {String} user
* @param {String} pass
*/
client.basicAuth = function(user, pass){
api.basicAuth(user, pass);
};
/**
* Retreive the status of your ummon server
*
* Returns:
*
* // TODO
*
* @param {Function} callback(err, result)
*/
client.getStatus = function(callback) {
api.get(apiUrls.status, function(err, req, res, result) {
callback(err, result);
})
};
/**
* Return ummon-servers configuration
*
* Returns:
*
* // TODO
*
* @param {Function} callback(err, result)
*/
client.getConfig = function(callback) {
api.get(apiUrls.config, function(err, req, res, result) {
callback(err, result);
})
};
/**
* Set configuration options
*
* Example:
*
* client.setConfig( {pause: true}, function(err, result){
* console.log('Ummon has been paused');
* });
*
*
* @param {Object} options configuration options to set
* @param {Function} callback(err, result)
*/
client.setConfig = function(options, callback) {
var setConfigUrl = url.parse(apiUrls.config);
setConfigUrl.query = options;
setConfigUrl = url.format(setConfigUrl);
api.put(setConfigUrl, function(err, req, res, result) {
callback(err, result);
})
};
/**
* Get the tasks currently in the queue
*
* @return {Function} callback(err, result)
*/
client.getQueue = function(callback) {
api.get('/queue', function(err, req, res, result) {
callback(err, result);
})
};
/**
* Clear the entire queue or a specific task from it
*
* @param {String} task The id of the task, eg: collection.name
* @param {Function} callback callback(err, result)
*/
client.clearQueue = function(task, callback) {
var url = '/queue/clear';
if (task) { url += '?task=' + task; }
api.del(url, function(err, req, res, result) {
callback(err, result);
})
};
/**
* Return a portion of the server's log
*
* ## Example:
*
* client.showLog({ lines: 500, filter: {collection: 'ummon'}}, function(err, result){
* console.log(result)
* })
*
* ## Options:
*
* * lines: The number of lines to retreive
* * runsOnly: Return only a history of completed tasks, without task output
* * filter: Object with key collection, task or run
*
* @param {Object} options Options to control the filtering of the logs
* @param {Function} callback callback(err, result)
*/
client.showLog = function(options, callback){
if (!callback && 'function' === typeof options){
callback = options;
}
var query = {};
['filter', 'runsOnly', 'start', 'from', 'to', 'follow'].forEach(function(key){
if (options[key]) { query[key] = options[key]; }
});
// Grossly build out the url
var logUrl = apiUrls.log;
if (Object.keys(query).length) {
var count = 0;
logUrl += '?';
for (var key in query) {
if (count > 0) logUrl += '&';
logUrl += key + '=' + query[key];
count++;
}
}
api.get(logUrl, function(err, req, res, result) {
if (err) return callback(err);
callback(null, res.body); // This is weird that result is empty and res.body isn't
});
};
/**
* Return the config for a collection
*
* @param {String} collection A collection name
* @param {Function} callback callback(err, result)
*/
client.getCollectionConfig = function(collection, callback) {
api.get(apiUrls.collections+'/'+collection, function(err, req, res, result) {
callback(err, result);
});
};
/**
* Sets the config for a collection
*
* @param {String} collection A collection name
* @param {Function} callback callback(err, result)
*/
client.setCollectionConfig = function(collection, config, callback) {
api.put(apiUrls.collections+'/'+collection, config, function(err, req, res, result) {
callback(err, result);
});
};
/**
* Return the defautls for a particular collection
*
* @param {String} collection A collection name
* @param {Function} callback callback(err, result)
*/
client.getCollectionDefaults = function(collection, callback) {
api.get(apiUrls.collections+'/'+collection+'/defaults', function(err, req, res, result) {
callback(err, result);
});
};
/**
* Set the defaults for a particular collection
*
* @param {String} collection A collection name
* @param {Object} config The values to set as defaults, eg: cwd
* @param {Function} callback callback(err, result)
*/
client.setCollectionDefaults = function(collection, config, callback) {
api.put(apiUrls.collections+'/'+collection+'/defaults', config, function(err, req, res, result) {
callback(err, result);
});
};
/**
* Create a new collection. Also useful for renaming collections
*
* @param {String} collection The new collection's name
* @param {Object} config An object containing defaults, settings and tasks
* @param {Function} callback callback(err, result)
*/
client.setTasks = function(collection, config, callback) {
api.put(apiUrls.collections+'/'+collection, config, function(err, req, res, result) {
callback(err, result);
});
};
/**
* Enable an individual task or a collection of tasks
*
* ## Examples:
*
* // Enable a single task
* client.enableTasks({ task:'ummon.doAwesome' }, callback)
*
* // Enable a collection
* client.enableTasks({ collection:'ummon' }, callback)
*
*
* @param {Object} options Object describing what to enable
* @param {Function} callback callback(err, result)
*/
client.enableTasks = function(options, callback) {
var enableTasksUrl;
if (options.collection) {
enableTasksUrl = apiUrls.collections+'/'+options.collection + '/enable'
} else if (options.task) {
enableTasksUrl = apiUrls.tasks+'/' + options.task + '/enable'
} else {
return callback(new Error('You did not specify a collection or task to enable'));
}
api.put(enableTasksUrl, function(err, req, res, result) {
if (res.statusCode === 304) {
err = new Error('Collection already disabled')
}
callback(err, result);
});
};
/**
* Disable an individual task or a collection of tasks
*
* ## Examples:
*
* // Enable a single task
* client.disableTasks({ task:'ummon.doAwesome' }, callback)
*
* // Enable a collection
* client.disableTasks({ collection:'ummon' }, callback)
*
*
* @param {Object} options Object describing what to disable
* @param {Function} callback callback(err, result)
*/
client.disableTasks = function(options, callback) {
var disableTasksUrl;
if (options.collection) {
disableTasksUrl = apiUrls.collections+'/'+options.collection + '/disable'
} else if (options.task) {
disableTasksUrl = apiUrls.tasks+'/' + options.task + '/disable'
} else {
return callback(new Error('You did not specify a collection or task to disable'));
}
api.put(disableTasksUrl, function(err, req, res, result) {
if (res.statusCode === 304) {
err = new Error('Collection already disabled')
}
callback(err, result);
});
};
/**
* Return an individual task or a collection of tasks
*
* @param {Object} options Object describing what to get
* @param {Function} callback callback(err, result)
*/
client.getTasks = function(options, callback){
if (!callback && "function" === typeof options) {
callback = options;
options = false;
}
// URL BUILDER!
var taskurl;
if (options.collection) {
taskurl = apiUrls.collections + '/' + options.collection;
} else if (options.task) {
if (options.task.indexOf('.*') !== -1) { options.task = options.task.substr(0, options.task.length-2)}
taskurl = apiUrls.tasks + '/' + options.task;
} else {
taskurl = apiUrls.tasks;
}
api.get(taskurl, function(err, req, res, result) {
// console.log(err, result);
if (err) {
return callback(err);
}
callback(null, result);
});
};
/**
* Create a task
*
* ## Example
*
* client.createTask({
* collection:"ummon",
* name: "doAwesome",
* command: "sh fixTheWorld.sh",
* trigger: {time: '1 * * * *'}},
* callback)
*
* @param {Object} options Object describing what to create
* @param {Function} callback callback(err, result)
*/
client.createTask = function(config, callback){
api.post(apiUrls.createTask, config, function(err, req, res, result) {
if (err) return callback(err);
callback(null, result);
});
};
/**
* Update an existing task
*
* @param {Object} config A tasks updated config object
* @param {Function} callback callback(err, result)
*/
client.updateTask = function(taskid, config, callback){
api.put(apiUrls.tasks + '/' + taskid, config, function(err, req, res, result) {
if (err) {
return callback(err);
}
callback(null, result);
});
};
/**
* Delete a task
*
* @param {String} taskid
* @param {Function} callback callback(err)
*/
client.deleteTask = function(taskid, callback){
api.del(apiUrls.tasks + '/' + taskid, function(err, req, res) {
callback(err);
});
};
/**
* Delete a collection, it's defaults and all tasks in it
*
* @param {String} collection
* @param {Function} callback callback(err)
*/
client.deleteCollection = function(collection, callback){
api.del(apiUrls.collections + '/' + collection, function(err, req, res) {
callback(err);
});
};
/**
* Run a task or a one-off command
*
* @param {String} task
* @param {Function} callback callback(err)
*/
client.run = function(task, callback){
api.post(apiUrls.run, {task: task}, function(err, req, res) {
callback(err);
});
};
return client;
};