UNPKG

compose-as-code

Version:

Provides a module to express docker compose files as code

197 lines (196 loc) 11.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileServices = void 0; const compilerUtils_1 = require("./compilerUtils"); const compileServiceVolumes = (serviceVolumes, baseIndentationDepth) => { let result = ''; serviceVolumes.forEach(entry => { const extractedHostLocation = typeof entry.hostLocation === 'string' ? entry.hostLocation : entry.hostLocation.id; result += (0, compilerUtils_1.indent)(baseIndentationDepth); result += `- ${extractedHostLocation}:${entry.containerLocation}${entry.accessMode ? ':' + entry.accessMode : ''}\n`; }); return result; }; const compileService = (service) => { let serviceTextBlock = ''; const baseIndentation = 1; serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)(service.id, '', baseIndentation); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('image', service.image, baseIndentation + 1); if (service.pullPolicy) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('pull_policy', service.pullPolicy, baseIndentation + 1); } if (service.restart) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('restart', service.restart, baseIndentation + 1); } if (service.expose && service.expose.length > 0) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('expose', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileList)(service.expose, baseIndentation + 2); } if (service.ports && service.ports.length > 0) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('ports', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileList)(service.ports, baseIndentation + 2); } if (service.environment && Object.keys(service.environment).length > 0) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('environment', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileObject)(service.environment, baseIndentation + 2); } if (service.cpuCount) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpu_count', service.cpuCount, baseIndentation + 1); } if (service.cpuPercent) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpu_percent', service.cpuPercent, baseIndentation + 1); } if (service.cpuShares) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpu_shares', service.cpuShares, baseIndentation + 1); } if (service.cpuPeriod) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpu_period', service.cpuPeriod, baseIndentation + 1); } if (service.cpuQuota) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpu_quota', service.cpuQuota, baseIndentation + 1); } if (service.cpuRtRuntime) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpu_rt_runtime', service.cpuRtRuntime, baseIndentation + 1); } if (service.cpuRtPeriod) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpu_rt_period', service.cpuRtPeriod, baseIndentation + 1); } if (service.cpuSet) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpuset', service.cpuSet, baseIndentation + 1); } if (service.deviceReadBps) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('device_read_bps', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('path', service.deviceReadBps.path, baseIndentation + 2); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('rate', service.deviceReadBps.rate, baseIndentation + 2); } if (service.deviceWriteBps) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('device_write_bps', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('path', service.deviceWriteBps.path, baseIndentation + 2); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('rate', service.deviceWriteBps.rate, baseIndentation + 2); } if (service.deviceReadIops) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('device_read_iops', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('path', service.deviceReadIops.path, baseIndentation + 2); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('rate', service.deviceReadIops.rate, baseIndentation + 2); } if (service.deviceWriteIops) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('device_write_iops', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('path', service.deviceWriteIops.path, baseIndentation + 2); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('rate', service.deviceWriteIops.rate, baseIndentation + 2); } if (service.weightDevice) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('weight_device', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('path', service.weightDevice.path, baseIndentation + 2); serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('weight', service.weightDevice.weight, baseIndentation + 2); } if (service.weight) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('weight', service.weight, baseIndentation + 1); } if (service.capAdd) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cap_add', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileList)(service.capAdd, baseIndentation + 2); } if (service.securityOpt && service.securityOpt) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('security_opt', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileList)(service.securityOpt.map(entry => `${entry.key}:${entry.value}`), baseIndentation + 2); } if (service.capDrop) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cap_drop', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileList)(service.capDrop, baseIndentation + 2); } if (service.deploy) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('deploy', '', baseIndentation + 1); if (service.deploy.mode) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('mode', service.deploy.mode, baseIndentation + 2); } if (service.deploy.labels && Object.keys(service.deploy.labels).length > 0) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('labels', '', baseIndentation + 2); serviceTextBlock += (0, compilerUtils_1.compileObject)(service.deploy.labels, baseIndentation + 3); } if (service.deploy.resources) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('resources', '', baseIndentation + 2); if (service.deploy.resources.limits) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('limits', '', baseIndentation + 3); if (service.deploy.resources.limits.cpus) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpus', service.deploy.resources.limits.cpus, baseIndentation + 4); } if (service.deploy.resources.limits.memory) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('memory', service.deploy.resources.limits.memory, baseIndentation + 4); } if (service.deploy.resources.limits.pids) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('pids', service.deploy.resources.limits.pids, baseIndentation + 4); } } if (service.deploy.resources.reservations) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('reservations', '', baseIndentation + 3); if (service.deploy.resources.reservations.cpus) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cpus', service.deploy.resources.reservations.cpus, baseIndentation + 4); } if (service.deploy.resources.reservations.memory) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('memory', service.deploy.resources.reservations.memory, baseIndentation + 4); } } } } if (service.capDrop) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cap_drop', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileList)(service.capDrop, baseIndentation + 2); } if (service.cgroupParent) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('cgroup_parent', service.cgroupParent, baseIndentation + 1); } if (service.memReservation) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('mem_reservation', service.memReservation, baseIndentation + 1); } if (service.memLimit) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('mem_limit', service.memLimit, baseIndentation + 1); } if (service.command) { if (typeof service.command === 'string') { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('command', service.command, baseIndentation + 1, { noQuotes: true, }); } else if (Array.isArray(service.command)) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('command', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileList)(service.command, baseIndentation + 2); } } if (service.healthCheck) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('healthcheck', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileObject)(service.healthCheck, baseIndentation + 2); } if (service.containerName) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('container_name', service.containerName, baseIndentation + 1); } if (service.entryPoint) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('entrypoint', service.entryPoint, baseIndentation + 1); } if (service.networks && service.networks.length > 0) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('networks', '', baseIndentation + 1); service.networks.forEach(network => { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)(network.id, '', baseIndentation + 2); }); } if (service.dependsOn && service.dependsOn.length > 0) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('depends_on', '', baseIndentation + 1); serviceTextBlock += (0, compilerUtils_1.compileObjectListWithId)(service.dependsOn, baseIndentation + 2); } if (service.volumes && service.volumes.length > 0) { serviceTextBlock += (0, compilerUtils_1.compileKeyValuePair)('volumes', '', baseIndentation + 1); serviceTextBlock += compileServiceVolumes(service.volumes, baseIndentation + 2); } return serviceTextBlock; }; const compileServices = (composition) => { const baseIndentation = 0; let servicesTextBlock = (0, compilerUtils_1.compileKeyValuePair)('services', '', baseIndentation); composition.services.forEach(service => { servicesTextBlock += compileService(service); }); return servicesTextBlock; }; exports.compileServices = compileServices;