@storm-software/k8s-tools
Version:
Tools for managing Kubernetes (k8s) infrastructure within a Nx workspace.
235 lines (218 loc) • 8.64 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
var _chunkXJT7AOEUjs = require('./chunk-XJT7AOEU.js');
var _chunkHFE7ETH4js = require('./chunk-HFE7ETH4.js');
var _chunkHEVP2Q77js = require('./chunk-HEVP2Q77.js');
// src/executors/container-publish/executor.ts
var _devkit = require('@nx/devkit');
// ../workspace-tools/src/utils/apply-workspace-tokens.ts
var applyWorkspaceExecutorTokens = async (option, tokenizerOptions) => {
let result = option;
if (!result) {
return result;
}
let projectName;
let projectRoot;
let sourceRoot;
if (_optionalChain([tokenizerOptions, 'optionalAccess', _ => _.projectName])) {
const context = tokenizerOptions;
projectName = context.projectName;
projectRoot = context.root;
sourceRoot = context.sourceRoot;
} else {
const projectConfig = tokenizerOptions;
projectRoot = projectConfig.root;
if (projectConfig.name) {
projectName = projectConfig.name;
}
if (projectConfig.sourceRoot) {
sourceRoot = projectConfig.sourceRoot;
}
}
if (tokenizerOptions.config) {
const configKeys = Object.keys(tokenizerOptions.config);
if (configKeys.some((configKey) => result.includes(`{${configKey}}`))) {
for (const configKey of configKeys) {
if (result.includes(`{${configKey}}`)) {
result = result.replaceAll(
`{${configKey}}`,
tokenizerOptions.config[configKey]
);
}
}
}
}
if (result.includes("{projectName}")) {
result = result.replaceAll("{projectName}", projectName);
}
if (result.includes("{projectRoot}")) {
result = result.replaceAll("{projectRoot}", projectRoot);
}
if (result.includes("{sourceRoot}")) {
result = result.replaceAll("{sourceRoot}", sourceRoot);
}
if (result.includes("{workspaceRoot}")) {
result = result.replaceAll(
"{workspaceRoot}",
_nullishCoalesce(tokenizerOptions.workspaceRoot, () => ( _chunkHEVP2Q77js.findWorkspaceRoot.call(void 0, )))
);
}
return result;
};
// src/executors/container-publish/executor.ts
var _https = require('https'); var _https2 = _interopRequireDefault(_https);
async function* publishExecutor(options, context) {
const isDryRun = process.env.NX_DRY_RUN === "true" || options.dryRun || false;
if (!context.projectName) {
throw new Error("The executor requires a projectName.");
}
console.info(
`\u{1F680} Running Storm Container Registry Publish executor on the ${context.projectName} crate`
);
const workspaceRoot = _chunkHEVP2Q77js.findWorkspaceRoot.call(void 0, );
const config = await _chunkHEVP2Q77js.getConfig.call(void 0, workspaceRoot);
const projectConfig = _optionalChain([context, 'access', _2 => _2.projectsConfigurations, 'optionalAccess', _3 => _3.projects, 'access', _4 => _4[context.projectName]]);
if (!projectConfig) {
throw new Error(
`The executor requires a valid projectsConfiguration - No configuration found for project ${context.projectName}`
);
}
const projectRoot = _nullishCoalesce(_optionalChain([projectConfig, 'optionalAccess', _5 => _5.root]), () => ( workspaceRoot));
const sourceRoot = _nullishCoalesce(_optionalChain([projectConfig, 'optionalAccess', _6 => _6.sourceRoot]), () => ( workspaceRoot));
const projectName = _nullishCoalesce(_optionalChain([projectConfig, 'optionalAccess', _7 => _7.name]), () => ( context.projectName));
config.workspaceRoot = workspaceRoot;
const tokenized = await _chunkHFE7ETH4js.applyWorkspaceTokens.call(void 0,
options,
{
config,
workspaceRoot,
projectRoot,
sourceRoot,
projectName,
...projectConfig
},
applyWorkspaceExecutorTokens
);
tokenized.engine ??= "docker";
tokenized.registry ??= config.registry.container;
try {
if (isDryRun) {
console.log(
`Would publish to ${tokenized.registry}, but [dry-run] was set`
);
} else {
console.log(`Published to ${tokenized.registry}`);
const packageManager = _chunkXJT7AOEUjs.getPackageInfo.call(void 0, projectConfig);
if (packageManager) {
tokenized["build-args"] ??= [
"ENVIRONMENT=production",
"DEBUG_IMAGE=false"
];
tokenized["labels"] ??= [];
let version = "";
if (process.env.TAG) {
version = process.env.TAG;
} else {
if (packageManager.type === "Cargo.toml") {
version = packageManager.content.package.version;
} else if (packageManager.type === "package.json") {
version = packageManager.content.version;
}
}
tokenized["build-args"].push(`RELEASE=${version}`);
tokenized["labels"].push(`org.opencontainers.image.version=${version}`);
const tags = await getRegistryVersion(projectName, config);
if (tags.length === 0) {
tokenized["labels"].push(
`org.opencontainers.image.created=${(/* @__PURE__ */ new Date()).toISOString()}`
);
} else if (tags.includes(version)) {
console.warn(
`Skipped package "${projectName}" because v${version} already exists in ${tokenized.registry}`
);
return {
success: true
};
}
} else {
console.warn(
`No package manager found for project "${projectName}" - Skipping container publishing`
);
return {
success: true
};
}
const { project, target, configuration } = _devkit.parseTargetString.call(void 0,
"container",
context
);
for await (const output of await _devkit.runExecutor.call(void 0, { project, target, configuration }, tokenized, context)) {
if (!output.success) {
throw new Error("Could not compile application files");
}
yield;
}
}
return {
success: true
};
} catch (error) {
console.error(`Failed to publish to ${tokenized.registry}`);
console.error(error);
console.log("");
return {
success: false
};
}
}
var getRegistryVersion = (name, config) => {
if (!name) {
throw new Error(
"The `getRegistryVersion` function requires a container name."
);
}
try {
const tagsApiUrl = `${config.registry.container}/v2/namespaces/${encodeURIComponent(config.namespace ? config.namespace : "storm-software")}/repositories/${encodeURIComponent(
name.replace(`${config.namespace}-`, "")
)}/tags`;
console.log(`Checking for existing version at ${tagsApiUrl}`);
return new Promise(
(resolve, reject) => _https2.default.get(tagsApiUrl, (res) => {
if (res.statusCode === 404) {
console.log(`No existing version found at ${tagsApiUrl}`);
return resolve([]);
}
res.on("data", (data) => {
if (data) {
console.log(
`Existing versions found at ${tagsApiUrl} - ${data}`
);
const json = JSON.parse(data.toString());
return resolve(
json.results.filter(
(result) => result.status === "active" && result.name && result.name !== "latest"
).map((result) => result.name)
);
}
return reject(
new Error(
"No data returned from container registry, expected a 404 if no tags exist"
)
);
});
}).on("error", (e) => {
throw e;
})
);
} catch (error) {
console.error(`Failed to get version from ${config.registry.container}`);
console.error(error);
console.log("");
throw new Error(
`Could not get version from container registry - ${config.registry.container}`,
{
cause: error
}
);
}
};
exports.publishExecutor = publishExecutor; exports.getRegistryVersion = getRegistryVersion;