@controlplane/cli
Version:
Control Plane Corporation CLI
209 lines • 7.74 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractHelmConfig = exports.extractHelmDefaultValues = exports.extractHelmCustomValues = exports.extractHelmChart = exports.getActiveHelmDeployment = exports.copyHelmResource = exports.getHelmStateName = exports.generateHelmTemplate = exports.validateHelmArgs = exports.processHelmArgs = void 0;
const path = require("path");
const jsyaml = require("js-yaml");
const child_process_1 = require("child_process");
const names_1 = require("../util/names");
const constants_1 = require("./constants");
const fs_1 = require("fs");
const io_1 = require("../util/io");
// Exported //
function processHelmArgs(args) {
// Generate a random name
if (args.generateName || !args.release) {
if (args.release && !args.chart) {
args.chart = args.release;
}
args.release = (0, names_1.nextName)();
}
// Use current directory for the chart location if it is not specified
if (!args.chart) {
args.chart = '.';
}
}
exports.processHelmArgs = processHelmArgs;
function validateHelmArgs(session, args) {
if (args.release && args.chart && args.generateName) {
session.abort({ message: 'ERROR: Cannot set --generate-name and also specify a release name' });
}
if (!session.context.org) {
session.abort({ message: 'ERROR: Must provide an org' });
}
}
exports.validateHelmArgs = validateHelmArgs;
function generateHelmTemplate(args, org, gvc, debug) {
let options = [];
const customOptions = [`--set cpln.org=${org}`, `--set globals.cpln.org=${org}`];
if (gvc) {
customOptions.push(`--set cpln.gvc=${gvc}`);
customOptions.push(`--set globals.cpln.gvc=${gvc}`);
}
// Handle helm template options
if (args.set && args.set.length > 0) {
if (typeof args.set === 'string') {
args.set = [args.set];
}
options.push(args.set.map((set) => `--set ${safeSet(set)}`).join(' '));
}
if (args.setString) {
if (typeof args.setString === 'string') {
args.setString = [args.setString];
}
options.push(args.setString.map((setString) => `--set-string ${safeSet(setString)}`).join(' '));
}
if (args.setFile) {
if (typeof args.setFile === 'string') {
args.setFile = [args.setFile];
}
options.push(args.setFile.map((setFile) => `--set-file ${safeSet(setFile)}`).join(' '));
}
if (args.description) {
options.push(`--description "${args.description}"`);
}
if (args.postRenderer) {
options.push(`--post-renderer ${args.postRenderer}`);
}
if (args.postRendererArgs && args.postRendererArgs.length > 0) {
if (typeof args.postRendererArgs === 'string') {
args.postRendererArgs = [args.postRendererArgs];
}
options.push(args.postRendererArgs.map((postRendererArgs) => `--post-renderer-args ${safeSet(postRendererArgs)}`).join(' '));
}
if (args.repo) {
options.push(`--repo ${args.repo}`);
}
if (args.values && args.values.length > 0) {
if (typeof args.values === 'string') {
args.values = [args.values];
}
options.push(args.values.map((values) => `--values ${values}`).join(' '));
}
if (args.verify) {
options.push(`--verify`);
}
if (debug) {
options.push(`--debug`);
}
const helmTemplateCommand = `helm template ${args.release} ${args.chart} ${options.join(' ')} ${customOptions.join(' ')}`;
return (0, child_process_1.execSync)(helmTemplateCommand).toString();
}
exports.generateHelmTemplate = generateHelmTemplate;
function getHelmStateName(release) {
return constants_1.CPLN_RELEASE_NAME_PREFIX + release;
}
exports.getHelmStateName = getHelmStateName;
function copyHelmResource(resource) {
return {
id: resource.id,
kind: resource.kind,
version: resource.version,
link: resource.link,
template: { ...resource.template },
};
}
exports.copyHelmResource = copyHelmResource;
function getActiveHelmDeployment(release) {
return release.deployments.find((deployment) => deployment.status === 'deployed');
}
exports.getActiveHelmDeployment = getActiveHelmDeployment;
function extractHelmChart(chartDirPath) {
const filePath = path.join(chartDirPath, 'Chart.yaml');
return (0, io_1.loadObject)((0, fs_1.readFileSync)(filePath, 'utf-8'));
}
exports.extractHelmChart = extractHelmChart;
function extractHelmCustomValues(pathsToValues) {
let values = [];
// Read and add custom values if specified
if (pathsToValues) {
if (typeof pathsToValues === 'string') {
pathsToValues = [pathsToValues];
}
// Iterate over each custom values path and add it
for (const path of pathsToValues) {
const customValues = (0, fs_1.readFileSync)(path, 'utf-8');
if (customValues) {
values.push(customValues.trim());
}
}
}
return values;
}
exports.extractHelmCustomValues = extractHelmCustomValues;
/**
* Get default chart values, typically values.yaml
* @param chartDirPath The path of the chart.
* @returns The default values of a chart.
*/
function extractHelmDefaultValues(chartDirPath) {
return jsyaml.load((0, child_process_1.execSync)(`helm show values ${chartDirPath}`).toString());
}
exports.extractHelmDefaultValues = extractHelmDefaultValues;
function extractHelmConfig(args, valuesFiles) {
let config = {};
// Apply "values"
for (const valuesFile of valuesFiles) {
Object.assign(config, jsyaml.safeLoad(valuesFile));
}
// Apply "set"
applySetProperty(config, args.set);
// Apply "set-string"
applySetProperty(config, args.setString);
// Apply "set-file"
applySetFileProperty(config, args.setFile);
return config;
}
exports.extractHelmConfig = extractHelmConfig;
// Local //
function safeSet(input) {
const index = input.indexOf('=');
if (index === -1) {
return input;
}
const key = input.substring(0, index);
const value = input.substring(index + 1);
return `"${key}"="${value}"`;
}
function applySetProperty(config, input) {
if (!input) {
return;
}
// Make sure we have an array
const entries = Array.isArray(input) ? input : [input];
// Iterate over each entry and update config
for (const entry of entries) {
const index = entry.indexOf('=');
// Skip entry if it doesn't have an equal sign, it will be caught later by helm template command
if (index === -1) {
continue;
}
const key = entry.substring(0, index);
const value = entry.substring(index + 1);
// Update helm config
config[key] = value;
}
}
function applySetFileProperty(config, input) {
if (!input) {
return;
}
// Make sure we have an array
const entries = Array.isArray(input) ? input : [input];
// Iterate over each entry and update config
for (const entry of entries) {
const index = entry.indexOf('=');
// Skip entry if it doesn't have an equal sign, it will be caught later by helm template command
if (index === -1) {
continue;
}
const key = entry.substring(0, index);
const value = entry.substring(index + 1);
// Skip if the path in value doesn't exists, it will be caught later by helm template command
if (!(0, fs_1.existsSync)(value)) {
continue;
}
// Update helm config
config[key] = (0, fs_1.readFileSync)(value, 'utf-8');
}
}
//# sourceMappingURL=functions.js.map
;