-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
1160 lines (1039 loc) · 44.2 KB
/
core.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, Samuel Colbran <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var crypto = require('crypto');
var express = require('express');
var override = require("./override");
var Store = require('./ministore')('database');
var configuration = Store('config');
var portscanner = require('portscanner');
var readline = require('readline');
var dom = require('domain');
fs = require('fs');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
/*
process.on('uncaughtException', function(err) {
console.log('Threw Exception: ', err);
});
*/
XMLHttpRequest = require('xhr2');//require("xmlhttprequest").XMLHttpRequest;
module.exports.createAgent = (function (app, callback) {
require('crypto').randomBytes(48, function (ex, buf) {
var secret = buf.toString('hex');
var root = {secret: secret,
app: app};
root.flushSettings = function()
{
root.global_port = configuration.get("agent_port");
root.is_simple = configuration.get("is_simple");
root.agent_uid = configuration.get("agent_uid");
root.agent_key = configuration.get("agent_key");
root.agent_host = configuration.get("agent_host");
root.broker_url = configuration.get("broker_url");
root.broker_port = configuration.get("broker_port");
root.verbose = configuration.get("verbose");
root.show_requests = configuration.get("show_requests");
}
root.flushSettings();
root.pluginSettings = {};
const preferredLineLength = 40;
var protocol = "wrapper-json";
var access_denied_error = "Access denied.";
root.pluginDomains = {};
function killPlugin(pluginName)
{
if (pluginName in root.pluginDomains)
{
var existing_domain = root.pluginDomains[pluginName];
root.pluginDomains.dispose();
delete root.pluginDomains[pluginName];
}
}
function loadPlugin(pluginName)
{
killPlugin(pluginName);
var plugin_domain = require('domain').create();
//In the event of an error, kill or restart the plugin.
plugin_domain.on('error', function(err){
console.log("Plugin '" + pluginName + "' crashed");
console.log(err);
});
var pluginLocation = "./plugins/" + pluginName + "/plugin.js";
fs.exists(pluginLocation, function (exists) {
if (exists) {
var database = Store(pluginName);
//Each plugin will get its own domain.
plugin_domain.run(function(){
var plug = require(pluginLocation);
plug.setupPlugin(root, database);
});
}
else
{
console.log("plugin.js is missing for plugin '"+pluginName+"'");
}
});
}
function sendSetupToServer(host, port, data_dictionary, callback) {
var xhr = new XMLHttpRequest();
xhr.open('post', "http://" + host + ":" + port + "/wrapper-setup", true);
xhr.timeout = 5000;
xhr.setRequestHeader("Content-Type", "application/json");
xhr.ontimeout = function () {
callback('', "Server is not responding. Request timed out.");
}
xhr.onerror = function (e) {
callback('', "Server is not responding. " + xhr.statusText);
};
xhr.onload = function () {
var xmlDoc = xhr.responseText;
var jsonResponse = JSON.parse(xmlDoc);
callback(jsonResponse, '');
}
var json_data = JSON.stringify(data_dictionary);
xhr.send(json_data);
}
function sendActionToServer(data_dictionary, callback) {
data_dictionary['time-stamp'] = new Date().getTime();
data_dictionary['uid'] = root.agent_uid;
data_dictionary['token'] = '';
var dictionaryAttribute = JSON.stringify(data_dictionary);
var computedSignature = hmacsha1(root.agent_key, root.agent_uid + dictionaryAttribute);
data_dictionary['token'] = computedSignature;
var xhr = new XMLHttpRequest();
xhr.timeout = 5000;
xhr.open('post', "http://" + root.broker_url + ":" + root.broker_port + "/" + protocol, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (e) {
callback('', xhr.statusText);
};
xhr.ontimeout = function (e) {
callback('', "Server is not responding. Request timed out.");
};
xhr.onload = function () {
var xmlDoc = xhr.responseText;
var jsonResponse = JSON.parse(xmlDoc);
callback(jsonResponse, '');
}
var json_data = JSON.stringify(data_dictionary);
xhr.send(json_data);
}
root.sendActionToServer = sendActionToServer;
function hmacsha1(key, text) {
return crypto.createHmac('sha1', key).update(text).digest('base64')
}
function sendReplyToClient(client, data_dictionary) {
if (client.type == "json") {
var json_string = JSON.stringify(data_dictionary);
client.response.writeHead(200, {'Content-Type': 'application/json'});
client.response.write(json_string);
client.response.end();
}
else if (client.type == "jsonp")
client.response.jsonp(data_dictionary);
else
console.log("Unknown client protocol");
}
root.sendReplyToClient = sendReplyToClient;
function rejectDataFromClient(client) {
if (client.json.action == "submit") {
return sendReplyToClient(client, {
vReport: {
accepted: false
}
});
}
else if (client.json.action == "submit") {
return sendReplyToClient(client, {
accepted: false,
estRuntime: "0"
});
}
return sendReplyToClient(client, {error: "Your request was rejected by the server"});
}
root.rejectDataFromClient = rejectDataFromClient;
function receiveDataFromClient(client) {
var user = client.json['uid'];
if (client.json.experimentID != null) //TODO: Add check for action
{
if (user != null) {
if (client.json.action == "submit") {
var responseFunction = (function (response_client) {
return function (obj, err) {
//Associate the returned experiment with the user id
if (root.verbose) console.log(JSON.stringify(obj));
//Reply to the client
sendReplyToClient(response_client, obj);
};
})(client);
sendActionToServer(client.json, responseFunction);
}
else {
//Check whether this experiment was submitted by this user (or if they have sufficient privlidges to do this action)
if (client.json.action == "retrieveResult") {
var responseFunction = (function (response_client) {
return function (obj, err) {
if (root.verbose) console.log(JSON.stringify(obj));
sendReplyToClient(response_client, obj);
};
})(client);
sendActionToServer(client.json, responseFunction);
}
else {
//Pass this action through to the server
var responseFunction = (function (response_client) {
return function (obj, err) {
sendReplyToClient(response_client, obj);
};
})(client);
sendActionToServer(client.json, responseFunction);
}
}
}
else return console.log("Invalid user (null)");
}
else {
var responseFunction = (function (response_client) {
return function (obj, err) {
sendReplyToClient(response_client, obj);
};
})(client);
sendActionToServer(client.json, responseFunction);
}
}
root.receiveDataFromClient = receiveDataFromClient;
function isAuthenticated(req) {
if (root.verbose) console.log("Checking authentication");
if (req) {
var uid = req['uid'];
var token = req['token'];
if (uid && token) {
var computedSignature = hmacsha1(secret, uid);
computedSignature = computedSignature.split("+").join(" ");
token = token.split("+").join(" ");
if (computedSignature == token) {
if (root.verbose) console.log("Authentication successful");
return true;
}
else {
if (root.verbose) console.log("Javascript authentication failed (" + uid + "). Incorrect signature: " + computedSignature + " should be " + token);
}
}
else {
if (root.verbose) console.log("Javascript authentication failed (" + uid + "). Missing UUID or Token.");
}
}
if (root.verbose) console.log("Missing request");
return false;
}
root.isAuthenticated = isAuthenticated;
function javascriptToken(uid) {
var computedSignature = hmacsha1(secret, uid);
var JS_Script = '<script type="text/javascript">var token_string = {u:"' + uid + '",t:"' + computedSignature + '"};var agent_host = "' + root.agent_host + '";var agent_port = "' + root.global_port /*config.wrapper_port*/ + '";</script>';
return JS_Script;
}
function tokenDictionary(uid) {
var computedSignature = hmacsha1(secret, uid);
return {uid: uid, hash: computedSignature};
}
root.tokenDictionary = tokenDictionary;
root.javascriptToken = javascriptToken;
function startMessage() {
console.log("");
console.log("iLab Agent");
console.log("Version: 1.0.3");
console.log(" Build: 5");
console.log(" Date: 2/9/2014");
printSeparator();
}
function setupExpress(secret) {
var passport = require("passport");
var path = require('path');
// Domain on every request
app.use(function(req, res, next) {
var domain = dom.create();
domain.add(req);
domain.add(res);
domain.run(function() {
next();
});
domain.on('error', function(e) {
console.log("A caught express error occured");
console.log(e.stack);
res.send('500: Internal Server Error', 500);
});
});
//app.use(require('express-domain-middleware'));
app.set('port', root.global_port);//config.wrapper_port);
if (root.show_requests) {
app.use(express.logger("dev"));
}
app.use(express.cookieParser());
app.use(express.bodyParser());
var cookieName = 'agentCookies' + root.global_port;
app.use(express.session({secret: secret, key: cookieName}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.methodOverride());
app.use(app.router);
app.use('/public', express.static(path.join(__dirname, 'public')));
if (root.show_requests) {
app.use(express.logger());
}
}
function printDots(dotNum)
{
var d = 0;
for (d = 0; d < dotNum; d++) {
setupConsole(".");
}
}
function flushPlugins(callback) {
fs.readdir("./plugins", function (err, files) {
if (err) {
console.log("Unable to read plugins folder");
return console.log(err);
}
files = removeHiddenFiles(files);
var loaded_plugins = Object.keys(root.pluginSettings);
var i;
for (i=0;i<files.length;i++)
{
var plugin_name = files[i];
if (loaded_plugins.indexOf(plugin_name) == -1)
{
//console.log("Discovered new plugin '" + plugin_name + "'");
root.pluginSettings[plugin_name] = {enabled:false};
}
}
configuration.set("plugins", root.pluginSettings);
callback();
});
}
function setupPlugins(secret) {
var k = 0;
console.log("Loading plugins");
root.pluginSettings = configuration.get("plugins");
var plugins = Object.keys(root.pluginSettings);
for (k = 0; k < plugins.length; k++) {
var plugin_name = plugins[k];
setupConsole(plugin_name);
if (root.pluginSettings[plugin_name].enabled)
{
var dotNum = preferredLineLength - plugin_name.length-6;
printDots(dotNum);
loadPlugin(plugin_name);
/*
var database = Store(plugin_name);
var plug = require("./plugins/" + plugin_name + "/plugin.js");
plug.setupPlugin(root, database);
*/
setupConsole("loaded\n");
}
else
{
var dotNum = preferredLineLength - plugin_name.length-8;
printDots(dotNum);
setupConsole("disabled\n");
}
}
console.log("");
}
function startServer(callback) {
require("http").createServer(app).listen(app.get('port'), function () {
callback();
});
}
function clearConsole() {
if (typeof process.stdout.getWindowSize !== 'undefined') {
var lines = process.stdout.getWindowSize()[1];
for (var i = 0; i < lines; i++) {
console.log('\r\n');
}
}
}
clearConsole();
startMessage();
//Setup functions
function setupConsole(message) {
process.stdout.write(message);
}
function setupYes(entry) {
var trimmed = entry.trim();
return (trimmed.indexOf("y") == 0);
}
var currentSetupPlugin = 0;
var setupPluginList = [];
function setupNextPlugin() {
if (currentSetupPlugin < setupPluginList.length) {
var currentPlugin = setupPluginList[currentSetupPlugin];
currentSetupPlugin++;
var pluginLocation = "./plugins/" + currentPlugin + "/plugin.js";
fs.exists(pluginLocation, function (exists) {
if (exists) {
rl.question("Enable " + currentPlugin + " (y/n)? ", function (enablePlugin) {
if (setupYes(enablePlugin)) {
//Load the plugin
setupConsole("Loading " + currentPlugin + "...");
var database = Store(currentPlugin);
var plug = require(pluginLocation);
setupConsole("success.\n");
if (typeof(plug.setupGUI) === "function") {
plug.setupGUI(rl, database, function (setupNextPlugin) {
return function () {
console.log(currentPlugin + " was setup successfully.");
if (typeof(plug.setupPlugin) === "function") {
loadPlugin(currentPlugin);
//plug.setupPlugin(root, database);
root.pluginSettings[currentPlugin] = {enabled: true};
return setupNextPlugin();
}
else {
console.log("Plugin is broken. Skipping.");
root.pluginSettings[currentPlugin] = {enabled: false};
return setupNextPlugin();
}
}
}(setupNextPlugin));
}
else //Plugin does not require a setup
{
if (typeof(plug.setupPlugin) === "function") {
loadPlugin(currentPlugin);
//plug.setupPlugin(root, database);
root.pluginSettings[currentPlugin] = {enabled: true};
return setupNextPlugin();
}
else {
console.log("Plugin is broken. Skipping.");
root.pluginSettings[currentPlugin] = {enabled: false};
return setupNextPlugin();
}
}
}
else {
root.pluginSettings[currentPlugin] = {enabled: false};
return setupNextPlugin();
}
});
} else {
//setupConsole("failed.\n");
//console.log("plugin.js is missing. Skipping.");
root.pluginSettings[currentPlugin] = {enabled: false};
return setupNextPlugin();
}
});
}
else //No more plugins!
{
configuration.set("plugins", root.pluginSettings);
configuration.set("setup_complete", true);
console.log(" ");
console.log("Agent setup complete!");
commandlineGUI();
}
}
function removeHiddenFiles(files)
{
var i;
for (i=0; i < files.length; i++)
{
if (files[i].indexOf('.') == 0)
{
files.splice(i, 1);
i--;
}
}
return files;
}
function setupAllowedPlugins() {
setupConsole("Finding plugins...");
fs.readdir("./plugins", function (err, files) {
if (err) {
setupConsole("failed\n");
return console.log(err);
}
setupConsole("success\n");
console.log("For each of the plugins below, type 'y' to enable it.");
currentSetupPlugin = 0;
setupPluginList = removeHiddenFiles(files);
setupNextPlugin();
});
}
function setupPromtSimpleAgent() {
rl.question("Does this agent need experiment results (y/n)? ", setupSimpleAgent);
}
function setupSimpleAgent(simpleAnswer) {
if (setupYes(simpleAnswer)) {
rl.question("Agent Host: ", function (agent_hostname) {
root.agent_host = agent_hostname;
configuration.set("agent_host", root.agent_host);
setupConsole("Attempting to register full agent with service broker...");
root.is_simple = false;
configuration.set("is_simple", false);
registerBroker(function (data, err) {
if (err) {
setupConsole("failed\n");
console.log(err);
setupPromptAgentRegistration();
}
else if (data.error) {
setupConsole("failed\n");
console.log(data.error);
setupPromptAgentRegistration();
} else if (!data) {
setupConsole("failed\n");
console.log("The service broker is not responding.");
setupPromptAgentRegistration();
} else {
setupConsole("success\n");
setupAllowedPlugins();
}
})
});
}
else //Skip the non-simple setup
{
setupAllowedPlugins();
configuration.set("is_simple", true);
}
}
function setupPromptAgentRegistration() {
rl.question("Agent GUID: ", setupAgentRegistration);
}
function setupAgentRegistration(agent_guid) {
root.agent_uid = agent_guid;
configuration.set("agent_uid", root.agent_uid);
rl.question("Agent Passkey: ", function (agent_pkey) {
root.agent_key = agent_pkey;
configuration.set("agent_key", root.agent_key);
setupConsole("Attempting to register simple agent with service broker...");
root.is_simple = true;
registerBroker(function (data, err) {
if (err) {
setupConsole("failed\n");
console.log(err);
setupPromptAgentRegistration();
}
else if (data.error) {
setupConsole("failed\n");
console.log(data.error);
setupPromptAgentRegistration();
} else {
setupConsole("success\n");
setupPromtSimpleAgent();
}
});
});
}
function setupPromptBroker() {
rl.question("Broker Host: ", setupBrokerURL);
}
function setupBrokerURL(broker_url) {
var brokerFound = function (json) {
setupConsole("available\n");
console.log("Broker vendor: " + json['vendor']);
rl.question("Is this correct (y/n)? ", function (vendorValid) {
if (setupYes(vendorValid)) {
root.broker_url = broker_url;
configuration.set('broker_url', broker_url);
setupPromptAgentRegistration();
}
else {
return setupPromptBroker();
}
});
};
rl.question("Broker Port: ", function (brokerPort) {
root.broker_port = brokerPort;
configuration.set('broker_port', brokerPort);
setupConsole("Testing connection to " + broker_url + ":" + brokerPort + "...");
sendSetupToServer(broker_url, brokerPort, {action: "ping"}, function (json, status) {
if (status) {
setupConsole("unavailable\n");
console.log(status);
return setupPromptBroker();
}
if (json['success'] == true) {
brokerFound(json);
}
else if (json['success'] == false) {
setupConsole("available\n");
console.log("Broker server is being uncooperative. Check with the owner.");
console.log("Error: " + json['error']);
return setupPromptBroker();
}
});
});
}
function setupAgentStart() {
setupConsole("available\nStarting server...");
root.global_port = configuration.get("agent_port");
setupExpress(secret);
startServer(function () {
setupConsole("success\n");
return setupPromptBroker();
});
}
function setupAgentPrompt() {
rl.question("Port (for this agent): ", setupAgentPort);
}
function setupAgentPort(agent_port) {
var continueFunction = function (port_number) {
//Store this port
configuration.set("agent_port", port_number);
//Check whether the port is valid
setupConsole("Checking port " + port_number + "...");
portscanner.checkPortStatus(port_number, '127.0.0.1', function (error, status) {
// Status is 'open' if currently in use or 'closed' if available
if (error)
console.log(error);
if (status == 'open') {
setupConsole("unavailable.\n");
return setupAgentPrompt();
}
else {
setupAgentStart();
}
});
};
if (agent_port == '') {
setupConsole("Finding port...");
return portscanner.findAPortNotInUse(2000, 20000, '127.0.0.1', function (error, port) {
if (error) {
setupConsole("failed.\n");
return setupAgentPrompt();
}
setupConsole("" + port + "\n");
return continueFunction(parseInt(port));
})
}
else if (agent_port == 'skip') {
setupConsole("Skipping port check -> assuming port is ");
return setupAgentStart();
}
return continueFunction(parseInt(agent_port));
}
function setupAgent() {
//Does the agent need to be setup?
var setup_complete = configuration.get("setup_complete");
if (setup_complete != true) {
rl.question("Press enter to begin Agent setup.", function (answer) {
rl.question("Port (for this agent): ", setupAgentPort);
});
return false;
}
return true;
}
function registerAgent(success, failure)
{
setupConsole("Connecting to service broker...");
registerBroker(function (data, err) {
if (err) {
setupConsole("failed\n");
console.log(err);
failure();
}
else if (data.error) {
setupConsole("failed\n");
console.log(data.error);
failure();
} else if (!data) {
setupConsole("failed\n");
console.log("The service broker is not responding");
failure();
} else {
setupConsole("success\n");
success();
}
})
}
function commandlineGUI()
{
rl.question(">>> ", function (command) {
var args = command.split(" ");
if (args[0] == "help")
{
console.log("commands");
console.log(" connect - connect to the broker");
console.log(" test - get some details about this agent from the broker");
console.log(" ");
console.log(" stop - stop the agent");
console.log(" ");
console.log(" set [key] [value] - modify configuration settings");
console.log(" get [key] - retrieve configuration settings");
console.log(" config - retrieve all configuration settings");
console.log(" ");
console.log(" plugins - show a list of plugins");
console.log(" enable [plugin] - enable a plugin");
console.log(" disable [plugin] - disable a plugin (requires server restart)");
console.log(" config [plugin] - retrieve all configuration settings for a plugin");
console.log(" set [plugin] [key] [value] - modify configuration settings for a plugin");
console.log(" get [plugin] [key] - retrieve configuration settings for a plugin");
console.log(" ");
console.log("configuration keys");
console.log(" is_simple - [true/false]");
console.log(" agent_host - agent hostname");
console.log(" agent_port - agent port");
console.log(" agent_uid - agent guid");
console.log(" agent_key - agent passkey");
console.log(" broker_url - broker hostname");
console.log(" broker_port - broker port");
console.log(" verbose - show verbose logging");
console.log(" show_requests - show express requests");
}
else if (args[0] == "plugins") {
return flushPlugins(function(){
root.pluginSettings = configuration.get("plugins");
var plugins = Object.keys(root.pluginSettings);
for (k = 0; k < plugins.length; k++) {
var plugin_name = plugins[k];
setupConsole(plugin_name);
if (root.pluginSettings[plugin_name].enabled)
{
var dotNum = preferredLineLength - plugin_name.length-7;
printDots(dotNum);
setupConsole("enabled\n");
}
else
{
var dotNum = preferredLineLength - plugin_name.length-8;
printDots(dotNum);
setupConsole("disabled\n");
}
}
commandlineGUI();
});
}
else if (args[0] == "test") {
return sendActionToServer({
action: "getAgentInfo"
}, function (data, err) {
if (err) {
console.log("error: " + err);
}
else if (!data.message)
{
console.log("an expected error occurred");
}
else {
console.log(data.message);
}
commandlineGUI();
});
}
else if (args[0] == "stop") {
process.exit();
}
else if (args[0] == "connect") {
return registerAgent(function () {
printBrokerInfo();
commandlineGUI();
}, function () {
printBrokerInfo();
commandlineGUI();
});
}
else if (args[0] == "enable") {
if (args.length == 2) {
return flushPlugins(function(){
var currentPlugin = args[1];
if (Object.keys(root.pluginSettings).indexOf(currentPlugin) != -1)
{
function finishedPlugin(){
configuration.set("plugins", root.pluginSettings);
console.log("Restart the agent to see plugin changes.");
commandlineGUI();
}
var pluginLocation = "./plugins/" + currentPlugin + "/plugin.js";
fs.exists(pluginLocation, function (exists) {
if (exists) {
var database = Store(currentPlugin);
var plug = require(pluginLocation);
if (typeof(plug.setupGUI) === "function") {
plug.setupGUI(rl, database, function (finishedPlugin) {
return function () {
console.log(currentPlugin + " was setup successfully.");
if (typeof(plug.setupPlugin) === "function") {
loadPlugin(currentPlugin);
//plug.setupPlugin(root, database);
root.pluginSettings[currentPlugin] = {enabled: true};
return finishedPlugin();
}
else {
console.log("Plugin is broken.");
root.pluginSettings[currentPlugin] = {enabled: false};
return finishedPlugin();
}
}
}(finishedPlugin));
}
else //Plugin does not require a setup
{
if (typeof(plug.setupPlugin) === "function") {
loadPlugin(currentPlugin);
//plug.setupPlugin(root, database);
root.pluginSettings[currentPlugin] = {enabled: true};
return finishedPlugin();
}
else {
console.log("Plugin is broken.");
root.pluginSettings[currentPlugin] = {enabled: false};
return finishedPlugin();
}
}
}
else
{
console.log("Plugin '" + currentPlugin + "' is broken. Missing plugin.js");
commandlineGUI();
}
});
}
else
{
console.log("Invalid plugin '" + currentPlugin + "'");
commandlineGUI();
}
});
} else {
console.log("invalid arguments. 1 argument expected (had " + args.length-1 + ")");
}
}
else if (args[0] == "disable") {
if (args.length == 2) {
return flushPlugins(function(){
var pluginName = args[1];
if (Object.keys(root.pluginSettings).indexOf(pluginName) != -1)
{
root.pluginSettings[pluginName].enabled = false;
configuration.set("plugins", root.pluginSettings);
killPlugin(pluginName);
console.log("Restart the agent to see plugin changes.");
}
else
{
console.log("Invalid plugin '" + pluginName + "'");
}
commandlineGUI();
});
} else {
console.log("invalid arguments. 1 argument expected (had " + args.length-1 + ")");
}
}
else if (args[0] == "set") {
if (args.length == 3) {
var key = args[1];
var value = args[2];
configuration.set(key,value);
console.log(key + ": " + configuration.get(key));
root.flushSettings();
} else if (args.length == 4) {
//Is this a plugin?
var pluginName = args[1];
if (Object.keys(root.pluginSettings).indexOf(pluginName) != -1)
{
var database = Store(args[1]);
var key = args[2];
var value = args[3];
configuration.set(key,value);
console.log(key + ": " + configuration.get(key));
console.log("Restart the agent to see plugin changes.");
}
else
{
console.log("Invalid plugin '" + pluginName + "'");
}
} else {
console.log("invalid arguments. 2 arguments expected (had " + args.length-1 + ")");
}
}
else if (args[0] == "get") {
if (args.length == 2) {
var key = args[1];
console.log(key + ": " + configuration.get(key));
} else if (args.length == 4) {
//Is this a plugin?
var pluginName = args[1];
if (Object.keys(root.pluginSettings).indexOf(pluginName) != -1)
{
var database = Store(args[1]);
var key = args[2];
console.log(key + ": " + database.get(key));
}
else
{
console.log("Invalid plugin '" + pluginName + "'");
}
} else {