Skip to content

Commit 9a33e73

Browse files
update new flag to create re and cluster (#238)
1 parent 325d423 commit 9a33e73

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

lib/interface/cli/commands/cluster/create.cmd.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ const command = new Command({
1616
},
1717
builder: (yargs) => {
1818
return yargs
19-
.positional('name', {
20-
describe: 'cluster name',
19+
.option('kubernetes-cluster', {
20+
describe: 'kubernetes cluster name',
21+
alias: 'kc',
2122
required: true,
2223
})
23-
.example('codefresh create cluster [name]', 'Creating a cluster');
24+
.example('codefresh create cluster --kubernetes-cluster production', 'Creating a cluster in codefresh');
2425
},
2526
handler: async (argv) => {
2627
const context = authManager.getCurrentContext();
27-
const { name } = argv;
28+
const name = argv['kubernetes-cluster'];
2829
await cluster.createCluster({
2930
name,
3031
context,

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

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
const debug = require('debug')('codefresh:cli:create:context');
2+
const _ = require('lodash');
23
const Command = require('../../Command');
34
const fs = require('fs');
45
const { spawn } = require('child_process');
56
const { homedir } = require('os');
67
const rp = require('request-promise');
78
const createRoot = require('../root/create.cmd');
89
const authManager = require('../../../../logic/auth').manager; // eslint-disable-line
10+
const { cluster } = require('../../../../logic').api;
11+
const CFError = require('cf-errors');
912

1013
const scriptUrl = 'https://raw.githubusercontent.com/codefresh-io/k8s-dind-config/master/codefresh-k8s-configure.sh';
1114
let filePath = `${homedir()}/.Codefresh/runtime/codefresh-k8s-configure.sh`;
1215
const dirPath = `${homedir()}/.Codefresh/runtime`;
1316
const codefreshPath = `${homedir()}/.Codefresh`;
1417

1518

16-
const callToScript = (k8sScript) =>{
19+
const callToScript = (k8sScript) => {
1720
k8sScript.stdout.pipe(process.stdout);
1821
k8sScript.stderr.pipe(process.stderr);
1922
process.stdin.pipe(k8sScript.stdin);
@@ -41,49 +44,61 @@ const command = new Command({
4144
},
4245
builder: (yargs) => {
4346
return yargs
44-
.positional('cluster', {
45-
describe: 'cluster name',
47+
.option('kubernetes-cluster', {
48+
describe: 'kubernetes cluster name',
49+
alias: 'kc',
4650
required: true,
4751
})
4852
.option('namespace', {
4953
describe: 'namespace',
54+
alias: 'n',
55+
default: 'codefresh',
5056
})
5157
.option('context', {
52-
describe: 'set your kubectl context',
58+
describe: 'set the desire kubernetes context',
5359
})
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');
5561
},
5662
handler: async (argv) => {
5763
const currentContext = authManager.getCurrentContext();
58-
const { namespace, cluster } = argv;
64+
const { namespace } = argv;
65+
const clusterName = argv['kubernetes-cluster'];
5966
let { context } = argv;
6067
if (!context) {
6168
context = '';
6269
}
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);
7881
}
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]);
8198
callToScript(k8sScript);
82-
});
99+
}
83100
} 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}`);
87102
}
88103
},
89104
});

0 commit comments

Comments
 (0)