UNPKG

@veecode-platform/safira-cli

Version:

Generate a microservice project from your spec.

119 lines (118 loc) 5.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.kubernetesSetIngressHostFromVKPR = exports.kubernetesGetJsonPathAsString = exports.KubernetesCreateMockServerSecret = exports.KubernetesListAllPods = exports.KubernetesDeleteString = exports.KubernetesDelete = exports.KubernetesApplyString = exports.KubernetesApply = void 0; const kubectl_1 = require("./kubectl"); const uuid_1 = require("uuid"); const yaml_utils_1 = require("../utils/yaml-utils"); const mockserver_constants_1 = require("../mockserver/mockserver-constants"); const command_execute_1 = require("../command/command-execute"); class KubernetesApply { constructor() { } async command(filePaths) { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); const commandList = filePaths.map(str => command_execute_1.CommandExecute.instance.exec(`${KubectlBin} apply -f ${str.replace(/ /g, "\\ ")}`, false)); return Promise.all(commandList); } static get instance() { if (!this._instance) { this._instance = new this(); } return this._instance; } } exports.KubernetesApply = KubernetesApply; class KubernetesApplyString { constructor() { } async command(commands) { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); const commandList = commands.map(str => command_execute_1.CommandExecute.instance.exec(`echo "${str}" | ${KubectlBin} apply -f -`)); return Promise.all(commandList); } static get instance() { if (!this._instance) { this._instance = new this(); } return this._instance; } } exports.KubernetesApplyString = KubernetesApplyString; class KubernetesDelete { constructor() { } async command(commands, ignoreNotfound = false) { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); const commandList = commands.map(str => command_execute_1.CommandExecute.instance.exec(`${KubectlBin} apply -f ${str.replace(/ /g, "\\ ")} ${ignoreNotfound ? "--ignore-not-found=true" : ""}`)); return Promise.all(commandList); } async deleteResourceLabeled(kinds, labelKeyValue, namespace, ignoreNotfound = false) { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); await command_execute_1.CommandExecute.instance.exec(`${KubectlBin} delete ${kinds?.join(",")} -n ${namespace} -l ${labelKeyValue} ${ignoreNotfound ? "--ignore-not-found=true" : ""}`) .catch(error => error); } static get instance() { if (!this._instance) { this._instance = new this(); } return this._instance; } } exports.KubernetesDelete = KubernetesDelete; class KubernetesDeleteString { constructor() { } async command(commands, ignoreNotfound = false) { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); const commandList = commands.map(str => command_execute_1.CommandExecute.instance.exec(`echo "${str}" | ${KubectlBin} delete ${ignoreNotfound ? "--ignore-not-found=true" : ""} -f -`)); return Promise.all(commandList); } static get instance() { if (!this._instance) { this._instance = new this(); } return this._instance; } } exports.KubernetesDeleteString = KubernetesDeleteString; class KubernetesListAllPods { constructor() { } async command() { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); return command_execute_1.CommandExecute.instance.exec(`${KubectlBin} get pods --all-namespaces -o json`) .then(value => value ? JSON.parse(value) : ""); } static get instance() { if (!this._instance) { this._instance = new this(); } return this._instance; } } exports.KubernetesListAllPods = KubernetesListAllPods; class KubernetesCreateMockServerSecret { constructor() { } async createSecret(ignoreNotfound = false) { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); const uuid = (0, uuid_1.v4)(); await command_execute_1.CommandExecute.instance.exec(`${KubectlBin} delete secret mockserver-admin-apikey -n vkpr ${ignoreNotfound ? "--ignore-not-found=true" : ""}`); await command_execute_1.CommandExecute.instance.exec(`${KubectlBin} create secret generic mockserver-admin-apikey --from-literal=kongCredType=key-auth --from-literal=key=${uuid} -n vkpr `); console.log(`****YOUR MOCKSERVER APIKEY IS: ${uuid} (****You must save the apikey!)`); console.log("*******the apikey can not be created again!"); } static get instance() { if (!this._instance) { this._instance = new this(); } return this._instance; } } exports.KubernetesCreateMockServerSecret = KubernetesCreateMockServerSecret; async function kubernetesGetJsonPathAsString(kind, name, namespace, configToFind) { const KubectlBin = await new kubectl_1.Kubectl().getKubectl(); return command_execute_1.CommandExecute.instance.exec(`${KubectlBin} get ${kind} ${name} -n ${namespace} -o jsonpath='{ ${configToFind} }'`); } exports.kubernetesGetJsonPathAsString = kubernetesGetJsonPathAsString; async function kubernetesSetIngressHostFromVKPR() { const objectYaml = yaml_utils_1.YamlUtils.parse(mockserver_constants_1.mockserverIngressSecureCpanel); objectYaml.spec.rules[0].host = await kubernetesGetJsonPathAsString("ingress", "mockserver", "vkpr", ".spec.rules[].host"); const yamlStringData = yaml_utils_1.YamlUtils.stringify(objectYaml); return yamlStringData; } exports.kubernetesSetIngressHostFromVKPR = kubernetesSetIngressHostFromVKPR;