Skip to content

Commit ba4990c

Browse files
olegs-codefreshitai-codefresh
authored andcommitted
Support install-chart command (#56)
1 parent 4bc32cf commit ba4990c

File tree

7 files changed

+174
-64
lines changed

7 files changed

+174
-64
lines changed

lib/interface/cli/codefresh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ recursive(path.resolve(__dirname, 'commands'), (err, files) => {
3737
default: DEFAULTS.CFCONFIG,
3838
global: false,
3939
})
40-
.option('version', {
41-
alias: 'v',
42-
global: false,
43-
})
4440
.config('cfconfig', 'Custom path for authentication contexts config file', (configFilePath) => {
4541
try {
4642
authManager.loadContexts(configFilePath, process.env.CF_TOKEN, process.env.CF_URL || DEFAULTS.URL);
@@ -59,6 +55,7 @@ recursive(path.resolve(__dirname, 'commands'), (err, files) => {
5955
.completion()
6056
.demandCommand(1, 'You need at least one command before moving on')
6157
.wrap(null)
58+
.version(false)
6259
.help('help')
6360
.option('help', {
6461
global: false,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const Command = require('../../Command');
2+
const {
3+
installChart,
4+
} = require('./../../../../logic/api/helm');
5+
const { printError } = require('./../../helpers/general');
6+
7+
const install = new Command({
8+
root: true,
9+
command: 'install-chart',
10+
description: 'Install or upgrade Helm chart',
11+
builder: (yargs) => {
12+
return yargs
13+
.usage('Install or upgrade helm chart on cluster')
14+
.option('cluster', {
15+
description: 'Install on cluster',
16+
type: 'string',
17+
required: true,
18+
})
19+
.option('namespace', {
20+
description: 'Install on namespace',
21+
default: 'default',
22+
type: 'string',
23+
})
24+
.option('tiller-namespace', {
25+
description: 'Where tiller has been installed',
26+
default: 'kube-system',
27+
type: 'string',
28+
})
29+
.option('repository', {
30+
description: 'Helm repository',
31+
type: 'string',
32+
required: true,
33+
})
34+
.option('name', {
35+
description: 'Name of the chart in the repository',
36+
type: 'string',
37+
required: true,
38+
})
39+
.option('version', {
40+
description: 'Version of the chart in the repository',
41+
type: 'string',
42+
required: true,
43+
})
44+
.option('context', {
45+
description: 'Contexts (helm-plain-text-values) to be passed to the install',
46+
array: true,
47+
})
48+
.option('release-name', {
49+
description: 'The name to set to the release',
50+
});
51+
},
52+
handler: async (argv) => {
53+
try {
54+
const workflowId = await installChart({
55+
releaseName: argv.releaseName,
56+
cluster: argv.cluster,
57+
namespace: argv.namespace,
58+
name: argv.name,
59+
repository: argv.repository,
60+
version: argv.version,
61+
values: argv.values,
62+
tillerNamespace: argv.tillerNamespace,
63+
});
64+
console.log(`Started with id: ${workflowId}`);
65+
} catch (err) {
66+
printError(err);
67+
process.exit(1);
68+
}
69+
},
70+
});
71+
72+
module.exports = install;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const Command = require('../../Command');
2+
const cliPackage = require('./../../../../../package.json');
3+
4+
const version = new Command({
5+
root: true,
6+
command: 'version',
7+
description: 'Print version',
8+
builder: (yargs) => {
9+
return yargs;
10+
},
11+
handler: async () => {
12+
console.log(cliPackage.version);
13+
},
14+
});
15+
16+
module.exports = version;

lib/logic/api/helm.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const Promise = require('bluebird');
2+
const _ = require('lodash');
3+
const CFError = require('cf-errors'); // eslint-disable-line
4+
const { sendHttpRequest } = require('./helper');
5+
const { getContextByName } = require('./context');
6+
7+
const SUPPORTED_TYPES = [
8+
'helm-plain-text-values',
9+
];
10+
11+
12+
const _normalizeValues = async (values = []) => {
13+
const normalizedValues = await Promise.reduce(values, async (normalized, name) => {
14+
let context;
15+
try {
16+
context = await getContextByName(name);
17+
} catch (err) {
18+
console.log(`Failed to get context: ${err.message}`);
19+
throw err;
20+
}
21+
const type = context.getType();
22+
if (_.includes(SUPPORTED_TYPES, type)) {
23+
normalized.push({
24+
name,
25+
});
26+
} else {
27+
console.log(`${name} has type ${type} which not supported, skipping`);
28+
}
29+
return normalized;
30+
}, []);
31+
32+
return normalizedValues;
33+
};
34+
35+
const installChart = async ({
36+
cluster,
37+
namespace,
38+
name,
39+
repository,
40+
version,
41+
values,
42+
releaseName,
43+
tillerNamespace,
44+
}) => {
45+
const normalizedValues = await _normalizeValues(values);
46+
const options = {
47+
url: '/api/kubernetes/chart/install',
48+
method: 'POST',
49+
qs: {
50+
selector: cluster,
51+
namespace: tillerNamespace,
52+
},
53+
body: {
54+
name,
55+
repository,
56+
namespace,
57+
values: normalizedValues,
58+
version,
59+
releaseName,
60+
},
61+
};
62+
63+
const res = await sendHttpRequest(options);
64+
return res.id;
65+
};
66+
67+
module.exports = {
68+
installChart,
69+
};

lib/logic/entities/Context.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Entity = require('./Entity');
2+
const _ = require('lodash');
23

34
class Context extends Entity {
45
constructor(data) {
@@ -8,6 +9,11 @@ class Context extends Entity {
89
this.defaultColumns = ['apiVersion', 'kind', 'name'];
910
this.wideColumns = ['apiVersion', 'kind', 'name', 'owner', 'type'];
1011
}
12+
13+
// get the type of the context from spec.type
14+
getType() {
15+
return _.get(this, 'info.type');
16+
}
1117
}
1218

1319
module.exports = Context;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.4.8",
3+
"version": "0.4.9",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

yarn.lock

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,7 @@ ajv@^4.9.1:
6060
co "^4.6.0"
6161
json-stable-stringify "^1.0.1"
6262

63-
ajv@^5.1.0:
64-
version "5.4.0"
65-
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.4.0.tgz#32d1cf08dbc80c432f426f12e10b2511f6b46474"
66-
dependencies:
67-
co "^4.6.0"
68-
fast-deep-equal "^1.0.0"
69-
fast-json-stable-stringify "^2.0.0"
70-
json-schema-traverse "^0.3.0"
71-
72-
ajv@^5.2.3, ajv@^5.3.0:
63+
ajv@^5.1.0, ajv@^5.2.3, ajv@^5.3.0:
7364
version "5.5.0"
7465
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.0.tgz#eb2840746e9dc48bd5e063a36e3fd400c5eab5a9"
7566
dependencies:
@@ -1018,7 +1009,7 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
10181009
version "1.0.5"
10191010
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
10201011

1021-
escodegen@1.8.1:
1012+
escodegen@1.8.1, escodegen@^1.6.1:
10221013
version "1.8.1"
10231014
resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.8.1.tgz#5a5b53af4693110bebb0867aa3430dd3b70a1018"
10241015
dependencies:
@@ -1029,17 +1020,6 @@ escodegen@1.8.1:
10291020
optionalDependencies:
10301021
source-map "~0.2.0"
10311022

1032-
escodegen@^1.6.1:
1033-
version "1.9.0"
1034-
resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.9.0.tgz#9811a2f265dc1cd3894420ee3717064b632b8852"
1035-
dependencies:
1036-
esprima "^3.1.3"
1037-
estraverse "^4.2.0"
1038-
esutils "^2.0.2"
1039-
optionator "^0.8.1"
1040-
optionalDependencies:
1041-
source-map "~0.5.6"
1042-
10431023
eslint-config-airbnb-base@^12.1.0:
10441024
version "12.1.0"
10451025
resolved "https://registry.yarnpkg.com/eslint-config-airbnb-base/-/eslint-config-airbnb-base-12.1.0.tgz#386441e54a12ccd957b0a92564a4bafebd747944"
@@ -1145,10 +1125,6 @@ esprima@^2.7.1:
11451125
version "2.7.3"
11461126
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"
11471127

1148-
esprima@^3.1.3:
1149-
version "3.1.3"
1150-
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"
1151-
11521128
esprima@^4.0.0:
11531129
version "4.0.0"
11541130
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804"
@@ -2602,30 +2578,26 @@ mimic-fn@^1.0.0:
26022578
version "1.1.0"
26032579
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.1.0.tgz#e667783d92e89dbd342818b5230b9d62a672ad18"
26042580

2605-
minimatch@3.0.3:
2581+
minimatch@3.0.3, minimatch@^3.0.2, minimatch@^3.0.3:
26062582
version "3.0.3"
26072583
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774"
26082584
dependencies:
26092585
brace-expansion "^1.0.0"
26102586

2611-
minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
2587+
minimatch@^3.0.0, minimatch@^3.0.4:
26122588
version "3.0.4"
26132589
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
26142590
dependencies:
26152591
brace-expansion "^1.1.7"
26162592

2617-
minimist@0.0.8:
2593+
minimist@0.0.8, minimist@~0.0.1:
26182594
version "0.0.8"
26192595
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
26202596

26212597
minimist@1.2.0, minimist@^1.1.1, minimist@^1.2.0:
26222598
version "1.2.0"
26232599
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
26242600

2625-
minimist@~0.0.1:
2626-
version "0.0.10"
2627-
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"
2628-
26292601
"mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1:
26302602
version "0.5.1"
26312603
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
@@ -2883,7 +2855,7 @@ p-locate@^2.0.0:
28832855
dependencies:
28842856
p-limit "^1.1.0"
28852857

2886-
package-json@2.3.3:
2858+
package-json@2.3.3, package-json@^2.0.0:
28872859
version "2.3.3"
28882860
resolved "https://registry.yarnpkg.com/package-json/-/package-json-2.3.3.tgz#14895311a963d18edf8801e06b67ea87795d15b9"
28892861
dependencies:
@@ -2899,15 +2871,6 @@ package-json@^1.0.0:
28992871
got "^3.2.0"
29002872
registry-url "^3.0.0"
29012873

2902-
package-json@^2.0.0:
2903-
version "2.4.0"
2904-
resolved "https://registry.yarnpkg.com/package-json/-/package-json-2.4.0.tgz#0d15bd67d1cbbddbb2ca222ff2edb86bcb31a8bb"
2905-
dependencies:
2906-
got "^5.0.0"
2907-
registry-auth-token "^3.0.1"
2908-
registry-url "^3.0.3"
2909-
semver "^5.1.0"
2910-
29112874
parse-github-repo-url@1.3.0:
29122875
version "1.3.0"
29132876
resolved "https://registry.yarnpkg.com/parse-github-repo-url/-/parse-github-repo-url-1.3.0.tgz#d4de02d68e2e60f0d6a182e7a8cb21b6f38c730b"
@@ -3135,7 +3098,7 @@ randomatic@^1.1.3:
31353098
is-number "^3.0.0"
31363099
kind-of "^4.0.0"
31373100

3138-
rc@^1.0.1, rc@^1.1.2, rc@^1.1.6, rc@^1.1.7:
3101+
rc@^1.0.1, rc@^1.1.2, rc@^1.1.7:
31393102
version "1.2.2"
31403103
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.2.tgz#d8ce9cb57e8d64d9c7badd9876c7c34cbe3c7077"
31413104
dependencies:
@@ -3224,13 +3187,6 @@ regex-cache@^0.4.2:
32243187
dependencies:
32253188
is-equal-shallow "^0.1.3"
32263189

3227-
registry-auth-token@^3.0.1:
3228-
version "3.3.1"
3229-
resolved "https://registry.yarnpkg.com/registry-auth-token/-/registry-auth-token-3.3.1.tgz#fb0d3289ee0d9ada2cbb52af5dfe66cb070d3006"
3230-
dependencies:
3231-
rc "^1.1.6"
3232-
safe-buffer "^5.0.1"
3233-
32343190
registry-url@3.1.0, registry-url@^3.0.0, registry-url@^3.0.3:
32353191
version "3.1.0"
32363192
resolved "https://registry.yarnpkg.com/registry-url/-/registry-url-3.1.0.tgz#3d4ef870f73dde1d77f0cf9a381432444e174942"
@@ -3396,18 +3352,12 @@ resolve@1.1.7:
33963352
version "1.1.7"
33973353
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
33983354

3399-
resolve@1.4.0:
3355+
resolve@1.4.0, resolve@^1.2.0:
34003356
version "1.4.0"
34013357
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.4.0.tgz#a75be01c53da25d934a98ebd0e4c4a7312f92a86"
34023358
dependencies:
34033359
path-parse "^1.0.5"
34043360

3405-
resolve@^1.2.0:
3406-
version "1.5.0"
3407-
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.5.0.tgz#1f09acce796c9a762579f31b2c1cc4c3cddf9f36"
3408-
dependencies:
3409-
path-parse "^1.0.5"
3410-
34113361
restore-cursor@^1.0.1:
34123362
version "1.0.1"
34133363
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-1.0.1.tgz#34661f46886327fed2991479152252df92daa541"
@@ -3574,7 +3524,7 @@ source-map@^0.4.4:
35743524
dependencies:
35753525
amdefine ">=0.0.4"
35763526

3577-
source-map@^0.5.3, source-map@^0.5.6, source-map@~0.5.1, source-map@~0.5.6:
3527+
source-map@^0.5.3, source-map@^0.5.6, source-map@~0.5.1:
35783528
version "0.5.7"
35793529
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
35803530

0 commit comments

Comments
 (0)