forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionImpl.ts
More file actions
147 lines (135 loc) · 6.16 KB
/
functionImpl.ts
File metadata and controls
147 lines (135 loc) · 6.16 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
/*-----------------------------------------------------------------------------------------------
* 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 path from 'path';
import * as fs from 'fs-extra';
import { Uri, window, workspace } from 'vscode';
import { FunctionContent, FunctionObject, FunctionStatus } from './types';
import { ServerlessCommand, Utils } from './commands';
import { OdoImpl } from '../odo';
import { BuildAndDeploy } from './build-run-deploy';
import { stringify } from 'yaml';
import { CliChannel, CliExitData } from '../cli';
import * as cp from 'child_process';
import { VsCommandError } from '../vscommand';
import { CommandText } from '../base/command';
import { DeploymentConfig } from '../k8s/deploymentConfig';
import { ServerlessFunctionView } from './view';
export interface ServerlessFunction {
getLocalFunctions(): Promise<FunctionObject[]>;
createFunction(language: string, template: string, location: string, image: string): Promise<CliExitData>;
}
export class ServerlessFunctionImpl implements ServerlessFunction {
private static instance: ServerlessFunction;
public static get Instance(): ServerlessFunction {
if (!ServerlessFunctionImpl.instance) {
ServerlessFunctionImpl.instance = new ServerlessFunctionImpl();
}
return ServerlessFunctionImpl.instance;
}
private async getListItems(command: CommandText, fail = false) {
const listCliExitData = await CliChannel.getInstance().executeTool(command, undefined, fail);
try {
return JSON.parse(listCliExitData.stdout) as FunctionObject[];
} catch(err) {
return [];
}
}
private async getDeployedFunctions(): Promise<FunctionObject[]> {
return this.getListItems(DeploymentConfig.command.getDeploymentFunctions());
}
async createFunction(language: string, template: string, location: string, image: string): Promise<CliExitData> {
let funnctionResponse: CliExitData;
try {
const response = await OdoImpl.Instance.execute(ServerlessCommand.createFunction(language, template, location));
if (response && !response.error) {
const yamlContent = await Utils.getFuncYamlContent(location);
if (yamlContent) {
yamlContent.image = image;
fs.rmSync(path.join(location, 'func.yaml'));
fs.writeFileSync(path.join(location, 'func.yaml'), stringify(yamlContent), 'utf-8');
funnctionResponse = {
error: undefined,
stderr: '',
stdout: 'Success'
};
}
} else {
fs.rmdirSync(location);
funnctionResponse = response;
}
} catch (err) {
if (err instanceof VsCommandError) {
void window.showErrorMessage(err.message);
}
fs.rmdirSync(location);
funnctionResponse = {
error: err as cp.ExecException,
stderr: '',
stdout: ''
}
}
return funnctionResponse;
}
async getLocalFunctions(): Promise<FunctionObject[]> {
const functionList: FunctionObject[] = [];
const folders: Uri[] = [];
if (workspace.workspaceFolders) {
// eslint-disable-next-line no-restricted-syntax
for (const wf of workspace.workspaceFolders) {
if (fs.existsSync(path.join(wf.uri.fsPath, 'func.yaml'))) {
folders.push(wf.uri);
}
}
}
const deployedFunctions = await this.getDeployedFunctions();
if (folders.length > 0) {
for (const folderUri of folders) {
const funcData: FunctionContent = await Utils.getFuncYamlContent(folderUri.fsPath);
const funcStatus = this.getFunctionStatus(funcData, deployedFunctions);
const functionNode: FunctionObject = {
name: funcData.name,
runtime: funcData.runtime,
folderURI: folderUri,
context: funcStatus,
hasImage: await this.checkImage(folderUri),
isRunning: BuildAndDeploy.getInstance().checkRunning(folderUri.fsPath)
}
functionList.push(functionNode);
fs.watchFile(path.join(folderUri.fsPath, 'func.yaml'), (_eventName, _filename) => {
ServerlessFunctionView.getInstance().refresh();
});
}
}
if (deployedFunctions.length > 0) {
for (const deployedFunction of deployedFunctions) {
if (functionList.filter((functionParam) => functionParam.name === deployedFunction.name).length === 0) {
const functionNode: FunctionObject = {
name: deployedFunction.name,
runtime: deployedFunction.runtime,
context: FunctionStatus.CLUSTERONLY
}
functionList.push(functionNode);
}
}
}
return functionList;
}
getFunctionStatus(funcData: FunctionContent, deployedFunctions: FunctionObject[]): FunctionStatus {
if (deployedFunctions.length > 0) {
const func = deployedFunctions.find((deployedFunction) => deployedFunction.name === funcData.name && deployedFunction.namespace === funcData.deploy?.namespace)
if (func) {
return FunctionStatus.CLUSTERLOCALBOTH;
}
}
return FunctionStatus.LOCALONLY;
}
async checkImage(folderUri: Uri): Promise<boolean> {
const yamlContent = await Utils.getFuncYamlContent(folderUri.fsPath);
return BuildAndDeploy.imageRegex.test(yamlContent?.image);
}
}
export function serverlessInstance(): ServerlessFunction {
return ServerlessFunctionImpl.Instance;
}