forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.ts
More file actions
367 lines (328 loc) · 15.3 KB
/
functions.ts
File metadata and controls
367 lines (328 loc) · 15.3 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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import validator from 'validator';
import { Uri, commands, window } from 'vscode';
import { CliChannel } from '../cli';
import { Oc } from '../oc/ocWrapper';
import { Odo } from '../odo/odoWrapper';
import { isTektonAware } from '../tekton/tekton';
import { Progress } from '../util/progress';
import { OpenShiftTerminalApi, OpenShiftTerminalManager } from '../webview/openshift-terminal/openShiftTerminal';
import { ServerlessCommand, Utils } from './commands';
import { GitModel, getGitBranchInteractively, getGitRepoInteractively, getGitStateByPath } from './git/git';
import { isKnativeServingAware } from './knative';
import { multiStep } from './multiStepInput';
import { FunctionContent, FunctionObject, FunctionSession } from './types';
export class Functions {
private static instance: Functions;
protected static readonly cli = CliChannel.getInstance();
private buildTerminalMap = new Map<string, OpenShiftTerminalApi>();
public runTerminalMap = new Map<string, OpenShiftTerminalApi>();
public static imageRegex = RegExp('[^/]+\\.[^/.]+\\/([^/.]+)(?:\\/[\\w\\s._-]*([\\w\\s._-]))*(?::[a-z0-9\\.-]+)?$');
static getInstance(): Functions {
if (!Functions.instance) {
Functions.instance = new Functions();
}
return Functions.instance;
}
checkRunning(fsPath: string): boolean {
return this.runTerminalMap.has(`run-${fsPath}`);
}
private pollForBuildTerminalDead(resolve: () => void, functionUri: Uri, timeout: number) {
return () => {
if (!this.buildTerminalMap.get(`build-${functionUri.fsPath}`)) {
resolve();
} else {
setTimeout(this.pollForBuildTerminalDead(resolve, functionUri, timeout * 2), timeout * 2);
}
}
}
private async getGitModel(fsPath?: string): Promise<GitModel> {
const gitState = await getGitStateByPath(fsPath);
const gitRemote = await getGitRepoInteractively(gitState);
if (!gitRemote) {
return null;
}
const gitBranch = await getGitBranchInteractively(gitState, gitRemote);
if (!gitBranch) {
return null;
}
return {
remoteUrl: gitState.remotes.filter((r) => r.name === gitRemote).map((r) => r.fetchUrl)[0],
branchName: gitBranch,
};
}
public async onClusterBuild(context: FunctionObject): Promise<void> {
if (!await isTektonAware()) {
await window.showWarningMessage(
'This action requires Tekton to be installed on the cluster. Please install it and then proceed to build the function on the cluster.',
);
return null;
}
if (!await isKnativeServingAware()) {
await window.showWarningMessage(
'This action requires Knative Serving to be installed on the cluster. Please install it and then proceed to build the function on the cluster.',
);
return null;
}
const gitModel = await this.getGitModel(context.folderURI?.fsPath);
if (!gitModel) {
return null;
}
const buildImage = await this.getImage(context.folderURI);
if (!buildImage) {
return null;
}
const currentNamespace: string = await Odo.Instance.getActiveProject();
const yamlContent = await Utils.getFuncYamlContent(context.folderURI.fsPath);
if (!yamlContent) {
return null;
}
const deployedNamespace = yamlContent.deploy?.namespace || undefined;
if (deployedNamespace && deployedNamespace !== currentNamespace) {
const response = await window.showInformationMessage(`Function namespace (declared in func.yaml) is different from the current active namespace. Deploy function ${context.name} to current namespace ${currentNamespace}?`,
'Ok',
'Cancel');
if (response !== 'Ok') {
return null;
}
}
await this.clustrBuildTerminal(context, currentNamespace, buildImage, gitModel);
}
private async clustrBuildTerminal(context: FunctionObject, namespace: string, buildImage: string, gitModel: GitModel) {
const isOpenShiftCluster = await Oc.Instance.isOpenShiftCluster();
const terminal = await OpenShiftTerminalManager.getInstance().createTerminal(
ServerlessCommand.onClusterBuildFunction(context.folderURI.fsPath, namespace, buildImage, gitModel, isOpenShiftCluster),
`On Cluster Build: ${context.name}`,
context.folderURI.fsPath,
process.env, {
onExit: undefined,
} , true
);
const session = {
sessionName: `On Cluster Build: ${context.name}`,
sessionPath: context.folderURI,
teminal: terminal
};
this.addSession(context, session);
void commands.executeCommand('openshift.Serverless.refresh', context);
}
private addSession(context: FunctionObject, session: FunctionSession) {
if (context.sessions?.length > 0) {
const withoutExistingSameSession = context.sessions.filter((exSession) => exSession.sessionName !== session.sessionName);
context.sessions = withoutExistingSameSession;
context.sessions.push(session);
} else {
context.sessions = [];
context.sessions.push(session);
}
}
public async build(context: FunctionObject, s2iBuild: boolean): Promise<void> {
const existingTerminal: OpenShiftTerminalApi = this.buildTerminalMap.get(`build-${context.folderURI.fsPath}`);
if (existingTerminal) {
void window.showWarningMessage(`Do you want to restart ${context.name} build ?`, 'Yes', 'No').then(async (value: string) => {
if (value === 'Yes') {
existingTerminal.focusTerminal();
existingTerminal.kill();
// wait for old build to exit using polling with a back off
await new Promise<void>((resolve) => {
const INIT_TIMEOUT = 100;
setTimeout(this.pollForBuildTerminalDead(resolve, context.folderURI, INIT_TIMEOUT), INIT_TIMEOUT);
});
// start new build
await this.buildProcess(context, s2iBuild);
}
});
} else {
await this.buildProcess(context, s2iBuild);
}
}
private async buildProcess(context: FunctionObject, s2iBuild: boolean) {
const isOpenShiftCluster = await Oc.Instance.isOpenShiftCluster();
const buildImage = await this.getImage(context.folderURI);
const terminalKey = `build-${context.folderURI.fsPath}`;
await this.buildTerminal(context, s2iBuild ? 's2i' : 'pack', buildImage, isOpenShiftCluster, terminalKey);
}
private async buildTerminal(context: FunctionObject, builder: string, buildImage: string, isOpenShiftCluster: boolean, terminalKey: string) {
const terminal = await OpenShiftTerminalManager.getInstance().createTerminal(
ServerlessCommand.buildFunction(context.folderURI.fsPath, builder, buildImage, isOpenShiftCluster),
`Build: ${context.name}`,
context.folderURI.fsPath,
process.env,
{
onExit: () => {
this.buildTerminalMap.delete(terminalKey);
},
}, true
);
const session: FunctionSession = {
sessionName: `Build: ${context.name}`,
sessionPath: context.folderURI,
teminal: terminal
}
this.addSession(context, session);
void commands.executeCommand('openshift.Serverless.refresh', context);
this.buildTerminalMap.set(terminalKey, terminal);
}
public async run(context: FunctionObject, runBuild = false) {
const terminal = await OpenShiftTerminalManager.getInstance().createTerminal(
ServerlessCommand.runFunction(context.folderURI.fsPath, runBuild),
`${runBuild ? 'Build and ' : ''}Run: ${context.name}`,
context.folderURI.fsPath,
process.env,
{
onSpawn: () => {
void commands.executeCommand('openshift.Serverless.refresh', context);
},
onExit: () => {
this.runTerminalMap.delete(`run-${context.folderURI.fsPath}`);
void commands.executeCommand('openshift.Serverless.refresh', context);
}
}, true
);
const session: FunctionSession = {
sessionName: `${runBuild ? 'Build and ' : ''}Run: ${context.name}`,
sessionPath: context.folderURI,
teminal: terminal
}
this.addSession(context, session);
void commands.executeCommand('openshift.Serverless.refresh', context);
this.runTerminalMap.set(`run-${context.folderURI.fsPath}`, terminal);
}
public stop(context: FunctionObject) {
const terminal = this.runTerminalMap.get(`run-${context.folderURI.fsPath}`);
if (terminal) {
terminal.kill();
}
}
public undeploy(context: FunctionObject) {
void Progress.execFunctionWithProgress(`Undeploying the function ${context.name}`, async () => {
const result = await Odo.Instance.execute(ServerlessCommand.undeployFunction(context.name));
if (result.error) {
void window.showErrorMessage(result.error.message);
} else {
void commands.executeCommand('openshift.Serverless.refresh');
}
});
}
public async deploy(context: FunctionObject) {
const currentNamespace: string = await Odo.Instance.getActiveProject();
const yamlContent = await Utils.getFuncYamlContent(context.folderURI.fsPath);
if (yamlContent) {
const deployedNamespace = yamlContent.deploy?.namespace || undefined;
if (!deployedNamespace || (deployedNamespace === currentNamespace)) {
await this.deployProcess(context, deployedNamespace, yamlContent);
} else if (deployedNamespace !== currentNamespace) {
const response = await window.showInformationMessage(`Function namespace (declared in func.yaml) is different from the current active namespace. Deploy function ${context.name} to current namespace ${currentNamespace}?`,
'Ok',
'Cancel');
if (response === 'Ok') {
await this.deployProcess(context, currentNamespace, yamlContent);
}
}
}
}
private async deployProcess(context: FunctionObject, deployedNamespace: string, yamlContent: FunctionContent) {
if (!yamlContent.image || !Functions.imageRegex.test(yamlContent.image)) {
void window.showErrorMessage(`Function ${context.name} has invalid imaage`)
return;
}
const isOpenShiftCluster = await Oc.Instance.isOpenShiftCluster();
const buildImage = await this.getImage(context.folderURI);
// fail after two failed login attempts
let triedLoginTwice = false;
const terminal = await OpenShiftTerminalManager.getInstance().createTerminal(
ServerlessCommand.deployFunction(context.folderURI.fsPath, buildImage, deployedNamespace, isOpenShiftCluster),
`Deploy: ${context.name}`,
context.folderURI.fsPath,
undefined,
{
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onText: (text) => {
if (text.includes('Please provide credentials for image registry')) {
void this.provideUserNameAndPassword(terminal, 'Please provide credentials for image registry.');
}
if (text.includes('Incorrect credentials, please try again')) {
if (triedLoginTwice) {
terminal.kill();
} else {
triedLoginTwice = true;
void this.provideUserNameAndPassword(terminal, 'Please provide credentials for image registry.', true);
}
}
},
onExit() {
void commands.executeCommand('openshift.Serverless.refresh');
},
}, true
);
const session = {
sessionName: `Deploy: ${context.name}`,
sessionPath: context.folderURI,
teminal: terminal
};
this.addSession(context, session);
void commands.executeCommand('openshift.Serverless.refresh', context);
}
public async config(title: string, context: FunctionObject, mode: string, isAdd = true) {
await OpenShiftTerminalManager.getInstance().executeInTerminal(ServerlessCommand.config(context.folderURI.fsPath, mode, isAdd),
context.folderURI.fsPath, title);
}
private async provideUserNameAndPassword(
openshiftTerminalApi: OpenShiftTerminalApi,
message: string,
reattemptForLogin?: boolean,
): Promise<void> {
const userInfo = 'Provide username for image registry.';
const userName = await this.getUsernameOrPassword(
message,
userInfo,
false,
'Provide an username for image registry.',
reattemptForLogin,
);
if (!userName) {
openshiftTerminalApi.kill();
return null;
}
const passMessage = 'Provide password for image registry.';
const userPassword = await this.getUsernameOrPassword(message, passMessage, true, 'Provide a password for image registry.');
if (!userPassword) {
openshiftTerminalApi.kill();
return null;
}
openshiftTerminalApi.sendText(`${userName}\n`);
await new Promise(resolve => { setTimeout(resolve, 100) });
openshiftTerminalApi.sendText(`${userPassword}\n`);
}
private async getUsernameOrPassword(
message: string,
infoMessage: string,
passwordType?: boolean,
errorMessage?: string,
reattemptForLogin?: boolean,
): Promise<string | null> {
return multiStep.showInputBox({
title: message,
prompt: infoMessage,
placeholder: infoMessage,
reattemptForLogin,
validate: (value: string) => {
if (validator.isEmpty(value)) {
return errorMessage;
}
return null;
},
password: passwordType,
});
}
public async getImage(folderURI: Uri): Promise<string> {
const yamlContent = await Utils.getFuncYamlContent(folderURI.fsPath);
if (yamlContent?.image && Functions.imageRegex.test(yamlContent.image)) {
return yamlContent.image
}
return null;
}
}