forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.ts
More file actions
355 lines (296 loc) · 18.5 KB
/
component.ts
File metadata and controls
355 lines (296 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { OpenShiftItem } from './openshiftItem';
import { OpenShiftObject, Command } from '../odo';
import { window, commands, QuickPickItem, Uri } from 'vscode';
import { Progress } from '../util/progress';
import open = require('open');
import { ChildProcess } from 'child_process';
import { CliExitData } from '../cli';
import { V1ServicePort, V1Service } from '@kubernetes/client-node';
import { isURL } from 'validator';
import { Refs, Ref, Type } from '../util/refs';
import { Delayer } from '../util/async';
import { contextGlobalState } from '../extension';
export class Component extends OpenShiftItem {
static async getOpenshiftData(context: OpenShiftObject): Promise<OpenShiftObject> {
return await Component.getOpenShiftCmdData(context,
"In which Project you want to create a Component",
"In which Application you want to create a Component"
);
}
static async create(context: OpenShiftObject): Promise<string> {
const application = await Component.getOpenshiftData(context);
if (!application) return null;
const sourceTypes: QuickPickItem[] = [
{
label: 'Git Repository',
description: 'Use an existing git repository as a source for the Component'
},
{
label: 'Binary File',
description: 'Use binary file as a source for the Component'
},
{
label: 'Workspace Directory',
description: 'Use workspace directory as a source for the Component'
}
];
const componentSource = await window.showQuickPick(sourceTypes, {
placeHolder: "Select source type for Component"
});
if (!componentSource) return null;
let command: Promise<string>;
if (componentSource.label === 'Git Repository') {
command = Component.createFromGit(application);
} else if (componentSource.label === 'Binary File') {
command = Component.createFromBinary(application);
} else {
command = Component.createFromLocal(application);
}
return command.catch((err) => Promise.reject(`Failed to create Component with error '${err}'`));
}
static async del(treeItem: OpenShiftObject): Promise<string> {
const component = await Component.getOpenShiftCmdData(treeItem,
"From which Project do you want to delete Component",
"From which Application you want to delete Component",
"Select Component to delete");
if (!component) return null;
const name: string = component.getName();
const value = await window.showWarningMessage(`Do you want to delete Component '${name}\'?`, 'Yes', 'Cancel');
if (value === 'Yes') {
return Progress.execFunctionWithProgress(`Deleting the Component '${component.getName()} '`, () => {
return Component.odo.deleteComponent(component);
}).then(() => `Component '${name}' successfully deleted`)
.catch((err) => Promise.reject(`Failed to delete Component with error '${err}'`));
}
}
static async describe(context: OpenShiftObject): Promise<string> {
const component = await Component.getOpenShiftCmdData(context,
"From which Project you want to describe Component",
"From which Application you want to describe Component",
"Select Component you want to describe");
if (!component) return null;
Component.odo.executeInTerminal(Command.describeComponent(component.getParent().getParent().getName(), component.getParent().getName(), component.getName()));
}
static async log(context: OpenShiftObject): Promise<string> {
const component = await Component.getOpenShiftCmdData(context,
"In which Project you want to see Log",
"In which Application you want to see Log",
"For which Component you want to see Log");
if (!component) return null;
Component.odo.executeInTerminal(Command.showLog(component.getParent().getParent().getName(), component.getParent().getName(), component.getName()));
}
static async followLog(context: OpenShiftObject): Promise<string> {
const component = await Component.getOpenShiftCmdData(context,
"In which Project you want to follow Log",
"In which Application you want to follow Log",
"For which Component you want to follow Log"
);
if (!component) return null;
Component.odo.executeInTerminal(Command.showLogAndFollow(component.getParent().getParent().getName(), component.getParent().getName(), component.getName()));
}
static async linkComponent(context: OpenShiftObject): Promise<String> {
const component = await Component.getOpenShiftCmdData(context,
'Select a Project',
'Select an Application',
'Select a Component');
if (!component) return null;
const componentPresent = await Component.odo.getComponents(component.getParent());
if (componentPresent.length === 1) throw Error('You have no Components available to link, please create new OpenShift Component and try again.');
const componentToLink = await window.showQuickPick(componentPresent.filter((comp)=> comp.getName() !== component.getName()), {placeHolder: "Select a Component to link"});
if (!componentToLink) return null;
const portsResult: CliExitData = await Component.odo.execute(Command.listComponentPorts(component.getParent().getParent().getName(), component.getParent().getName(), componentToLink.getName()));
let ports: string[] = portsResult.stdout.trim().split(',');
ports = ports.slice(0, ports.length-1);
let port: string;
if (ports.length === 1) {
port = ports[0];
} else if (ports.length > 1) {
port = await window.showQuickPick(ports, {placeHolder: "Select Port to link"});
} else {
return Promise.reject(`Component '${component.getName()}' has no Ports declared.`);
}
return Progress.execFunctionWithProgress(`Link Component '${componentToLink.getName()}' with Component '${component.getName()}'`,
(progress) => Component.odo.execute(Command.linkComponentTo(component.getParent().getParent().getName(), component.getParent().getName(), component.getName(), componentToLink.getName(), port))
.then(() => `Component '${componentToLink.getName()}' successfully linked with Component '${component.getName()}'`)
.catch((err) => Promise.reject(`Failed to link component with error '${err}'`))
);
}
static async linkService(context: OpenShiftObject): Promise<String> {
const component = await Component.getOpenShiftCmdData(context,
'Select a Project',
'Select an Application',
'Select a Component');
if (!component) return null;
const serviceToLink: OpenShiftObject = await window.showQuickPick(Component.getServiceNames(component.getParent()), {placeHolder: "Select the service to link"});
if (!serviceToLink) return null;
return Progress.execFunctionWithProgress(`Link Service '${serviceToLink.getName()}' with Component '${component.getName()}'`,
(progress) => Component.odo.execute(Command.linkComponentTo(component.getParent().getParent().getName(), component.getParent().getName(), component.getName(), serviceToLink.getName()))
.then(() => `Service '${serviceToLink.getName()}' successfully linked with Component '${component.getName()}'`)
.catch((err) => Promise.reject(`Failed to link Service with error '${err}'`))
);
}
static getPushCmd(): Thenable< string | undefined> {
return contextGlobalState.globalState.get('PUSH');
}
static setPushCmd(component: string, application: string, project: string): Thenable<void> {
return contextGlobalState.globalState.update('PUSH', Command.pushComponent(project, application, component));
}
static async push(context: OpenShiftObject): Promise<string> {
const component = await Component.getOpenShiftCmdData(context,
"In which Project you want to push the changes",
"In which Application you want to push the changes",
"For which Component you want to push the changes");
if (!component) return null;
Component.setPushCmd(component.getName(), component.getParent().getName(), component.getParent().getParent().getName());
Component.odo.executeInTerminal(Command.pushComponent(component.getParent().getParent().getName(), component.getParent().getName(), component.getName()))
}
static async lastPush() {
const getPushCmd = await Component.getPushCmd();
if (getPushCmd) {
Component.odo.executeInTerminal(getPushCmd);
} else {
throw Error('No existing push command found');
}
}
static async watch(context: OpenShiftObject): Promise<void> {
const component = await Component.getOpenShiftCmdData(context,
'Select a Project',
'Select an Application',
'Select a Component you want to watch');
if (!component) return null;
Component.odo.executeInTerminal(Command.watchComponent(component.getParent().getParent().getName(), component.getParent().getName(), component.getName()));
}
static async openUrl(context: OpenShiftObject): Promise<ChildProcess> {
const component = await Component.getOpenShiftCmdData(context,
'Select a Project',
'Select an Application',
'Select a Component you want to open in browser');
if (!component) return null;
const app: OpenShiftObject = component.getParent();
const namespace: string = app.getParent().getName();
if (await Component.checkRouteCreated(namespace, component)) {
const value = await window.showInformationMessage(`No URL for Component '${component.getName()}' in Application '${app.getName()}'. Do you want to create a URL and open it?`, 'Create', 'Cancel');
if (value === 'Create') {
await commands.executeCommand('openshift.url.create', component);
}
}
if (! await Component.checkRouteCreated(namespace, component)) {
const UrlDetails = await Component.odo.execute(Command.getComponentUrl(namespace, app.getName(), component.getName()));
let result: any[] = [];
let selectRoute: QuickPickItem;
try {
result = JSON.parse(UrlDetails.stdout).items;
} catch (ignore) {
// should give empty list if no url configured
// see https://github.com/openshift/odo/issues/1515
}
const hostName: QuickPickItem[] = result.map((value) => ({ label: `${value.spec.protocol}://${value.spec.path}`, description: `Target Port is ${value.spec.port}`}));
if (hostName.length >1) {
selectRoute = await window.showQuickPick(hostName, {placeHolder: "This Component has multiple URLs. Select the desired URL to open in browser."});
if (!selectRoute) return null;
return open(`${selectRoute.label}`);
} else {
return open(`${hostName[0].label}`);
}
}
}
static async checkRouteCreated(namespace: string, component: OpenShiftObject): Promise<boolean> {
const routeCheck = await Component.odo.execute(Command.getRouteHostName(namespace, component.getName()));
return routeCheck.stdout.trim() === '';
}
static async createFromLocal(context: OpenShiftObject): Promise<string> {
let application: OpenShiftObject = context;
if (!application) application = await Component.getOpenshiftData(context);
if (!application) return null;
const folder = await window.showWorkspaceFolderPick({
placeHolder: 'Select the target workspace folder'
});
if (!folder) return null;
const componentList: Array<OpenShiftObject> = await Component.odo.getComponents(application);
const componentName = await Component.getName('Component name', componentList, application.getName());
if (!componentName) return null;
const componentTypeName = await window.showQuickPick(Component.odo.getComponentTypes(), {placeHolder: "Component type"});
if (!componentTypeName) return null;
const componentTypeVersion = await window.showQuickPick(Component.odo.getComponentTypeVersions(componentTypeName), {placeHolder: "Component type version"});
if (!componentTypeVersion) return null;
await Progress.execFunctionWithProgress(`Creating new Component '${componentName}'`, () => Component.odo.createComponentFromFolder(application, componentTypeName, componentTypeVersion, componentName, folder.uri.fsPath));
return `Component '${componentName}' successfully created`;
}
static async createFromFolder(folder: Uri): Promise<string> {
const application = await Component.getOpenShiftCmdData(undefined,
"In which Project you want to create a Component",
"In which Application you want to create a Component"
);
const componentList: Array<OpenShiftObject> = await Component.odo.getComponents(application);
const componentName = await Component.getName('Component name', componentList, application.getName());
if (!componentName) return null;
const componentTypeName = await window.showQuickPick(Component.odo.getComponentTypes(), {placeHolder: "Component type"});
if (!componentTypeName) return null;
const componentTypeVersion = await window.showQuickPick(Component.odo.getComponentTypeVersions(componentTypeName), {placeHolder: "Component type version"});
if (!componentTypeVersion) return null;
await Progress.execFunctionWithProgress(`Creating new Component '${componentName}'`, () => Component.odo.createComponentFromFolder(application, componentTypeName, componentTypeVersion, componentName, folder.fsPath));
return `Component '${componentName}' successfully created`;
}
static async createFromGit(context: OpenShiftObject): Promise<string> {
let application: OpenShiftObject = context;
if (!application) application = await Component.getOpenshiftData(context);
if (!application) return null;
const delayer = new Delayer<string>(500);
const repoURI = await window.showInputBox({
prompt: 'Git repository URI',
validateInput: (value: string) => {
return delayer.trigger(async () => {
if (!value.trim()) return 'Empty Git repository URL';
if (!isURL(value)) return 'Invalid URL provided';
const references = await Refs.fetchTag(value);
if (!references.get('HEAD')) return 'There is no git repository at provided URL.';
});
}
});
if (!repoURI) return null;
const references: Map<string, Ref> = await Refs.fetchTag(repoURI);
const gitRef = await window.showQuickPick([...references.values()].map(value => ({label: value.name, description: value.type === Type.TAG? `Tag at ${value.hash}` : value.hash })) , {placeHolder: "Select git reference (branch/tag)"});
if (!gitRef) return null;
const componentList: Array<OpenShiftObject> = await Component.odo.getComponents(application);
const componentName = await Component.getName('Component name', componentList, application.getName());
if (!componentName) return null;
const componentTypeName = await window.showQuickPick(Component.odo.getComponentTypes(), {placeHolder: "Component type"});
if (!componentTypeName) return null;
const componentTypeVersion = await window.showQuickPick(Component.odo.getComponentTypeVersions(componentTypeName), {placeHolder: "Component type version"});
if (!componentTypeVersion) return null;
await window.showInformationMessage('Do you want to clone git repository for created Component?', 'Yes', 'No').then((value) => {
value === 'Yes' && commands.executeCommand('git.clone', repoURI);
});
await Component.odo.createComponentFromGit(application, componentTypeName, componentTypeVersion, componentName, repoURI, gitRef.label);
return `Component '${componentName}' successfully created`;
}
static async createFromBinary(context: OpenShiftObject): Promise<string> {
let application: OpenShiftObject = context;
if (!application) application = await Component.getOpenshiftData(context);
if (!application) return null;
const binaryFile = await window.showOpenDialog({
openLabel: 'Select the binary file'
});
if (!binaryFile) return null;
const componentList: Array<OpenShiftObject> = await Component.odo.getComponents(application);
const componentName = await Component.getName('Component name', componentList, application.getName());
if (!componentName) return null;
const componentTypeName = await window.showQuickPick(Component.odo.getComponentTypes(), {placeHolder: "Component type"});
if (!componentTypeName) return null;
const componentTypeVersion = await window.showQuickPick(Component.odo.getComponentTypeVersions(componentTypeName), {placeHolder: "Component type version"});
if (!componentTypeVersion) return null;
await Component.odo.createComponentFromBinary(application, componentTypeName, componentTypeVersion, componentName, binaryFile[0].fsPath);
return `Component '${componentName}' successfully created`;
}
public static async getComponentPorts(context: OpenShiftObject): Promise<V1ServicePort[]> {
const app: OpenShiftObject = context.getParent();
const project: OpenShiftObject = app.getParent();
const portsResult: CliExitData = await Component.odo.execute(Command.getComponentJson(project.getName(), app.getName(), context.getName()));
const serviceOpj: V1Service = JSON.parse(portsResult.stdout) as V1Service;
return serviceOpj.spec.ports;
}
}