@controlplane/cli
Version:
Control Plane Corporation CLI
81 lines • 3.26 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const io_1 = require("../util/io");
const network_1 = require("./compose-resources/network");
const objects_1 = require("../util/objects");
const util_1 = require("./util");
async function convertDockerCompose(dockerComposeBody, path, context, registry, profile, build = true) {
const composeObject = (0, io_1.loadObject)(dockerComposeBody);
const { networks, volumes, secrets, services } = (0, util_1.getResourcesFromCompose)(composeObject, path, context, registry, profile);
const allResources = [...volumes, ...secrets, ...services];
// Throwing error if resources find invalid data
const issues = [];
for (const resource of allResources) {
resource.resourceIssues();
issues.push(...resource.issues);
}
if (issues.length > 0) {
throw new Error(issues.join('\n'));
}
// init (right now means just read secrets)
await Promise.all(allResources.map((r) => r.init()));
// Creating networks for network_mode and if no networks specified
if (networks.length === 0) {
let networkModeUsed = false;
// Check if services specify network in "network_mode"
for (const service of services) {
const networkMode = service.getNetworkMode();
// Configure network mode
if (networkMode !== undefined) {
networkModeUsed = true;
// Creating network for services
if (networkMode !== 'host' && networkMode !== 'none') {
const referencedService = services.find((s) => s.name === networkMode);
if (referencedService !== undefined) {
const net = new network_1.default(`${service.name} network`);
net.addService(service);
net.addService(referencedService);
networks.push(net);
}
}
}
}
// Adding global network if no service specified network_mode
if (!networkModeUsed) {
const net = new network_1.default('Global Network');
for (const service of services) {
net.addService(service);
}
networks.push(net);
}
}
// build resources
if (build) {
await Promise.all(allResources.map((r) => r.build()));
}
// configuring service networks
for (const network of networks) {
for (const serviceA of network.services) {
for (const serviceB of network.services) {
if (serviceA !== serviceB) {
serviceA.addService(serviceB);
serviceB.addService(serviceA);
}
}
}
}
// Create cpln object
const output = [];
for (const resource of allResources) {
const resourceCpln = resource.toResource();
if (Array.isArray(resourceCpln)) {
output.push(...resourceCpln);
continue;
}
output.push(resourceCpln);
}
(0, objects_1.removeUndefinedValues)(output);
return output;
}
exports.default = convertDockerCompose;
//# sourceMappingURL=convert.js.map
;