2017-05-02 01:28:03 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Hoek = require('hoek');
|
|
|
|
const Penseur = require('penseur');
|
|
|
|
|
|
|
|
|
|
|
|
const internals = {
|
|
|
|
defaults: {
|
|
|
|
name: 'portal'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = class Data {
|
|
|
|
constructor (options) {
|
|
|
|
const settings = Hoek.applyToDefaults(options || {}, internals.defaults);
|
|
|
|
const name = settings.name;
|
|
|
|
delete settings.name;
|
|
|
|
|
|
|
|
// Penseur will assert that the options are correct
|
|
|
|
this._db = new Penseur.Db(name, settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect (cb) {
|
2017-05-02 23:31:05 +03:00
|
|
|
this._db.establish(['activities', 'datacenters', 'deployments', 'manifests', 'metrics'], cb);
|
2017-05-02 01:28:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
createDeployment (deployment) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
deployment.services = [];
|
|
|
|
deployment.state = { current: 'stopped' };
|
|
|
|
|
2017-05-02 01:28:03 +03:00
|
|
|
this._db.deployments.insert(deployment, (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
deployment.id = key;
|
|
|
|
|
|
|
|
resolve(deployment);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getDeployment (id) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._db.deployments.get(id, (err, deployment) => {
|
|
|
|
return err ? reject(err) : resolve(deployment);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDeployment (deployment) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._db.deployments.update(deployment.id, deployment, (err) => {
|
|
|
|
return err ? reject(err) : resolve(deployment);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteDeployment (id) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._db.deployments.remove(id, (err) => {
|
|
|
|
return err ? reject(err) : resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getDeployments () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._db.deployments.all((err, deployments) => {
|
|
|
|
return err ? reject(err) : resolve(deployments);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getDatacenters () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._db.datacenters.all((err, datacenters) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
return err ? reject(err) : resolve(datacenters || []);
|
2017-05-02 01:28:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createManifest (deploymentId, manifest) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
manifest.deploymentId = deploymentId;
|
|
|
|
manifest.created = Date.now();
|
|
|
|
|
|
|
|
this._db.manifests.insert(manifest, (err, id) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest.id = id;
|
|
|
|
resolve(manifest);
|
2017-05-02 01:28:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-05-02 23:31:05 +03:00
|
|
|
getManifest (id) {
|
2017-05-02 01:28:03 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
this._db.manifests.get(id, (err, manifest) => {
|
2017-05-02 01:28:03 +03:00
|
|
|
return err ? reject(err) : resolve(manifest);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getActivities (deploymentId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._db.activities.query({ deploymentId }, (err, activities) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
return err ? reject(err) : resolve(activities || []);
|
2017-05-02 01:28:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getMetrics (deploymentId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
this._db.metrics.query({ deploymentId }, (err, metrics) => {
|
|
|
|
return err ? reject(err) : resolve(metrics || []);
|
2017-05-02 01:28:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getServices (deploymentId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
this._db.deployments.get(deploymentId, { filter: 'services' }, (err, deployment) => {
|
|
|
|
return err ? reject(err) : resolve(deployment.services);
|
2017-05-02 01:28:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
updateService (deploymentId, service) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-02 23:31:05 +03:00
|
|
|
this._db.deployments.get(deploymentId, { filter: 'services' }, (err, deployment) => {
|
2017-05-02 01:28:03 +03:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:31:05 +03:00
|
|
|
const services = deployment.services.map((currentService) => {
|
|
|
|
if (currentService.name === service.name) {
|
|
|
|
currentService.count = service.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentService;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._db.deployments.update(deploymentId, { services }, (err, keys) => {
|
|
|
|
return err ? reject(err) : resolve(service);
|
2017-05-02 01:28:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|