|
1 | 1 | const debug = require('debug')('codefresh:cli:create:context');
|
| 2 | +const _ = require('lodash'); |
2 | 3 | const Command = require('../../Command');
|
3 | 4 | const fs = require('fs');
|
4 | 5 | const { spawn } = require('child_process');
|
5 | 6 | const { homedir } = require('os');
|
6 | 7 | const rp = require('request-promise');
|
7 | 8 | const createRoot = require('../root/create.cmd');
|
8 | 9 | const authManager = require('../../../../logic/auth').manager; // eslint-disable-line
|
| 10 | +const { cluster } = require('../../../../logic').api; |
| 11 | +const CFError = require('cf-errors'); |
9 | 12 |
|
10 | 13 | const scriptUrl = 'https://raw.githubusercontent.com/codefresh-io/k8s-dind-config/master/codefresh-k8s-configure.sh';
|
11 | 14 | let filePath = `${homedir()}/.Codefresh/runtime/codefresh-k8s-configure.sh`;
|
12 | 15 | const dirPath = `${homedir()}/.Codefresh/runtime`;
|
13 | 16 | const codefreshPath = `${homedir()}/.Codefresh`;
|
14 | 17 |
|
15 | 18 |
|
16 |
| -const callToScript = (k8sScript) =>{ |
| 19 | +const callToScript = (k8sScript) => { |
17 | 20 | k8sScript.stdout.pipe(process.stdout);
|
18 | 21 | k8sScript.stderr.pipe(process.stderr);
|
19 | 22 | process.stdin.pipe(k8sScript.stdin);
|
@@ -41,49 +44,61 @@ const command = new Command({
|
41 | 44 | },
|
42 | 45 | builder: (yargs) => {
|
43 | 46 | return yargs
|
44 |
| - .positional('cluster', { |
45 |
| - describe: 'cluster name', |
| 47 | + .option('kubernetes-cluster', { |
| 48 | + describe: 'kubernetes cluster name', |
| 49 | + alias: 'kc', |
46 | 50 | required: true,
|
47 | 51 | })
|
48 | 52 | .option('namespace', {
|
49 | 53 | describe: 'namespace',
|
| 54 | + alias: 'n', |
| 55 | + default: 'codefresh', |
50 | 56 | })
|
51 | 57 | .option('context', {
|
52 |
| - describe: 'set your kubectl context', |
| 58 | + describe: 'set the desire kubernetes context', |
53 | 59 | })
|
54 |
| - .example('codefresh create re [cluster] --namespace codefresh --context kubeCodefresh', 'Creating a runtime environment'); |
| 60 | + .example('codefresh create re --kubernetes-cluster prod --namespace codefresh --context kubeCodefresh', 'Creating a runtime environment which configured to cluster prod and namespace codefresh'); |
55 | 61 | },
|
56 | 62 | handler: async (argv) => {
|
57 | 63 | const currentContext = authManager.getCurrentContext();
|
58 |
| - const { namespace, cluster } = argv; |
| 64 | + const { namespace } = argv; |
| 65 | + const clusterName = argv['kubernetes-cluster']; |
59 | 66 | let { context } = argv;
|
60 | 67 | if (!context) {
|
61 | 68 | context = '';
|
62 | 69 | }
|
63 |
| - if (!process.env.LOCAL) { |
64 |
| - if (!fs.existsSync(codefreshPath)) { |
65 |
| - fs.mkdirSync(codefreshPath); |
66 |
| - fs.mkdirSync(dirPath); |
67 |
| - } else if (!fs.existsSync(dirPath)) { |
68 |
| - fs.mkdirSync(dirPath); |
69 |
| - } |
70 |
| - const options = { |
71 |
| - url: scriptUrl, |
72 |
| - method: 'GET', |
73 |
| - }; |
74 |
| - const response = await rp(options); |
75 |
| - fs.writeFile(filePath, response, (err) => { |
76 |
| - if (err) { |
77 |
| - throw err; |
| 70 | + const clusters = await cluster.getAllClusters(); |
| 71 | + const validCluster = _.find(clusters, (c) => { |
| 72 | + return _.isEqual(c.info.name, clusterName); |
| 73 | + }); |
| 74 | + if (validCluster) { |
| 75 | + if (!process.env.LOCAL) { |
| 76 | + if (!fs.existsSync(codefreshPath)) { |
| 77 | + fs.mkdirSync(codefreshPath); |
| 78 | + fs.mkdirSync(dirPath); |
| 79 | + } else if (!fs.existsSync(dirPath)) { |
| 80 | + fs.mkdirSync(dirPath); |
78 | 81 | }
|
79 |
| - fs.chmodSync(filePath, '644'); |
80 |
| - const k8sScript = spawn('bash', [filePath, '--api-token', currentContext.token, '--api-host', currentContext.url, '--namespace', namespace, '--image-tag', 'master', '--remote', '--context', context, cluster]); |
| 82 | + const options = { |
| 83 | + url: scriptUrl, |
| 84 | + method: 'GET', |
| 85 | + }; |
| 86 | + const response = await rp(options); |
| 87 | + fs.writeFile(filePath, response, (err) => { |
| 88 | + if (err) { |
| 89 | + throw err; |
| 90 | + } |
| 91 | + fs.chmodSync(filePath, '644'); |
| 92 | + const k8sScript = spawn('bash', [filePath, '--api-token', currentContext.token, '--api-host', currentContext.url, '--namespace', namespace, '--image-tag', 'master', '--remote', '--context', context, clusterName]); |
| 93 | + callToScript(k8sScript); |
| 94 | + }); |
| 95 | + } else { |
| 96 | + filePath = './codefresh-k8s-configure.sh'; |
| 97 | + const k8sScript = spawn('bash', [filePath, '--api-token', currentContext.token, '--api-host', currentContext.url, '--namespace', namespace,'--context', context, '--image-tag', 'master', clusterName]); |
81 | 98 | callToScript(k8sScript);
|
82 |
| - }); |
| 99 | + } |
83 | 100 | } else {
|
84 |
| - filePath = './codefresh-k8s-configure.sh'; |
85 |
| - const k8sScript = spawn('bash', [filePath, '--api-token', currentContext.token, '--api-host', currentContext.url, '--namespace', namespace,'--context', context, '--image-tag', 'master', cluster]); |
86 |
| - callToScript(k8sScript); |
| 101 | + throw new CFError(`No cluster exists with the name: ${clusterName}`); |
87 | 102 | }
|
88 | 103 | },
|
89 | 104 | });
|
|
0 commit comments