@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
80 lines (79 loc) • 3.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchDeployment = exports.fetchDeploymentFromContent = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const fs_1 = __importDefault(require("fs"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const path_1 = __importDefault(require("path"));
const fetchDeploymentFromContent = (content) => {
let domains = [];
let deployContent = content;
let deploymentData = js_yaml_1.default.loadAll(deployContent);
// console.log("deploymentData :>> ", deploymentData);
let IMAGE_NAME = "", BUILD_TAG = "", APP_NAME = "", REPLICAS = 1, ENV_VARS = [], NAMESPACE = "", SERVICE_NAME = "", INGRESS_NAME = "", PORT;
// End point of the application:
let endpoint = "";
deploymentData.map((doc) => {
if (doc && doc.kind == "Namespace")
NAMESPACE = doc.metadata.name;
if (doc && doc.kind == "Service")
SERVICE_NAME = doc.metadata.name;
if (doc && doc.kind == "Ingress") {
INGRESS_NAME = doc.metadata.name;
const protocol = typeof doc.spec.tls != "undefined" ? "https" : "http";
endpoint += protocol + "://" + doc.spec.rules[0].host + doc.spec.rules[0].http.paths[0].path;
doc.spec.rules.map((rule) => domains.push(rule.host));
}
if (doc && doc.kind == "Deployment") {
const deploy = doc;
PORT = deploy.spec.template.spec.containers[0].ports[0].containerPort;
IMAGE_NAME = doc.spec.template.spec.containers[0].image;
BUILD_TAG = IMAGE_NAME.split(":")[1];
APP_NAME = doc.metadata.name;
NAMESPACE = doc.metadata.namespace;
REPLICAS = doc.spec.replicas;
ENV_VARS = doc.spec.template.spec.containers[0].env;
}
});
// get build number:
BUILD_TAG = IMAGE_NAME.split(":")[1];
// console.log("BUILD_NUMBER :>> ", BUILD_NUMBER);
return {
domains,
endpoint,
deployContent,
deployYaml: deployContent,
NAMESPACE,
SERVICE_NAME,
INGRESS_NAME,
IMAGE_NAME,
ENV_VARS,
APP_NAME,
REPLICAS,
PORT,
BUILD_TAG,
};
};
exports.fetchDeploymentFromContent = fetchDeploymentFromContent;
/**
* @param {InputOptions} options
*/
function fetchDeployment(filePath, options = { env: "dev" }) {
const appDirectory = options.targetDirectory;
const { env } = options;
// current deployment file:
const deployFile = `deployment/deployment.${env}.yaml`;
const DEPLOYMENT_FILE = filePath ? filePath : path_1.default.resolve(appDirectory, deployFile);
if (!fs_1.default.existsSync(DEPLOYMENT_FILE)) {
const msg = `Không tìm thấy "${deployFile}", chạy "diginext deploy generate --env=${env}" để khởi tạo.`;
(0, log_1.logError)(msg);
throw new Error(msg);
}
let deployContent = fs_1.default.readFileSync(DEPLOYMENT_FILE, "utf8");
// Thay thế IMAGE_NAME vào deployment YAML:
return (0, exports.fetchDeploymentFromContent)(deployContent);
}
exports.fetchDeployment = fetchDeployment;