mirror of
https://github.com/yldio/copilot.git
synced 2024-11-14 23:30:05 +02:00
feat(portal-api): support sub-services
This commit is contained in:
parent
7b8c4f1de9
commit
7b5cf714d2
@ -71,7 +71,7 @@ exports.fromService = function ({ service, instances, packages }) {
|
|||||||
slug: service.slug,
|
slug: service.slug,
|
||||||
instances,
|
instances,
|
||||||
connections: service.service_dependency_ids,
|
connections: service.service_dependency_ids,
|
||||||
parent: service.parent_id,
|
branches: service.branches,
|
||||||
config: service.config ? service.config : undefined,
|
config: service.config ? service.config : undefined,
|
||||||
status: service.status,
|
status: service.status,
|
||||||
hasPlan: service.has_plan
|
hasPlan: service.has_plan
|
||||||
@ -92,7 +92,7 @@ exports.toService = function (clientService) {
|
|||||||
}) :
|
}) :
|
||||||
undefined,
|
undefined,
|
||||||
service_dependency_ids: clientService.connections,
|
service_dependency_ids: clientService.connections,
|
||||||
parent_id: clientService.parent ? clientService.parent : undefined,
|
branches: clientService.branches,
|
||||||
config: clientService.config ? clientService.config : undefined,
|
config: clientService.config ? clientService.config : undefined,
|
||||||
status: clientService.status,
|
status: clientService.status,
|
||||||
has_plan: clientService.hasPlan
|
has_plan: clientService.hasPlan
|
||||||
|
@ -9,6 +9,8 @@ const CIDRMatcher = require('cidr-matcher');
|
|||||||
const ForceArray = require('force-array');
|
const ForceArray = require('force-array');
|
||||||
const Get = require('lodash.get');
|
const Get = require('lodash.get');
|
||||||
const Uniq = require('lodash.uniq');
|
const Uniq = require('lodash.uniq');
|
||||||
|
const Uuid = require('uuid/v4');
|
||||||
|
const ParamCase = require('param-case');
|
||||||
const Queue = require('./queue');
|
const Queue = require('./queue');
|
||||||
|
|
||||||
module.exports = class ContainerPilotWatcher extends Events {
|
module.exports = class ContainerPilotWatcher extends Events {
|
||||||
@ -275,7 +277,7 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
}, cb);
|
}, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
_saveService ({ id, instances, connections }, cb) {
|
_saveService ({ id, instances, connections, branches }, cb) {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
@ -292,7 +294,8 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
|
|
||||||
this._data.updateService({
|
this._data.updateService({
|
||||||
id,
|
id,
|
||||||
connections
|
connections,
|
||||||
|
branches
|
||||||
}, cb);
|
}, cb);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -306,14 +309,13 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
}, cb);
|
}, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
check (cb) {
|
_resolveServiceConnections ({ services, service }) {
|
||||||
if (!this._triton) {
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
|
|
||||||
const resolveServiceConnections = ({ services, service }) => {
|
|
||||||
const watches = Uniq(
|
const watches = Uniq(
|
||||||
Flatten(ForceArray(service.instances).map(({ watches }) => { return watches; }))
|
Flatten(
|
||||||
|
ForceArray(service.instances).map(({ watches }) => {
|
||||||
|
return watches;
|
||||||
|
})
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
return watches
|
return watches
|
||||||
@ -324,7 +326,11 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const thisServiceJobs = Uniq(
|
const thisServiceJobs = Uniq(
|
||||||
Flatten(ForceArray(service.instances).map(({ jobs }) => { return jobs; }))
|
Flatten(
|
||||||
|
ForceArray(service.instances).map(({ jobs }) => {
|
||||||
|
return jobs;
|
||||||
|
})
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (thisServiceJobs.indexOf(jobName) >= 0) {
|
if (thisServiceJobs.indexOf(jobName) >= 0) {
|
||||||
@ -335,11 +341,11 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
}, null);
|
}, null);
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
};
|
}
|
||||||
|
|
||||||
const resolveInstanceHealth = ({ name }, instance) => {
|
_resolveInstanceHealth ({ name }, instance) {
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
return;
|
return 'UNAVAILABLE';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!instance.cp) {
|
if (!instance.cp) {
|
||||||
@ -348,7 +354,9 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
|
|
||||||
const jobNames = Get(instance, 'cp.Services');
|
const jobNames = Get(instance, 'cp.Services');
|
||||||
|
|
||||||
const serviceJobs = jobNames.filter(({ Name }) => { return Name === name; });
|
const serviceJobs = jobNames.filter(({ Name }) => {
|
||||||
|
return Name === name;
|
||||||
|
});
|
||||||
|
|
||||||
if (serviceJobs.length) {
|
if (serviceJobs.length) {
|
||||||
return serviceJobs.shift().Status.toUpperCase();
|
return serviceJobs.shift().Status.toUpperCase();
|
||||||
@ -364,14 +372,64 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 'UNKNOWN';
|
return 'UNKNOWN';
|
||||||
};
|
|
||||||
|
|
||||||
const handleStatuses = (err, results) => {
|
|
||||||
if (err) {
|
|
||||||
this.emit('error', err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const dgs = ForceArray((results || {}).successes)
|
_resolveServiceBranches ({ name, slug, instances }) {
|
||||||
|
const deviantJobNames = Uniq(Flatten(instances.map(({ jobs }) => {
|
||||||
|
return Flatten(jobs.filter((jobName) => {
|
||||||
|
return new RegExp(`${name}-.*`).test(jobName);
|
||||||
|
}));
|
||||||
|
})));
|
||||||
|
|
||||||
|
if (!deviantJobNames) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultBranch = instances.reduce((service, { id, jobs }) => {
|
||||||
|
if (jobs.indexOf(name) >= 0) {
|
||||||
|
return Object.assign(service, {
|
||||||
|
instances: service.instances.concat(id)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return service;
|
||||||
|
}, {
|
||||||
|
id: Uuid(),
|
||||||
|
name: name,
|
||||||
|
slug: slug,
|
||||||
|
instances: []
|
||||||
|
});
|
||||||
|
|
||||||
|
const branches = instances.reduce((branches, { id, jobs }) => {
|
||||||
|
if (defaultBranch.instances.indexOf(id) >= 0) {
|
||||||
|
return branches;
|
||||||
|
}
|
||||||
|
|
||||||
|
const branchName = jobs
|
||||||
|
.filter((jobName) => {
|
||||||
|
return deviantJobNames.indexOf(jobName) >= 0;
|
||||||
|
})
|
||||||
|
.shift();
|
||||||
|
|
||||||
|
if (!branches[branchName]) {
|
||||||
|
branches[branchName] = {
|
||||||
|
id: Uuid(),
|
||||||
|
name: branchName,
|
||||||
|
slug: ParamCase(branchName),
|
||||||
|
instances: []
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
branches[branchName].instances.push(id);
|
||||||
|
|
||||||
|
return branches;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return Object.values(branches).concat(defaultBranch);
|
||||||
|
}
|
||||||
|
|
||||||
|
_resolveDeploymentGroups (dgs) {
|
||||||
|
return dgs
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.map((dg) => {
|
.map((dg) => {
|
||||||
return Object.assign({}, dg, {
|
return Object.assign({}, dg, {
|
||||||
@ -379,10 +437,10 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
return Object.assign({}, service, {
|
return Object.assign({}, service, {
|
||||||
instances: ForceArray(service.instances).map((instance) => {
|
instances: ForceArray(service.instances).map((instance) => {
|
||||||
return Object.assign({}, instance, {
|
return Object.assign({}, instance, {
|
||||||
healthy: resolveInstanceHealth(service, instance),
|
healthy: this._resolveInstanceHealth(service, instance),
|
||||||
jobs: Get(instance, 'cp.Services', []).map(
|
jobs: Get(instance, 'cp.Services', []).map(({ Name }) => {
|
||||||
({ Name }) => { return Name; }
|
return Name;
|
||||||
),
|
}),
|
||||||
watches: Get(instance, 'cp.Watches', [])
|
watches: Get(instance, 'cp.Watches', [])
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@ -394,7 +452,8 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
return Object.assign({}, dg, {
|
return Object.assign({}, dg, {
|
||||||
services: ForceArray(dg.services).map((service) => {
|
services: ForceArray(dg.services).map((service) => {
|
||||||
return Object.assign({}, service, {
|
return Object.assign({}, service, {
|
||||||
connections: resolveServiceConnections({
|
branches: this._resolveServiceBranches(service),
|
||||||
|
connections: this._resolveServiceConnections({
|
||||||
services: dg.services,
|
services: dg.services,
|
||||||
service
|
service
|
||||||
})
|
})
|
||||||
@ -402,6 +461,21 @@ module.exports = class ContainerPilotWatcher extends Events {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
check (cb) {
|
||||||
|
if (!this._triton) {
|
||||||
|
return cb();
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleStatuses = (err, results) => {
|
||||||
|
if (err) {
|
||||||
|
this.emit('error', err);
|
||||||
|
}
|
||||||
|
|
||||||
|
const dgs = this._resolveDeploymentGroups(
|
||||||
|
ForceArray((results || {}).successes)
|
||||||
|
);
|
||||||
|
|
||||||
VAsync.forEachParallel({
|
VAsync.forEachParallel({
|
||||||
inputs: dgs,
|
inputs: dgs,
|
||||||
|
Loading…
Reference in New Issue
Block a user