1
0
mirror of https://github.com/yldio/copilot.git synced 2024-12-12 04:50:06 +02:00
copilot/legacy/portal-data/lib/index.js

170 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-05-11 23:18:51 +03:00
'use strict';
const Hoek = require('hoek');
const Penseur = require('penseur');
const DCClient = require('docker-compose-client');
const awaitify = require('apr-awaitify');
2017-05-11 23:18:51 +03:00
const internals = {
defaults: {
name: 'portal'
}
};
module.exports = class Data {
constructor(options) {
2017-05-11 23:18:51 +03:00
const settings = Hoek.applyToDefaults(options || {}, internals.defaults);
// Penseur will assert that the options are correct
this._db = new Penseur.Db(settings.name, settings);
this._docker = new DCClient(settings.dockerHost);
this._db.establish = awaitify(this._db.establish);
this._db.deployments = awaitify(this._db.deployments);
// promisify Penseur
[
'activities',
'datacenters',
'deployments',
'manifests',
'metrics'
].forEach(tableName =>
['insert', 'get', 'update', 'remove', 'all'].forEach(methodName => {
this._db[tableName][methodName] = awaitify(
this._db[tableName][methodName]
);
})
);
}
connect(cb) {
return this._db.establish();
}
/*
* DeploymentGroupUuid
* Manifest
* id: UUID,
* created: Date.now(),
* type: 'docker-compose',
* format: 'yml',
* raw: 'original yml file content',
* obj: { }
*/
createDeployment({ deploymentGroupUuid, manifest, deployment }) {
// trigger deployment
// create deployment queue (we should think about what is a deployment queue)
// create the ConvergencePlans
// create a DeploymentPlan
// create a Version
// update the DeploymentGroup
// TODO
const updateDb = plan => {
// deployment.services = [];
// deployment.state = { current: 'stopped' };
this._db.deployments
.insert({
name:
})
.then(key => {
deployment.id = key;
return deployment;
});
};
2017-05-11 23:18:51 +03:00
const provision = ({ name }) =>
this._docker
.provision({
projectName: name,
manifest: manifest.raw
})
.then(updateDb);
2017-05-11 23:18:51 +03:00
this.getDeployment(deploymentGroupUuid).then(provision);
2017-05-11 23:18:51 +03:00
}
getDeployment(id) {
return this._db.deployments.get(id);
2017-05-11 23:18:51 +03:00
}
updateDeployment(deployment) {
return this._db.deployments.update(deployment.id, deployment);
2017-05-11 23:18:51 +03:00
}
deleteDeployment(id) {
return this._db.deployments.remove(id);
2017-05-11 23:18:51 +03:00
}
getDeployments() {
return this._db.deployments.all();
2017-05-11 23:18:51 +03:00
}
getDatacenters() {
return this._db.datacenters.all();
2017-05-11 23:18:51 +03:00
}
createManifest(deploymentId, manifest) {
manifest.deploymentId = deploymentId;
manifest.created = Date.now();
2017-05-11 23:18:51 +03:00
return this._db.manifests.insert().then(id => {
manifest.id = id;
return manifest;
2017-05-11 23:18:51 +03:00
});
}
getManifest(id) {
return this._db.manifests.get();
2017-05-11 23:18:51 +03:00
}
getActivities(deploymentId) {
return this._db.activities.query({ deploymentId });
2017-05-11 23:18:51 +03:00
}
getMetrics(containerId) {
return this._db.metrics.get(containerId);
}
insertMetrics(containerId, metrics) {
return this._db.metrics.get(containerId).then(existing => {
if (existing) {
return this._db.metrics.update(containerId, {
metrics: this._db.append(metrics)
});
}
2017-05-11 23:18:51 +03:00
const entry = { id: containerId, metrics };
return this._db.metrics.insert(entry, { merge: true });
2017-05-11 23:18:51 +03:00
});
}
getServices(deploymentId) {
this._db.deployments.get(deploymentId, { filter: 'services' });
}
2017-05-11 23:18:51 +03:00
updateService(deploymentId, service) {
this._db.deployments.get(deploymentId, { filter: 'services' }).then(() => {
const serviceToUpdate = deployment.services.find(currentService => {
return currentService.name === service.name;
});
2017-05-11 23:18:51 +03:00
if (!serviceToUpdate) {
deployment.services.push(service);
} else {
serviceToUpdate.count = service.count;
serviceToUpdate.containers = service.containers;
}
return this._db.deployments.update(deploymentId, {
services: deployment.services
2017-05-11 23:18:51 +03:00
});
});
}
deploymentChanges(handler) {
return this._db.deployments.changes('*', { reconnect: true, handler });
}
2017-05-11 23:18:51 +03:00
};