1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-14 23:30:05 +02:00

feat(portal-data): insert ip address in instance record

This commit is contained in:
geek 2017-06-27 14:42:53 -05:00 committed by Sérgio Ramos
parent a14672355c
commit aa853fe4c8

View File

@ -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,10 +46,8 @@ 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 }
}
};
const resolveCb = (resolve, reject) => {
},
resolveCb: (resolve, reject) => {
return (err, ...args) => {
if (err) {
return reject(err);
@ -50,8 +55,10 @@ const resolveCb = (resolve, reject) => {
resolve(...args);
};
}
};
module.exports = class Data extends EventEmitter {
constructor (options) {
super();
@ -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]; }); }