|
| 1 | +/* eslint-disable max-len */ |
| 2 | +const Command = require('../../Command'); |
| 3 | +const runnerRoot = require('../root/runner.cmd'); |
| 4 | +const inquirer = require('inquirer'); |
| 5 | +const { getAllKubeContexts, getKubeContext } = require('../../helpers/kubernetes'); |
| 6 | +const unInstallRuntime = require('../runtimeEnvironments/uninstall.cmd'); |
| 7 | +const unInstallAgent = require('../agent/uninstall.cmd'); |
| 8 | +const unInstallMonitor = require('../monitor/uninstall.cmd'); |
| 9 | +const colors = require('colors'); |
| 10 | +const DEFAULTS = require('../../defaults'); |
| 11 | +const sdk = require('../../../../logic/sdk'); |
| 12 | +const _ = require('lodash'); |
| 13 | + |
| 14 | +const defaultNamespace = 'codefresh'; |
| 15 | + |
| 16 | +const deleteCmd = new Command({ |
| 17 | + root: false, |
| 18 | + parent: runnerRoot, |
| 19 | + command: 'delete', |
| 20 | + requiresAuthentication: false, |
| 21 | + description: 'Deletes codefresh runner solution\'s components on kubernetes cluster', |
| 22 | + webDocs: { |
| 23 | + category: 'Runner', |
| 24 | + title: 'Delete', |
| 25 | + weight: 100, |
| 26 | + }, |
| 27 | + // requiresAuthentication: argv => argv && !argv.token, |
| 28 | + builder: yargs => yargs |
| 29 | + .env('CF_ARG_') // this means that every process.env.CF_ARG_* will be passed to argv |
| 30 | + .option('name', { |
| 31 | + describe: 'Agent\'s name to be deleted', |
| 32 | + }) |
| 33 | + .option('url', { |
| 34 | + describe: 'Codefresh system custom url', |
| 35 | + default: DEFAULTS.URL, |
| 36 | + }) |
| 37 | + .option('kube-context-name', { |
| 38 | + describe: 'Name of the kubernetes context on which venona should be installed [$CF_ARG_KUBE_CONTEXT_NAME]', |
| 39 | + }) |
| 40 | + .option('kube-namespace', { |
| 41 | + describe: 'Name of the namespace on which venona should be installed [$CF_ARG_KUBE_NAMESPACE]', |
| 42 | + }) |
| 43 | + .option('kube-config-path', { |
| 44 | + describe: 'Path to kubeconfig file (default is $HOME/.kube/config)', |
| 45 | + }) |
| 46 | + .option('verbose', { |
| 47 | + describe: 'Print logs', |
| 48 | + }), |
| 49 | + handler: async (argv) => { |
| 50 | + const { |
| 51 | + 'kube-config-path': kubeConfigPath, |
| 52 | + } = argv; |
| 53 | + let { |
| 54 | + 'kube-context-name': kubeContextName, |
| 55 | + 'kube-namespace': kubeNamespace, |
| 56 | + name: agentName, |
| 57 | + } = argv; |
| 58 | + |
| 59 | + const questions = []; |
| 60 | + if (!kubeContextName) { |
| 61 | + const contexts = getAllKubeContexts(kubeConfigPath); |
| 62 | + const currentKubeContext = getKubeContext(kubeConfigPath); |
| 63 | + |
| 64 | + questions.push({ |
| 65 | + type: 'list', |
| 66 | + name: 'context', |
| 67 | + message: 'Name of Kubernetes context to use', |
| 68 | + default: currentKubeContext, |
| 69 | + choices: contexts, |
| 70 | + }); |
| 71 | + } |
| 72 | + if (!kubeNamespace) { |
| 73 | + questions.push({ |
| 74 | + type: 'input', |
| 75 | + name: 'namespace', |
| 76 | + default: defaultNamespace, |
| 77 | + message: 'Kubernetes namespace to remove Codefresh Runner components from ', |
| 78 | + validate: value => (value !== undefined && value !== '') || 'Please enter namespace\'s name', |
| 79 | + }); |
| 80 | + } |
| 81 | + const agents = await sdk.agents.list({}); |
| 82 | + if (!agentName) { |
| 83 | + questions.push({ |
| 84 | + type: 'list', |
| 85 | + name: 'name', |
| 86 | + message: 'Runner manager name to uninstall', |
| 87 | + choices: agents, |
| 88 | + }); |
| 89 | + } |
| 90 | + console.log(colors.green('This uninstaller will guide you through the runner uninstallation process')); |
| 91 | + const answers = await inquirer.prompt(questions); |
| 92 | + kubeContextName = kubeContextName || answers.context; |
| 93 | + kubeNamespace = kubeNamespace || answers.namespace; |
| 94 | + agentName = agentName || answers.name; |
| 95 | + // check that agent exists |
| 96 | + const agent = _.find(agents, curr => curr.name === agentName); |
| 97 | + if (!agent) { |
| 98 | + console.log(colors.red(`Runner Manager with name ${agentName} doesn\'t exists`)); |
| 99 | + return; |
| 100 | + } |
| 101 | + if (agent.runtimes && agent.runtimes > 1) { |
| 102 | + console.log('Can\'t delete runner with more than one runtime , use runtime delete command'); |
| 103 | + return; |
| 104 | + } |
| 105 | + console.log(colors.green(`Uninstallation options summary : \n Context: ${colors.blue(kubeContextName)} \n Namespace: ${colors.blue(kubeNamespace)} \n Agent name: ${colors.blue(agentName)} `)); |
| 106 | + if (agent.runtimes.length === 1) { |
| 107 | + await unInstallRuntime.handler({ |
| 108 | + 'agent-name': agentName, |
| 109 | + 'runtime-kube-namespace': kubeNamespace, |
| 110 | + 'runtime-kube-context-name': kubeContextName, |
| 111 | + 'agent-kube-context-name': kubeContextName, |
| 112 | + 'agent-kube-namespace': kubeNamespace, |
| 113 | + name: agent.runtimes[0], |
| 114 | + terminateProcess: false, |
| 115 | + }); |
| 116 | + } |
| 117 | + await unInstallAgent.handler({ |
| 118 | + 'kube-namespace': kubeNamespace, |
| 119 | + 'kube-context-name': kubeContextName, |
| 120 | + name: agentName, |
| 121 | + terminateProcess: false, |
| 122 | + }); |
| 123 | + await unInstallMonitor.handler({ |
| 124 | + 'kube-namespace': kubeNamespace, |
| 125 | + 'kube-context-name': kubeContextName, |
| 126 | + noExit: true, // to prevent if from calling: process.exit() |
| 127 | + |
| 128 | + }); |
| 129 | + console.log('Successfully uninstalled Codefresh Runner'); |
| 130 | + process.exit(); // TODO : This is not needed - needed to be fixed |
| 131 | + }, |
| 132 | +}); |
| 133 | + |
| 134 | +module.exports = deleteCmd; |
0 commit comments