From f94ba08c0690d04199b35667ce95243c0a4b3008 Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 28 Jun 2017 17:44:14 -0500 Subject: [PATCH] feat(portal-api): populate ip addresses for instances --- packages/portal-api/lib/data/index.js | 73 +++++++---------------- packages/portal-api/lib/data/transform.js | 6 +- packages/portal-api/lib/index.js | 11 +++- packages/portal-api/lib/watch/index.js | 3 +- 4 files changed, 35 insertions(+), 58 deletions(-) diff --git a/packages/portal-api/lib/data/index.js b/packages/portal-api/lib/data/index.js index 16ad7963..ace3ba61 100644 --- a/packages/portal-api/lib/data/index.js +++ b/packages/portal-api/lib/data/index.js @@ -10,7 +10,6 @@ const Util = require('util'); const DockerClient = require('docker-compose-client'); const Dockerode = require('dockerode'); const Hoek = require('hoek'); -const Triton = require('triton'); const ParamCase = require('param-case'); const Penseur = require('penseur'); const { DEPLOYMENT_GROUP, SERVICE, HASH } = require('../watch'); @@ -71,18 +70,10 @@ module.exports = class Data extends EventEmitter { this._dockerCompose = new DockerClient(settings.dockerComposeHost); this._docker = new Dockerode(settings.docker); this._watcher = null; - this._triton = null; - Triton.createClient({ - profile: settings.triton - }, (err, client) => { - if (err) { - this.emit('error', err); - return; - } - - this._triton = client.cloudapi; - }); + // if (settings.consul && settings.consul.address) { + // CPClient.config(settings.consul); + // } this._dockerCompose.on('error', (err) => { this.emit('error', err); @@ -1011,11 +1002,8 @@ module.exports = class Data extends EventEmitter { return next(err); } - if (!this._triton) { - return next(); - } - - this._triton.stopMachine(instance.machine_id, next); + const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); + container.stop(next); }); }, inputs: instanceIds @@ -1050,11 +1038,8 @@ module.exports = class Data extends EventEmitter { return next(err); } - if (!this._triton) { - return next(); - } - - this._triton.startMachine(instance.machine_id, next); + const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); + container.start(next); }); }, inputs: instanceIds @@ -1089,11 +1074,8 @@ module.exports = class Data extends EventEmitter { return next(err); } - if (!this._triton) { - return next(); - } - - this._triton.rebootMachine(instance.machine_id, next); + const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); + container.restart(next); }); }, inputs: instanceIds @@ -1145,11 +1127,9 @@ module.exports = class Data extends EventEmitter { return next(err); } - if (!this._triton) { - return next(); - } - - this._triton.deleteMachine(instance.machine_id, next); + const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); + // Use force in case the container is running. TODO: should we keep force? + container.remove({ force: true }, next); }); }, inputs: instanceIds @@ -1240,11 +1220,8 @@ module.exports = class Data extends EventEmitter { VAsync.forEachParallel({ func: (instance, next) => { - if (!this._triton) { - return next(); - } - - this._triton.stopMachine(instance.machine_id, next); + const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); + container.stop(next); }, inputs: instances }, (err, results) => { @@ -1269,26 +1246,19 @@ module.exports = class Data extends EventEmitter { VAsync.forEachParallel({ func: (instance, next) => { - if (!this._triton) { - return next(); - } - - this._triton.startMachine(instance.machine_id, (err) => { + const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); + container.start((err) => { if (err) { return next(err); } - const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); - // 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); + this._db.instances.update(instance.id, { ips: [details.NetworkSettings.IPAddress] }, next); }); }); }, @@ -1315,11 +1285,10 @@ module.exports = class Data extends EventEmitter { VAsync.forEachParallel({ func: (instance, next) => { - if (!this._triton) { - return next(); - } - - this._triton.rebootMachine(instance.machine_id, next); + this.updateInstance({ id: instance.id, status: 'RESTARTING' }, () => { + const container = this._docker.getContainer(instance.machine_id.split(/-/)[0]); + container.restart(next); + }); }, inputs: instances }, (err, results) => { diff --git a/packages/portal-api/lib/data/transform.js b/packages/portal-api/lib/data/transform.js index 4a747014..418f67bb 100644 --- a/packages/portal-api/lib/data/transform.js +++ b/packages/portal-api/lib/data/transform.js @@ -170,7 +170,8 @@ exports.fromInstance = function (instance) { id: instance.id, name: instance.name, machineId: instance.machine_id, - status: instance.status || '' + status: instance.status || '', + ips: instance.ips || [] }; }; @@ -179,7 +180,8 @@ exports.toInstance = function (clientInstance) { id: clientInstance.id, name: clientInstance.name, machine_id: clientInstance.machineId, - status: clientInstance.status || '' + status: clientInstance.status || '', + ips: clientInstance.ips || [] }; }; diff --git a/packages/portal-api/lib/index.js b/packages/portal-api/lib/index.js index 421b1158..7d3c8b59 100644 --- a/packages/portal-api/lib/index.js +++ b/packages/portal-api/lib/index.js @@ -13,9 +13,14 @@ const internals = {}; module.exports = function (server, options, next) { - const docker = Piloted.service('docker-compose-api'); - if (docker) { - options.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`; + try { + const docker = Piloted.service('docker-compose-api'); + if (docker) { + options.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`; + } + } + catch (ex) { + console.error(ex); } const data = new Data(options.data); diff --git a/packages/portal-api/lib/watch/index.js b/packages/portal-api/lib/watch/index.js index a448eb9f..faaa43f0 100644 --- a/packages/portal-api/lib/watch/index.js +++ b/packages/portal-api/lib/watch/index.js @@ -131,7 +131,8 @@ module.exports = class Watcher { const instance = { name: machine.name, status, - machineId: machine.id + machineId: machine.id, + ips: machine.ips }; console.log('-> creating instance', util.inspect(instance));