jchaos
Version:
module to access chaos resources
177 lines (154 loc) • 6.01 kB
JavaScript
;
/**
* This tiny wrapper file checks for known node flags and appends them
* when found, before invoking the "real" _mocha(1) executable.
*/
var ops = ["start", "stop", "init", "deinit", "load", "unload", "kill", "shutdown", "health"];
var usops = ["start", "stop", "kill", "shutdown", "health"];
function checker(value, vect) {
for (var i = 0; i < vect.length; i++) {
if (value == vect[i]) {
return true;
}
}
return false;
}
function Usage() {
var pjson = require('../package.json');
console.log("Version:" + pjson.version);
console.log("Usage:\n" + process.argv[1] + "< --server server:port>\n[--upload <config to upload>]\n[--download <config output>>]\n" +
"[--findlive < cu | us | agent | mds | webui | variable | snapshotsof | snapshots | script | zone | class >]\n" +
"[--find < cu | us | agent | mds | webui | variable | snapshotsof | snapshots | script | zone | class >]\n" +
"[--op < start | stop | init | deinit| load | unload | kill | shutdown |health | push <JSON> >]\n" +
"[--uid <!CHAOS node UID>]\n" + "[--uid <UID> --register <JSON>]\n" + "[--uid <UID> --push <JSON>]\n" + "[--unregister <UID>]\n" +
"[--help]"
);
}
const jchaos = require('../jchaos');
const args = require('minimist')(process.argv.slice(2))
if (args.hasOwnProperty("help")) {
Usage();
process.exit(0);
}
var cnode = {
uid: "",
desc: {}
};
if (typeof args['server'] === "undefined") {
Usage();
process.exit(-1);
}
jchaos.setOptions({ 'uri': args['server'] });
if (typeof args['unregister'] === "string") {
jchaos.unregisterCU(args['unregister'], obj, function (data) {
console.log("- unregister ok");
process.exit(0);
}, function (err) {
console.error("# unregister failed:" + JSON.stringify(err));
process.exit(-1);
});
return 0;
}
if (typeof args['uid'] === "string") {
if (typeof args['register'] === "string") {
var obj = JSON.parse(args['register']);
jchaos.registerCU(args['uid'], obj, function (data) {
console.log("- registration ok");
process.exit(0);
}, function (err) {
console.error("# registration failed:" + JSON.stringify(err));
process.exit(-1);
});
return 0;
}
if (typeof args['push'] === "string") {
var obj = JSON.parse(args['push']);
jchaos.pushCU(args['uid'], obj, function (data) {
console.log("- push ok");
process.exit(0);
}, function (err) {
console.error("# push failed:" + JSON.stringify(err));
process.exit(-1);
});
return 0;
}
cnode['uid'] = args['uid'];
cnode['desc'] = jchaos.node(args['uid'], "desc", "all");
if ((cnode['desc']!=null) &&(typeof cnode['desc'] === "object")) {
if(!cnode['desc'].hasOwnProperty("ndk_type")){
console.error("MISSING key ndk_type in:"+JSON.stringify(cnode['desc']));
process.exit(-2);
}
console.log("type \"" + args['uid'] + "\"=" + cnode['desc'].ndk_type);
} else {
jchaos.perror("cannot get description of " + args['uid']);
process.exit(-1);
}
}
if (typeof args['upload'] === "string") {
// upload configuration
const fs = require('fs');
fs.readFile(args['upload'], 'utf8', function (err, contents) {
if (err) {
throw err;
}
var conf = JSON.parse(contents);
jchaos.restoreFullConfig(conf, function (j) {
jchaos.print("restore ok");
}, function (err) {
jchaos.perror("restore failed:" + err);
process.exit(-1);
});
});
} else if (typeof args['download'] === "string") {
jchaos.saveFullConfig(args['download']);
} else if (typeof args['findlive'] === "string") {
var obj = jchaos.search("", args['findlive'], true);
jchaos.print(JSON.stringify(obj));
} else if (typeof args['find'] === "string") {
var obj = jchaos.search("", args['findlive'], false);
jchaos.print(JSON.stringify(obj));
} else if (typeof args['op'] === "string") {
var typ = "";
if (cnode['desc'].ndk_type == "nt_unit_server") {
typ = "us";
if (!checker(args['op'], usops)) {
jchaos.perror("\"" + args['op'] + "\" invalid operation for " + typ);
process.exit(1);
}
if (cnode['desc'].parent = "") {
jchaos.print("%% " + cnode['uid'] + " is not controlled by agent, many operations are not supported");
}
} else if (cnode['desc'].ndk_type == "nt_control_unit") {
typ = "cu";
if (!checker(args['op'], ops)) {
jchaos.perror("\"" + args['op'] + "\" invalid operation for " + typ);
process.exit(1);
}
} else if (cnode['desc'].ndk_type == "nt_agent") {
typ = "agent";
if (!checker(args['op'], ["shutdown", "health"])) {
jchaos.perror("\"" + args['op'] + "\" invalid operation for " + typ + " just shutdown possible");
process.exit(1);
}
} else if (cnode['desc'].ndk_type == "nt_wan_proxy") {
typ = "webui";
if (!checker(args['op'], ["shutdown", "health"])) {
jchaos.perror("\"" + args['op'] + "\" invalid operation for " + typ);
process.exit(1);
}
}
if (typ != "") {
jchaos.node(cnode['uid'], args['op'], typ, function (res) {
jchaos.print(cnode['uid'] + "(" + typ + ") " + args['op'] + " OK");
if (typeof res === "object") {
jchaos.print(JSON.stringify(res));
}
}, function (err) {
jchaos.perror(cnode['uid'] + "(" + typ + ") " + args['op'] + " BAD:" + JSON.stringify(err));
});
} else {
jchaos.perror(" NOT SUPPORTED TYPE FOR " + cnode['uid'] + "(" + cnode['desc'].ndk_type + ") " + args['op'] + " BAD:" + err);
}
}