-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunction.js
More file actions
160 lines (143 loc) · 4.69 KB
/
Copy pathfunction.js
File metadata and controls
160 lines (143 loc) · 4.69 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
const { packTo } = require('@serverless-devs/s-zip');
const { Logger } = require('@serverless-devs/core');
const path = require('path');
const fs = require('fs');
const { sleep, randomStr } = require('../utils')
const trustPolicyDocument = `{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["sts:AssumeRole"],
"Principal": {
"Service": ["lambda.amazonaws.com"]
}
}]
}`;
class Function {
constructor(aws) {
// super();
const lambda = aws.lambda();
const iam = aws.iam();
this.lambda = async (functionKey, inputs) => {
return await new Promise((resolve, reject) => {
lambda[functionKey](inputs, (e, data) => {
if (e) {
reject(e);
}
resolve(data);
});
})
}
this.iam = async (functionKey, inputs) => {
return await new Promise((resolve, reject) => {
iam[functionKey](inputs, (e, data) => {
if (e) {
reject(e);
}
resolve(data);
});
})
}
}
async prepareCode (inputs) {
let codeUri = inputs.Code || inputs.CodeUri;
const artifactConfigured = (codeUri.endsWith('.zip') || codeUri.endsWith('.jar') || codeUri.endsWith('.war'))
if (!artifactConfigured) {
const outputFileName = `${inputs.FunctionName}.zip`;
const packToParame = {
codeUri,
outputFileName
};
const test = await packTo(packToParame);
if (!test.count) {
throw new Error('Zip file error')
}
codeUri = path.join(process.cwd(), './.s/.cache', outputFileName);
}
const data = await fs.readFileSync(codeUri);
return {
ZipFile: data
};
}
async createRole (functionName) {
Logger.log(`The configuration does not have role information, and the component generates the role automatically.`, 'yellow');
const roleName = `s-${functionName}-role-${randomStr()}`;
Logger.log(`Start generating roleName: ${roleName}`);
const { Role } = await this.iam('createRole', {
RoleName: roleName,
AssumeRolePolicyDocument: trustPolicyDocument,
Description: 's auto generate role.'
});
await this.iam('attachRolePolicy', {
RoleName: roleName,
PolicyArn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
});
Logger.log('Successfully generated role.');
return Role.Arn;
}
/**
*
* @param {*} functionInput 参数
* @param {*} retry 重试次数
* @param {*} times 第几次重试
*/
async createFunction (functionInput, retry = 1, times = 1) {
const functionName = functionInput.FunctionName;
if (!functionInput.Role) {
retry = 5;
functionInput.Role = await this.createRole(functionName);
Logger.log('Start trying to create functions after 10 seconds of sleep.', 'yellow');
await sleep(10 * 1000);
}
try {
return await this.lambda('createFunction', functionInput);
} catch (e) {
if (times < retry && e.message === 'The role defined for the function cannot be assumed by Lambda.') {
Logger.log(`Create Failure: ${e.message}`, 'red');
Logger.log(`Retry ${times} times`);
await sleep(1500);
return await this.createFunction(functionInput, retry, times + 1);
}
throw new Error(e.message)
}
}
async deploy (properties) {
const functionInput = JSON.parse(JSON.stringify(properties.Function));
const functionName = functionInput.FunctionName;
Logger.log('Start compressing code.');
const code = await this.prepareCode(functionInput);
Logger.log('Successful compression code.');
try {
await this.checkStatus(functionName);
await this.lambda('updateFunctionCode', {
FunctionName: functionName,
ZipFile: code.ZipFile
});
delete functionInput.Code;
delete functionInput.CodeUri;
await this.checkStatus(functionName);
return await this.lambda('updateFunctionConfiguration', functionInput);
} catch (e) {
if (e.code === 'ResourceNotFoundException') {
functionInput.Code = code;
delete functionInput.CodeUri;
return await this.createFunction(functionInput);
}
throw e;
}
}
async checkStatus(FunctionName) {
for (let i = 0; i < 10; i++) {
const { LastUpdateStatus } = await this.lambda('getFunctionConfiguration', { FunctionName })
if (LastUpdateStatus === 'Successful' || LastUpdateStatus === 'Failed') {
return true;
}
await sleep(1500);
}
}
async remove (properties) {
const functionName = properties.Function.FunctionName;
return await this.lambda('deleteFunction', { FunctionName: functionName });
}
}
module.exports = Function;