Exemple #1
0
aws.ec2DescribeInstances = function(options, callback)
{
    if (typeof options == "function") callback = options, options = {};
    if (!options) options = {};

    var i = 1, req = {}, map = { vpcId: "vpc-id", stateName: "instance-state-name", tagName: "tag:Name", tagKey: "tag-key", groupName: "group-name" };

    if (options.instanceId) {
        lib.strSplit(options.instanceId).forEach(function(x, j) { req["InstanceId." + (j + 1)] = x });
    }
    for (const p in map) {
        if (!options[p]) continue;
        req["Filter." + i + ".Name"] = map[p];
        lib.strSplit(options[p]).forEach(function(x, j) { req["Filter." + i + ".Value." + (j + 1)] = x; });
        i++;
    }
    for (const p in options.filters) {
        req["Filter." + i + ".Name"] = p;
        lib.strSplit(options.filters[p]).forEach(function(x, j) { req["Filter." + i + ".Value." + (j + 1)] = x; });
        i++;
    }
    logger.debug("ec2DescribeInstances:", req);
    this.queryEC2("DescribeInstances", req, options, function(err, rc) {
        var token = lib.objGet(rc, "DescribeInstancesResponse.nextToken");
        lib.tryCall(callback, err, aws.ec2ParseInstances(rc), token);
    });
}
Exemple #2
0
IpcRedisClient.prototype.onSentinelMessage = function(pattern, channel, msg)
{
    logger.debug("onSentinelMessage:", core.role, this.url, channel, msg)

    if (channel[0] == "+" || channel[0] == "-") channel = channel.substr(1);
    switch(channel) {
    case "reset-master":
        this.onSentinelConnect();
        break;

    case "sentinel":
        msg = lib.strSplit(msg, " ");
        if (!msg[1] || msg[5] != this.options.sentinel.name) break;
        if (this.options.sentinel.servers.indexOf(msg[1]) == -1) this.options.sentinel.servers.push(msg[1]);
        break;

    case 'switch-master':
        msg = lib.strSplit(msg, ' ');
        if (!msg[3] || msg[0] != this.options.sentinel.name) break;
        if (this.client && this.client.address == msg[3] + ":" + msg[4]) break;
        logger.error("onSentinelMessage:", "switch-master:", core.role, this.url, msg);
        this.initClient("client", msg[3], msg[4]);
        break;
    }
}
Exemple #3
0
aws.ec2DescribeSecurityGroups = function(options, callback)
{
    if (typeof options == "function") callback = options, options = {};
    if (!options) options = {};

    var req = this.vpcId ? { "Filter.1.Name": "vpc-id", "Filter.1.Value": this.vpcId } : {};
    if (options.name) {
        lib.strSplit(options.name).forEach(function(x, i) {
            req["Filter." + (i + 2) + ".Name"] = "group-name";
            req["Filter." + (i + 2) + ".Value"] = x;
        });
    }

    this.queryEC2("DescribeSecurityGroups", req, options, function(err, rc) {
        if (err) return typeof callback == "function" && callback(err);

        var groups = lib.objGet(rc, "DescribeSecurityGroupsResponse.securityGroupInfo.item", { list: 1 });
        // Filter by name regexp
        if (options.filter) {
            groups = groups.filter(function(x) { return x.groupName.match(options.filter) });
        }
        if (typeof callback == "function") callback(err, groups);
    });
}
Exemple #4
0
shell.cmdTestRun = function(options)
{
    var tests = shell;
    core.addModule("tests", tests);

    tests.test = { role: cluster.isMaster ? "master" : "worker", iterations: 0, stime: Date.now() };
    tests.test.countdown = tests.getArgInt("-test-iterations", options, 1);
    tests.test.forever = tests.getArgInt("-test-forever", options, 0);
    tests.test.timeout = tests.getArgInt("-test-timeout", options, 0);
    tests.test.interval = tests.getArgInt("-test-interval", options, 0);
    tests.test.keepmaster = tests.getArgInt("-test-keepmaster", options, 0);
    tests.test.workers = tests.getArgInt("-test-workers", options, 0);
    tests.test.workers_delay = tests.getArgInt("-test-workers-delay", options, 500);
    tests.test.delay = tests.getArgInt("-test-delay", options, 0);
    tests.test.cmd = tests.getArg("-test-run", options);
    tests.test.file = tests.getArg("-test-file", options, "tests/tests.js");
    if (tests.test.file) this.loadFile(tests.test.file);

    var cmds = lib.strSplit(tests.test.cmd);
    for (var i in cmds) {
        if (!this['test_' + cmds[i]]) {
            var avail = Object.keys(tests).filter(function(x) { return x.substr(0, 5) == "test_" && typeof tests[x] == "function" }).map(function(x) { return x.substr(5) }).join(", ");
            logger.error("cmdTestRun:", "invaid test:", cmds[i], "usage: -test-run CMD where CMD is one of:", avail, "ARGS:", process.argv, "TEST:", tests.test);
            process.exit(1);
        }
    }

    if (cluster.isMaster) {
        setTimeout(function() { for (var i = 0; i < tests.test.workers; i++) cluster.fork(); }, tests.test.workers_delay);
        cluster.on("exit", function(worker) {
            if (!Object.keys(cluster.workers).length && !tests.test.forever && !tests.test.keepmaster) process.exit(0);
        });
    } else {
        if (!tests.test.workers) return "continue";
    }

    setTimeout(function() {
        logger.log("tests started:", cluster.isMaster ? "master" : "worker", 'cmd:', tests.test.cmd, 'db-pool:', core.modules.db.pool);

        lib.whilst(
          function () {
              return tests.test.countdown > 0 || tests.test.forever || options.running
          },
          function (next) {
              tests.test.countdown--;
              lib.forEachSeries(cmds, function(cmd, next2) {
                  tests["test_" + cmd](function(err) {
                      tests.test.iterations++;
                      if (tests.test.forever) err = null;
                      setTimeout(next2.bind(null, err), tests.test.interval);
                  });
              }, next);
          },
          function(err) {
              tests.test.etime = Date.now();
              if (err) {
                  logger.inspectArgs.errstack = 1;
                  logger.error("FAILED:", tests.test.role, 'cmd:', tests.test.cmd, err);
                  process.exit(1);
              }
              logger.log("SUCCESS:", tests.test.role, 'cmd:', tests.test.cmd, 'db-pool:', core.modules.db.pool, 'time:', tests.test.etime - tests.test.stime, "ms");
              process.exit(0);
          });
    }, tests.test.delay);
}
Exemple #5
0
 this.client.info(function(e,v) {
     v = lib.strSplit(v, "\n").filter(function(x) { return x.indexOf(":") > -1 }).map(function(x) { return x.split(":") }).reduce(function(x,y) { x[y[0]] = y[1]; return x }, {});
     callback(e, v);
 });
Exemple #6
0
Pool.prototype.queryCondition = function(obj, options)
{
    var o = {};
    for (var p in obj) {
        if (p[0] == '_') continue;
        switch (options.ops && options.ops[p]) {
        case "regexp":
            o[p] = { '$regex': obj[p] };
            break;

        case "between":
            var val = lib.strSplit(obj[p]);
            if (val.length == 2) {
                o[p] = { '$gte': val[0], '$lte': val[1] };
            } else {
                o[p] = obj[p];
            }
            break;

        case "like%":
        case "begins_with":
            o[p] = { '$regex': "^" + obj[p] };
            break;

        case "in":
            o[p] = { '$in': lib.strSplit(obj[p]) };
            break;

        case ">":
        case "gt":
            o[p] = { '$gt': obj[p] };
            break;

        case "<":
        case "lt":
            o[p] = { '$lt': obj[p] };
            break;

        case ">=":
        case "ge":
            o[p] = { '$gte': obj[p] };
            break;

        case "<=":
        case "le":
            o[p] = { '$lte': obj[p] };
            break;

        case "ne":
        case "!=":
        case "<>":
            o[p] = { '$ne': obj[p] };
            break;

        case "eq":
            o[p] = obj[p];
            break;

        default:
            if (typeof obj[p] == "string" && !obj[p]) break;
            o[p] = obj[p];
        }
    }
    return o;
}