UNPKG

@catladder/cli

Version:

Panter cli tool for cloud CI/CD and DevOps

97 lines (96 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.commandTriggerCronjobProject = exports.commandTriggerCronjobGeneral = void 0; const client_node_1 = require("@kubernetes/client-node"); const defineCommand_1 = require("../../../core/defineCommand"); const k8sApi_1 = require("../../../k8sApi"); const log_1 = require("../../../utils/log"); const projects_1 = require("../../../utils/projects"); const ensureCluster_1 = require("../../../apps/cli/commands/project/utils/ensureCluster"); const autocompletions_1 = require("../../../apps/cli/commands/project/utils/autocompletions"); const availability_1 = require("../../availability"); const getCronjobChoices = async namespace => { const { body: { items: jobs } } = await (0, k8sApi_1.getk8sApiBatchBeta)().listNamespacedCronJob(namespace); return jobs.map(j => j.metadata.name); }; const runCronjob = async (io, namespace, jobName) => { const { body: { items: jobs } } = await (0, k8sApi_1.getk8sApiBatchBeta)().listNamespacedCronJob(namespace); const cronjob = jobs.find(j => j.metadata.name === jobName); const jobSpec = cronjob.spec.jobTemplate.spec; const job = new client_node_1.V1Job(); const metadata = { name: `manual-${Math.round(Date.now() / 1000)}-${cronjob.metadata.name}` }; job.metadata = metadata; job.spec = jobSpec; try { const result = await (0, k8sApi_1.getk8sApiBatch)().createNamespacedJob(namespace, job); io.log(""); io.log(`yeah, you got a job, man. 😺 ${result.body.metadata.name}`); io.log(""); } catch (e) { (0, log_1.logError)(io, "command failed", e.body); } }; exports.commandTriggerCronjobGeneral = (0, defineCommand_1.defineCommand)({ name: "k8s trigger-cronjob", description: "trigger cronjob", group: "general", inputs: { namespace: { type: "string", message: "kubernetes namespace", positional: true }, jobName: { type: "string", message: "Which cronjob? 🤔", choices: async ctx => getCronjobChoices(await ctx.get("namespace")) } }, execute: async ctx => { const namespace = await ctx.get("namespace"); const jobName = await ctx.get("jobName"); await runCronjob(ctx, namespace, jobName); } }); exports.commandTriggerCronjobProject = (0, defineCommand_1.defineCommand)({ name: "project k8s trigger-cronjob", description: "trigger cronjob", group: "project", isAvailable: (0, availability_1.hasDeployType)("kubernetes"), inputs: { envComponent: { type: "string", message: "environment:component", positional: true, choices: async () => (0, autocompletions_1.envAndComponents)() }, jobName: { type: "string", message: "Which cronjob? 🤔", choices: async ctx => { const envComponent = await ctx.get("envComponent"); const namespace = await (0, projects_1.getProjectNamespace)(envComponent); return getCronjobChoices(namespace); } } }, execute: async ctx => { const envComponent = await ctx.get("envComponent"); await (0, ensureCluster_1.ensureCluster)(ctx, envComponent); const namespace = await (0, projects_1.getProjectNamespace)(envComponent); const jobName = await ctx.get("jobName"); await runCronjob(ctx, namespace, jobName); } });