2017-06-09 07:26:25 +03:00
|
|
|
const { v4: uuid } = require('uuid');
|
|
|
|
const paramCase = require('param-case');
|
|
|
|
const camelCase = require('camel-case');
|
|
|
|
const yaml = require('js-yaml');
|
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
const {
|
|
|
|
datacenter,
|
|
|
|
portal,
|
|
|
|
deploymentGroups,
|
|
|
|
services,
|
|
|
|
instances
|
|
|
|
} = require('./data.json');
|
|
|
|
|
|
|
|
const find = (query = {}) => item =>
|
|
|
|
Object.keys(query).every(key => item[key] === query[key]);
|
|
|
|
|
|
|
|
const cleanQuery = (q = {}) => JSON.parse(JSON.stringify(q));
|
|
|
|
|
2017-06-26 15:25:46 +03:00
|
|
|
const getInstances = query => {
|
|
|
|
return Promise.resolve(instances.filter(find(cleanQuery(query))));
|
|
|
|
};
|
2017-06-08 00:15:48 +03:00
|
|
|
|
2017-06-26 15:25:46 +03:00
|
|
|
const getUnfilteredServices = query => {
|
2017-06-08 00:15:48 +03:00
|
|
|
const instancesResolver = ({ id }) => query =>
|
|
|
|
getInstances(
|
|
|
|
Object.assign({}, query, {
|
|
|
|
serviceId: id
|
2017-05-18 21:21:33 +03:00
|
|
|
})
|
2017-06-08 00:15:48 +03:00
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
const addNestedResolvers = service =>
|
|
|
|
Object.assign({}, service, {
|
|
|
|
instances: instancesResolver(service)
|
|
|
|
});
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
return Promise.resolve(
|
|
|
|
services.filter(find(cleanQuery(query))).map(addNestedResolvers)
|
|
|
|
);
|
|
|
|
};
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-26 15:25:46 +03:00
|
|
|
const getServices = query => {
|
|
|
|
const services = getUnfilteredServices(query)
|
|
|
|
.then(services => {
|
|
|
|
// loop through services and for each of them get all services that's parent id is the service
|
|
|
|
// once done - this will be a Promise all - need to remove any duplicates from the services list - the original
|
|
|
|
// then we can do below...
|
|
|
|
return (
|
|
|
|
Promise.all(
|
|
|
|
services.map(service => getUnfilteredServices({ parent: service.id }))
|
|
|
|
)
|
|
|
|
// this is going to be an array of arrays of services
|
|
|
|
.then(childServices => {
|
|
|
|
return childServices.reduce((childServices, childService) => {
|
|
|
|
return childServices.concat(childService);
|
|
|
|
}, []);
|
|
|
|
})
|
|
|
|
// now it's at least flat
|
|
|
|
.then(childServices => {
|
|
|
|
return services.concat(
|
|
|
|
childServices.reduce((childServices, childService) => {
|
|
|
|
const exists = services.filter(
|
|
|
|
service => service.id === childService.id
|
|
|
|
).length;
|
|
|
|
if (!exists) {
|
|
|
|
childServices.push(childService);
|
|
|
|
}
|
|
|
|
return childServices;
|
|
|
|
}, [])
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(services => {
|
|
|
|
return Promise.all(
|
|
|
|
services.map(service => service.instances())
|
|
|
|
).then(instances => {
|
|
|
|
return { services, instances };
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(({ services, instances }) => {
|
|
|
|
const activeServices = services.reduce((services, service, index) => {
|
|
|
|
const active = instances[index].filter(
|
|
|
|
instance =>
|
|
|
|
instance.status !== 'DELETED' && instance.status !== 'EXITED'
|
|
|
|
).length;
|
|
|
|
if (active) {
|
|
|
|
services.push(service);
|
|
|
|
}
|
|
|
|
return services;
|
|
|
|
}, []);
|
|
|
|
const allServices = activeServices.reduce((allServices, service) => {
|
|
|
|
if (service.parent) {
|
|
|
|
const parentService = services.filter(s => s.id === service.parent);
|
|
|
|
const exists = allServices.filter(s => s.id === service.parent)
|
|
|
|
.length;
|
|
|
|
if (!exists && parentService) {
|
|
|
|
allServices.push(parentService[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allServices.push(service);
|
|
|
|
return allServices;
|
|
|
|
}, []);
|
|
|
|
return allServices;
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(services);
|
|
|
|
};
|
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
const getDeploymentGroups = query => {
|
|
|
|
const servicesResolver = ({ id }) => query =>
|
|
|
|
getServices(
|
|
|
|
Object.assign({}, query, {
|
|
|
|
deploymentGroupId: id
|
|
|
|
})
|
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
const addNestedResolvers = dg =>
|
|
|
|
Object.assign({}, dg, {
|
|
|
|
services: servicesResolver(dg)
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(
|
|
|
|
deploymentGroups.filter(find(cleanQuery(query))).map(addNestedResolvers)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPortal = () =>
|
|
|
|
Promise.resolve(
|
|
|
|
Object.assign({}, portal, {
|
|
|
|
datacenter,
|
|
|
|
deploymentGroups: getDeploymentGroups
|
|
|
|
})
|
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-09 07:26:25 +03:00
|
|
|
const createDeploymentGroup = ({ name }) => {
|
|
|
|
const dg = {
|
|
|
|
id: uuid(),
|
|
|
|
slug: paramCase(name),
|
|
|
|
name
|
|
|
|
};
|
|
|
|
|
|
|
|
deploymentGroups.push(dg);
|
|
|
|
|
|
|
|
return Promise.resolve(dg);
|
|
|
|
};
|
|
|
|
|
|
|
|
const createServicesFromManifest = ({ deploymentGroupId, raw }) => {
|
|
|
|
const manifest = yaml.safeLoad(raw);
|
|
|
|
|
|
|
|
Object.keys(manifest).forEach(name => {
|
|
|
|
const service = {
|
|
|
|
id: uuid(),
|
|
|
|
deploymentGroup: deploymentGroupId,
|
|
|
|
slug: paramCase(name),
|
|
|
|
name
|
|
|
|
};
|
|
|
|
|
|
|
|
const instance = {
|
|
|
|
id: uuid(),
|
|
|
|
name: camelCase(`${service.slug}_01`),
|
|
|
|
service: service.id,
|
|
|
|
deploymentGroup: deploymentGroupId
|
|
|
|
};
|
|
|
|
|
|
|
|
services.push(service);
|
|
|
|
instances.push(instance);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
};
|
|
|
|
|
2017-06-16 17:12:28 +03:00
|
|
|
const scale = options => {
|
|
|
|
const service = getServices({ id: options.serviceId })[0];
|
2017-06-19 15:10:57 +03:00
|
|
|
|
2017-06-16 17:12:28 +03:00
|
|
|
return {
|
|
|
|
scale: [
|
|
|
|
{
|
|
|
|
id: service.id,
|
|
|
|
serviceName: service.name,
|
|
|
|
replicas: 2
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const restartServices = options => {
|
|
|
|
const service = getServices({ id: options.ids[0] });
|
|
|
|
return service;
|
|
|
|
};
|
|
|
|
|
2017-06-26 15:25:46 +03:00
|
|
|
const updateInstancesStatus = (is, status) => {
|
|
|
|
is.forEach(i => {
|
|
|
|
const instance = instances.filter(instance => instance.id === i.id)[0];
|
|
|
|
instance.status = status;
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(instances);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateServiceStatus = (serviceId, status) => {
|
|
|
|
return getServices({ id: serviceId })
|
|
|
|
.then(services => services.shift().instances())
|
|
|
|
.then(instances => updateInstancesStatus(instances, status))
|
|
|
|
.then(instances => getServices({ id: serviceId }));
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteServices = options => {
|
|
|
|
const serviceId = options.ids[0];
|
|
|
|
const deleteService = getServices({ id: serviceId }).then(services => {
|
|
|
|
const service = services.shift();
|
|
|
|
return service.instances().then(instances => {
|
|
|
|
if (instances.length) {
|
|
|
|
updateInstancesStatus(instances, 'DELETED');
|
|
|
|
return [service];
|
|
|
|
}
|
|
|
|
|
|
|
|
return getUnfilteredServices({ parent: serviceId }).then(services => {
|
|
|
|
return Promise.all(
|
|
|
|
services.map(service => service.instances())
|
|
|
|
).then(instances => {
|
|
|
|
const is = instances.reduce((is, i) => is.concat(i), []);
|
|
|
|
updateInstancesStatus(is, 'DELETED');
|
|
|
|
return [service];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return Promise.resolve(deleteService);
|
|
|
|
};
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const stopServices = options => {
|
2017-06-26 15:25:46 +03:00
|
|
|
const stopService = updateServiceStatus(options.ids[0], 'STOPPED');
|
|
|
|
return Promise.resolve(stopService);
|
2017-06-19 15:10:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const startServices = options => {
|
2017-06-26 15:25:46 +03:00
|
|
|
const startService = updateServiceStatus(options.ids[0], 'RUNNING');
|
|
|
|
return Promise.resolve(startService);
|
2017-06-19 15:10:57 +03:00
|
|
|
};
|
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
module.exports = {
|
2017-06-08 00:15:48 +03:00
|
|
|
portal: getPortal,
|
|
|
|
deploymentGroups: getDeploymentGroups,
|
2017-06-16 15:41:30 +03:00
|
|
|
deploymentGroup: query => getDeploymentGroups(query).then(dgs => dgs.shift()),
|
2017-06-08 00:15:48 +03:00
|
|
|
services: getServices,
|
|
|
|
service: query => getServices(query).then(services => services.shift()),
|
|
|
|
instances: getInstances,
|
2017-06-09 07:26:25 +03:00
|
|
|
instance: query => getInstances(query).then(instances => instances.shift()),
|
|
|
|
createDeploymentGroup,
|
|
|
|
provisionManifest: options =>
|
|
|
|
createServicesFromManifest(options).then(() => ({
|
|
|
|
id: uuid(),
|
|
|
|
type: options.type,
|
|
|
|
format: options.format
|
2017-06-16 17:12:28 +03:00
|
|
|
})),
|
|
|
|
deleteServices: (options, request, fn) => fn(null, deleteServices(options)),
|
2017-06-19 15:10:57 +03:00
|
|
|
scale: (options, reguest, fn) => fn(null, scale(options)),
|
|
|
|
restartServices: (options, request, fn) => fn(null, restartServices(options)),
|
|
|
|
stopServices: (options, request, fn) => fn(null, stopServices(options)),
|
|
|
|
startServices: (options, request, fn) => fn(null, startServices(options))
|
2017-05-18 21:21:33 +03:00
|
|
|
};
|