Skip to content

Commit

Permalink
runner delete (#455)
Browse files Browse the repository at this point in the history
* runner delete
  • Loading branch information
oren-codefresh authored May 14, 2020
1 parent f2c84f5 commit 70cbfbe
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/interface/cli/commands/agent/uninstall.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const unInstallAgentCmd = new Command({
name,
'kube-namespace': kubeNamespace,
'kube-config-path': kubeConfigPath,
terminateProcess,

} = argv;

Expand Down Expand Up @@ -80,7 +81,9 @@ const unInstallAgentCmd = new Command({
console.log('Agent uninsalled successfully');
await deleteAgent.handler({ name, id: name });
}
process.exit(0);
if (terminateProcess !== false) {
process.exit(0);
}
},
});

Expand Down
134 changes: 134 additions & 0 deletions lib/interface/cli/commands/hybrid/delete.cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* eslint-disable max-len */
const Command = require('../../Command');
const runnerRoot = require('../root/runner.cmd');
const inquirer = require('inquirer');
const { getAllKubeContexts, getKubeContext } = require('../../helpers/kubernetes');
const unInstallRuntime = require('../runtimeEnvironments/uninstall.cmd');
const unInstallAgent = require('../agent/uninstall.cmd');
const unInstallMonitor = require('../monitor/uninstall.cmd');
const colors = require('colors');
const DEFAULTS = require('../../defaults');
const sdk = require('../../../../logic/sdk');
const _ = require('lodash');

const defaultNamespace = 'codefresh';

const deleteCmd = new Command({
root: false,
parent: runnerRoot,
command: 'delete',
requiresAuthentication: false,
description: 'Deletes codefresh runner solution\'s components on kubernetes cluster',
webDocs: {
category: 'Runner',
title: 'Delete',
weight: 100,
},
// requiresAuthentication: argv => argv && !argv.token,
builder: yargs => yargs
.env('CF_ARG_') // this means that every process.env.CF_ARG_* will be passed to argv
.option('name', {
describe: 'Agent\'s name to be deleted',
})
.option('url', {
describe: 'Codefresh system custom url',
default: DEFAULTS.URL,
})
.option('kube-context-name', {
describe: 'Name of the kubernetes context on which venona should be installed [$CF_ARG_KUBE_CONTEXT_NAME]',
})
.option('kube-namespace', {
describe: 'Name of the namespace on which venona should be installed [$CF_ARG_KUBE_NAMESPACE]',
})
.option('kube-config-path', {
describe: 'Path to kubeconfig file (default is $HOME/.kube/config)',
})
.option('verbose', {
describe: 'Print logs',
}),
handler: async (argv) => {
const {
'kube-config-path': kubeConfigPath,
} = argv;
let {
'kube-context-name': kubeContextName,
'kube-namespace': kubeNamespace,
name: agentName,
} = argv;

const questions = [];
if (!kubeContextName) {
const contexts = getAllKubeContexts(kubeConfigPath);
const currentKubeContext = getKubeContext(kubeConfigPath);

questions.push({
type: 'list',
name: 'context',
message: 'Name of Kubernetes context to use',
default: currentKubeContext,
choices: contexts,
});
}
if (!kubeNamespace) {
questions.push({
type: 'input',
name: 'namespace',
default: defaultNamespace,
message: 'Kubernetes namespace to remove Codefresh Runner components from ',
validate: value => (value !== undefined && value !== '') || 'Please enter namespace\'s name',
});
}
const agents = await sdk.agents.list({});
if (!agentName) {
questions.push({
type: 'list',
name: 'name',
message: 'Runner manager name to uninstall',
choices: agents,
});
}
console.log(colors.green('This uninstaller will guide you through the runner uninstallation process'));
const answers = await inquirer.prompt(questions);
kubeContextName = kubeContextName || answers.context;
kubeNamespace = kubeNamespace || answers.namespace;
agentName = agentName || answers.name;
// check that agent exists
const agent = _.find(agents, curr => curr.name === agentName);
if (!agent) {
console.log(colors.red(`Runner Manager with name ${agentName} doesn\'t exists`));
return;
}
if (agent.runtimes && agent.runtimes > 1) {
console.log('Can\'t delete runner with more than one runtime , use runtime delete command');
return;
}
console.log(colors.green(`Uninstallation options summary : \n Context: ${colors.blue(kubeContextName)} \n Namespace: ${colors.blue(kubeNamespace)} \n Agent name: ${colors.blue(agentName)} `));
if (agent.runtimes.length === 1) {
await unInstallRuntime.handler({
'agent-name': agentName,
'runtime-kube-namespace': kubeNamespace,
'runtime-kube-context-name': kubeContextName,
'agent-kube-context-name': kubeContextName,
'agent-kube-namespace': kubeNamespace,
name: agent.runtimes[0],
terminateProcess: false,
});
}
await unInstallAgent.handler({
'kube-namespace': kubeNamespace,
'kube-context-name': kubeContextName,
name: agentName,
terminateProcess: false,
});
await unInstallMonitor.handler({
'kube-namespace': kubeNamespace,
'kube-context-name': kubeContextName,
noExit: true, // to prevent if from calling: process.exit()

});
console.log('Successfully uninstalled Codefresh Runner');
process.exit(); // TODO : This is not needed - needed to be fixed
},
});

module.exports = deleteCmd;
10 changes: 7 additions & 3 deletions lib/interface/cli/commands/monitor/uninstall.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const unInstallAgentCmd = new Command({
'kube-namespace': kubeNamespace,
'kube-config-path': kubeConfigPath,
'kube-context-name': kubeContextName,

noExit,
} = argv;

const events = new ProgressEvents();
Expand Down Expand Up @@ -63,9 +63,13 @@ const unInstallAgentCmd = new Command({
});
if (exitCode === 0) {
console.log('Monitor uninsalled successfully');
process.exit(0);
if (!noExit) {
process.exit(0);
}
}
if (!noExit) {
process.exit(1);
}
process.exit(1);
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const unInstallRuntimeCmd = new Command({
'restart-agent': restartAgent,
'agent-kube-config-path': agentKubeConfigPath,
verbose,
terminateProcess,

} = argv;

Expand Down Expand Up @@ -137,7 +138,7 @@ const unInstallRuntimeCmd = new Command({
restartAgent,
storageClassName,
verbose,
terminateProcess: true,
terminateProcess: (terminateProcess !== false),
events,
});
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codefresh",
"version": "0.60.1",
"version": "0.60.2",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit 70cbfbe

Please sign in to comment.