function ConnectionPool(opts) {
    EventEmitter.call(this);
    assert.object(opts, 'opts');
    assert.number(opts.connectTimeout, 'opts.connectTimeout');
    assert.number(opts.max);
    assert.object(opts.log);

    this.log = opts.log;
    this.port = opts.port;
    this.max = opts.max;
    this.connectTimeout = opts.connectTimeout;
    if (typeof (opts.retries) === 'number') {
        this.retry = {
            minTimeout: 1000,
            maxTimeout: 10000,
            retries: opts.retries
        };
    } else {
        this.retry = opts.retries || { retries: Infinity };
    }

    this.ring = new Ring();
    var self = this;
    var forwardEvents = ['online', 'offline', 'activate', 'deactivate'];
    forwardEvents.forEach(function (event) {
        self.ring.on(event, self.emit.bind(self, event));
    });

    this.__defineGetter__('connected', function () {
        return (self.ring.active.length !== 0);
    });
}
Example #2
0
function loadConfigSync(opts) {
    assert.object(opts, 'opts');
    assert.object(opts.log, 'opts.log');

    var configPath = path.resolve(__dirname, '..', 'etc', 'config.json');
    opts.log.info('Loading config from "%s"', configPath);
    var config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));

    // Validation. XXX backend-specific config validation should not be here.
    assert.number(config.port, 'config.port');
    assert.number(config.defaultMemory, 'config.defaultMemory');
    assert.string(config.packagePrefix, 'config.packagePrefix');
    assert.string(config.logLevel, 'config.logLevel');
    assert.object(config.cnapi, 'config.cnapi');
    assert.string(config.cnapi.url, 'config.cnapi.url');
    assert.object(config.imgapi, 'config.imgapi');
    assert.string(config.imgapi.url, 'config.imgapi.url');
    assert.object(config.napi, 'config.napi');
    assert.string(config.napi.url, 'config.papi.url');
    assert.object(config.papi, 'config.napi');
    assert.string(config.papi.url, 'config.papi.url');
    assert.object(config.vmapi, 'config.vmapi');
    assert.string(config.vmapi.url, 'config.vmapi.url');

    return config;
}
Example #3
0
/*
 * MedusaControl handles the creation of MedusaSessions and the management
 * of the Medusa Session Directory (a moray bucket).
 */
function
MedusaControl(opts)
{
	mod_assert.object(opts.log, 'log');
	mod_assert.object(opts.identity, 'identity');
	mod_assert.string(opts.identity.medusa_host, 'identity.medusa_host');
	mod_assert.string(opts.identity.medusa_ip, 'identity.medusa_ip');
	mod_assert.number(opts.identity.medusa_port, 'identity.medusa_port');
	mod_assert.object(opts.moray, 'moray');
	mod_assert.string(opts.moray.host, 'moray.host');
	mod_assert.number(opts.moray.port, 'moray.port');

	this.mdc_log = opts.log.child({
		component: 'Control'
	});

	this.mdc_sessions = {};

	this.mdc_moray_config = mod_common.clone(opts.moray);
	this.mdc_moray = null;

	this.mdc_moray_config.log = opts.log.child({
		component: 'MorayClient'
	});

	this.mdc_identity = mod_common.clone(opts.identity);

	mod_events.EventEmitter.call(this);
}
Example #4
0
// --- Helpers


// --- Image object


/**
 * Image model constructor
 */
function Image(params) {
    assert.object(params, 'image params');
    assert.number(params.created, 'params.created');
    assert.string(params.docker_id, 'params.docker_id');
    assert.string(params.image_uuid, 'params.image_uuid');
    assert.string(params.owner_uuid, 'params.owner_uuid');
    assert.optionalArrayOfString(params.heads, 'params.heads');
    assert.number(params.size, 'params.size');
    assert.number(params.virtual_size, 'params.virtual_size');
    assert.optionalObject(params.config, 'params.config');
    assert.optionalObject(params.container_config, 'params.container_config');
    assert.optionalString(params.parent, 'params.parent');

    this.params = params;
    if (!this.params.heads)
        this.params.heads = [];

    this.__defineGetter__('docker_id', function () {
        return this.params.docker_id;
    });
    this.__defineGetter__('image_uuid', function () {
        return this.params.image_uuid;
    });
    this.__defineGetter__('owner_uuid', function () {
        return this.params.owner_uuid;
    });
    this.__defineGetter__('refcount', function () {
        return this.params.heads.length;
    });
}
Example #5
0
function assertRecovery(obj, name) {
	if (name === undefined)
		name = 'recovery';
	mod_assert.object(obj, name);
	var ks = {};
	Object.keys(obj).forEach(function (k) { ks[k] = true; });
	mod_assert.number(obj.retries, name + '.retries');
	mod_assert.ok(isFinite(obj.retries), name + '.retries must be finite');
	mod_assert.ok(obj.retries >= 0, name + '.retries must be >= 0');
	delete (ks.retries);
	mod_assert.number(obj.timeout, name + '.timeout');
	mod_assert.ok(isFinite(obj.timeout), name + '.timeout must be finite');
	mod_assert.ok(obj.timeout > 0, name + '.timeout must be > 0');
	delete (ks.timeout);
	mod_assert.optionalNumber(obj.maxTimeout, name + '.maxTimeout');
	mod_assert.ok(obj.maxTimeout === undefined ||
	    obj.timeout < obj.maxTimeout,
	    name + '.maxTimeout must be > timeout');
	delete (ks.maxTimeout);
	mod_assert.number(obj.delay, name + '.delay');
	mod_assert.ok(isFinite(obj.delay), name + '.delay must be finite');
	mod_assert.ok(obj.delay >= 0, name + '.delay must be >= 0');
	delete (ks.delay);
	mod_assert.optionalNumber(obj.maxDelay, name + '.maxDelay');
	mod_assert.ok(obj.maxDelay === undefined ||
	    obj.delay < obj.maxDelay,
	    name + '.maxDelay must be > delay');
	delete (ks.maxDelay);
	mod_assert.deepEqual(Object.keys(ks), []);
}
Example #6
0
function Pool(options) {
        assert.object(options, 'options');
        assert.optionalFunc(options.assert, 'options.assert');
        assert.func(options.check, 'options.check');
        assert.number(options.checkInterval, 'options.checkInterval');
        assert.func(options.create, 'options.create');
        assert.object(options.log, 'options.log');
        assert.number(options.max, 'options.max');
        assert.number(options.maxIdleTime, 'options.maxIdleTime');
        assert.string(options.name, 'options.name');

        EventEmitter.call(this, options);

        this.available = [];
        this.assert = options.assert || function () { return (true); };
        this.create = options.create;
        this.check = options.check;
        this.checkInterval = options.checkInterval;
        this.destroy = options.destroy || false;
        this.events = (options.events || DEFAULT_EVENTS).slice();
        this.log = options.log.child({pool: options.name}, true);
        this.max = Math.max(options.max, 1);
        this.maxIdleTime = options.maxIdleTime;
        this.name = options.name;
        this.pendingResources = 0;
        this.queue = [];
        this.resources = [];
        this.stopped = false;
        this.stopping = false;
        this.timer = false;

        this._scheduleReaper();
}
Example #7
0
function Client(options) {
    assert.object(options, 'options');
    assert.number(options.connectTimeout, 'options.connectTimeout');
    assert.string(options.host, 'options.host');
    assert.number(options.port, 'options.port');
    assert.object(options.retry, 'options.retry');

    EventEmitter.call(this);

    this.fast_msgid = 0;
    this.fast_conn = null;
    this.fast_requests = {};
    this._options = options;

    if (options.reconnect) {
        var r = options.reconnect;
        var num = (typeof (r) === 'number' ? r : 1000);
        var self = this;

        this.fast_reconnect = function () {
            self.fast_timer = setTimeout(function () {
                self.connect();
            }, num);
        };
    } else {
        this.fast_reconnect = false;
    }

    this.connect();
}
Example #8
0
function connect(client, options, callback) {
        assert.object(client, 'client');
        assert.object(options, 'options');
        assert.number(options.connectTimeout, 'options.connectTimeout');
        assert.string(options.host, 'options.host');
        assert.number(options.port, 'options.port');
        assert.object(options.retry, 'options.retry');
        assert.func(callback, 'callback');

        callback = once(callback);

        var maxRetries;
        var retry = backoff.call(createSocket, options, callback);
        retry.on('backoff', client.emit.bind(client, 'connectAttempt'));

        retry.setStrategy(new backoff.ExponentialStrategy({
                initialDelay: options.retry.minTimeout || 1000,
                maxDelay: options.retry.maxTimeout || Infinity
        }));

        if (typeof (options.retry.retries) === 'number') {
                maxRetries = options.retry.retries;
        } else {
                maxRetries = Infinity;
        }

        retry.failAfter(maxRetries);
        retry.start();

        return (retry);
}
Example #9
0
SocketManager.prototype.pushResize = function (id, resize) {
    assert.string(id, 'socket identifier');
    assert.object(resize, 'resize object');
    assert.number(resize.w, 'resize columns');
    assert.number(resize.h, 'resize rows');

    this.resizes[id] = resize;
    return resize;
};
/**
 * An implementation of the Token Bucket algorithm.
 *
 * Basically, in network throttling, there are two "mainstream"
 * algorithms for throttling requests, Token Bucket and Leaky Bucket.
 * For restify, I went with Token Bucket.  For a good description of the
 * algorithm, see: http://en.wikipedia.org/wiki/Token_bucket
 *
 * In the options object, you pass in the total tokens and the fill rate.
 * Practically speaking, this means "allow `fill rate` requests/second,
 * with bursts up to `total tokens`".  Note that the bucket is initialized
 * to full.
 *
 * Also, in googling, I came across a concise python implementation, so this
 * is just a port of that. Thanks http://code.activestate.com/recipes/511490 !
 *
 * @param {Object} options contains the parameters:
 *                   - {Number} capacity the maximum burst.
 *                   - {Number} fillRate the rate to refill tokens.
 */
function TokenBucket(options) {
    assert.object(options, 'options');
    assert.number(options.capacity, 'options.capacity');
    assert.number(options.fillRate, 'options.fillRate');

    this.tokens = this.capacity = options.capacity;
    this.fillRate = options.fillRate;
    this.time = Date.now();
}
Example #11
0
/*
 * Generates a self-contained HTML page describing the job.
 */
function jobshare(args, cb)
{
    assert.string(args.jobdir, 'jobdir');
    assert.optionalString(args.readme, 'readme');
    assert.object(args.log, 'log');
    assert.object(args.client, 'client');
    assert.number(args.maxObjects, 'opts.maxObjects');
    assert.number(args.maxBytesPerObject, 'opts.maxBytesPerObject');
    assert.number(args.maxErrors, 'opts.maxErrors');

    var j = {
        /* input parameters */
        'j_jobdir': args.jobdir,                /* path to job directory */
        'j_readme': args.readme,                /* readme file */
        'j_log': args.log,                      /* bunyan logger */
        'j_manta': args.client,                 /* node-manta client */
        'j_maxobjects': args.maxObjects,        /* max nr of objs to fetch */
        'j_maxbytes': args.maxBytesPerObject,   /* max bytes per object */
        'j_maxerrors': args.maxErrors,          /* max errors to fetch */

        /* "jobex" internal state */
        'j_error': null,                        /* current error */
        'j_assets': [],                         /* names of available assets */
        'j_data': {},                           /* file and object contents */
        'j_job': null,                          /* job details record */
        'j_errors': [],                         /* raw JSON error objects */
        'j_inout': {
            'in': [],                           /* list of input obj names */
            'out': []                           /* list of output obj names */
        },
        'j_rendered': null,                     /* rendered HTML output */

        /* debug state */
        'j_pipeline': null,                     /* pipeline for this op */
        'j_barrier': null                       /* barrier for current stage */
    };

    j.j_pipeline = vasync.pipeline({
        'arg': j,
        'funcs': [
            jStageLoad,
            jStageFetchJob,
            jStageFetchJobDetails,
            jStageRender
        ]
    }, function (err) {
        if (err)
            cb(err);
        else
            cb(null, { 'html': j.j_rendered });
    });
}
Example #12
0
/**
 * ZFS client. Interactions with the underlying ZFS dataset -- which is used by
 * PostgreSQL -- are managed through this client.
 *
 * This class is responsible for restoring the dataset from a remote instance
 * of manatee.
 *
 * @constructor
 *
 * @param {object} options Options object.
 * @param {Bunyan} options.log Bunyan logger.
 * @param {string} options.dataset ZFS datset.
 * @param {string} options.dbUser PostgreSQL DB user.
 * @param {string} options.mountpoint Mountpoint for the ZFS dataset.
 * @param {string} options.zfsHost IP address used for ZFS recv.
 * @param {number} options.zfsPort Port used for ZFS recv.
 * @param {number} options.pollInterval How often to poll the backup server in
 * ms.
 * @param {string} options.zfsPath Path of the ZFS binary
 *
 * @throws {Error} If the options object is malformed.
 */
function ZfsClient(options) {
    assert.object(options, 'options');
    assert.object(options.log, 'options.log');

    assert.string(options.dataset, 'options.dataset');
    assert.string(options.dbUser, 'options.dbUser');
    assert.string(options.mountpoint, 'options.mountpoint');
    assert.number(options.pollInterval, 'options.pollInterval');
    assert.string(options.zfsHost, 'options.zfsHost');
    assert.string(options.zfsPath, 'options.zfsPath');
    assert.number(options.zfsPort, 'options.zfsPort');

    var self = this;

    /** @type {Bunyan} The bunyan log object */
    this._log = options.log.child({component: 'ZfsClient'}, true);
    self._log.info('initializing ZfsClient with options', options);

    /** @type {string} My ZFS dataset */
    this._dataset = options.dataset;
    /** @type {string} My parent ZFS dataset */
    this._parentDataset = path.dirname(self._dataset);

    /** @type {string} ZFS mountpoint of the datset */
    this._mountpoint = options.mountpoint;

    /** @type {string} PostgreSQL user */
    this._dbUser = options.dbUser;

    /** @type {number} The backup server poll interval */
    this._pollInterval = options.pollInterval;
    /** @type {Object} The current/last restore object */
    this._restoreObject = null;

    /** @type {string} IP addr used for zfs recv */
    this._zfsHost = options.zfsHost;
    /** @type {number} Port used for zfs recv */
    this._zfsPort = options.zfsPort;
    /** @type {string} Path to the zfs binary */
    this._zfsPath = options.zfsPath;

    self._log.info({
        dataset: self._dataset,
        parentDataset: self._parentDataset,
        mountpoint: self._mountpoint,
        zfsHost: self._zfsHost,
        zfsPort: self._zfsPort,
        zfsPath: self._zfsPath,
        pollInterval: self._pollInterval
    }, 'initalized ZfsClient');
}
Example #13
0
/*
 * Helper Functions.
 */
function
mdr_validate_config(config)
{
	/*
	 * HTTP Server Listen Port:
	 */
	mod_assert.optionalString(config.bind_ip, 'bind_ip');
	mod_assert.number(config.port, 'port');
	mod_assert.ok(config.port > 0 && config.port < 65536,
	    'port not in range 1-65535');
	mod_assert.object(config.moray, 'moray');
	mod_assert.string(config.moray.host, 'moray.host');
	mod_assert.number(config.moray.port, 'moray.port');
}
Example #14
0
function TimeBucket(opts) {
        assert.object(opts, 'opts');
        assert.object(opts.log, 'opts.log');
        assert.number(opts.periodSeconds, 'opts.periodSeconds');
        assert.number(opts.numberPeriods, 'opts.numberPeriods');

        var self = this;
        self.log = opts.log;
        self.period = opts.periodSeconds * 1000;
        self.numberPeriods = opts.numberPeriods;
        self.duration = self.period * self.numberPeriods;
        self.count = 0;
        self.vals = {};
        self.buckets = {};
}
Example #15
0
/**
 * Validate the config
 */
function validateConfig(config) {
    assert.string(config.datacenter, 'datacenter (Datacenter name)');
    assert.string(config.macOUI,
            'macOUI (MAC address OUI for provisioning nics');
    assert.optionalNumber(config.maxHttpSockets,
            'maxHttpSockets (maximum open connections)');
    assert.number(config.port, 'port (port number)');
    assert.string(config.ufdsAdminUuid, 'ufdsAdminUuid (admin user uuid)');

    assert.object(config.moray, 'moray (moray config section)');
    assert.string(config.moray.host, 'moray.host (moray IP)');
    assert.number(config.moray.port, 'moray.port (moray port number)');

    assert.object(config.overlay, 'overlay (overlay config section)');
}
Example #16
0
/*
 * To boot a CN, booter needs information from other services, such as which
 * platform image to use.  However, those services could be located on the down
 * CNs that are trying to boot, resulting in a circular dependency in need of
 * manual intervention.  To guard against this situation, the bootparams module
 * writes out local cache of the last used values for each CN.  When there is an
 * error connecting to the required services, those local files are used as a
 * fallback.  CacheSentinel performs two periodic tasks for the maintenance of
 * those cache files:
 *
 * Refresh: On a configurable time interval, gather the list of *all* current
 * CNs (not just recently booted ones) and pre-populate the cache with their
 * current configuration.  This narrows the vulnerability window of bad or
 * missing data to the refresh period.  CNs that boot during the window, or have
 * their configuration changed through a sdcadm workflow will still have their
 * cached values updated normally.
 *
 * Purge: On a configurable time interval, delete cache files that have not been
 * updated in a long (configurable) time.  This is to remove cache files for
 * decommissioned CNs so the cache size does not grow indefinitely with obsolete
 * data.  Purging is only done if the last refresh completed without error.
 */
function CacheSentinel(opts) {
    assert.object(opts, 'opts');
    assert.object(opts.log, 'opts.log');
    assert.object(opts.cnapi, 'opts.cnapi');
    assert.object(opts.napi, 'opts.napi');
    assert.object(opts.adminPoolCache, 'opts.adminPoolCache');
    assert.object(opts.config, 'opts.config');
    assert.uuid(opts.config.adminUuid, 'opts.config.adminUuid');

    assert.object(opts.config.cache, 'opts.config.cache');
    assert.string(opts.config.cache.dir, 'opts.config.cache.dir');
    assert.number(opts.config.cache.refreshIntervalSeconds,
                  'opts.config.cache.refreshIntervalSeconds');
    assert.ok(isSafeTimerValueSeconds(opts.config.cache.refreshIntervalSeconds),
              'Invalid timer value refreshIntervalSeconds');
    assert.number(opts.config.cache.purgeIntervalSeconds,
                  'opts.config.cache.purgeIntervalSeconds');
    assert.ok(isSafeTimerValueSeconds(opts.config.cache.purgeIntervalSeconds),
              'Invalid timer value purgeIntervalSeconds');
    assert.number(opts.config.cache.maxCacheFileAgeSeconds,
                  'opts.config.cache.maxCacheFileAgeSeconds');
    assert.ok(opts.config.cache.maxCacheFileAgeSeconds > 0,
              'maxCacheFileAgeSeconds must be positive');
    assert.number(opts.config.cache.refreshConcurrency,
                  'opts.config.cache.refreshConcurrency');
    assert.ok(opts.config.cache.refreshConcurrency > 0,
              'refreshConcurrency must be positive');

    this.log = opts.log;
    this.cnapi = opts.cnapi;
    this.napi = opts.napi;
    this.adminUuid = opts.config.adminUuid;
    this.adminPoolCache = opts.adminPoolCache;

    this.config = opts.config;
    this.cacheDir = this.config.cache.dir;
    this.refreshInterval = this.config.cache.refreshIntervalSeconds * 1000;
    this.purgeInterval = this.config.cache.purgeIntervalSeconds * 1000;
    this.maxCacheFileAge = this.config.cache.maxCacheFileAgeSeconds * 1000;
    this.refreshConcurrency = this.config.cache.refreshConcurrency;

    this.refreshStartTime = undefined;
    this.purgeStartTime = undefined;
    // TODO(cburroughs): This should be exposed as a metric for operators to
    // monitor
    this.lastRefreshSuccessTime = undefined;
    this.lastRefreshSuccessful = undefined;
}
Example #17
0
exports.udp = function(port, logger) {
	assert.number(port, "port");
	assert.object(logger, "logger");
	var emitter = new events.EventEmitter();
	var s = dgram.createSocket("udp4");
	s.on("message", function(msg) {
		libsyslog.decodeMessage(msg.toString("utf8"), function(err, syslog) {
			if (err) {
				logger.error("syslogpipe", "Could not parse udp syslog msg", syslog);
			} else {
				var level = mapSeverityToLevel(syslog.severityCode, syslog.severity);
				var origin = mapFacilityToOrigin(syslog.facilityCode, syslog.facility);
				logger[level](origin, syslog.msg, syslog);
			}
		});
	});
	logger.on("endpoint_error", function(endpoint, err) {
		emitter.emit("error", err);
	});
	s.on("error", function(err) {
		emitter.emit("error", err);
	});
	s.on("close", function() {
		emitter.emit("closed");
	});
	s.bind(port, function() {
		emitter.emit("bound");
	});
	emitter.close = function() {
		emitter.emit("close");
		s.close();
	};
	return emitter;
};
Example #18
0
VMAPI.prototype.validateConfig = function (options) {
    assert.object(options, 'VMAPI configuration');
    assert.object(options.api, 'VMAPI config.api');

    // WFAPI
    assert.object(options.wfapi, 'VMAPI config.wfapi');
    assert.string(options.wfapi.url, 'VMAPI config.wfapi.url');
    assert.arrayOfString(options.wfapi.workflows,
        'VMAPI config.wfapi.workflows');

    // CNAPI
    assert.object(options.cnapi, 'VMAPI config.cnapi');
    assert.string(options.cnapi.url, 'VMAPI config.cnapi.url');

    // IMGAPI
    assert.object(options.imgapi, 'VMAPI config.imgapi');
    assert.string(options.imgapi.url, 'VMAPI config.imgapi.url');

    // NAPI
    assert.object(options.napi, 'VMAPI config.napi');
    assert.string(options.napi.url, 'VMAPI config.napi.url');

    // PAPI
    assert.object(options.papi, 'VMAPI config.papi');
    assert.string(options.papi.url, 'VMAPI config.papi.url');

    // Moray
    assert.object(options.moray, 'Moray config.host');
    assert.string(options.moray.host, 'Moray config.moray.host');
    assert.number(options.moray.port, 'Moray config.moray.port');
};
Example #19
0
        func: function runSingleMigration(migration, next) {
            assert.number(migration.DATA_VERSION, 'migration.DATA_VERSION');
            assert.ok(migration.DATA_VERSION >= 1,
                'migration.DATA_VERSION >= 1');

            self._runSingleMigration(modelName, migration, {
                log: log
            }, function onMigration(migrationErr) {
                if (migrationErr) {
                    if (self._latestErrors === undefined) {
                        self._latestErrors = {};
                    }

                    self._latestErrors[modelName] = migrationErr;

                    log.error({err: migrationErr},
                        'Error when running migration to data version: ' +
                            migration.DATA_VERSION);
                } else {
                    self._latestCompletedMigrations[modelName] =
                        migration.DATA_VERSION;
                    if (self._latestErrors && self._latestErrors[modelName]) {
                        delete self._latestErrors[modelName];
                        if (Object.keys(self._latestErrors).length === 0) {
                            self._latestErrors = undefined;
                        }
                    }
                    log.info('Data migration to data version: ' +
                        migration.DATA_VERSION + ' ran successfully');
                }

                next(migrationErr);
            });
        },
Example #20
0
/**
 * Create a a token-based throttle instance.
 * @param {Object} options Options to create the throttle with.
 *              - {Number} rate The actions per time window to replenish. REQUIRED
 *              - {Number} burst The number allowed to burst to in a time window. Default = rate
 *              - {Number} window The time window in milliseconds to measure actions in. Default = 1000
 *              - {TokensTable} tokensTable A replacement token table to use. Must provide get/set.
 *              - {Object} overrides A set of overrides for certain tokens.
 *                                   Can specify alternate rate, burst, or window.
 *                                   Does not inherit, so defaults apply.
 */
function Throttle(options) {
  if (!(this instanceof Throttle)) return new Throttle(options)
  assert.object(options, "options")
  assert.number(options.rate, "options.rate")

  this.burst = options.burst || options.rate
  this.rate = options.rate
  this.window = options.window || 1000
  var table = options.tokensTable || TokenTable({size: options.maxKeys})
  this.table = table
  this.overrides = options.overrides

  this.getter = table.get
  this.putter = table.put
  if (table.put.length == 2 && table.get.length == 1) {
    // This looks like a synchronous table, wrap the methods to make them async.
    this.getter = function (key, cb) {
      process.nextTick(function () {
        cb(null, table.get(key))
      })
    }
    this.putter = function (key, bucket, cb) {
      process.nextTick(function () {
        cb(null, table.put(key, bucket))
      })
    }
  }
  else if (table.put.length != 3 && table.get.length != 2) {
    throw new Error("Unable to detect TokenTable implementation type (sync/async) check API.")
  }
}
Example #21
0
// --- Common create/updates/delete pipeline functions

/**
 * Provided with a vnet_id, appends the list of vnet cns to opts.vnetCns.
 */
function listVnetCns(opts, callback) {
    assert.object(opts, 'opts');
    assert.number(opts.vnet_id, 'opts.vnet_id');
    assert.object(opts.moray, 'opts.moray');
    assert.object(opts.log, 'opts.log');

    opts.log.debug({ vnet_id: opts.vnet_id }, 'listVnetCns: enter');

    mod_portolan_moray.vl2LookupCns(opts, function (listErr, cns) {
        if (listErr) {
            opts.log.error({ err: listErr, vnet_id: opts.vnet_id },
                'listVnetCns: error fetching cn list on vnet');
            return callback(listErr);
        }

        var vnetCns = Object.keys(cns.reduce(function (acc, cn) {
            acc[cn.cn_uuid] = true;
            return acc;
        }, {}));

        opts.log.debug({ vnetCns: vnetCns }, 'listVnetCns: exit');

        return callback(null, vnetCns);
    });
}
Example #22
0
URClient.prototype.exec = function
exec(options, callback)
{
	var self = this;

	self._assert_ready();

	mod_assert.object(options, 'options');
	mod_assert.func(callback, 'callback');
	mod_assert.string(options.script, 'options.script');
	mod_assert.string(options.server_uuid, 'options.server_uuid');
	mod_assert.number(options.timeout, 'options.timeout');
	mod_assert.optionalObject(options.env, 'options.env');
	mod_assert.optionalArrayOfString(options.args, 'options.args');

	var infl = self.ur_urconn.send_command(options.server_uuid,
	    options.script, options.args || [], options.env, options);
	infl.start_timeout(options.timeout);
	infl.on('command_reply', function (reply) {
		infl.cancel_timeout();
		infl.complete();
		callback(null, reply);
	});
	infl.on('timeout', function () {
		infl.complete();
		callback(new VError('timeout for host %s',
		    options.server_uuid));
	});

	return (infl);
};
Example #23
0
JuteBuffer.prototype.writeInt = function (v, assertName) {
	mod_assert.number(v, assertName || 'value');
	while (this.jb_offset + 4 > this.jb_size)
		this.expand();
	this.jb_buffer.writeInt32BE(v, this.jb_offset);
	this.jb_offset += 4;
};
Example #24
0
ZookeeperMgr.prototype.putClusterState = function putClusterState(state, cb) {
    assert.object(state, 'state');
    assert.number(state.generation, 'state.generation');

    var self = this;
    var data = new Buffer(JSON.stringify(state));

    function onPut(err, stat) {
        if (err) {
            return (cb(err));
        }
        self._clusterState = state;
        self._clusterStateVersion = stat.version;
        return (cb());
    }

    var hp = self._historyPath + '/' + state.generation + '-';
    var t = self._zk.transaction().
        create(hp, data, zkClient.CreateMode.PERSISTENT_SEQUENTIAL);
    if (self._clusterState) {
        t.setData(self._clusterStatePath, data, self._clusterStateVersion);
    } else {
        t.create(self._clusterStatePath, data, zkClient.CreateMode.PERSISTENT);
    }
    t.commit(onPut);
};
Example #25
0
/*
 * With Docker 1.10 (API version 1.22) the filters changed from an array to an
 * object with the value true.
 *
 * In 1.9.1:
 *
 *     filters={"label":["com.joyent.package=sample-2G"]}
 *
 * In 1.10.0:
 *
 *     filters={"label":{"com.joyent.package=sample-2G":true}}
 *
 * This function takes the filters[label] value (as the filters parameter here)
 * and returns a normalized array of the packages we want to filter on.
 *
 */
function getLabelFilters(opts, filters) {
    assert.object(opts, 'opts');
    assert.number(opts.apiVersion, 'opts.apiVersion');
    assert.object(opts.log, 'opts.log');
    assert.object(filters, 'filters');

    var log = opts.log;
    var val = filters;

    if ((common.apiVersionCmp(opts.apiVersion, 1.22) >= 0)
        && !Array.isArray(val)) {
        // the version is >= 1.22 and we got a non array object, convert to
        // array based on keys.
        val = Object.keys(val);
    }

    // val should have either been an array, or we should have turned it into
    // one. Any other value is illegal.
    if (!Array.isArray(val)) {
        log.warn({
            filters: filters,
            type: typeof (val),
            val: JSON.stringify(val)
        }, 'WARNING: filter value is not an array');

        return new Error('invalid filter');
    }

    return (val);
}
Example #26
0
RpcServer.prototype._rpc = function _rpc(opts) {
    assert.object(opts, 'options');
    assert.string(opts.name, 'options.name');
    assert.number(opts.procedure, 'options.procedure');
    assert.optionalFunc(opts.call, 'options.call');
    assert.func(opts.reply, 'options.reply');

    var args = this.saved_handlers.concat(slice(arguments, 1));
    assert.arrayOfFunc(args, 'handler chain');

    var self = this;

    this.rpc_table[opts.procedure] = {
        name: opts.name,
        procedure: opts.procedure,
        call: opts.call,
        reply: opts.reply,
        chain: args
    };
    this.on(opts.name, function onRpcCall(call, reply) {
        var log = self.log.child({
            procedure: opts.name,
            xid: call.xid
        }, true);

        call.log = log;
        reply.log = log;

        call._rpc_proc_name = opts.name;

        self._run(call, reply, self.rpc_table[opts.procedure].chain);
    });
};
Example #27
0
/**
 * Reindexes all objects in the bucket if it has been updated
 *
 * @param moray {MorayClient}
 * @param bucket {Bucket schema object}
 * @param callback {Function} `function (err)`
 */
function reindexBucket(moray, bucket, callback) {
    assert.object(moray, 'moray');
    assert.object(bucket, 'bucket');
    assert.string(bucket.desc, 'bucket.desc');
    assert.string(bucket.name, 'bucket.name');
    assert.object(bucket.schema, 'bucket.schema');
    assert.number(bucket.version, 'bucket.version');

    var rowsPerCall = 100;
    var processed = rowsPerCall;

    async.whilst(
        function () { return processed > 0; },
        function (cb) {
            moray.reindexObjects(bucket.name, rowsPerCall, { noCache: true },
            function (err, res) {
                if (err) {
                    return cb(err);
                }

                processed = res.processed;
                cb();
            });
        },
        callback
    );
}
Example #28
0
function Writer(host, port, useIPv6) {
    assert.string(host, "host");
    assert.number(port, "port");
    assert.optionalBool(useIPv6, "useIPv6");

    // == private functions and declarations
    
    var self = this;
    var sock;
    
    // == overridden methods
    
    self._write = function(chunk, encoding, cb) {
        var buf = new Buffer(chunk.join(" "), "ascii");

        sock.send(buf, 0, buf.length, port, host, function(err /* , bytes */) {
            cb(err);
        });
    };
    
    // == and finally, initialization
    
    stream.Writable.call(self, {
        objectMode: true
    });
    
    sock = dgram.createSocket(useIPv6 ? "udp6" : "udp4");
}
function opensslKeyDeriv(cipher, salt, passphrase, count) {
	assert.buffer(salt, 'salt');
	assert.buffer(passphrase, 'passphrase');
	assert.number(count, 'iteration count');

	var clen = CIPHER_LEN[cipher];
	assert.object(clen, 'supported cipher');

	salt = salt.slice(0, PKCS5_SALT_LEN);

	var D, D_prev, bufs;
	var material = Buffer.alloc(0);
	while (material.length < clen.key + clen.iv) {
		bufs = [];
		if (D_prev)
			bufs.push(D_prev);
		bufs.push(passphrase);
		bufs.push(salt);
		D = Buffer.concat(bufs);
		for (var j = 0; j < count; ++j)
			D = crypto.createHash('md5').update(D).digest();
		material = Buffer.concat([material, D]);
		D_prev = D;
	}

	return ({
	    key: material.slice(0, clen.key),
	    iv: material.slice(clen.key, clen.key + clen.iv)
	});
}
Example #30
0
/**
 * Check that a network's NIC tag, VLAN ID, and owner match what's present on
 * the NIC. If the VNET ID needs checking, check that, too.
 */
function checkNetwork(parsedParams, name, network) {
    assert.object(parsedParams, 'parsedParams');
    assert.string(name, 'name');
    assert.object(network, 'network');

    if (badOwnerUUID(parsedParams, network)) {
        return errors.invalidParam('owner_uuid', constants.OWNER_MATCH_MSG);
    }

    if (parsedParams.nic_tag === undefined) {
        assert.string(network.nic_tag, 'networks should have a nic_tag');
        parsedParams.nic_tag = network.nic_tag;
    } else if (parsedParams.nic_tag !== network.nic_tag) {
        return errors.invalidParam(name,
            util.format(constants.fmt.NIC_TAGS_DIFFER,
            parsedParams.nic_tag, network.nic_tag));
    }

    if (parsedParams.vlan_id === undefined) {
        assert.number(network.params.vlan_id, 'networks should have a vlan_id');
        parsedParams.vlan_id = network.params.vlan_id;
    } else if (parsedParams.vlan_id !== network.params.vlan_id) {
        return errors.invalidParam(name,
            util.format(constants.fmt.VLAN_IDS_DIFFER,
            parsedParams.vlan_id, network.params.vlan_id));
    }

    return null;
}