|
| 1 | +const Command = require('../../Command'); |
| 2 | +const { sdk } = require('../../../../logic'); |
| 3 | +const Output = require('../../../../output/Output'); |
| 4 | +const _ = require('lodash'); |
| 5 | + |
| 6 | +const install = new Command({ |
| 7 | + root: true, |
| 8 | + command: 'set-helm-config <cluster>', |
| 9 | + description: 'Set Helm config', |
| 10 | + webDocs: { |
| 11 | + category: 'Predefined Pipelines', |
| 12 | + title: 'Set Helm config', |
| 13 | + weight: 20, |
| 14 | + }, |
| 15 | + builder: (yargs) => { |
| 16 | + return yargs |
| 17 | + .positional('cluster', { |
| 18 | + describe: 'Cluster name on integrations page', |
| 19 | + required: true, |
| 20 | + }) |
| 21 | + .option('version', { |
| 22 | + describe: 'Major part of helm version (helm2 | helm3)', |
| 23 | + alias: 'v', |
| 24 | + }) |
| 25 | + .option('test-release-pipeline', { |
| 26 | + describe: 'Pipeline for testing release', |
| 27 | + alias: 't', |
| 28 | + }) |
| 29 | + .option('rollback-pipeline', { |
| 30 | + describe: 'Pipeline for rollback', |
| 31 | + alias: 'r', |
| 32 | + }) |
| 33 | + .option('delete-release-pipeline', { |
| 34 | + describe: 'Pipeline for deleting release', |
| 35 | + alias: 'd', |
| 36 | + }) |
| 37 | + .option('tiller-namespace', { |
| 38 | + describe: 'Tiller namespace for helm2', |
| 39 | + }) |
| 40 | + .option('release-namespaces', { |
| 41 | + describe: 'List of namespaces for using', |
| 42 | + }) |
| 43 | + .example('codefresh set-helm-config cluster -v 2 -t pip1 -r pip2 -d pip3 --tiller-namespace kube-system', 'Helm2 config') |
| 44 | + .example('codefresh set-helm-config cluster -v 3 -t pip1 -r pip2 -d pip3 --release-namespaces ns1 --release-namespaces ns2', 'Helm3 config'); |
| 45 | + }, |
| 46 | + handler: async (argv) => { |
| 47 | + try { |
| 48 | + if (!['helm2', 'helm3'].includes(argv.version)) { |
| 49 | + throw new Error('Wrong version value'); |
| 50 | + } |
| 51 | + let options = {}; |
| 52 | + options.selector = argv.cluster; |
| 53 | + options.helmVersion = argv.version; |
| 54 | + |
| 55 | + const pipelines = await sdk.pipelines.getNames({}); |
| 56 | + |
| 57 | + if (argv.deleteReleasePipeline) { |
| 58 | + const pipeline = _.find(pipelines, p => p.metadata.name === argv.deleteReleasePipeline); |
| 59 | + options.deleteReleasePipeline = _.get(pipeline, 'metadata.id'); |
| 60 | + if (!options.deleteReleasePipeline) { |
| 61 | + throw new Error(`Pipeline ${argv.deleteReleasePipeline} not found`); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (argv.testReleasePipeline) { |
| 66 | + const pipeline = _.find(pipelines, p => p.metadata.name === argv.testReleasePipeline); |
| 67 | + options.testReleasePipeline = _.get(pipeline, 'metadata.id'); |
| 68 | + if (!options.testReleasePipeline) { |
| 69 | + throw new Error(`Pipeline ${argv.testReleasePipeline} not found`); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (argv.rollbackPipeline) { |
| 74 | + const pipeline = _.find(pipelines, p => p.metadata.name === argv.rollbackPipeline); |
| 75 | + options.rollbackPipeline = _.get(pipeline, 'metadata.id'); |
| 76 | + if (!options.rollbackPipeline) { |
| 77 | + throw new Error(`Pipeline ${argv.rollbackPipeline} not found`); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + options = _.merge(options, _.pick(argv, ['releaseNamespaces', 'tillerNamespace'])); |
| 82 | + |
| 83 | + await sdk.helm['cluster-config'].store(options); |
| 84 | + console.log('Helm config was set successfully'); |
| 85 | + process.exit(); |
| 86 | + } catch (err) { |
| 87 | + Output.printError(err); |
| 88 | + process.exit(1); |
| 89 | + } |
| 90 | + }, |
| 91 | +}); |
| 92 | + |
| 93 | +module.exports = install; |
0 commit comments