Skip to content

Commit 1b89be4

Browse files
Saas 7146 - runner upgrade (#475)
* improved runner delete cmd * improved runner init cmd * added runner upgrade cmd
1 parent 553f5f9 commit 1b89be4

File tree

11 files changed

+665
-168
lines changed

11 files changed

+665
-168
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const sdk = require('../../../../logic/sdk');
2+
const _ = require('lodash');
3+
4+
async function getNewAgentName(kubeContextName, kubeNamespace, agents) {
5+
const defaultName = `${kubeContextName}_${kubeNamespace}`;
6+
if (!agents) {
7+
// eslint-disable-next-line no-param-reassign
8+
agents = await sdk.agents.list({ });
9+
}
10+
let name;
11+
12+
if (!_.isArray(agents) || !_.find(agents, a => a.name === defaultName)) {
13+
name = defaultName; // use the default name if there are no collisions
14+
} else {
15+
const agentsNames = new Set(_.map(agents, a => a.name)); // for fast lookup
16+
let i = 1;
17+
while (agentsNames.has(`${defaultName}_${i}`)) {
18+
i += 1;
19+
}
20+
name = `${defaultName}_${i}`;
21+
}
22+
23+
return name;
24+
}
25+
26+
module.exports = {
27+
getNewAgentName,
28+
};

lib/interface/cli/commands/agent/install.cmd.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,10 @@ const { getKubeContext } = require('../../helpers/kubernetes');
77
const ProgressEvents = require('../../helpers/progressEvents');
88
const cliProgress = require('cli-progress');
99
const colors = require('colors');
10-
const _ = require('lodash');
10+
const { getNewAgentName } = require('./helper');
1111

1212
const defaultNamespace = 'codefresh';
1313

14-
async function newAgentName(kubeContextName, kubeNamespace) {
15-
const defaultName = `${kubeContextName}_${kubeNamespace}`;
16-
const agents = await sdk.agents.list({ });
17-
let name;
18-
19-
if (!_.isArray(agents) || !_.find(agents, a => a.name === defaultName)) {
20-
name = defaultName; // use the default name if there are no collisions
21-
} else {
22-
const agentsNames = new Set(_.map(agents, a => a.name)); // for fast lookup
23-
let i = 1;
24-
while (agentsNames.has(`${defaultName}_${i}`)) {
25-
i += 1;
26-
}
27-
name = `${defaultName}_${i}`;
28-
}
29-
30-
return name;
31-
}
32-
3314
const installAgentCmd = new Command({
3415
root: false,
3516
parent: installRoot,
@@ -139,7 +120,7 @@ const installAgentCmd = new Command({
139120
}
140121

141122
if (!token) { // Create an agent if not provided
142-
name = name || await newAgentName(kubeContextName, kubeNamespace);
123+
name = name || await getNewAgentName(kubeContextName, kubeNamespace);
143124
agent = await sdk.agents.create({ name });
144125
// eslint-disable-next-line prefer-destructuring
145126
token = agent.token;
@@ -194,7 +175,7 @@ const installAgentCmd = new Command({
194175
skipClusterTest,
195176
verbose,
196177
agentId: name,
197-
terminateProcess: !installRuntime,
178+
terminateProcess,
198179
events,
199180
});
200181
if (agentInstallStatusCode !== 0) {

lib/interface/cli/commands/hybrid/delete.cmd.js

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const Command = require('../../Command');
33
const runnerRoot = require('../root/runner.cmd');
44
const inquirer = require('inquirer');
5-
const { getAllKubeContexts, getKubeContext, getAllNamespaces } = require('../../helpers/kubernetes');
5+
const { getAllKubeContexts, getKubeContext } = require('../../helpers/kubernetes');
66
const unInstallRuntime = require('../runtimeEnvironments/uninstall.cmd');
77
const unInstallAgent = require('../agent/uninstall.cmd');
88
const unInstallMonitor = require('../monitor/uninstall.cmd');
@@ -11,54 +11,11 @@ const DEFAULTS = require('../../defaults');
1111
const sdk = require('../../../../logic/sdk');
1212
const _ = require('lodash');
1313
const { to } = require('./../../../../logic/cli-config/errors/awaitTo');
14-
const { prettyError } = require('../../../../logic/cli-config/errors/helpers');
14+
const { createErrorHandler, getRelatedAgents, getRelatedNamespaces } = require('./helper');
1515

1616
const defaultNamespace = 'codefresh';
17-
18-
async function handleError(error, message) {
19-
if (!error) {
20-
return;
21-
}
22-
23-
console.log(`${colors.red('Error:')} ${message}: ${prettyError(error)}`);
24-
console.log(colors.green(`If you had any issues with the uninstallation process please report them at: ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`));
25-
process.exit(1);
26-
}
27-
28-
// Try to get the most relevant namespaces
29-
async function getRelatedNamespaces(kubeConfigPath, kubeContextName, runtimes) {
30-
const [, namespacesOnCluster] = await to(getAllNamespaces(kubeConfigPath, kubeContextName));
31-
const nsOnCluster = new Set(namespacesOnCluster || []);
32-
33-
return _(runtimes)
34-
.filter(re => nsOnCluster.has(_.get(re, 'runtimeScheduler.cluster.namespace')))
35-
.map(re => _.get(re, 'runtimeScheduler.cluster.namespace'))
36-
.uniq()
37-
.value();
38-
}
39-
40-
async function getRelatedAgents(kubeNamespace, runtimes) {
41-
const [listAgentsErr, agents] = await to(sdk.agents.list({}));
42-
await handleError(listAgentsErr, 'Failed to get agents');
43-
44-
const relatedREs = new Set();
45-
_.forEach(runtimes, (r) => {
46-
if (_.get(r, 'runtimeScheduler.cluster.namespace') === kubeNamespace) {
47-
relatedREs.add(r.metadata.name);
48-
}
49-
});
50-
51-
const relatedAgents = [];
52-
_.forEach(agents, (a) => {
53-
_.forEach(_.get(a, 'runtimes', []), (r) => {
54-
if (relatedREs.has(r)) {
55-
relatedAgents.push(a);
56-
}
57-
});
58-
});
59-
60-
return relatedAgents;
61-
}
17+
const openIssueMessage = `If you had any issues with the uninstallation process please report them at: ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`;
18+
const handleError = createErrorHandler(openIssueMessage);
6219

6320
const deleteCmd = new Command({
6421
root: false,
@@ -105,6 +62,8 @@ const deleteCmd = new Command({
10562

10663
const [listReErr, runtimes] = await to(sdk.runtimeEnvs.list({ }));
10764
await handleError(listReErr, 'Failed to get runtime environments');
65+
const [listAgentsErr, agents] = await to(sdk.agents.list({ }));
66+
await handleError(listAgentsErr, 'Failed to get agents');
10867

10968
console.log(colors.green('This uninstaller will guide you through the runner uninstallation process'));
11069

@@ -124,7 +83,8 @@ const deleteCmd = new Command({
12483
}
12584

12685
if (!kubeNamespace) {
127-
const relatedNamespaces = await getRelatedNamespaces(kubeConfigPath, kubeContextName, runtimes);
86+
const [getNamespacesErr, relatedNamespaces] = await to(getRelatedNamespaces(kubeConfigPath, kubeContextName, runtimes));
87+
handleError(getNamespacesErr, 'Could not get namespaces in the selected kubernetes cluster');
12888
let answer;
12989
if (!relatedNamespaces.length) {
13090
answer = await inquirer.prompt({
@@ -147,18 +107,18 @@ const deleteCmd = new Command({
147107
kubeNamespace = answer.namespace;
148108
}
149109

150-
const agents = await getRelatedAgents(kubeNamespace, runtimes);
151-
if (!agents.length) {
152-
console.log('No agents related to the specified kubernetes cluster and namespace were found');
153-
process.exit(1);
154-
}
155-
156110
if (!agentName) {
111+
const relatedAgents = await getRelatedAgents(kubeNamespace, runtimes, agents, handleError);
112+
let agentsChoices = relatedAgents;
113+
if (!relatedAgents.length) {
114+
console.log(colors.yellow('No agents related to the specified kubernetes cluster and namespace were found, displaying all agents'));
115+
agentsChoices = agents;
116+
}
157117
const answer = await inquirer.prompt({
158118
type: 'list',
159119
name: 'name',
160120
message: 'Agent name to uninstall',
161-
choices: agents,
121+
choices: agentsChoices.map(a => `${a.name}\t${a.runtimes.length ? `(attached runtimes: ${a.runtimes.join(', ')})` : ''}`),
162122
});
163123
agentName = answer.name;
164124
}
@@ -180,19 +140,39 @@ const deleteCmd = new Command({
180140
3. Agent name: ${colors.cyan(agentName)}
181141
`);
182142

183-
if (agent.runtimes.length === 1) {
143+
const attachedRuntimes = agent.runtimes || [];
144+
145+
// prompt confirmation message
146+
console.log(`${colors.red('This process will attempt to delete the following:')}`);
147+
console.log(`\u2022 Codefresh runner with the name "${colors.cyan(agentName)}"`);
148+
attachedRuntimes.forEach((reName) => { console.log(`\u2022 Codefresh runtime with the name "${colors.cyan(reName)}"`); });
149+
console.log('\u2022 Codefresh runner monitor component');
150+
console.log(`* The kubernetes namespace "${colors.cyan(kubeNamespace)}" will ${colors.underline('not')} be deleted\n`);
151+
152+
const answer = await inquirer.prompt({
153+
type: 'confirm',
154+
name: 'deletionConfirmed',
155+
default: false,
156+
message: 'Are you sure you want to delete all of the above? (default is NO)',
157+
});
158+
if (!answer.deletionConfirmed) {
159+
console.log('Deletion process aborted, exiting...');
160+
process.exit(1);
161+
}
162+
163+
attachedRuntimes.forEach(async (reName) => {
184164
const uninstallRuntimeOptions = {
185165
'agent-name': agentName,
186166
'runtime-kube-namespace': kubeNamespace,
187167
'runtime-kube-context-name': kubeContextName,
188168
'agent-kube-context-name': kubeContextName,
189169
'agent-kube-namespace': kubeNamespace,
190-
name: agent.runtimes[0],
170+
name: reName,
191171
terminateProcess: false,
192172
};
193173
const [uninstallReErr] = await to(unInstallRuntime.handler(uninstallRuntimeOptions));
194-
handleError(uninstallReErr, 'Failed to uninstall runtime-environment');
195-
}
174+
handleError(uninstallReErr, `Failed to uninstall runtime-environment "${colors.cyan(reName)}"`);
175+
});
196176

197177
const uninstallAgentOptions = {
198178
'kube-namespace': kubeNamespace,

0 commit comments

Comments
 (0)