@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
154 lines (153 loc) • 6.26 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stopBuild = exports.build = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const lodash_1 = require("lodash");
const config_1 = require("../../config/config");
const plugins_1 = require("../../plugins");
const docker_1 = require("./docker");
const processes = {};
/**
* Build & push image using Podman
* @param imageName Image name = "image_url:tag"
* @returns Image URL of the build
*/
const build = async (imageName, options) => {
const { dockerFile, buildDirectory,
// driver = "docker-container",
builder, cacheFroms, args, platforms, shouldPush = false, onBuilding, onError, } = options;
/**
* @example
* docker buildx build -f Dockerfile --push -t asia.gcr.io/top-group-k8s/test-cli/front-end:2022-12-26-23-20-07 --cache-from type=registry,ref=asia.gcr.io/top-group-k8s/test-cli/front-end:2022-12-26-23-20-07 .
**/
const argsFlags = !(0, lodash_1.isEmpty)(args)
? args.map(({ name, value }) => {
if (name.indexOf(" ") > -1)
throw new Error(`Name of an argument in "--build-arg" SHOULD NOT contains spacing.`);
if (value.indexOf(" ") > -1)
throw new Error(`Value of an argument in "--build-arg" SHOULD NOT contains spacing.`);
return `--build-arg ${name}=${value}`;
})
: [];
// const buildContextNameFlag = !isEmpty(builder) ? `--name ${builder}` : "";
const platformFlag = !(0, lodash_1.isEmpty)(platforms) && `--arch=${platforms.map((p) => p.split("/")[1]).join(",")}`;
// await execCmd(
// `docker buildx create ${platformFlag} --driver ${driver} ${buildContextNameFlag}`,
// "Docker build context instance was existed, no worries, just ignoring this message."
// );
// latestBuild ? ` --cache-from type=registry,ref=${latestBuild.image}` : "";
/**
* --cache-from: repository must contain neither a tag nor digest
*/
const cacheFlags = !(0, lodash_1.isEmpty)(cacheFroms)
? cacheFroms.map((cache) => {
let cacheURL = cache.value.indexOf(":") > -1 ? cache.value.split(":")[0] : cache.value;
return `--cache-from ${cacheURL}`;
})
: [];
/**
* ulimit flag
* - https://manpages.ubuntu.com/manpages/lunar/man1/podman-build.1.html
*/
const ulimitFlag = `--ulimit "nofile=65535:65535"`;
/**
* Path to "Dockerfile" flag
*/
const dockerFileFlag = `-f ${dockerFile}`;
/**
* Image tag
*/
const tagFlag = `-t ${imageName}`;
/**
* Context directory
*/
const directoryFlag = buildDirectory !== null && buildDirectory !== void 0 ? buildDirectory : ".";
// all build options tags
const optionFlags = [ulimitFlag, ...argsFlags, platformFlag, dockerFileFlag, tagFlag, ...cacheFlags, directoryFlag]
.filter((opt) => typeof opt !== "undefined") // <-- filter empty flags
.join(" ");
// docker build command:
const buildCmd = `podman build ${optionFlags}`;
try {
const { execa, execaCommand, execaSync } = await Promise.resolve().then(() => __importStar(require("execa")));
let stream = execaCommand(buildCmd, config_1.cliOpts);
processes[builder] = stream;
const skippedErrors = ["User-selected graph driver"];
stream.stdio.forEach((_stdio) => {
if (_stdio) {
_stdio.on("data", (data) => {
let logMsg = data.toString();
for (const skippedErr of skippedErrors) {
if (logMsg.indexOf(skippedErr) > -1)
logMsg = "";
}
if (onBuilding && logMsg)
onBuilding(logMsg);
});
}
});
await stream;
if (shouldPush) {
stream = execaCommand(`podman push ${imageName}`, config_1.cliOpts);
processes[builder] = stream;
stream.stdio.forEach((_stdio) => {
if (_stdio)
_stdio.on("data", (data) => {
if (onBuilding)
onBuilding(data.toString());
});
});
await stream;
}
return imageName;
}
catch (e) {
throw new docker_1.BuildContainerError({ imageName }, "An error occurred while building this image with Podman.");
}
};
exports.build = build;
/**
* Stop the build
* @returns Image URL of the build
*/
const stopBuild = async (builder) => {
var _a;
try {
(_a = processes[builder]) === null || _a === void 0 ? void 0 : _a.kill("SIGTERM", {
forceKillAfterTimeout: 2000,
});
delete processes[builder];
// await execaCommand(`docker buildx stop ${builder}`, cliOpts);
// await execaCommand(`docker buildx stop buildx_buildkit_${builder}`, cliOpts);
await (0, plugins_1.wait)(500); // <-- just to be sure...
}
catch (e) {
(0, log_1.logError)(`[BUILDER] Podman > stopBuild :>>`, e);
return false;
}
return true;
};
exports.stopBuild = stopBuild;