-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathindex.ts
More file actions
638 lines (572 loc) · 16.1 KB
/
index.ts
File metadata and controls
638 lines (572 loc) · 16.1 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* Tencent is pleased to support the open source community by making CloudBaseFramework - 云原生一体化部署工具 available.
*
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
*
* Please refer to license text included with this package for license details.
*/
import path from 'path';
import archiver from 'archiver';
import fs from 'fs';
import { Plugin, PluginServiceApi, Builder } from '@cloudbase/framework-core';
import { mkdirSync } from '@cloudbase/toolbox';
import merge from 'lodash.merge';
/**
* 导出接口用于生成 JSON Schema 来进行智能提示
*/
export interface IFrameworkPluginFunctionInputs {
/**
* 函数根目录
* @default functions
*/
functionRootPath?: string;
/**
* 云函数默认配置
* CloudBaseFramework 1.6.1 以后支持
* 单个函数的配置会在该默认配置的基础上进行 merge
* @default {}
*/
functionDefaultConfig?: ICloudFunction;
/**
* 函数配置数组
*/
functions?: ICloudFunction[];
/**
*
* 服务路径配置
*
* 如
*
* ```json
* {
* "hello-world": "/helloworld"
* }
* ```
*/
servicePaths?: Record<string, string>;
/**
* 服务配置
*
* 如
*
* ```json
* {
* "hello-world": {
* "httpPath": "/helloworld",
* "httpPathEnableAuth": false
* }
* }
* ```
*/
serviceConfig?: Record<string, IServiceConfig>,
/**
* 1.6.16 版本以后支持
* 如果指定,则只发布列表中的函数
* 字符串格式,格式如 'fn1,fn2'
*/
publishIncludeList?: string;
}
export interface IFunctionTriggerOptions {
functionName: string;
triggers?: ICloudFunctionTrigger[];
triggerName?: string;
envId: string;
}
export interface ICloudFunctionTrigger {
name: string;
type: string;
config: string;
}
export interface ICloudFunction {
/**
* 云函数名称,即为函数部署后的名称
*/
name: string;
/**
* 函数处理方法名称,名称格式支持“文件名称.函数名称”形式
* @default index.main
*/
handler?: string;
/**
* 函数超时时间(1 - 60S)
*/
timeout?: number;
/**
* 包含环境变量的键值对
*/
envVariables?: Record<string, string>;
/**
* 运行时环境配置,可选值: `Nodejs12.16,Nodejs8.9, Nodejs10.15 Php7, Java8, Go1`
* @default Nodejs10.15
*/
runtime?: 'Nodejs16.13' |'Nodejs12.16' | 'Nodejs10.15' | 'Nodejs8.9' | 'Php7' | 'Java8' | 'Go1';
/**
* 函数运行时内存配置
* @default 128
* @deprecated
*/
memory?: 128 | 256 | 512 | 1024 | 2048;
/**
* 函数运行时内存配置
* @default 128
*/
memorySize?: 128 | 256 | 512 | 1024 | 2048;
/**
* VPC
*/
vpc?: IFunctionVPC;
/**
* 是否云端安装依赖,目前仅支持 Node.js
*/
installDependency?: boolean;
isWaitInstall?: boolean;
/**
* 函数产物路径,相对于函数根目录 functionRootPath,例如 Go 语言可指定二进制文件路径,Java 可以指定 jar 包文件地址
*/
functionDistPath?: string;
/**
* 忽略的文件
*/
ignore?: string[];
/**
* 安全规则,配置前先阅读文档 https://docs.cloudbase.net/cloud-function/security-rules.html
*
* @example { invoke: true }
*/
aclRule?: Record<string, any>;
/**
* 代码保护密钥,传入此参数将保护代码,在控制台/IDE中无法看到代码明文
* 格式为 36 位大小字母和数字
* @length 36
* @pattern ^[a-zA-Z0-9]$
*/
codeSecret?: string;
/**
* 是否自动创建新版本
*/
bumpVersion?: boolean;
/**
* 函数触发器配置
*/
triggers?: ICloudFunctionTrigger[];
/**
* 是否可以在云函数访问公网,默认情况开启,配置云函数VPC后,默认公网访问会关闭
* 取值['ENABLE','DISABLE']
*/
publicNet?: 'ENABLE' | 'DISABLE';
/**
* 是否开启 eip 固定外网 ip 能力,免费环境不可用
* 取值['ENABLE','DISABLE']
*/
eip?: 'ENABLE' | 'DISABLE';
}
export interface IFunctionVPC {
/**
* vpc 的id
*/
vpcId: string;
/**
* 子网id
*/
subnetId: string;
}
export interface IServiceConfig {
httpPath: string
httpPathEnableAuth?: boolean
}
type ResolveInputs = IFrameworkPluginFunctionInputs & {
functionRootPath: string;
functions: ICloudFunction[];
servicePaths: {};
};
type AclTag = 'READONLY' | 'PRIVATE' | 'ADMINWRITE' | 'ADMINONLY' | 'CUSTOM';
class FunctionPlugin extends Plugin {
protected resolvedInputs: ResolveInputs;
protected buildOutput: any;
protected functions: ICloudFunction[];
protected functionRootPath: string;
protected builder: FunctionBuilder;
protected outputs: Record<string, any>;
constructor(
public name: string,
public api: PluginServiceApi,
public inputs: IFrameworkPluginFunctionInputs
) {
super(name, api, inputs);
const config = this.api.projectConfig;
const DEFAULT_INPUTS = {
functionRootPath: config?.functionRoot || 'cloudfunctions',
functions: config?.functions,
servicePaths: {},
functionDefaultConfig: config?.functionDefaultConfig,
};
this.resolvedInputs = resolveInputs(this.inputs, DEFAULT_INPUTS);
let publishIncludeList: string[];
if (this.resolvedInputs.publishIncludeList) {
this.api.logger.debug(
'publishIncludeList',
this.resolvedInputs.publishIncludeList
);
publishIncludeList = this.resolvedInputs.publishIncludeList.split(',');
}
this.resolvedInputs.functions = this.resolvedInputs.functions
.filter((func) => {
if (publishIncludeList) {
return publishIncludeList.includes(func.name);
} else {
return true;
}
})
.map((func: any) => {
return merge(
{},
{
runtime: 'Nodejs10.15',
installDependency: true,
handler: 'index.main',
},
this.resolvedInputs.functionDefaultConfig,
func
);
});
this.functions = this.resolvedInputs.functions;
this.functionRootPath = path.isAbsolute(
this.resolvedInputs.functionRootPath
)
? this.resolvedInputs.functionRootPath
: path.join(this.api.projectPath, this.resolvedInputs.functionRootPath);
this.builder = new FunctionBuilder({
projectPath: this.api.projectPath,
});
this.outputs = {};
}
/**
* 初始化
*/
async init() {
this.api.logger.debug('FunctionPlugin: init', this.resolvedInputs);
if (!this.functions?.length) {
throw new Error('云函数插件配置有误,函数列表为空');
}
}
async compile() {
this.api.logger.debug('FunctionPlugin: compile', this.resolvedInputs);
const builderOptions = this.functions.map((func) => {
let fileName: string = func.name;
let localFunctionPath: string;
let isNeedZip = true;
if (func.runtime?.includes('Java')) {
fileName = func.name + '.jar';
isNeedZip = false;
}
if (func.functionDistPath) {
localFunctionPath = path.join(
this.functionRootPath,
func.functionDistPath
);
} else {
localFunctionPath = path.join(this.functionRootPath, fileName);
}
if (func.runtime?.includes('Node') && func.installDependency) {
const packageJSONExists = fs.existsSync(
path.join(localFunctionPath, 'package.json')
);
if (!packageJSONExists) {
this.api.logger.warn(
`函数 ${func.name} 设置了云端安装依赖,但函数代码根目录下未提供 package.json`
);
func.installDependency = false;
}
}
const zipName = `${func.name + Date.now()}.zip`;
return {
name: func.name,
localPath: localFunctionPath,
zipFileName: zipName,
isNeedZip,
ignore: func.installDependency
? ['node_modules/**/*', 'node_modules', '.git/**', ...(func.ignore || [])]
: [...(func.ignore || [])],
};
});
const buildResult = await this.builder.build(builderOptions);
const codeUris = await this.api.samManager.uploadFile(
buildResult.functions.map((func) => {
return {
fileType: 'FUNCTION',
fileName: `${func.name}.zip`,
filePath: func.source,
};
})
);
buildResult.functions.forEach((func, index) => {
this.outputs[func.name] = codeUris[index];
});
let a: Record<string, any> = {};
return {
EntryPoint: Object.values(this.resolvedInputs.servicePaths).map(
(servicePath) => {
return {
Label: '服务地址',
EntryType: 'HttpService',
HttpEntryPath: servicePath,
};
}
),
Resources: this.functions.reduce((resources, func) => {
resources[this.toConstantCase(func.name)] = this.functionConfigToSAM(
func
);
return resources;
}, a),
};
}
/**
* 执行本地命令
*/
async run() {}
/**
* 删除资源
*/
async remove() {}
/**
* 生成代码
*/
async genCode() {}
/**
* 构建
*/
async build() {
this.api.logger.debug('FunctionPlugin: build', this.resolvedInputs);
}
/**
* 部署
*/
async deploy() {
this.api.logger.debug(
'FunctionPlugin: deploy',
this.resolvedInputs,
this.buildOutput
);
// 批量部署云函数
await Promise.all(
this.functions.map(async (func: any) => {
this.api.logger.info(
`${this.api.emoji('🚀')} [${func.name}] 云函数部署成功`
);
})
);
this.api.logger.info(`${this.api.emoji('🚀')} 云函数部署成功`);
}
functionConfigToSAM(functionConfig: ICloudFunction) {
const networkConfig = this.api.appConfig.network;
return Object.assign({
Type: 'CloudBase::Function',
Properties: Object.assign(
{
Handler: functionConfig.handler || 'index.main',
Description:
'无服务器执行环境,帮助您在无需购买和管理服务器的情况下运行代码',
Runtime: functionConfig.runtime,
FunctionName: functionConfig.name,
MemorySize: functionConfig.memorySize || functionConfig.memory || 128,
Timeout: functionConfig.timeout || 5,
Environment: {
Variables: normalizeEnvironments(functionConfig.envVariables),
},
VpcConfig: {
VpcId:
functionConfig.vpc?.vpcId ||
(networkConfig?.uniqVpcId
? '${Outputs.Network.Properties.InstanceId}'
: undefined),
SubnetId: functionConfig.vpc?.subnetId,
},
HttpPath: this.resolvedInputs.servicePaths[functionConfig.name],
InstallDependency:
functionConfig.runtime?.includes('Node') &&
'installDependency' in functionConfig
? functionConfig.installDependency
: false,
CodeUri: this.outputs[functionConfig.name]?.codeUri,
CodeSecret: !!functionConfig.codeSecret,
Role: 'TCB_QcsRole',
PublicNetStatus: functionConfig.publicNet,
EipStatus: functionConfig.eip,
},
this.resolvedInputs.serviceConfig?.[functionConfig.name] && {
HttpPath: this.resolvedInputs.serviceConfig?.[functionConfig.name].httpPath,
HttpPathEnableAuth: this.resolvedInputs.serviceConfig?.[functionConfig.name].httpPathEnableAuth || false
},
(this.api.bumpVersion || functionConfig.bumpVersion) && {
NewVersion: true,
},
this.api.versionRemark && {
VersionRemark: this.api.versionRemark,
},
functionConfig.aclRule && {
AclTag: 'CUSTOM' as AclTag,
AclRule: this.genAclRule(functionConfig),
},
functionConfig.triggers?.length && {
Events: functionConfig.triggers.reduce((prev, cur) => {
(prev as Record<string, any>)[cur.name] = {
Type: 'Timer',
Properties: {
CronExpression: cur.config,
Message: cur.name,
Enable: true,
},
};
return prev;
}, {}),
}
),
});
}
toConstantCase(name: string) {
let result = '';
let lastIsDivide = true;
for (let letter of name) {
if (letter === '-' || letter === '_') {
lastIsDivide = true;
} else if (lastIsDivide) {
result += letter.toUpperCase();
lastIsDivide = false;
} else {
result += letter.toLowerCase();
lastIsDivide = false;
}
}
return result;
}
genAclRule(functionConfig: ICloudFunction): string {
const aclRule: Record<string, any> = {};
aclRule[functionConfig.name] = functionConfig.aclRule;
return JSON.stringify(aclRule);
}
}
function resolveInputs(inputs: any, defaultInputs: any) {
return Object.assign({}, defaultInputs, inputs);
}
interface FunctionBuilderBuildOptions {
name: string;
localPath: string;
zipFileName: string;
ignore: string[];
isNeedZip: boolean;
}
interface FunctionBuilderOptions {
/**
* 项目根目录的绝对路径
*/
projectPath: string;
}
export class FunctionBuilder extends Builder {
constructor(options: FunctionBuilderOptions) {
super({
type: 'function',
...options,
});
}
async build(options: FunctionBuilderBuildOptions[]) {
const functions = await Promise.all(
options.map(async (option) => {
let source: string;
if (!fs.existsSync(this.distDir)) {
mkdirSync(this.distDir);
}
if (!fs.existsSync(option.localPath)) {
throw new Error(
`函数目录或者文件 ${path.basename(option.localPath)} 不存在`
);
}
const fileStats = fs.statSync(option.localPath);
if (!option.isNeedZip) {
source = option.localPath;
} else {
const localZipPath = path.join(this.distDir, option.zipFileName);
source = localZipPath;
if (fileStats.isFile()) {
this.logger.debug(
'option.localPath',
option.localPath,
localZipPath
);
await this.zipFile(option.localPath, localZipPath);
} else if (fileStats.isDirectory()) {
this.logger.debug(
'option.localPath',
option.localPath,
localZipPath
);
await this.zipDir(option.localPath, localZipPath, option.ignore);
}
}
return {
name: option.name,
options: {},
source,
entry: option.zipFileName,
};
})
);
return {
functions,
};
}
async zipFile(src: string, dest: string) {
return new Promise((resolve, reject) => {
// create a file to stream archive data to.
var output = fs.createWriteStream(dest);
var archive = archiver('zip', {
zlib: { level: 9 }, // Sets the compression level.
});
output.on('close', () => {
resolve(void 0);
});
archive.on('error', reject);
archive.file(src, {
name: path.basename(src),
});
archive.pipe(output);
archive.finalize();
});
}
async zipDir(src: string, dest: string, ignore?: string[]) {
return new Promise((resolve, reject) => {
// create a file to stream archive data to.
var output = fs.createWriteStream(dest);
var archive = archiver('zip', {
zlib: { level: 9 }, // Sets the compression level.
});
output.on('close', () => {
resolve(void 0);
});
archive.on('error', reject);
archive.glob(
'**/*',
{
cwd: src,
ignore: ignore || [],
dot: true,
},
{}
);
archive.pipe(output);
archive.finalize();
});
}
}
function normalizeEnvironments(envVariables: Record<string, any> = {}) {
if (!envVariables) return {};
let result: Record<string, string> = {};
for (let i in envVariables) {
let value = envVariables[i];
if (value) {
result[i] = String(value);
}
}
return result;
}
export const plugin = FunctionPlugin;