From aa853fe4c857b6ad554d88b1e79c0b42265c8667 Mon Sep 17 00:00:00 2001 From: geek Date: Tue, 27 Jun 2017 14:42:53 -0500 Subject: [PATCH] feat(portal-data): insert ip address in instance record --- packages/portal-data/lib/index.js | 74 ++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/packages/portal-data/lib/index.js b/packages/portal-data/lib/index.js index 1609047a..663e6282 100644 --- a/packages/portal-data/lib/index.js +++ b/packages/portal-data/lib/index.js @@ -1,17 +1,24 @@ + 'use strict'; -const ParamCase = require('param-case'); +// core modules const EventEmitter = require('events'); +const Util = require('util'); + +// 3rd party modules +const CPClient = require('cp-client'); const DockerClient = require('docker-compose-client'); -const { DEPLOYMENT_GROUP, SERVICE, HASH } = require('portal-watch'); const Dockerode = require('dockerode'); const Hoek = require('hoek'); +const ParamCase = require('param-case'); const Penseur = require('penseur'); -const VAsync = require('vasync'); -const uniqBy = require('lodash.uniqby'); -const Transform = require('./transform'); +const { DEPLOYMENT_GROUP, SERVICE, HASH } = require('portal-watch'); +const UniqBy = require('lodash.uniqby'); const Uuid = require('uuid/v4'); -const Util = require('util'); +const VAsync = require('vasync'); + +// local modules +const Transform = require('./transform'); const NON_IMPORTABLE_STATES = [ @@ -39,18 +46,18 @@ const internals = { 'packages': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false }, 'instances': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false }, 'users': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false } + }, + resolveCb: (resolve, reject) => { + return (err, ...args) => { + if (err) { + return reject(err); + } + + resolve(...args); + }; } }; -const resolveCb = (resolve, reject) => { - return (err, ...args) => { - if (err) { - return reject(err); - } - - resolve(...args); - }; -}; module.exports = class Data extends EventEmitter { constructor (options) { @@ -64,6 +71,10 @@ module.exports = class Data extends EventEmitter { this._docker = new Dockerode(settings.docker); this._watcher = null; + if (settings.consul && settings.consul.address) { + CPClient.config(settings.consul); + } + this._dockerCompose.on('error', (err) => { this.emit('error', err); }); @@ -116,21 +127,21 @@ module.exports = class Data extends EventEmitter { // Sub query/filter for deploymentGroups const deploymentGroups = (args) => { return new Promise((resolve, reject) => { - this.getDeploymentGroups(args, resolveCb(resolve, reject)); + this.getDeploymentGroups(args, internals.resolveCb(resolve, reject)); }); }; // Sub query/filter for user const user = () => { return new Promise((resolve, reject) => { - this.getUser({}, resolveCb(resolve, reject)); + this.getUser({}, internals.resolveCb(resolve, reject)); }); }; // Sub query/filter for datacenter const datacenter = () => { return new Promise((resolve, reject) => { - this.getDatacenter({ id: portal.datacenter_id }, resolveCb(resolve, reject)); + this.getDatacenter({ id: portal.datacenter_id }, internals.resolveCb(resolve, reject)); }); }; @@ -255,7 +266,7 @@ module.exports = class Data extends EventEmitter { args.deploymentGroupId = deploymentGroupId; return new Promise((resolve, reject) => { - this.getServices(args, resolveCb(resolve, reject)); + this.getServices(args, internals.resolveCb(resolve, reject)); }); }; }; @@ -304,7 +315,7 @@ module.exports = class Data extends EventEmitter { args.deploymentGroupId = deploymentGroup.id; return new Promise((resolve, reject) => { - this.getServices(args, resolveCb(resolve, reject)); + this.getServices(args, internals.resolveCb(resolve, reject)); }); }; @@ -811,7 +822,7 @@ module.exports = class Data extends EventEmitter { remove(); }; - // deactivate pruned servcies + // deactivate pruned services const pruneServices = (err, result) => { if (err) { return cb(err); @@ -952,7 +963,7 @@ module.exports = class Data extends EventEmitter { return new Promise((resolve, reject) => { query.ids = instanceIds; - this.getInstances(query, resolveCb(resolve, reject)); + this.getInstances(query, internals.resolveCb(resolve, reject)); }); }; } @@ -1223,10 +1234,23 @@ module.exports = class Data extends EventEmitter { VAsync.forEachParallel({ func: (instance, next) => { const container = this._docker.getContainer(instance.machine_id); - container.start(next); + container.start((err) => { + if (err) { + return next(err); + } + + // Update the IPAddress for the instance + container.inspect((err, details) => { + if (err) { + return next(err); + } + + this._db.instances.update(instance.id, { ip_address: details.NetworkSettings.IPAddress }, next); + }); + }); }, inputs: instances - }, (err, results) => { + }, (err) => { if (err) { return cb(err); } @@ -1358,7 +1382,7 @@ module.exports = class Data extends EventEmitter { return cb( null, - uniqBy( + UniqBy( machines .filter(({ state }) => { return NON_IMPORTABLE_STATES.indexOf(state.toUpperCase()) < 0; }) .filter(({ tags = {} }) => { return [DEPLOYMENT_GROUP, SERVICE, HASH].every((name) => { return tags[name]; }); }