forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclusterViewLoader.ts
More file actions
413 lines (391 loc) · 20.9 KB
/
clusterViewLoader.ts
File metadata and controls
413 lines (391 loc) · 20.9 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { ChildProcess, spawn } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { CommandText } from '../../base/command';
import { Cluster } from '../../openshift/cluster';
import { createSandboxAPI } from '../../openshift/sandbox';
import { ExtCommandTelemetryEvent } from '../../telemetry';
import { ChildProcessUtil } from '../../util/childProcessUtil';
import { ExtensionID } from '../../util/constants';
import { vsCommand } from '../../vscommand';
import { loadWebviewHtml } from '../common-ext/utils';
import { OpenShiftTerminalManager } from '../openshift-terminal/openShiftTerminal';
let panel: vscode.WebviewPanel;
const channel: vscode.OutputChannel = vscode.window.createOutputChannel('OpenShift Local Logs');
const sandboxAPI = createSandboxAPI();
async function clusterEditorMessageListener (event: any ): Promise<any> {
let sessionCheck: vscode.AuthenticationSession;
if (event.action?.startsWith('sandbox')) {
sessionCheck = await vscode.authentication.getSession('redhat-account-auth', ['openid'], { createIfNone: false });
if(!sessionCheck && event.action !== 'sandboxLoginRequest') {
void panel.webview.postMessage({action: 'sandboxPageLoginRequired'});
return;
}
}
switch (event.action) {
case 'openLaunchSandboxPage':
case 'openCreateClusterPage':
case 'openCrcAddClusterPage':
case 'crcSetup':
case 'crcStart':
case 'crcStop':
await vscode.commands.executeCommand(`openshift.explorer.addCluster.${event.action}`, event);
break;
case 'crcSaveSettings':
void ClusterViewLoader.crcSaveSettings(event);
break;
case 'checksetting': {
const binaryFromSetting: string = vscode.workspace.getConfiguration('openshiftToolkit').get('crcBinaryLocation');
if (binaryFromSetting) {
await panel.webview.postMessage({action: 'crcsetting'});
void ClusterViewLoader.checkCrcStatus(binaryFromSetting, 'crcstatus', panel);
}
break;
}
case 'checkcrcstatus':
await ClusterViewLoader.checkCrcStatus(event.data, 'crcstatus', panel);
break;
case 'crcLogin':
void vscode.commands.executeCommand(
'openshift.explorer.login.credentialsLogin',
true,
event.url,
event.data.username,
event.data.password
);
break;
case 'sandboxCheckAuthSession':
void panel.webview.postMessage({action: 'sandboxPageDetectStatus'});
break;
case 'sandboxRequestSignup': {
const telemetryEventSignup = new ExtCommandTelemetryEvent('openshift.explorer.addCluster.sandboxRequestSignup');
try {
const signupResponse = await sandboxAPI.signUp((sessionCheck as any).idToken);
await panel.webview.postMessage({action: 'sandboxPageDetectStatus'});
if (!signupResponse) {
void vscode.window.showErrorMessage('Sign up request for OpenShift Sandbox failed, please try again.');
telemetryEventSignup.sendError('Sign up request for OpenShift Sandbox failed.');
} else {
telemetryEventSignup.send();
}
} catch(ex) {
void vscode.window.showErrorMessage('Sign up request for OpenShift Sandbox failed, please try again.');
telemetryEventSignup.sendError('Sign up request for OpenShift Sandbox timed out.');
}
break;
}
case 'sandboxLoginRequest': {
const telemetryEventLogin = new ExtCommandTelemetryEvent('openshift.explorer.addCluster.sandboxLoginRequest');
try {
const session: vscode.AuthenticationSession = await vscode.authentication.getSession('redhat-account-auth', ['openid'], { createIfNone: true });
if (session) {
await panel.webview.postMessage({action: 'sandboxPageDetectStatus'});
} else {
await panel.webview.postMessage({action: 'sandboxPageLoginRequired'});
}
telemetryEventLogin.send();
} catch (ex) {
await panel.webview.postMessage({action: 'sandboxPageLoginRequired'});
telemetryEventLogin.sendError('Request for authentication session failed.');
}
break;
}
case 'sandboxDetectStatus': {
const telemetryEventDetect = new ExtCommandTelemetryEvent('openshift.explorer.addCluster.sandboxDetectStatus');
try {
const signupStatus = await sandboxAPI.getSignUpStatus((sessionCheck as any).idToken);
if (!signupStatus) {
// User does not signed up for sandbox, show sign up for sandbox page
await panel.webview.postMessage({action: 'sandboxPageRequestSignup'});
} else {
if (signupStatus.status.ready) {
const oauthInfo = await sandboxAPI.getOauthServerInfo(signupStatus.apiEndpoint);
let errCode = '';
if (!Cluster.validateLoginToken(await vscode.env.clipboard.readText())) {
errCode = 'invalidToken';
}
await panel.webview.postMessage({ action: 'sandboxPageProvisioned', statusInfo: signupStatus.username, consoleDashboard: signupStatus.consoleURL, apiEndpoint: signupStatus.apiEndpoint, oauthTokenEndpoint: oauthInfo.token_endpoint, errorCode: errCode });
await pollClipboard(signupStatus);
} else {
// cluster is not ready and the reason is
if (signupStatus.status.verificationRequired) {
await panel.webview.postMessage({action: 'sandboxPageRequestVerificationCode'});
} else {
// user phone number verified
//
if (signupStatus.status.reason === 'PendingApproval') {
await panel.webview.postMessage({action: 'sandboxPageWaitingForApproval'});
} else {
await panel.webview.postMessage({action: 'sandboxPageWaitingForProvision'})
}
}
}
}
telemetryEventDetect.send();
} catch (ex) {
void vscode.window.showErrorMessage('OpenShift Sandbox status request timed out, please try again.');
await panel.webview.postMessage({action: 'sandboxPageDetectStatus', errorCode: 'statusDetectionError'});
telemetryEventDetect.sendError('OpenShift Sandbox status request timed out.');
}
break;
}
case 'sandboxRequestVerificationCode': {
const telemetryEventRequestCode = new ExtCommandTelemetryEvent('openshift.explorer.addCluster.sandboxRequestVerificationCode');
try {
const requestStatus = await sandboxAPI.requestVerificationCode((sessionCheck as any).idToken, event.payload.fullCountryCode, event.payload.rawPhoneNumber);
if (requestStatus.ok) {
await panel.webview.postMessage({action: 'sandboxPageEnterVerificationCode'});
telemetryEventRequestCode.send();
} else {
void vscode.window.showErrorMessage(`Request for verification code failed: ${requestStatus.json.details}`);
await panel.webview.postMessage({action: 'sandboxPageRequestVerificationCode'});
telemetryEventRequestCode.sendError('Request for verification code failed.');
}
} catch (ex) {
void vscode.window.showErrorMessage('Request for verification code timed out, please try again.');
await panel.webview.postMessage({action: 'sandboxPageRequestVerificationCode'});
telemetryEventRequestCode.sendError('Request for verification code timed out.');
}
break;
}
case 'sandboxValidateVerificationCode': {
const telemetryEventValidateCode = new ExtCommandTelemetryEvent('openshift.explorer.addCluster.sandboxValidateVerificationCode');
try {
const requestStatus = await sandboxAPI.validateVerificationCode((sessionCheck as any).idToken, event.payload.verificationCode);
if (requestStatus) {
await panel.webview.postMessage({action: 'sandboxPageDetectStatus'});
telemetryEventValidateCode.send();
} else {
void vscode.window.showErrorMessage('Verification code does not match, please try again.');
await panel.webview.postMessage({action: 'sandboxPageEnterVerificationCode', errorCode: 'verificationFailed'});
telemetryEventValidateCode.sendError('Verification code does not match');
}
} catch(ex) {
void vscode.window.showErrorMessage('Verification code validation request timed out, please try again.');
await panel.webview.postMessage({action: 'sandboxPageEnterVerificationCode', errorCode: 'verificationFailed'});
telemetryEventValidateCode.sendError('Verification code validation request failed');
}
break;
}
case 'sandboxLoginUsingDataInClipboard': {
const telemetryEventLoginToSandbox = new ExtCommandTelemetryEvent('openshift.explorer.addCluster.sandboxLoginUsingDataInClipboard');
try {
const result = await Cluster.loginUsingClipboardToken(event.payload.apiEndpointUrl, event.payload.oauthRequestTokenUrl);
if (result) void vscode.window.showInformationMessage(`${result}`);
telemetryEventLoginToSandbox.send();
} catch (err) {
void vscode.window.showErrorMessage(err.message);
telemetryEventLoginToSandbox.sendError('Login into Sandbox Cluster failed.');
}
break;
}
default:
void vscode.window.showErrorMessage(`Unexpected message from webview: '${event.action}'`);
break;
}
}
async function pollClipboard(signupStatus) {
const oauthInfo = await sandboxAPI.getOauthServerInfo(signupStatus.apiEndpoint);
while (panel) {
const previousContent = await vscode.env.clipboard.readText();
await new Promise(r => setTimeout(r, 500));
const currentContent = await vscode.env.clipboard.readText();
if (previousContent && previousContent !== currentContent) {
let errCode = '';
if (!Cluster.validateLoginToken(currentContent)){
errCode = 'invalidToken';
}
void panel.webview.postMessage({action: 'sandboxPageProvisioned', statusInfo: signupStatus.username, consoleDashboard: signupStatus.consoleURL, apiEndpoint: signupStatus.apiEndpoint, oauthTokenEndpoint: oauthInfo.token_endpoint, errorCode: errCode});
}
}
}
export default class ClusterViewLoader {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
static get extensionPath() {
return vscode.extensions.getExtension(ExtensionID).extensionPath
}
@vsCommand('openshift.explorer.addCluster.openLaunchSandboxPage')
static async openLaunchSandboxPage(url: string) {
// fake command to report crc selection through telemetry
}
@vsCommand('openshift.explorer.addCluster.openCreateClusterPage')
static async openCreateClusterPage(url: string) {
// fake command to report crc selection through telemetry
}
@vsCommand('openshift.explorer.addCluster.openCrcAddClusterPage')
static async openCrcAddClusterPage() {
const toolsJsonPath = vscode.Uri.file(path.join(ClusterViewLoader.extensionPath, 'src/tools.json'));
let crc: string, crcOpenShift: string;
try {
const content = fs.readFileSync(toolsJsonPath.fsPath, { encoding: 'utf-8' });
const json = JSON.parse(content);
crc = json.crc.crcVersion;
crcOpenShift = json.crc.openshiftVersion;
} catch (err) {
const telemetryEventLoginToSandbox = new ExtCommandTelemetryEvent('openshift.explorer.addCluster.openCrcAddClusterPage');
crc = '',
crcOpenShift = '';
void vscode.window.showErrorMessage(err.message);
telemetryEventLoginToSandbox.sendError('Unable to fetch CRC and OpenshiftCRC version');
} finally {
await panel.webview.postMessage(
{
action: 'openCrcAddClusterPage',
crc,
openShiftCRC: crcOpenShift
});
}
// fake command to report crc selection through telemetry
}
@vsCommand('openshift.explorer.addCluster.crcSetup')
static async crcSetup(event: any) {
await OpenShiftTerminalManager.getInstance().executeInTerminal(new CommandText(`${event.data.tool} setup`), undefined, 'OpenShift Local Setup');
}
@vsCommand('openshift.explorer.addCluster.crcStart')
static crcStart(event: any): Promise<void> {
let startProcess: ChildProcess;
channel.show();
if (event.isSetting) {
const binaryFromSetting: string = vscode.workspace.getConfiguration('openshiftToolkit').get('crcBinaryLocation');
const pullSecretFromSetting: string = vscode.workspace.getConfiguration('openshiftToolkit').get('crcPullSecretPath');
const cpuFromSetting: string = vscode.workspace.getConfiguration('openshiftToolkit').get('crcCpuCores');
const memoryFromSetting: string = vscode.workspace.getConfiguration('openshiftToolkit').get('crcMemoryAllocated');
const nameserver = vscode.workspace.getConfiguration('openshiftToolkit').get<string>('crcNameserver');
const nameserverOption = nameserver ? ['-n', nameserver] : [];
const crcOptions = ['start', '-p', `${pullSecretFromSetting}`, '-c', `${cpuFromSetting}`, '-m', `${memoryFromSetting}`, ...nameserverOption, '-o', 'json'];
startProcess = spawn(`${binaryFromSetting}`, crcOptions);
channel.append(`\n\n"${binaryFromSetting}" ${crcOptions.join(' ')}\n`);
} else {
startProcess = spawn(`${event.data.tool}`, event.data.options.split(' '));
channel.append(`\n\n"${event.data.tool}" ${event.data.options}\n`);
}
startProcess.stdout.setEncoding('utf8');
startProcess.stderr.setEncoding('utf8');
startProcess.stdout.on('data', (chunk) => {
channel.append(chunk);
});
startProcess.stderr.on('data', (chunk) => {
channel.append(chunk);
});
startProcess.on('close', (code) => {
const message = `'crc start' exited with code ${code}`;
channel.append(message);
if (code !== 0) {
void vscode.window.showErrorMessage(message);
}
const binaryLoc = event.isSetting ? vscode.workspace.getConfiguration('openshiftToolkit').get('crcBinaryLocation'): event.crcLoc;
void ClusterViewLoader.checkCrcStatus(binaryLoc, 'crcstartstatus', panel);
});
startProcess.on('error', (err) => {
const message = `'crc start' execution failed with error: '${err.message}'`;
channel.append(message);
void vscode.window.showErrorMessage(message);
});
return Promise.resolve();
}
@vsCommand('openshift.explorer.addCluster.crcStop')
static crcStop(event): Promise<void> {
let filePath: string;
channel.show();
if (event.data.tool === '') {
filePath = vscode.workspace.getConfiguration('openshiftToolkit').get('crcBinaryLocation');
} else {
filePath = event.data.tool;
}
const stopProcess = spawn(`${filePath}`, ['stop']);
channel.append(`\n\n"${filePath}" stop\n`);
stopProcess.stdout.setEncoding('utf8');
stopProcess.stderr.setEncoding('utf8');
stopProcess.stdout.on('data', (chunk) => {
channel.append(chunk);
});
stopProcess.stderr.on('data', (chunk) => {
channel.append(chunk);
});
stopProcess.on('close', (code) => {
const message = `'crc stop' exited with code ${code}`;
channel.append(message);
if (code !== 0) {
void vscode.window.showErrorMessage(message);
}
void ClusterViewLoader.checkCrcStatus(filePath, 'crcstopstatus', panel);
});
stopProcess.on('error', (err) => {
const message = `'crc stop' execution filed with error: '${err.message}'`;
channel.append(message);
void vscode.window.showErrorMessage(message);
});
return Promise.resolve();
}
@vsCommand('openshift.explorer.addCluster')
static async add(value: string): Promise<void> {
const webViewPanel: vscode.WebviewPanel = await ClusterViewLoader.loadView('Add OpenShift Cluster');
if(value?.length > 0){
await webViewPanel.webview.postMessage({action: 'cluster', param: value});
}
}
static async crcSaveSettings(event) {
const cfg = vscode.workspace.getConfiguration('openshiftToolkit');
await cfg.update('crcBinaryLocation', event.crcLoc, vscode.ConfigurationTarget.Global);
await cfg.update('crcPullSecretPath', event.pullSecret, vscode.ConfigurationTarget.Global);
await cfg.update('crcCpuCores', event.cpuSize, vscode.ConfigurationTarget.Global);
await cfg.update('crcMemoryAllocated', Number.parseInt(event.memory, 10), vscode.ConfigurationTarget.Global);
await cfg.update('crcNameserver', event.nameserver);
}
static async loadView(title: string): Promise<vscode.WebviewPanel> {
const localResourceRoot = vscode.Uri.file(path.join(ClusterViewLoader.extensionPath, 'out', 'clusterViewer'));
if (panel) {
// If we already have a panel, show it in the target column
panel.reveal(vscode.ViewColumn.One);
} else {
panel = vscode.window.createWebviewPanel('clusterView', title, vscode.ViewColumn.One, {
enableScripts: true,
localResourceRoots: [localResourceRoot],
retainContextWhenHidden: true
});
panel.iconPath = vscode.Uri.file(path.join(ClusterViewLoader.extensionPath, 'images/context/cluster-node.png'));
panel.webview.html = await loadWebviewHtml('clusterViewer', panel);
const messageListenerDisposable = panel.webview.onDidReceiveMessage(clusterEditorMessageListener);
panel.onDidDispose(()=> {
messageListenerDisposable.dispose();
panel = undefined;
});
// don't await this message being sent, since the webview may not be set up yet
void panel.webview.postMessage({action: 'cluster', data: ''});
}
return panel;
}
public static async checkCrcStatus(filePath: string, postCommand: string, p: vscode.WebviewPanel | undefined = undefined) {
const crcCredArray = [];
const crcVerInfo = await ChildProcessUtil.Instance.execute(`"${filePath}" version -o json`);
channel.append(`\n\n"${filePath}" version -o json\n`);
channel.append(crcVerInfo.stdout);
const result = await ChildProcessUtil.Instance.execute(`"${filePath}" status -o json`);
channel.append(`\n\n"${filePath}" status -o json\n`);
channel.append(result.stdout);
if (result.error || crcVerInfo.error) {
await p.webview.postMessage({action: postCommand, errorStatus: true});
} else {
await p.webview.postMessage({
action: postCommand,
status: JSON.parse(result.stdout),
errorStatus: false,
versionInfo: JSON.parse(crcVerInfo.stdout),
creds: crcCredArray
});
}
const crcCreds = await ChildProcessUtil.Instance.execute(`"${filePath}" console --credentials -o json`);
if (!crcCreds.error) {
try {
crcCredArray.push(JSON.parse(crcCreds.stdout).clusterConfig);
} catch(err) {
// show error message?
}
}
}
}