feat(portal-api): populate ip addresses for instances
This commit is contained in:
parent
da40ee11e8
commit
f94ba08c06
@ -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) => {
|
||||
|
@ -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 || []
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user