1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-28 22:20:06 +02:00
copilot/packages/portal-watch/lib/index.js

203 lines
5.1 KiB
JavaScript
Raw Normal View History

'use strict';
// const Assert = require('assert');
const TritonWatch = require('triton-watch');
const util = require('util');
const DEPLOYMENT_GROUP = 'docker:label:com.docker.compose.project';
const SERVICE = 'docker:label:com.docker.compose.service';
const HASH = 'docker:label:com.docker.compose.config-hash';
module.exports = class Watcher {
constructor (options) {
options = options || {};
// todo assert options
this._data = options.data;
this._tritonWatch = new TritonWatch({
frequency: 500,
triton: {
profile: {
url: options.url || process.env.SDC_URL,
account: options.account || process.env.SDC_ACCOUNT,
keyId: options.keyId || process.env.SDC_KEY_ID
}
}
});
this._tritonWatch.on('change', (container) => { return this.onChange(container); });
}
poll () {
this._tritonWatch.poll();
}
getDeploymentGroupId (name, cb) {
this._data.getDeploymentGroup({ name }, (err, deploymentGroup) => {
if (err) {
return cb(err);
}
return cb(null, deploymentGroup && deploymentGroup.id);
});
}
getService ({ serviceName, deploymentGroupId }, cb) {
this._data.getServices({ name: serviceName, deploymentGroupId }, (err, services) => {
if (err) {
return cb(err);
}
if (!services || !services.length) {
return cb();
}
return cb(null, services.pop());
});
}
getInstances (service, cb) {
service.instances()
.then((instances) => { return cb(null, instances); })
.catch((err) => { return cb(err); });
}
resolveChanges ({ machine, service, instances }) {
// 1. if instance doesn't exist, create new
// 2. if instance exist, update status
const handleError = (cb) => {
return (err, data) => {
if (err) {
console.error(err);
return;
}
if (cb) {
cb(err, data);
}
};
};
const isNew = instances
.every(({ machineId }) => { return machine.id !== machineId; });
const instance = instances
.filter(({ machineId }) => { return machine.id === machineId; })
.pop();
const create = () => {
const instance = {
name: machine.name,
status: (machine.state || '').toUpperCase(),
machineId: machine.id
};
console.log('-> creating instance', util.inspect(instance));
return this._data.createInstance(instance, handleError((_, instance) => {
const updatedService = {
id: service.id,
instances: instances.concat(instance)
};
console.log('-> updating service', util.inspect(updatedService));
return this._data.updateService(updatedService, handleError);
}));
};
const update = () => {
const updatedInstance = {
id: instance.id,
status: (machine.state || '').toUpperCase()
};
console.log('-> updating instance', util.inspect(updatedInstance));
return this._data.updateInstance(updatedInstance, handleError);
};
return isNew ?
create() :
update();
}
onChange (machine) {
if (!machine) {
console.error('-> `change` event received without machine data');
return;
}
console.log('-> `change` event received', util.inspect(machine));
const { id, tags = [] } = machine;
// assert id existence
if (!id) {
console.error('-> `change` event received for a machine without `id`');
return;
}
// assert that it's a docker-compose project
const isCompose = [DEPLOYMENT_GROUP, SERVICE, HASH].every(
(name) => { return tags[name]; }
);
if (!isCompose) {
console.error(`-> Changed machine ${id} was not provisioned by docker-compose`);
return;
}
const deploymentGroupName = tags[DEPLOYMENT_GROUP];
const serviceName = tags[SERVICE];
const handleError = (next) => {
return (err, item) => {
if (err) {
console.error(err);
return;
}
next(item);
};
};
const getInstances = (service) => {
this.getInstances(service, handleError((instances) => {
return this.resolveChanges({
machine,
service,
instances
});
}));
};
// assert that service exists
const assertService = (deploymentGroupId) => {
this.getService({ serviceName, deploymentGroupId }, handleError((service) => {
if (!service) {
console.error(`Service "${serviceName}" form DeploymentGroup "${deploymentGroupName}" for machine ${id} not found`);
return;
}
getInstances(service);
}));
};
// assert that project managed by this portal
const assertDeploymentGroup = () => {
this.getDeploymentGroupId(deploymentGroupName, handleError((deploymentGroupId) => {
if (!deploymentGroupId) {
console.error(`DeploymentGroup "${deploymentGroupName}" for machine ${id} not found`);
return;
}
assertService(deploymentGroupId);
}));
};
assertDeploymentGroup();
}
};