Skip to content

Commit 0c58d6c

Browse files
Runtime environment new model (#212)
1 parent ba14c93 commit 0c58d6c

File tree

14 files changed

+807
-82
lines changed

14 files changed

+807
-82
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const CFError = require('cf-errors');
2+
const Command = require('../../Command');
3+
const { crudFilenameOption } = require('../../helpers/general');
4+
const DEFAULTS = require('../../defaults');
5+
const yargs = require('yargs');
6+
7+
const diff = new Command({
8+
root: true,
9+
command: 'diff',
10+
description: 'Show diff between two resources',
11+
usage: 'Supported resources: runtime-environments',
12+
webDocs: {
13+
description: 'Show diff between two resources',
14+
category: 'Operate On Resources',
15+
title: 'Diff',
16+
weight: 100,
17+
},
18+
builder: (yargs) => {
19+
crudFilenameOption(yargs);
20+
return yargs;
21+
},
22+
handler: async (argv) => {
23+
if (!argv.filename) {
24+
yargs.showHelp();
25+
}
26+
27+
const data = argv.filename;
28+
const entity = data.kind;
29+
30+
switch (entity) {
31+
default:
32+
throw new CFError(`Entity: ${entity} not supported`);
33+
}
34+
35+
},
36+
});
37+
38+
module.exports = diff;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const debug = require('debug')('codefresh:cli:create:context');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { runtimeEnvironments } = require('../../../../logic').api;
6+
const applyRoot = require('../root/apply.cmd');
7+
8+
9+
const command = new Command({
10+
command: 'runtime-environments [name]',
11+
aliases: ['re','runtime-environment'],
12+
parent: applyRoot,
13+
description: 'apply changes to runtime-environments resource',
14+
usage: 'Use apply to patch an existing context',
15+
webDocs: {
16+
category: 'Runtime-Environments (On Prem)',
17+
title: 'Apply Runtime-Environments',
18+
weight: 90,
19+
},
20+
builder: (yargs) => {
21+
return yargs
22+
.positional('name', {
23+
describe: 'Runtime environments name',
24+
})
25+
.option('default', {
26+
describe: 'Set the chosen runtime environment as default',
27+
})
28+
.option('extends', {
29+
describe: 'Set the runtime environments you want to extends from',
30+
type: Array,
31+
})
32+
.option('namespace', {
33+
alias: 'ns',
34+
describe: 'Set the desire namespace',
35+
})
36+
.option('cluster', {
37+
alias: 'c',
38+
describe: 'Set the desire cluster',
39+
})
40+
.option('description', {
41+
alias: 'd',
42+
describe: 'Description of the new runtime environment',
43+
})
44+
.example('codefresh patch runtime-environments -f ./re.yml', 'Apply changes to a runtime-environment');
45+
},
46+
handler: async (argv) => {
47+
const option = {};
48+
option.body = argv.filename || {};
49+
option.name = _.get(option.body, 'metadata.name') || argv.name;
50+
option.extend = _.get(option.body, 'extends') || argv.extends;
51+
option.description = _.get(option.body, 'description') || argv.description;
52+
option.dockerDaemonCluster = _.get(option.body, 'dockerDaemonScheduler.cluster.clusterProvider.selector') || argv.cluster;
53+
option.dockerDaemonNamespace = _.get(option.body, 'dockerDaemonScheduler.cluster.namespace') || argv.namespace;
54+
option.runtimeSchedulerCluster = _.get(option.body, 'runtimeScheduler.cluster.clusterProvider.selector') || argv.cluster;
55+
option.runtimeSchedulerNamespace = _.get(option.body, 'runtimeScheduler.cluster.namespace') || argv.namespace;
56+
if (!option.name) {
57+
throw new CFError('Must Provide a runtime environment name');
58+
}
59+
if (argv.default) {
60+
try {
61+
await runtimeEnvironments.setDefaultForAccount(option.name);
62+
console.log(`Runtime-Environment: '${option.name}' set as a default.`);
63+
} catch (err) {
64+
throw new CFError(err);
65+
}
66+
}
67+
if (!option.extend) {
68+
throw new CFError('Must specify the runtime you want to extends from');
69+
}
70+
if (!option.dockerDaemonCluster || !option.runtimeSchedulerCluster || option.dockerDaemonNamespace || option.runtimeSchedulerNamespace) {
71+
let extendsFromAccountRe = false;
72+
_.forEach(argv.extends, (runtime) => {
73+
if (!runtime.startsWith('system')) {
74+
extendsFromAccountRe = true;
75+
}
76+
});
77+
if (!extendsFromAccountRe) {
78+
throw new CFError('Must Provide cluster name and namespace or extends from existing account runtime environment');
79+
}
80+
} if (!argv.filename) {
81+
if (option.dockerDaemonCluster) {
82+
_.set(option.body, 'dockerDaemonScheduler.cluster.clusterProvider.selector', option.dockerDaemonCluster);
83+
}
84+
if (option.runtimeSchedulerCluster) {
85+
_.set(option.body, 'runtimeScheduler.cluster.clusterProvider.selector', option.runtimeSchedulerCluster);
86+
}
87+
if (option.dockerDaemonNamespace) {
88+
_.set(option.body, 'dockerDaemonScheduler.cluster.namespace', option.dockerDaemonNamespace);
89+
}
90+
if (option.runtimeSchedulerNamespace) {
91+
_.set(option.body, 'runtimeScheduler.cluster.namespace', option.runtimeSchedulerNamespace);
92+
}
93+
}
94+
await runtimeEnvironments.applyByNameForAccount(option);
95+
console.log(`Runtime-Environment: '${option.name}' patched.`);
96+
},
97+
});
98+
99+
100+
module.exports = command;
101+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { runtimeEnvironments } = require('../../../../logic/index').api;
6+
const deleteRoot = require('../root/delete.cmd');
7+
8+
9+
const command = new Command({
10+
command: 'runtime-environments name',
11+
aliases: ['re', 'runtime-environment'],
12+
parent: deleteRoot,
13+
description: 'Delete a runtime-environment',
14+
webDocs: {
15+
category: 'Runtime-Environments',
16+
title: 'Delete Runtime-Environments',
17+
},
18+
builder: (yargs) => {
19+
return yargs
20+
.positional('name', {
21+
describe: 'Runtime environment name',
22+
})
23+
.option('force', {
24+
describe: 'Delete runtime environment in force mode',
25+
type: 'boolean',
26+
});
27+
},
28+
handler: async (argv) => {
29+
const { name, force } = argv;
30+
31+
await runtimeEnvironments.deleteRuntimeEnvironmentByNameForAccount(name, force);
32+
console.log(`Runtime-Environment '${name}' deleted.`);
33+
},
34+
});
35+
36+
37+
module.exports = command;
38+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const { runtimeEnvironments } = require('../../../../logic/index').api;
4+
const jsdiff = require('diff');
5+
require('colors');
6+
7+
const diffRoot = require('../root/diff.cmd');
8+
9+
10+
const command = new Command({
11+
command: 'runtime-environments [first-re-name] [first-re-version] [second-re-name] [second-re-version]',
12+
aliases: ['re', 'runtime-environment'],
13+
parent: diffRoot,
14+
description: 'Show diff between two runtime environments configuration',
15+
webDocs: {
16+
category: 'Runtime-Environments (On Prem)',
17+
title: 'Diff Runtime-Environments',
18+
},
19+
builder: (yargs) => {
20+
return yargs
21+
.option('first-name', {
22+
describe: 'First runtime environment name',
23+
})
24+
.option('first-version', {
25+
describe: 'First runtime environment version',
26+
})
27+
.option('second-name', {
28+
describe: 'Second runtime environment name',
29+
})
30+
.option('second-version', {
31+
describe: 'Second runtime environment version',
32+
});
33+
},
34+
handler: async (argv) => {
35+
const firstEnvName = argv['first-name'];
36+
const firstEnvVersion = argv['first-version'];
37+
const secondEnvName = argv['second-name'];
38+
const secondEnvVersion = argv['second-version'];
39+
40+
const firstEnv = await runtimeEnvironments.getRuntimeEvironmentsByNameForAccount({
41+
diff: true,
42+
name: firstEnvName,
43+
version: firstEnvVersion,
44+
});
45+
46+
const secondtEnv = await runtimeEnvironments.getRuntimeEvironmentsByNameForAccount({
47+
diff: true,
48+
name: secondEnvName,
49+
version: secondEnvVersion,
50+
});
51+
52+
const firstEnvJson = JSON.stringify(firstEnv, null, '\t');
53+
const secondEnvJson = JSON.stringify(secondtEnv, null, '\t');
54+
55+
const diff = jsdiff.diffJson(firstEnvJson, secondEnvJson);
56+
57+
diff.forEach((part) => {
58+
const color = part.added ? 'green' : part.removed ? 'red' : 'white';
59+
process.stderr.write(part.value[color]);
60+
});
61+
62+
console.log();
63+
},
64+
});
65+
66+
module.exports = command;
67+

lib/interface/cli/commands/runtimeEnvironments/get.cmd.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,74 @@ const debug = require('debug')('codefresh:cli:create:pipelines2');
22
const Command = require('../../Command');
33
const _ = require('lodash');
44
const { runtimeEnvironments } = require('../../../../logic/index').api;
5-
const { specifyOutputForSingle } = require('../../helpers/get');
5+
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
6+
const DEFAULTS = require('../../defaults');
7+
68

79

810
const getRoot = require('../root/get.cmd');
911

1012

1113
const command = new Command({
12-
command: 'runtime-environments [name] [version]',
14+
command: 'runtime-environments [name]',
1315
aliases: ['re', 'runtime-environment'],
1416
parent: getRoot,
1517
description: 'Get a runtime environments configuration',
16-
onPremCommand: true,
1718
webDocs: {
18-
category: 'Runtime-Environments (On Prem)',
19+
category: 'Runtime-Environments',
1920
title: 'Get Runtime-Environments',
2021
},
2122
builder: (yargs) => {
2223
return yargs
2324
.positional('name', {
2425
describe: 'Runtime environments name',
25-
default: 'default',
2626
})
27-
.positional('version', {
27+
.option('version', {
2828
describe: 'Runtime environments version',
2929
})
30-
/*
31-
.option('account', {
32-
describe: 'Return a specific account configuration by the given name from the current runtime environments',
33-
})
34-
*/
3530
.option('history', {
3631
describe: 'Return all the history of the specific runtime environments',
3732
type: 'boolean',
33+
})
34+
.option('limit', {
35+
describe: 'Limit amount of returned results',
36+
default: DEFAULTS.GET_LIMIT_RESULTS,
37+
})
38+
.option('page', {
39+
describe: 'Paginated page',
40+
default: DEFAULTS.GET_PAGINATED_PAGE,
41+
})
42+
.option('extend', {
43+
describe: 'Return an extend runtime environment ',
44+
type: 'boolean',
45+
default: false,
3846
});
3947
},
4048
handler: async (argv) => {
41-
const { name, version, history, account } = argv;
42-
if (history){
43-
const allHistory = await runtimeEnvironments.getRuntimeEvironmentsHistory({
49+
const { name, version, history, extend, limit, page, output } = argv;
50+
const offset = (page - 1) * limit;
51+
if (history) {
52+
const allHistory = await runtimeEnvironments.getRuntimeEvironmentsByNameForAccount({
4453
name,
54+
history,
4555
});
4656
console.log(JSON.stringify(allHistory, null, '\t'));
4757

4858
}
49-
else {
50-
const currruntimeEnvironments = await runtimeEnvironments.getRuntimeEvironmentsByName({
59+
else if (name) {
60+
const currruntimeEnvironments = await runtimeEnvironments.getRuntimeEvironmentsByNameForAccount({
5161
name,
5262
version,
63+
extend,
5364
});
5465
specifyOutputForSingle(argv.output, currruntimeEnvironments);
5566
}
67+
else {
68+
specifyOutputForArray(output, await runtimeEnvironments.getAllRuntimeEnvironmentsForAccount({
69+
limit,
70+
offset,
71+
}));
72+
}
5673
},
5774
});
5875

lib/interface/cli/commands/runtimeEnvironments/replace.cmd.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)