-
Notifications
You must be signed in to change notification settings - Fork 4
/
function.ts
334 lines (305 loc) · 13.3 KB
/
function.ts
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
import { FUNCTION_CONF_DEFAULT, FC_CODE_CACHE_DIR } from '../static';
import * as _ from 'lodash';
import { AlicloudAcr } from '../resource/acr';
import path from 'path';
import { isIgnored } from '../ignore';
import { pack } from '../zip';
import * as fse from 'fs-extra';
import { ServerlessProfile, ICredentials, replaceProjectName } from '../profile';
import FcDeploy from './fc-deploy';
import FcSync from '../component/fc-sync';
import * as core from '@serverless-devs/core';
import os from 'os';
import { addEnv } from '../env';
import { detailedDiff } from 'deep-object-diff';
import { promptForConfirmOrDetails } from '../utils/prompt';
import StdoutFormatter from '../component/stdout-formatter';
export interface FunctionConfig {
name: string;
description?: string;
codeUri?: string;
ossBucket?: string;
ossKey?: string; // conflict with codeUri
caPort?: number;
customContainerConfig?: CustomContainerConfig;
handler?: string;
memorySize?: number;
runtime: string;
timeout?: number;
layers?: string[];
environmentVariables?: {
[key: string]: any;
};
initializationTimeout?: number;
initializer?: string;
instanceConcurrency?: number;
instanceType?: string;
import?: boolean;
protect?: boolean;
instanceLifecycleConfig?: InstanceLifecycleConfig;
asyncConfiguration?: AsyncConfiguration;
}
export interface AsyncConfiguration {
destination: {
OnSuccess: string;
OnFailure: string;
};
maxAsyncEventAgeInSeconds: number;
maxAsyncRetryAttempts: number;
statefulInvocation: boolean;
}
export interface InstanceLifecycleConfig {
preFreeze?: {
handler?: string;
timeout?: number;
};
preStop?: {
handler?: string;
timeout?: number;
};
}
export interface CustomContainerConfig {
image: string;
command?: string;
args?: string;
instanceID?: string;
accelerationType?: 'Default' | 'None';
}
export function isCustomContainerRuntime(runtime: string): boolean {
return runtime === 'custom-container';
}
export class FcFunction extends FcDeploy<FunctionConfig> {
readonly serviceName: string;
readonly name: string;
originalCodeUri: string; // build 场景下赋值
static readonly DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX: string = path.join('.s', 'build', 'artifacts');
static readonly DEFAULT_SYNC_CODE_PATH: string = path.join(os.homedir(), '.s', 'cache', 'fc-deploy', 'remote-code');
constructor(functionConf: FunctionConfig, serviceName: string, serverlessProfile: ServerlessProfile, region: string, credentials: ICredentials, curPath?: string, args?: string) {
super(functionConf, serverlessProfile, region, credentials, curPath, args);
this.serviceName = serviceName;
this.name = functionConf?.name;
}
async initLocal(assumeYes?: boolean): Promise<void> {
this.validateConfig();
await this.initLocalConfig(assumeYes);
}
async getCodeUriWithBuildPath(): Promise<any> {
const baseDir: string = path.dirname(this.curPath);
const buildBasePath: string = path.join(baseDir, FcFunction.DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX);
const buildCodeUri: string = path.join(buildBasePath, this.serviceName, this.name);
if (!fse.pathExistsSync(buildBasePath) || fse.lstatSync(buildBasePath).isFile() || isCustomContainerRuntime(this.localConfig.runtime) || !fse.pathExistsSync(buildCodeUri) || fse.lstatSync(buildCodeUri).isFile()) {
return {
codeUri: this.localConfig.codeUri,
isBuild: false,
};
}
this.logger.info(`Fc detects that you have run build command for function: ${this.name}.`);
this.logger.info(StdoutFormatter.stdoutFormatter.using('codeUri', buildCodeUri));
return {
codeUri: buildCodeUri,
isBuild: true,
};
}
async initLocalConfig(assumeYes?: boolean): Promise<void> {
if (this.existOnline) {
Object.assign(this.localConfig, {
import: true,
protect: false,
});
}
const { codeUri, isBuild } = await this.getCodeUriWithBuildPath();
if (isBuild) {
this.originalCodeUri = this.localConfig.codeUri;
this.localConfig.codeUri = codeUri;
const resolvedEnvs: any = addEnv(this.localConfig.environmentVariables);
const message = 'Fc want to add/append some content to your origin environment variables for finding dependencies generated by build command. \nDo you agree with the behavior?';
const details: any = detailedDiff(this.localConfig.environmentVariables, resolvedEnvs);
if (assumeYes || await promptForConfirmOrDetails(message, details)) {
if (assumeYes) {
this.logger.info('Fc add/append some content to your origin environment variables for finding dependencies generated by build command.');
this.logger.log(JSON.stringify(resolvedEnvs, null, ' '), 'yellow');
}
this.localConfig.environmentVariables = resolvedEnvs;
}
}
}
async syncRemoteCode(): Promise<string> {
// 基于 fc-sync 获取函数代码
await fse.ensureDir(FcFunction.DEFAULT_SYNC_CODE_PATH, 0o777);
const profileOfFcSync = replaceProjectName(this.serverlessProfile, `${this.serverlessProfile?.project.projectName}-fc-sync-project`);
const fcSync: FcSync = new FcSync(this.serviceName, profileOfFcSync, this.region, this.credentials, this.curPath, '--type code -f', this.name, null, FcFunction.DEFAULT_SYNC_CODE_PATH);
const fcSyncComponentInputs: any = await fcSync.genComponentInputs('fc-sync');
const fcSyncComponentIns: any = await core.load('devsapp/fc-sync');
const syncRes: any = await fcSyncComponentIns.sync(fcSyncComponentInputs);
const codeUri: string = syncRes?.codeFiles[this.name];
this.logger.debug(`sync code of function ${this.serviceName}:${this.name} to ${codeUri}`);
return codeUri;
}
genStateID(): string {
return `${this.credentials.AccountID}-${this.region}-${this.serviceName}-${this.name}`;
}
validateConfig() {
if (!_.isNil(this.localConfig.codeUri) && !_.isNil(this.localConfig.ossKey)) {
throw new Error('\'codeUri\' and \'ossKey\' can not both exist in function config.');
}
if (_.isEmpty(this.localConfig) && _.isNil(this.localConfig.codeUri) && _.isNil(this.localConfig.ossKey)) {
throw new Error('\'codeUri\' and \'ossKey\' can not be empty in function config at the same time.');
}
}
makeFunctionConfig(): FunctionConfig {
if (this.useRemote) { return this.remoteConfig; }
if (_.isEmpty(this.localConfig)) { return undefined; }
const resolvedFunctionConf: FunctionConfig = {
name: this.name,
description: this.localConfig?.description || FUNCTION_CONF_DEFAULT.description,
handler: this.localConfig?.handler || FUNCTION_CONF_DEFAULT.handler,
memorySize: this.localConfig?.memorySize || FUNCTION_CONF_DEFAULT.memorySize,
timeout: this.localConfig?.timeout || FUNCTION_CONF_DEFAULT.timeout,
instanceConcurrency: this.localConfig?.instanceConcurrency || FUNCTION_CONF_DEFAULT.instanceConcurrency,
instanceType: this.localConfig?.instanceType || FUNCTION_CONF_DEFAULT.instanceType,
runtime: this.localConfig?.runtime || FUNCTION_CONF_DEFAULT.runtime,
instanceLifecycleConfig: this.localConfig?.instanceLifecycleConfig,
asyncConfiguration: this.localConfig?.asyncConfiguration,
layers: this.localConfig?.layers,
};
if (!_.isNil(this.localConfig?.initializer)) {
Object.assign(resolvedFunctionConf, {
initializer: this.localConfig?.initializer,
initializationTimeout: this.localConfig?.initializationTimeout || FUNCTION_CONF_DEFAULT.timeout,
});
}
if (!_.isEmpty(this.localConfig?.environmentVariables)) {
Object.assign(resolvedFunctionConf, {
environmentVariables: this.localConfig?.environmentVariables,
});
}
if (isCustomContainerRuntime(this.localConfig?.runtime)) {
Object.assign(resolvedFunctionConf, {
handler: this.localConfig?.handler || 'not-used',
caPort: this.localConfig?.caPort || FUNCTION_CONF_DEFAULT.caPort,
customContainerConfig: this.localConfig?.customContainerConfig,
});
} else if (!_.isNil(this.localConfig?.ossBucket) && !_.isNil(this.localConfig?.ossKey)) {
Object.assign(resolvedFunctionConf, {
ossBucket: this.localConfig?.ossBucket,
ossKey: this.localConfig?.ossKey,
});
} else if (_.isNil(this.localConfig?.ossBucket) && _.isNil(this.localConfig?.ossKey)) {
// 本地代码,codeUri 必填
Object.assign(resolvedFunctionConf, {
codeUri: this.localConfig?.codeUri,
});
}
if (this.existOnline) {
Object.assign(resolvedFunctionConf, {
import: true,
protect: false,
});
}
this.logger.debug('make function done');
return resolvedFunctionConf;
}
async generateCodeIngore(baseDir: string): Promise<Function | null> {
const codeUri = this.localConfig?.codeUri || FUNCTION_CONF_DEFAULT.codeUri;
const runtime = this.localConfig?.runtime || FUNCTION_CONF_DEFAULT.runtime;
const absCodeUri = path.resolve(baseDir, codeUri);
const absBaseDir = path.resolve(baseDir);
const relative = path.relative(absBaseDir, absCodeUri);
if (codeUri.startsWith('..') || relative.startsWith('..')) {
this.logger.warn(StdoutFormatter.stdoutFormatter.warn('.fcignore', `not supported for the codeUri: ${codeUri}`));
return null;
}
return await isIgnored(baseDir, runtime, this.originalCodeUri);
}
async zipCode(baseDir): Promise<string> {
let codeAbsPath;
const codeUri = this.localConfig?.codeUri || FUNCTION_CONF_DEFAULT.codeUri;
if (codeUri) {
codeAbsPath = path.resolve(baseDir, codeUri);
if (codeUri.endsWith('.zip') || codeUri.endsWith('.jar') || codeUri.endsWith('.war')) {
return codeAbsPath;
}
} else {
codeAbsPath = path.resolve(baseDir, './');
}
const codeignore = await this.generateCodeIngore(baseDir);
// await detectLibrary(codeAbsPath, runtime, baseDir, functionName, '\t');
await fse.ensureDir(FC_CODE_CACHE_DIR);
const zipPath = path.join(FC_CODE_CACHE_DIR, `${this.credentials.AccountID}-${this.region}-${this.serviceName}-${this.name}.zip`);
return await pack(codeAbsPath, codeignore, zipPath);
}
async removeZipCode(codeZipPath: string): Promise<void> {
if (this.useRemote && !isCustomContainerRuntime(this.localConfig?.runtime)) {
this.logger.debug(`removing zip code: ${codeZipPath} downloaded from remote.`);
try {
await fse.unlink(codeZipPath);
} catch (e) {
this.logger.warn(StdoutFormatter.stdoutFormatter.warn('remove sync code', `path: ${codeZipPath}, error: ${e.message}`));
}
return;
}
if (!isCustomContainerRuntime(this.localConfig?.runtime) && this.localConfig?.codeUri) {
if (!this.localConfig?.codeUri.endsWith('.zip') && !this.localConfig?.codeUri.endsWith('.jar') && !this.localConfig?.codeUri.endsWith('.war')) {
if (!_.isNil(codeZipPath)) {
this.logger.debug(`removing zip code: ${codeZipPath}`);
try {
await fse.unlink(codeZipPath);
} catch (e) {
this.logger.warn(StdoutFormatter.stdoutFormatter.warn('remove zipped code', `path: ${codeZipPath}, error: ${e.message}`));
}
}
}
}
}
async packRemoteCode(): Promise<string> {
const syncedCodePath: string = await this.syncRemoteCode();
await fse.ensureDir(FC_CODE_CACHE_DIR);
const zipPath = path.join(FC_CODE_CACHE_DIR, `${this.credentials.AccountID}-${this.region}-${this.serviceName}-${this.name}-remote.zip`);
return await pack(syncedCodePath, null, zipPath);
}
async makeFunctionCode(baseDir: string, pushRegistry?: string): Promise<{ codeZipPath?: string; codeOssObject?: string }> {
this.logger.debug('waiting for making function code.');
// return { codeZipPath, codeOssObject }
if (isCustomContainerRuntime(this.localConfig?.runtime) && !_.isNil(pushRegistry)) {
// push image
if (!this.useRemote) {
const alicloudAcr = new AlicloudAcr(pushRegistry, this.serverlessProfile, this.credentials, this.region);
await alicloudAcr.pushImage(this.localConfig?.customContainerConfig.image);
}
return {};
}
if (!isCustomContainerRuntime(this.localConfig?.runtime)) {
// zip
this.logger.debug(`waiting for packaging function: ${this.name} code...`);
let codeZipPath: string;
if (this.useRemote) {
codeZipPath = await this.packRemoteCode();
} else if (this.localConfig?.codeUri) {
codeZipPath = await this.zipCode(baseDir);
}
this.logger.debug(`zipped code path: ${codeZipPath}`);
if (this.localConfig?.ossBucket) {
// upload to oss, return codeOssObject
return {};
}
// return zip name
return { codeZipPath };
}
return {};
}
async makeFunction(baseDir: string, pushRegistry?: string): Promise<FunctionConfig> {
if (_.isEmpty(this.localConfig) && _.isEmpty(this.remoteConfig)) { return undefined; }
const resolvedFunctionConf: any = this.makeFunctionConfig();
const { codeZipPath, codeOssObject } = await this.makeFunctionCode(baseDir, pushRegistry);
if (!_.isNil(codeZipPath)) {
Object.assign(resolvedFunctionConf, {
codeUri: codeZipPath,
});
} else if (!_.isNil(codeOssObject)) {
Object.assign(resolvedFunctionConf, {
ossKey: codeOssObject,
});
}
return resolvedFunctionConf;
}
}