2017-05-11 23:18:51 +03:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
const EventEmitter = require('events');
|
2017-06-03 01:16:49 +03:00
|
|
|
const DockerClient = require('docker-compose-client');
|
2017-06-08 21:43:24 +03:00
|
|
|
const Dockerode = require('dockerode');
|
2017-05-11 23:18:51 +03:00
|
|
|
const Hoek = require('hoek');
|
|
|
|
const Penseur = require('penseur');
|
2017-05-27 19:35:38 +03:00
|
|
|
const VAsync = require('vasync');
|
|
|
|
const Transform = require('./transform');
|
|
|
|
|
2017-05-11 23:18:51 +03:00
|
|
|
|
|
|
|
const internals = {
|
|
|
|
defaults: {
|
2017-05-25 23:03:39 +03:00
|
|
|
name: 'portal',
|
|
|
|
db: {
|
|
|
|
test: false
|
|
|
|
},
|
2017-06-08 21:43:24 +03:00
|
|
|
dockerComposeHost: 'tcp://0.0.0.0:4242'
|
2017-05-25 23:03:39 +03:00
|
|
|
},
|
2017-05-27 19:35:38 +03:00
|
|
|
tables: {
|
2017-06-05 23:54:44 +03:00
|
|
|
'portals': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'datacenters': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'deployment_groups': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'versions': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'manifests': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'services': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'packages': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'instances': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false },
|
|
|
|
'users': { id: { type: 'uuid' }, primary: 'id', secondary: false, purge: false }
|
2017-05-27 19:35:38 +03:00
|
|
|
}
|
2017-05-11 23:18:51 +03:00
|
|
|
};
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
module.exports = class Data extends EventEmitter {
|
2017-05-25 23:03:39 +03:00
|
|
|
constructor (options) {
|
2017-06-02 00:25:45 +03:00
|
|
|
super();
|
2017-06-05 23:54:44 +03:00
|
|
|
const settings = Hoek.applyToDefaults(internals.defaults, options || {});
|
2017-05-11 23:18:51 +03:00
|
|
|
|
|
|
|
// Penseur will assert that the options are correct
|
2017-05-25 23:03:39 +03:00
|
|
|
this._db = new Penseur.Db(settings.name, settings.db);
|
2017-06-08 21:43:24 +03:00
|
|
|
this._dockerCompose = new DockerClient(settings.dockerComposeHost);
|
|
|
|
this._docker = new Dockerode(settings.docker);
|
2017-06-03 01:16:49 +03:00
|
|
|
|
2017-06-08 21:43:24 +03:00
|
|
|
this._dockerCompose.on('error', (err) => {
|
2017-06-03 01:16:49 +03:00
|
|
|
this.emit('error', err);
|
|
|
|
});
|
2017-05-16 18:54:39 +03:00
|
|
|
}
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
connect (cb) {
|
|
|
|
this._db.establish(internals.tables, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// portals
|
|
|
|
|
|
|
|
createPortal (clientPortal, cb) {
|
|
|
|
const portal = Transform.toPortal(clientPortal);
|
|
|
|
this._db.portals.insert(portal, (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
portal.id = key;
|
|
|
|
cb(null, Transform.fromPortal({ portal }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:54:44 +03:00
|
|
|
getPortal (options, cb) {
|
2017-05-27 19:35:38 +03:00
|
|
|
this._db.portals.all((err, portals) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:54:44 +03:00
|
|
|
if (!portals) {
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
const portal = portals[0];
|
|
|
|
VAsync.parallel({
|
|
|
|
funcs: [
|
|
|
|
(next) => {
|
|
|
|
this.getDatacenter({ id: portal.datacenter_id }, next);
|
|
|
|
},
|
2017-06-05 23:54:44 +03:00
|
|
|
(next) => {
|
|
|
|
this.getUser({}, next);
|
2017-05-27 19:35:38 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}, (err, results) => {
|
2017-05-25 23:03:39 +03:00
|
|
|
if (err) {
|
2017-05-27 19:35:38 +03:00
|
|
|
return cb(err);
|
2017-05-25 23:03:39 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 00:35:45 +03:00
|
|
|
// Sub query/filter for deploymentGroups
|
|
|
|
const deploymentGroups = (args) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.getDeploymentGroups(args, (err, groups) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(groups);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-05 23:54:44 +03:00
|
|
|
cb(null, Transform.fromPortal({
|
|
|
|
portal,
|
2017-06-08 00:35:45 +03:00
|
|
|
deploymentGroups,
|
|
|
|
datacenter: results.operations[0].result,
|
|
|
|
user: results.operations[1].result
|
2017-06-05 23:54:44 +03:00
|
|
|
}));
|
2017-05-25 23:03:39 +03:00
|
|
|
});
|
|
|
|
});
|
2017-05-16 18:54:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
// datacenters
|
2017-05-11 23:18:51 +03:00
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
createDatacenter (datacenter, cb) {
|
|
|
|
this._db.datacenters.insert(datacenter, (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2017-05-11 23:18:51 +03:00
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
datacenter.id = key;
|
|
|
|
cb(null, datacenter);
|
|
|
|
});
|
2017-05-11 23:18:51 +03:00
|
|
|
}
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
getDatacenters (cb) {
|
|
|
|
this._db.datacenters.all(cb);
|
2017-05-11 23:18:51 +03:00
|
|
|
}
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
getDatacenter ({ id, region }, cb) {
|
|
|
|
Hoek.assert(id || region, 'id or region are required to retrieve a datacenter');
|
2017-05-11 23:18:51 +03:00
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
if (region) {
|
2017-06-05 23:54:44 +03:00
|
|
|
return this._db.datacenters.query({ region }, (err, datacenters) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, datacenters && datacenters.length ? datacenters[0] : null);
|
|
|
|
});
|
2017-05-27 19:35:38 +03:00
|
|
|
}
|
|
|
|
|
2017-06-05 23:54:44 +03:00
|
|
|
this._db.datacenters.get(id, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// users
|
|
|
|
|
|
|
|
createUser (clientUser, cb) {
|
|
|
|
const user = Transform.toUser(clientUser);
|
|
|
|
this._db.users.insert(user, (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
user.id = key;
|
|
|
|
cb(null, Transform.fromUser(user));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getUser (options, cb) {
|
|
|
|
this._db.users.all((err, users) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!users || !users.length) {
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, Transform.fromUser(users[0]));
|
|
|
|
});
|
2017-05-11 23:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
// deployment_groups
|
|
|
|
|
2017-05-31 01:08:06 +03:00
|
|
|
createDeploymentGroup (clientDeploymentGroup, cb) {
|
2017-05-27 19:35:38 +03:00
|
|
|
// 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
|
|
|
|
|
2017-05-31 01:08:06 +03:00
|
|
|
const deploymentGroup = Transform.toDeploymentGroup(clientDeploymentGroup);
|
|
|
|
this._db.deployment_groups.insert(deploymentGroup, (err, key) => {
|
2017-05-27 19:35:38 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2017-05-31 01:08:06 +03:00
|
|
|
deploymentGroup.id = key;
|
|
|
|
cb(null, Transform.fromDeploymentGroup(deploymentGroup));
|
2017-05-11 23:18:51 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
updateDeploymentGroup ({ id, name }, cb) {
|
|
|
|
this._db.deployment_groups.update(id, { name }, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2017-05-11 23:18:51 +03:00
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
cb(null, Transform.fromDeploymentGroup({ id, name }));
|
|
|
|
});
|
2017-05-12 22:59:37 +03:00
|
|
|
}
|
|
|
|
|
2017-05-31 01:08:06 +03:00
|
|
|
getDeploymentGroups ({ ids, name, slug }, cb) {
|
|
|
|
const finish = (err, deploymentGroups) => {
|
2017-05-27 19:35:38 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
2017-05-16 18:54:39 +03:00
|
|
|
}
|
2017-05-11 23:18:51 +03:00
|
|
|
|
2017-06-08 00:35:45 +03:00
|
|
|
const getServices = (service_ids) => {
|
|
|
|
return (args) => {
|
|
|
|
args = args || {};
|
|
|
|
args.ids = service_ids;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.getServices(args, (err, services) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(services);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const convertedGroups = deploymentGroups ? deploymentGroups.map((deploymentGroup) => {
|
|
|
|
return Transform.fromDeploymentGroup(deploymentGroup, getServices(deploymentGroup.service_ids));
|
|
|
|
}) : [];
|
|
|
|
|
|
|
|
cb(null, convertedGroups);
|
2017-05-31 01:08:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (ids) {
|
|
|
|
return this._db.deployment_groups.get(ids, finish);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
return this._db.deployment_groups.query({ name }, finish);
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:54:44 +03:00
|
|
|
if (slug) {
|
|
|
|
return this._db.deployment_groups.query({ slug }, finish);
|
|
|
|
}
|
|
|
|
|
2017-06-08 00:35:45 +03:00
|
|
|
return this._db.deployment_groups.all(finish);
|
2017-05-11 23:18:51 +03:00
|
|
|
}
|
2017-05-12 22:59:37 +03:00
|
|
|
|
2017-05-31 01:08:06 +03:00
|
|
|
getDeploymentGroup (query, cb) {
|
2017-06-08 00:35:45 +03:00
|
|
|
this._db.deployment_groups.single(query, (err, deploymentGroup) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2017-06-03 01:16:49 +03:00
|
|
|
|
2017-06-08 00:35:45 +03:00
|
|
|
if (!deploymentGroup) {
|
|
|
|
return cb(null, {});
|
|
|
|
}
|
2017-05-27 19:35:38 +03:00
|
|
|
|
2017-06-08 00:35:45 +03:00
|
|
|
if (!deploymentGroup.service_ids || !deploymentGroup.service_ids.length) {
|
|
|
|
return cb(null, Transform.fromDeploymentGroup(deploymentGroup));
|
|
|
|
}
|
2017-06-03 01:16:49 +03:00
|
|
|
|
2017-06-08 00:35:45 +03:00
|
|
|
const getServices = (args) => {
|
|
|
|
args = args || {};
|
|
|
|
args.ids = deploymentGroup.service_ids;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.getServices(args, (err, services) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(services);
|
|
|
|
});
|
2017-06-03 01:16:49 +03:00
|
|
|
});
|
2017-06-08 00:35:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
cb(err, Transform.fromDeploymentGroup(deploymentGroup, getServices));
|
2017-05-27 19:35:38 +03:00
|
|
|
});
|
2017-05-16 18:54:39 +03:00
|
|
|
}
|
2017-05-11 23:18:51 +03:00
|
|
|
|
|
|
|
|
2017-05-27 19:35:38 +03:00
|
|
|
// versions
|
|
|
|
|
|
|
|
createVersion (clientVersion, cb) {
|
2017-06-02 00:25:45 +03:00
|
|
|
Hoek.assert(clientVersion, 'version is required');
|
|
|
|
Hoek.assert(clientVersion.manifestId, 'manifestId is required');
|
|
|
|
Hoek.assert(clientVersion.deploymentGroupId, 'deploymentGroupId is required');
|
2017-05-31 01:08:06 +03:00
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
const version = Transform.toVersion(clientVersion);
|
|
|
|
this._db.versions.insert(version, (err, key) => {
|
2017-05-27 19:35:38 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
2017-05-16 18:54:39 +03:00
|
|
|
}
|
2017-05-12 22:59:37 +03:00
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
const changes = {
|
|
|
|
id: clientVersion.deploymentGroupId,
|
|
|
|
version_id: key,
|
|
|
|
history_version_ids: this._db.append(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (clientVersion.serviceIds) {
|
|
|
|
changes['service_ids'] = clientVersion.serviceIds;
|
2017-05-31 01:08:06 +03:00
|
|
|
}
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
this._db.deployment_groups.update([changes], (err) => {
|
2017-05-31 01:08:06 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
version.id = key;
|
|
|
|
cb(null, Transform.fromVersion(version));
|
2017-05-31 01:08:06 +03:00
|
|
|
});
|
2017-05-11 23:18:51 +03:00
|
|
|
});
|
|
|
|
}
|
2017-05-12 22:59:37 +03:00
|
|
|
|
2017-05-31 01:08:06 +03:00
|
|
|
getVersion ({ id, manifestId }, cb) {
|
|
|
|
const query = id ? { id } : { manifest_id: manifestId };
|
|
|
|
this._db.versions.single(query, (err, version) => {
|
2017-05-27 19:35:38 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, Transform.fromVersion(version));
|
|
|
|
});
|
2017-05-12 22:59:37 +03:00
|
|
|
}
|
2017-05-31 01:08:06 +03:00
|
|
|
|
|
|
|
getVersions ({ manifestId, deploymentGroupId }, cb) {
|
|
|
|
const finish = (err, versions) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
versions = versions || [];
|
|
|
|
cb(null, versions.map(Transform.fromVersion));
|
|
|
|
};
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
// ensure the data is in sync
|
|
|
|
this._db.versions.sync(() => {
|
|
|
|
if (manifestId) {
|
|
|
|
return this._db.versions.query({ manifest_id: manifestId }, finish);
|
2017-05-31 01:08:06 +03:00
|
|
|
}
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
this.getDeploymentGroup({ id: deploymentGroupId }, (err, deploymentGroup) => {
|
|
|
|
if (err) {
|
|
|
|
return finish(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.versions.get(deploymentGroup.history, finish);
|
|
|
|
});
|
2017-05-31 01:08:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
scale ({ id, replicas }, cb) {
|
|
|
|
Hoek.assert(id, 'service id is required');
|
|
|
|
Hoek.assert(typeof replicas === 'number' && replicas >= 0, 'replicas must be a number no less than 0');
|
|
|
|
|
|
|
|
|
|
|
|
// get the service then get the deployment group
|
|
|
|
// use the deployment group to find the current version and manifest
|
|
|
|
// scale the service
|
|
|
|
// update the machine ids and instances
|
|
|
|
|
|
|
|
this._db.services.single({ id }, (err, service) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
return cb(new Error(`service not found for id: ${id}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.deployment_groups.single({ id: service.deployment_group_id }, (err, deployment_group) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!deployment_group) {
|
|
|
|
return cb(new Error(`deployment group not found for service with service id: ${id}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.versions.single({ id: deployment_group.version_id }, (err, version) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!version) {
|
|
|
|
return cb(new Error(`version not found for service with service id: ${id}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.manifests.single({ id: version.manifest_id }, (err, manifest) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!manifest) {
|
|
|
|
return cb(new Error(`manifest not found for service with service id: ${id}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._scale({ service, deployment_group, version, manifest, replicas }, cb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_scale ({ service, deployment_group, version, manifest, replicas }, cb) {
|
|
|
|
let isFinished = false;
|
|
|
|
const finish = () => {
|
|
|
|
if (isFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFinished = true;
|
|
|
|
const machineIds = [];
|
|
|
|
for (let i = 1; i <= replicas; ++i) {
|
|
|
|
machineIds.push(`${deployment_group.name}_${service.name}_${i}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.instances.remove(service.instance_ids, (err) => {
|
|
|
|
// emit error instead of returning early, this is a best effort to cleanup data
|
|
|
|
if (err) {
|
|
|
|
this.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
VAsync.forEachParallel({
|
|
|
|
func: (machineId, next) => {
|
|
|
|
const clientInstance = {
|
|
|
|
machineId,
|
|
|
|
status: 'CREATED',
|
|
|
|
name: service.name
|
|
|
|
};
|
|
|
|
this.createInstance(clientInstance, next);
|
|
|
|
},
|
|
|
|
inputs: machineIds
|
|
|
|
}, (err, results) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const instanceIds = results.successes.map((instance) => {
|
|
|
|
return instance.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._db.services.update(service.id, { instance_ids: instanceIds }, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientVersion = {
|
|
|
|
deploymentGroupId: deployment_group.id,
|
|
|
|
manifestId: manifest.id,
|
|
|
|
plan: {
|
|
|
|
running: true,
|
|
|
|
actions: [{
|
|
|
|
type: 'CREATE',
|
|
|
|
service: service.name,
|
|
|
|
machines: machineIds
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const scale = version.service_scales.find((scale) => {
|
|
|
|
return scale.service_name === service.name;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (scale) {
|
|
|
|
scale.replicas = replicas;
|
|
|
|
} else {
|
|
|
|
version.service_scales.push({
|
|
|
|
service_name: service.name,
|
|
|
|
replicas
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
clientVersion.scales = version.service_scales.map(Transform.fromScale);
|
|
|
|
|
|
|
|
this.createVersion(clientVersion, cb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
2017-06-05 19:33:13 +03:00
|
|
|
projectName: deployment_group.name,
|
2017-06-03 01:16:49 +03:00
|
|
|
services: {},
|
|
|
|
manifest: manifest.raw
|
|
|
|
};
|
|
|
|
options.services[service.name] = replicas;
|
2017-06-08 21:43:24 +03:00
|
|
|
this._dockerCompose.scale(options, (err) => {
|
2017-06-03 01:16:49 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-31 01:08:06 +03:00
|
|
|
|
|
|
|
// manifests
|
|
|
|
|
|
|
|
provisionManifest (clientManifest, cb) {
|
2017-06-03 01:16:49 +03:00
|
|
|
// get deployment group to verify it exists and get the name
|
2017-06-02 00:25:45 +03:00
|
|
|
// insert manifest
|
|
|
|
// callback with manifest
|
2017-06-03 01:16:49 +03:00
|
|
|
// provision containers and save service data
|
2017-06-02 00:25:45 +03:00
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
this.getDeploymentGroup({ id: clientManifest.deploymentGroupId }, (err, deploymentGroup) => {
|
2017-05-31 01:08:06 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
if (!deploymentGroup) {
|
|
|
|
return cb(new Error('Deployment group not found for manifest'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const manifest = Transform.toManifest(clientManifest);
|
|
|
|
this._db.manifests.insert(manifest, (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
setImmediate(() => {
|
|
|
|
let isHandled = false;
|
2017-06-08 21:43:24 +03:00
|
|
|
this._dockerCompose.provision({ projectName: deploymentGroup.name, manifest: clientManifest.raw }, (err, res) => {
|
2017-06-03 01:16:49 +03:00
|
|
|
if (err) {
|
|
|
|
this.emit('error', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback can execute multiple times, ensure responses are only handled once
|
|
|
|
if (isHandled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isHandled = true;
|
|
|
|
const options = {
|
|
|
|
manifestServices: manifest.json.services || manifest.json,
|
|
|
|
deploymentGroup,
|
2017-06-08 21:43:24 +03:00
|
|
|
manifestId: key,
|
|
|
|
provisionRes: res
|
2017-06-03 01:16:49 +03:00
|
|
|
};
|
|
|
|
this.provisionServices(options);
|
|
|
|
});
|
|
|
|
});
|
2017-06-02 00:25:45 +03:00
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
manifest.id = key;
|
|
|
|
cb(null, Transform.fromManifest(manifest));
|
|
|
|
});
|
2017-05-31 01:08:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getManifest ({ id }, cb) {
|
|
|
|
this._db.manifests.single({ id }, (err, manifest) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, Transform.fromManifest(manifest || {}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getManifests ({ type, deploymentGroupId }, cb) {
|
|
|
|
const query = type ? { type } : { deployment_group_id: deploymentGroupId };
|
|
|
|
this._db.manifests.query(query, (err, manifests) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
manifests = manifests || [];
|
|
|
|
cb(null, manifests.map(Transform.fromManifest));
|
|
|
|
});
|
|
|
|
}
|
2017-05-31 22:27:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
// services
|
|
|
|
|
2017-06-08 21:43:24 +03:00
|
|
|
provisionServices ({ manifestServices, deploymentGroup, manifestId, provisionRes }, cb) {
|
2017-06-02 00:25:45 +03:00
|
|
|
// insert instance information
|
|
|
|
// insert service information
|
|
|
|
// insert version information -- will update deploymentGroups
|
|
|
|
|
|
|
|
cb = cb || ((err) => {
|
|
|
|
if (err) {
|
|
|
|
this.emit('error', err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
VAsync.forEachPipeline({
|
|
|
|
func: (serviceName, next) => {
|
|
|
|
const manifestService = manifestServices[serviceName];
|
|
|
|
const clientInstance = {
|
|
|
|
name: serviceName,
|
2017-06-08 21:43:24 +03:00
|
|
|
machineId: provisionRes[serviceName].plan.containers[0].id,
|
2017-06-02 00:25:45 +03:00
|
|
|
status: 'CREATED'
|
|
|
|
};
|
|
|
|
this.createInstance(clientInstance, (err, createdInstance) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientService = {
|
|
|
|
hash: manifestService.image,
|
|
|
|
name: serviceName,
|
|
|
|
slug: serviceName,
|
2017-06-03 01:16:49 +03:00
|
|
|
deploymentGroupId: deploymentGroup.id,
|
2017-06-08 21:43:24 +03:00
|
|
|
instances: [createdInstance],
|
|
|
|
info: provisionRes[serviceName]
|
2017-06-02 00:25:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
this.createService(clientService, (err, createdService) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(null, {
|
|
|
|
action: {
|
|
|
|
type: 'CREATE',
|
|
|
|
service: serviceName,
|
|
|
|
machines: [createdInstance.machineId]
|
|
|
|
},
|
|
|
|
serviceId: createdService.id,
|
|
|
|
scale: {
|
|
|
|
serviceName,
|
|
|
|
replicas: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
inputs: Object.keys(manifestServices)
|
|
|
|
}, (err, results) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
const successes = results.successes;
|
|
|
|
if (!successes || !successes.length) {
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
const scales = successes.map((result) => {
|
|
|
|
return result.scale;
|
|
|
|
});
|
|
|
|
|
|
|
|
const actions = successes.map((result) => {
|
|
|
|
return result.action;
|
|
|
|
});
|
|
|
|
|
|
|
|
const serviceIds = successes.map((result) => {
|
|
|
|
return result.serviceId;
|
|
|
|
});
|
|
|
|
|
|
|
|
const plan = {
|
|
|
|
running: true,
|
|
|
|
actions
|
|
|
|
};
|
|
|
|
|
|
|
|
const clientVersion = {
|
2017-06-03 01:16:49 +03:00
|
|
|
deploymentGroupId: deploymentGroup.id,
|
2017-06-02 00:25:45 +03:00
|
|
|
manifestId,
|
|
|
|
scales,
|
|
|
|
plan,
|
|
|
|
serviceIds
|
|
|
|
};
|
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
this.createVersion(clientVersion, (err, version) => {
|
2017-06-02 00:25:45 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:16:49 +03:00
|
|
|
cb(null, version);
|
2017-06-02 00:25:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:27:53 +03:00
|
|
|
createService (clientService, cb) {
|
|
|
|
this._db.services.insert(Transform.toService(clientService), (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
clientService.id = key;
|
|
|
|
cb(null, clientService);
|
2017-05-31 22:27:53 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getService ({ id, hash }, cb) {
|
|
|
|
const query = id ? { id } : { version_hash: hash };
|
2017-06-02 00:25:45 +03:00
|
|
|
this._db.services.query(query, (err, service) => {
|
2017-05-31 22:27:53 +03:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2017-06-02 00:25:45 +03:00
|
|
|
if (!service) {
|
|
|
|
return cb(null, null);
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:27:53 +03:00
|
|
|
VAsync.parallel({
|
|
|
|
funcs: [
|
|
|
|
(next) => {
|
|
|
|
this._db.instances.get(service.instance_ids, next);
|
|
|
|
},
|
|
|
|
(next) => {
|
|
|
|
this._db.packages.single({ id: service.package_id }, next);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, (err, results) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, Transform.fromService({ service, instances: results.successes[0], package: results.successes[1] }));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 00:35:45 +03:00
|
|
|
getServices (options, cb) {
|
|
|
|
const query = {};
|
|
|
|
if (options.ids && options.ids.length) {
|
|
|
|
query.id = this._db.or(options.ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.name) {
|
|
|
|
query.name = options.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.slug) {
|
|
|
|
query.slug = options.slug;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.parentId) {
|
|
|
|
query.parent_id = options.parentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.deploymentGroupId) {
|
|
|
|
query.deployment_group_id = options.deploymentGroupId;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.services.query(query, (err, services) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!services || !services.length) {
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, services.map((service) => {
|
|
|
|
return Transform.fromService({ service });
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2017-05-31 22:27:53 +03:00
|
|
|
|
2017-06-08 21:43:24 +03:00
|
|
|
stopServices ({ ids }, cb) {
|
|
|
|
this._db.services.get(ids, (err, services) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!services || !services.length) {
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
let instanceIds = [];
|
|
|
|
services.forEach((service) => {
|
|
|
|
instanceIds = instanceIds.concat(service.instance_ids);
|
|
|
|
});
|
|
|
|
|
|
|
|
VAsync.forEachParallel({
|
|
|
|
func: (instanceId, next) => {
|
|
|
|
this._db.instances.get(instanceId, (err, instance) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const container = this._docker.getContainer(instance.machine_id);
|
|
|
|
|
|
|
|
container.stop((err) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateInstance({ id: instance.id, status: 'STOPPED' }, next);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
inputs: instanceIds
|
|
|
|
}, (err, results) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getServices({ ids }, cb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
startServices ({ ids }, cb) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
restartServices ({ ids }, cb) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteServices ({ ids }, cb) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:27:53 +03:00
|
|
|
|
|
|
|
// instances
|
|
|
|
|
|
|
|
createInstance (clientInstance, cb) {
|
|
|
|
this._db.instances.insert(Transform.toInstance(clientInstance), (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
clientInstance.id = key;
|
|
|
|
cb(null, clientInstance);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getInstance ({ id }, cb) {
|
|
|
|
this._db.instances.single({ id }, (err, instance) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, instance ? Transform.fromInstance(instance) : {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:43:24 +03:00
|
|
|
updateInstance ({ id, status }, cb) {
|
|
|
|
this._db.instances.update(id, { status }, (err, instance) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, instance ? Transform.fromInstance(instance) : {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:27:53 +03:00
|
|
|
|
|
|
|
// packages
|
|
|
|
|
|
|
|
createPackage (clientPackage, cb) {
|
|
|
|
this._db.packages.insert(Transform.toPackage(clientPackage), (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
clientPackage.id = key;
|
|
|
|
cb(null, clientPackage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getPackage ({ id }, cb) {
|
|
|
|
this._db.packages.single({ id }, (err, dbPackage) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, dbPackage ? Transform.fromPackage(dbPackage) : {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getPackages ({ name, type }, cb) {
|
|
|
|
const query = name ? { name } : { type };
|
|
|
|
this._db.packages.query(query, (err, dbPackages) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, dbPackages ? dbPackages.map(Transform.fromPackage) : []);
|
|
|
|
});
|
|
|
|
}
|
2017-05-11 23:18:51 +03:00
|
|
|
};
|