@deploystack/docker-to-iac
Version:
Translate docker run and docker compose file to Infrastructure as Code
132 lines (131 loc) • 5.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processEnvironmentVariablesGeneration = processEnvironmentVariablesGeneration;
const semver_1 = __importDefault(require("semver"));
function generateValue(config) {
switch (config.type) {
case 'password':
return generatePassword(config.length || 16, config.pattern);
case 'string':
return generateString(config.length || 8, config.pattern);
case 'number':
return generateNumber(config.min, config.max).toString();
default:
throw new Error(`Unsupported generation type: ${config.type}`);
}
}
function generatePassword(length, pattern) {
const charset = {
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
lowercase: 'abcdefghijklmnopqrstuvwxyz',
numbers: '0123456789',
special: '!@#$%^&*'
};
if (pattern) {
// TODO: Implement custom pattern matching
return generateString(length, pattern);
}
// Default strong password pattern
const allChars = charset.uppercase + charset.lowercase + charset.numbers + charset.special;
let password = '';
// Ensure at least one of each type
password += charset.uppercase[Math.floor(Math.random() * charset.uppercase.length)];
password += charset.lowercase[Math.floor(Math.random() * charset.lowercase.length)];
password += charset.numbers[Math.floor(Math.random() * charset.numbers.length)];
password += charset.special[Math.floor(Math.random() * charset.special.length)];
// Fill the rest randomly
for (let i = password.length; i < length; i++) {
password += allChars[Math.floor(Math.random() * allChars.length)];
}
// Shuffle the password
return password.split('').sort(() => Math.random() - 0.5).join('');
}
function generateString(length, pattern = 'default') {
const charsets = {
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
lowercase: 'abcdefghijklmnopqrstuvwxyz',
default: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
};
const selectedCharset = charsets[pattern] || charsets.default;
let result = '';
for (let i = 0; i < length; i++) {
result += selectedCharset[Math.floor(Math.random() * selectedCharset.length)];
}
return result;
}
function generateNumber(min, max) {
min = min ?? 1;
max = max ?? 1000000;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function findMatchingVersion(versions, targetVersion) {
// Handle undefined or 'latest' tag
if (!targetVersion || targetVersion === 'latest') {
// Find version that matches '*' or 'latest' first
const wildcardVersion = versions.find(v => v === '*' || v === 'latest');
if (wildcardVersion) {
return wildcardVersion;
}
// If no wildcard/latest version specified, take the highest version number
const numericVersions = versions.filter(v => semver_1.default.valid(semver_1.default.coerce(v)));
if (numericVersions.length > 0) {
return numericVersions.sort((a, b) => semver_1.default.rcompare(semver_1.default.coerce(a) || '0.0.0', semver_1.default.coerce(b) || '0.0.0'))[0];
}
return null;
}
// For specific versions, continue with existing logic
const sortedVersions = versions.sort((a, b) => semver_1.default.rcompare(semver_1.default.coerce(a) || '0.0.0', semver_1.default.coerce(b) || '0.0.0'));
for (const version of sortedVersions) {
// Handle '*' or 'latest' in version spec
if (version === '*' || version === 'latest') {
return version;
}
try {
if (semver_1.default.satisfies(semver_1.default.coerce(targetVersion) || '0.0.0', version)) {
return version;
}
}
catch (e) {
console.warn(`Invalid semver range: ${version} - ${e}`);
continue;
}
}
return null;
}
function processEnvironmentVariablesGeneration(environment, image, config) {
if (!config) {
return environment;
}
const result = { ...environment };
// Try different repository name formats
const possibleRepoNames = [
image.repository,
`library/${image.repository}`,
`docker.io/library/${image.repository}`,
`docker.io/${image.repository}`
];
let imageConfig;
for (const repoName of possibleRepoNames) {
if (config[repoName]) {
imageConfig = config[repoName];
break;
}
}
if (!imageConfig) {
return result;
}
const versions = Object.keys(imageConfig.versions);
const matchingVersion = findMatchingVersion(versions, image.tag || 'latest');
if (!matchingVersion) {
return result;
}
const versionConfig = imageConfig.versions[matchingVersion];
// Process each environment variable that has a generation rule
for (const [envKey, genConfig] of Object.entries(versionConfig.environment)) {
result[envKey] = generateValue(genConfig);
}
return result;
}