UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

207 lines 7.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertToMilicore = convertToMilicore; exports.convertToMebibytes = convertToMebibytes; exports.convertToEnvironment = convertToEnvironment; exports.portsFromRange = portsFromRange; exports.convertToPorts = convertToPorts; exports.envFileToEnv = envFileToEnv; exports.mergeEnv = mergeEnv; exports.durationToSeconds = durationToSeconds; exports.healthCheckCommandToCplnCommand = healthCheckCommandToCplnCommand; exports.getFilenameFromPath = getFilenameFromPath; exports.createSecretForBindMount = createSecretForBindMount; exports.getResourcesFromCompose = getResourcesFromCompose; const objects_1 = require("../util/objects"); const secret_1 = require("./compose-resources/secret"); const fs_1 = require("fs"); const network_1 = require("./compose-resources/network"); const service_1 = require("./compose-resources/service"); const volume_1 = require("./compose-resources/volume"); function convertToMilicore(cpu) { return Number(cpu) * 1000; } function convertToMebibytes(memory) { let number = Number(memory.replace(/(^\d+)(.+$)/i, '$1')); // Memory in bytes if (memory.endsWith('b') || memory.endsWith('B')) { return Math.ceil(number / 1000000); } // Memory in kilobytes if (memory.endsWith('k') || memory.endsWith('K') || memory.endsWith('kb') || memory.endsWith('KB')) { return Math.ceil(number / 1000); } // Memory in megabytes if (memory.endsWith('m') || memory.endsWith('M') || memory.endsWith('mb') || memory.endsWith('MB')) { return Math.ceil(number * 1.05); } // Memory in gigibytes if (memory.endsWith('g') || memory.endsWith('G') || memory.endsWith('gb') || memory.endsWith('GB')) { return Math.ceil(number * 1000); } return number; } function convertToEnvironment(environment) { if (Array.isArray(environment)) { return (0, objects_1.toEnv)(environment); } return Object.entries(environment).map(([name, value]) => ({ name, value: String(value) })); } function portsFromRange(portRange) { if (!portRange.includes('-')) { return [Number(portRange)]; } const [start, end] = portRange.split('-'); return Array.from({ length: +end - +start + 1 }, (value, index) => +start + +index); } function convertToPorts(portStrings, availableProtocols) { const cplnPorts = []; const portsObject = {}; // Fill in ports object. Ensures no duplicate ports portStrings.forEach((p) => { let ports = []; const [portString, composeProtocol] = p.split('/'); const portsInfo = portString.split(':'); if (portsInfo.length === 1) ports = portsFromRange(portsInfo[0]); else if (portsInfo.length === 2) { ports = portsFromRange(portsInfo[1]); } else { ports = portsFromRange(portsInfo[2]); } const protocol = availableProtocols.includes(composeProtocol) ? composeProtocol : undefined; for (const port of ports) { portsObject[port] = protocol; } }); // converts ports object to cpln object for (const [port, protocol] of Object.entries(portsObject)) { cplnPorts.push({ number: +port, protocol }); } return cplnPorts; } /** * Reads a .env file from disk and parses it into {name, value} entries. * - Automatically strips UTF-8 BOM if present. * * @param {string} filedir - Absolute or relative path to the .env file to read. * @returns {EnvEntry[]} - Parsed environment entries. * @throws {Error} - {Error} If the file cannot be read. */ function envFileToEnv(filedir) { // Read the file from disk as UTF-8 text const raw = (0, fs_1.readFileSync)(filedir, 'utf8'); // Determine whether the file starts with a UTF-8 BOM const hasBOM = raw.charCodeAt(0) === 0xfeff; // Remove the BOM if present; otherwise use the original content const content = hasBOM ? raw.slice(1) : raw; // Split the file content into physical lines supporting CRLF and LF const lines = content.split(/\r?\n/); // Delegate to the main line-based parser and capture the result const result = (0, objects_1.toEnv)(lines); // Return the parsed entries return result; } // merges env2 into env1, function mergeEnv(env1, env2) { for (const variable of env2) { // Adding variable only if it doesnt exist already if (env1.findIndex((v) => v.name === variable.name) === -1) { env1.push(variable); } } } function durationToSeconds(duration) { if (duration === undefined) { return undefined; } let timeInSeconds = 0; if (duration.includes('h')) { let [hours, rest] = duration.split('h'); timeInSeconds += Number(hours) * 60 * 60; duration = rest; } if (duration.includes('m')) { let [minutes, rest] = duration.split('m'); timeInSeconds += Number(minutes) * 60; duration = rest; } if (duration.includes('s')) { let [seconds, rest] = duration.split('s'); timeInSeconds += Number(seconds); duration = rest; } return timeInSeconds; } function healthCheckCommandToCplnCommand(command) { if (command === undefined) { return undefined; } if (typeof command === 'string') { return ['/bin/sh', '-c', command]; } else if (command.length < 2) { throw new Error('Health check command invalid'); } else { if (command[0] === 'CMD') { return command.slice(1); } else { // command[0] === 'CMD-SHELL' // No other way to use default shell, so I use /bin/sh return ['/bin/sh', '-c', command[1]]; } } } function getFilenameFromPath(filePath, includeExtension = false) { const paths = filePath.split('/'); const fileName = paths[paths.length - 1]; if (!includeExtension) { // Return file name without extension return fileName.split('.')[0]; } return fileName; } function createSecretForBindMount(composePath, filePath, secrets, context) { let secret; if (secrets.every((s) => s.body.file !== filePath)) { secret = new secret_1.default(getFilenameFromPath(filePath), { file: filePath }, context, composePath); secrets.push(secret); } else { secret = secrets.find((s) => s.body.file == filePath); } return secret; } function getResourcesFromCompose(composeObject, composePath, context, registry, profile) { var _a, _b, _c, _d, _e; const networks = []; const volumes = []; const secrets = []; const services = []; // Object.entries(composeObject.{key}) returns [name, body] for (const network of Object.entries((_a = composeObject.networks) !== null && _a !== void 0 ? _a : {})) { networks.push(new network_1.default(network[0])); } for (const volume of Object.entries((_b = composeObject.volumes) !== null && _b !== void 0 ? _b : {})) { volumes.push(new volume_1.default(volume[0], 'volumeset', volume[1], context, composePath)); } for (const secret of Object.entries((_c = composeObject.secrets) !== null && _c !== void 0 ? _c : {})) { secrets.push(new secret_1.default(secret[0], secret[1], context, composePath)); } for (const config of Object.entries((_d = composeObject.configs) !== null && _d !== void 0 ? _d : {})) { secrets.push(new secret_1.default(config[0], config[1], context, composePath)); } for (const service of Object.entries((_e = composeObject.services) !== null && _e !== void 0 ? _e : {})) { services.push(new service_1.default(service[0], service[1], context, networks, volumes, secrets, composeObject, composePath, registry, profile)); } return { networks, volumes, secrets, services, }; } //# sourceMappingURL=util.js.map