forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodo.ts
More file actions
316 lines (285 loc) · 13.8 KB
/
odo.ts
File metadata and controls
316 lines (285 loc) · 13.8 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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as cliInstance from './cli';
import { TreeItem, ProviderResult, TreeItemCollapsibleState, window, Terminal, Uri, commands, QuickPickItem } from 'vscode';
import { WindowUtil } from './util/windowUtils';
import { CliExitData } from './cli';
import * as path from 'path';
import jsYaml = require('js-yaml');
import { Platform } from './util/platform';
import * as fs from 'fs';
import { ToolsConfig } from './tools';
import { create } from 'domain';
export interface OpenShiftObject extends QuickPickItem {
getTreeItem(): TreeItem;
getChildren(): ProviderResult<OpenShiftObject[]>;
getParent(): OpenShiftObject;
getName(): string;
}
export interface OpenShiftApplication extends OpenShiftObject {
}
export interface OpenShiftComponent extends OpenShiftObject {
readonly url: string;
}
class OpenShiftObjectImpl implements OpenShiftObject {
constructor(private parent: OpenShiftObject, public readonly name, private readonly context, private readonly odo: Odo, private readonly expandable: TreeItemCollapsibleState = TreeItemCollapsibleState.Collapsed) {
}
get label(): string {
return this.getName();
}
getName(): string {
return this.name;
}
getTreeItem(): TreeItem {
const item = new TreeItem(this.name, this.expandable);
const contextType = {
cluster: ()=> {
item.iconPath = Uri.file(path.join(__dirname, "../../images/cluster.png"));
},
project: ()=> {
item.iconPath = Uri.file(path.join(__dirname, "../../images/project.png"));
item.tooltip = `Project: ${this.name}`;
},
application: ()=> {
item.iconPath = Uri.file(path.join(__dirname, "../../images/application.png"));
item.tooltip = `Application: ${this.name}`;
},
component: ()=> {
item.iconPath = Uri.file(path.join(__dirname, "../../images/component.png"));
item.tooltip = `Component: ${this.name}`;
},
service: ()=> {
item.iconPath = Uri.file(path.join(__dirname, "../../images/service.png"));
item.tooltip = `Service: ${this.name}`;
},
storage: ()=> {
item.iconPath = Uri.file(path.join(__dirname, "../../images/storage.png"));
item.tooltip = `Storage: ${this.name}`;
},
ClusterError: ()=> {
item.tooltip = 'Cannot connect to cluster';
item.iconPath = Uri.file(path.join(__dirname, "../../images/cluster-down.png"));
},
LoginError: ()=> {
item.tooltip = 'Log in to cluster';
item.iconPath = Uri.file(path.join(__dirname, "../../images/cluster-down.png"));
}
};
(contextType[this.context])();
item.contextValue = this.context;
return item;
}
getChildren(): ProviderResult<OpenShiftObject[]> {
if (this.context === 'project') {
return this.odo.getApplications(this);
} else if (this.context === 'application') {
return this.odo.getApplicationChildren(this);
} else if (this.context === 'component') {
return this.odo.getStorageNames(this);
} if (this.context === 'cluster') {
return this.odo.getProjects();
} else {
return [];
}
}
getParent(): OpenShiftObject {
return this.parent;
}
}
export interface Odo {
getClusters(): Promise<OpenShiftObject[]>;
getProjects(): Promise<OpenShiftObject[]>;
getApplications(project: OpenShiftObject): Promise<OpenShiftObject[]>;
getComponents(application: OpenShiftObject): Promise<OpenShiftObject[]>;
executeInTerminal(command: string, cwd?: string): void;
getComponentTypes(): Promise<string[]>;
getStorageNames(component: OpenShiftObject): Promise<OpenShiftObject[]>;
getComponentTypeVersions(componentName: string): Promise<string[]>;
getServiceTemplates(): Promise<string[]>;
getServiceTemplatePlans(svc: string): Promise<string[]>;
getServices(application: OpenShiftObject): Promise<OpenShiftObject[]>;
getApplicationChildren(application: OpenShiftObject): Promise<OpenShiftObject[]>;
execute(command: string, cwd?: string, fail?: boolean): Promise<CliExitData>;
requireLogin(): Promise<boolean>;
}
export function getInstance(): Odo {
return OdoImpl.getInstance();
}
export class OdoImpl implements Odo {
private static cli: cliInstance.ICli = cliInstance.Cli.getInstance();
private static instance: OdoImpl;
private constructor() {}
public static getInstance(): OdoImpl {
if (!OdoImpl.instance) {
OdoImpl.instance = new OdoImpl();
}
return OdoImpl.instance;
}
public async getProjects(): Promise<OpenShiftObject[]> {
return this.execute(
'oc get project -o jsonpath="{range .items[*]}{.metadata.name}{\\"\\n\\"}{end}"'
).then((result) => {
let projs: OpenShiftObject[] = [];
const stdout: string = result.stdout.trim();
if (stdout !== "" ) {
projs = stdout.split("\n").map<OpenShiftObject>((value) => new OpenShiftObjectImpl(undefined, value, 'project', this));
}
return projs;
}).catch((error) => {
window.showErrorMessage(`Cannot retrieve projects for current cluster. Error: ${error}`);
return [];
});
}
public async getApplications(project: OpenShiftObjectImpl): Promise<OpenShiftObject[]> {
const odoData = jsYaml.safeLoad(fs.readFileSync(path.join(Platform.getUserHomePath(), '.kube', 'odo'), 'utf8'));
const activeApps: any[] = odoData && odoData.activeApplications ? odoData.activeApplications : [];
const apps: string[] = activeApps.filter((value) => value.project === project.getName()).map((value) => value.name);
return apps.map<OpenShiftObject>((value) => new OpenShiftObjectImpl(project, value, 'application', this));
}
public async getComponents(application: OpenShiftObjectImpl): Promise<OpenShiftObject[]> {
const proj = application.getParent().getName();
const result: cliInstance.CliExitData = await this.execute(
`oc get dc --namespace ${proj} -o jsonpath="{range .items[?(.metadata.labels.app == \\"${application.getName()}\\")]}{.metadata.labels.app\\.kubernetes\\.io/component-name}{\\"\\n\\"}{end}"`
);
return result.stdout.trim().split('\n').filter((value)=>value!=='').map<OpenShiftObject>((value) => new OpenShiftObjectImpl(application, value, 'component', this, TreeItemCollapsibleState.Collapsed));
}
public async getComponentTypes(): Promise<string[]> {
const result: cliInstance.CliExitData = await this.execute(
`odo catalog list components`
);
return result.stdout.trim().split('\n').slice(1).map((value) => {
const name = value.replace(/\*/g, '').trim().replace(/\s{1,}/g, '|').split('|');
return name[0];
});
}
public async getStorageNames(component: OpenShiftObjectImpl): Promise<OpenShiftObject[]> {
const app = component.getParent();
const appName = app.getName();
const projName = app.getParent().getName();
const result: cliInstance.CliExitData = await this.execute(
`oc get pvc -o jsonpath="{range .items[?(.metadata.labels.app == \\"${appName}\\")]}{.metadata.labels.app\\.kubernetes\\.io/component-name}{\\" \\"}{.metadata.labels.app\\.kubernetes\\.io/storage-name}{\\"\\n\\"}{end}" --namespace ${projName}`
);
return result.stdout.trim().split('\n').filter((value) => value.trim().split(' ').length > 1 && value.trim().split(' ')[0] === component.getName()).map((value) => {
const name = value.split(' ');
return new OpenShiftObjectImpl(component, `${name[1]}`, 'storage', this, TreeItemCollapsibleState.None);
});
}
public async getComponentTypeVersions(componentName: string) {
const result: cliInstance.CliExitData = await this.execute(
`odo catalog list components`
);
const versions = result.stdout.trim().split('\n').slice(1).filter((value) => {
const data = value.replace(/\*/g, '').trim().replace(/\s{1,}/g, '|').split('|');
return data[0] === componentName;
}).map((value) => {
return value.replace(/\*/g, '').trim().replace(/\s{1,}/g, '|').split('|')[2];
});
return versions[0].split(',');
}
public async getClusters(): Promise<OpenShiftObject[]> {
let clusters: OpenShiftObject[] = await this.getClustersWithOdo();
if (clusters.length === 0) {
clusters = await this.getClustersWithOc();
}
return clusters;
}
private async getClustersWithOc(): Promise<OpenShiftObject[]> {
let clusters: OpenShiftObject[] = [];
const result: cliInstance.CliExitData = await this.execute(`oc version`, process.cwd(), false);
clusters = result.stdout.trim().split('\n').filter((value) => {
return value.indexOf('Server ') !== -1;
}).map((value) => {
const server: string = value.substr(value.indexOf(' ')+1).trim();
return new OpenShiftObjectImpl(null, server, 'cluster', this, TreeItemCollapsibleState.Expanded);
});
return clusters;
}
private async getClustersWithOdo(): Promise<OpenShiftObject[]> {
let clusters: OpenShiftObject[] = [];
const result: cliInstance.CliExitData = await this.execute(
`odo version && odo project list`
);
if (result.stdout.indexOf('Please log in to the cluster') > -1) {
const loginErrorMsg: string = 'Please log in to the cluster';
return[new OpenShiftObjectImpl(null, loginErrorMsg, 'LoginError', this, TreeItemCollapsibleState.None)];
}
if (result.stdout.indexOf("Unable to connect to OpenShift cluster, is it down?") > -1) {
const clusterDownMsg: string = 'Please start the cluster';
return [new OpenShiftObjectImpl(null, clusterDownMsg, 'ClusterError', this, TreeItemCollapsibleState.None)];
}
commands.executeCommand('setContext', 'isLoggedIn', true);
clusters = result.stdout.trim().split('\n').filter((value) => {
return value.indexOf('Server:') !== -1;
}).map((value) => {
const server: string = value.substr(value.indexOf(':')+1).trim();
return new OpenShiftObjectImpl(null, server, 'cluster', this, TreeItemCollapsibleState.Expanded);
});
return clusters;
}
public async getServiceTemplates(): Promise<string[]> {
const result: cliInstance.CliExitData = await this.execute(
`odo catalog list services`
);
if (result.error) {
throw new Error(result.stdout.trim());
}
return result.stdout.trim().split('\n').slice(1).map((value) => {
const name = value.trim().replace(/\s{1,}/g, '|').split('|');
return name[0];
});
}
public async getServiceTemplatePlans(svcName: string): Promise<string[]> {
const result: cliInstance.CliExitData = await this.execute(
`odo catalog list services`
);
const plans = result.stdout.trim().split('\n').slice(1).filter((value) => {
const data = value.trim().replace(/\s{1,}/g, '|').split('|');
return data[0] === svcName;
}).map((value) => {
return value.trim().replace(/\s{1,}/g, '|').split('|')[1];
});
return plans[0].split(',');
}
public async getServices(application: OpenShiftObjectImpl): Promise<OpenShiftObject[]> {
const appName: string = application.getName();
const projName: string = application.getParent().getName();
let services: OpenShiftObject[] = [];
try {
const result: cliInstance.CliExitData = await this.execute(
`oc get ServiceInstance -o jsonpath="{range .items[?(.metadata.labels.app == \\"${appName}\\")]}{.metadata.labels.app\\.kubernetes\\.io/component-name}{\\"\\n\\"}{end}" --namespace ${projName}`
);
services = result.stdout.trim().split('\n').filter((value)=>value!=='').map((value) => new OpenShiftObjectImpl(application, value, 'service', this, TreeItemCollapsibleState.None));
} catch (e) {
// ignore error in case service catalog is not configured
}
return services;
}
public async getApplicationChildren(application: OpenShiftObjectImpl): Promise<OpenShiftObject[]> {
return [... await this.getComponents(application), ... await this.getServices(application)];
}
public executeInTerminal(command: string, cwd: string = process.cwd(), name: string = 'OpenShift') {
const terminal: Terminal = WindowUtil.createTerminal(name, cwd);
terminal.sendText(command, true);
terminal.show();
}
public async execute(command: string, cwd?: string, fail: boolean = true): Promise<CliExitData> {
const cmd = command.split(' ')[0];
const toolLocation = await ToolsConfig.detectOrDownload(cmd);
return OdoImpl.cli.execute(
toolLocation ? command.replace(cmd, `"${toolLocation}"`).replace(new RegExp(`&& ${cmd}`, 'g'), `&& "${toolLocation}"`) : command,
cwd ? {cwd} : { }
).then(async (result) => {
if (result.error && fail) {
return Promise.reject(result.error);
} else {
return result;
}
});
}
public async requireLogin(): Promise<boolean> {
const result: cliInstance.CliExitData = await this.execute(`odo version && odo project list`);
return result.stdout.indexOf("Please log in to the cluster") > -1;
}
}