feat(portal-api): populate ip addresses for instances

This commit is contained in:
geek 2017-06-28 17:44:14 -05:00 committed by Sérgio Ramos
parent da40ee11e8
commit f94ba08c06
4 changed files with 35 additions and 58 deletions

View File

@ -10,7 +10,6 @@ const Util = require('util');
const DockerClient = require('docker-compose-client'); const DockerClient = require('docker-compose-client');
const Dockerode = require('dockerode'); const Dockerode = require('dockerode');
const Hoek = require('hoek'); const Hoek = require('hoek');
const Triton = require('triton');
const ParamCase = require('param-case'); const ParamCase = require('param-case');
const Penseur = require('penseur'); const Penseur = require('penseur');
const { DEPLOYMENT_GROUP, SERVICE, HASH } = require('../watch'); const { DEPLOYMENT_GROUP, SERVICE, HASH } = require('../watch');
@ -71,18 +70,10 @@ module.exports = class Data extends EventEmitter {
this._dockerCompose = new DockerClient(settings.dockerComposeHost); this._dockerCompose = new DockerClient(settings.dockerComposeHost);
this._docker = new Dockerode(settings.docker); this._docker = new Dockerode(settings.docker);
this._watcher = null; this._watcher = null;
this._triton = null;
Triton.createClient({ // if (settings.consul && settings.consul.address) {
profile: settings.triton // CPClient.config(settings.consul);
}, (err, client) => { // }
if (err) {
this.emit('error', err);
return;
}
this._triton = client.cloudapi;
});
this._dockerCompose.on('error', (err) => { this._dockerCompose.on('error', (err) => {
this.emit('error', err); this.emit('error', err);
@ -1011,11 +1002,8 @@ module.exports = class Data extends EventEmitter {
return next(err); return next(err);
} }
if (!this._triton) { const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
return next(); container.stop(next);
}
this._triton.stopMachine(instance.machine_id, next);
}); });
}, },
inputs: instanceIds inputs: instanceIds
@ -1050,11 +1038,8 @@ module.exports = class Data extends EventEmitter {
return next(err); return next(err);
} }
if (!this._triton) { const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
return next(); container.start(next);
}
this._triton.startMachine(instance.machine_id, next);
}); });
}, },
inputs: instanceIds inputs: instanceIds
@ -1089,11 +1074,8 @@ module.exports = class Data extends EventEmitter {
return next(err); return next(err);
} }
if (!this._triton) { const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
return next(); container.restart(next);
}
this._triton.rebootMachine(instance.machine_id, next);
}); });
}, },
inputs: instanceIds inputs: instanceIds
@ -1145,11 +1127,9 @@ module.exports = class Data extends EventEmitter {
return next(err); return next(err);
} }
if (!this._triton) { const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
return next(); // Use force in case the container is running. TODO: should we keep force?
} container.remove({ force: true }, next);
this._triton.deleteMachine(instance.machine_id, next);
}); });
}, },
inputs: instanceIds inputs: instanceIds
@ -1240,11 +1220,8 @@ module.exports = class Data extends EventEmitter {
VAsync.forEachParallel({ VAsync.forEachParallel({
func: (instance, next) => { func: (instance, next) => {
if (!this._triton) { const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
return next(); container.stop(next);
}
this._triton.stopMachine(instance.machine_id, next);
}, },
inputs: instances inputs: instances
}, (err, results) => { }, (err, results) => {
@ -1269,26 +1246,19 @@ module.exports = class Data extends EventEmitter {
VAsync.forEachParallel({ VAsync.forEachParallel({
func: (instance, next) => { func: (instance, next) => {
if (!this._triton) { const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
return next(); container.start((err) => {
}
this._triton.startMachine(instance.machine_id, (err) => {
if (err) { if (err) {
return next(err); return next(err);
} }
const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
// Update the IPAddress for the instance // Update the IPAddress for the instance
container.inspect((err, details) => { container.inspect((err, details) => {
if (err) { if (err) {
return next(err); return next(err);
} }
this._db.instances.update(instance.id, { this._db.instances.update(instance.id, { ips: [details.NetworkSettings.IPAddress] }, next);
ip_address: details.NetworkSettings.IPAddress
}, next);
}); });
}); });
}, },
@ -1315,11 +1285,10 @@ module.exports = class Data extends EventEmitter {
VAsync.forEachParallel({ VAsync.forEachParallel({
func: (instance, next) => { func: (instance, next) => {
if (!this._triton) { this.updateInstance({ id: instance.id, status: 'RESTARTING' }, () => {
return next(); const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]);
} container.restart(next);
});
this._triton.rebootMachine(instance.machine_id, next);
}, },
inputs: instances inputs: instances
}, (err, results) => { }, (err, results) => {

View File

@ -170,7 +170,8 @@ exports.fromInstance = function (instance) {
id: instance.id, id: instance.id,
name: instance.name, name: instance.name,
machineId: instance.machine_id, machineId: instance.machine_id,
status: instance.status || '' status: instance.status || '',
ips: instance.ips || []
}; };
}; };
@ -179,7 +180,8 @@ exports.toInstance = function (clientInstance) {
id: clientInstance.id, id: clientInstance.id,
name: clientInstance.name, name: clientInstance.name,
machine_id: clientInstance.machineId, machine_id: clientInstance.machineId,
status: clientInstance.status || '' status: clientInstance.status || '',
ips: clientInstance.ips || []
}; };
}; };

View File

@ -13,10 +13,15 @@ const internals = {};
module.exports = function (server, options, next) { module.exports = function (server, options, next) {
try {
const docker = Piloted.service('docker-compose-api'); const docker = Piloted.service('docker-compose-api');
if (docker) { if (docker) {
options.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`; options.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`;
} }
}
catch (ex) {
console.error(ex);
}
const data = new Data(options.data); const data = new Data(options.data);
const watcher = new Watch(Object.assign(options.watch, { const watcher = new Watch(Object.assign(options.watch, {

View File

@ -131,7 +131,8 @@ module.exports = class Watcher {
const instance = { const instance = {
name: machine.name, name: machine.name,
status, status,
machineId: machine.id machineId: machine.id,
ips: machine.ips
}; };
console.log('-> creating instance', util.inspect(instance)); console.log('-> creating instance', util.inspect(instance));