-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.js
162 lines (141 loc) · 5.59 KB
/
serverless.js
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
const ensureIterable = require('type/iterable/ensure')
const ensurePlainObject = require('type/plain-object/ensure')
const ensureString = require('type/string/ensure')
const random = require('ext/string/random')
const path = require('path')
const { Component } = require('@serverless/core')
const fs = require('fs')
const DEFAULTS = {
handler: 'index.main_handler',
runtime: 'Python3.6',
exclude: ['.git/**', '.gitignore', '.DS_Store']
}
class TencentDjango extends Component {
getDefaultProtocol(protocols) {
if (protocols.map((i) => i.toLowerCase()).includes('https')) {
return 'https'
}
return 'http'
}
async copyDir(src, dst) {
const paths = await fs.readdirSync(src)
if (!fs.existsSync(dst)) {
await fs.mkdirSync(dst)
}
for (let i = 0; i < paths.length; i++) {
const thisFileStat = await fs.statSync(path.join(src, paths[i]))
if (thisFileStat.isFile()) {
const readable = await fs.readFileSync(path.join(src, paths[i]))
await fs.writeFileSync(path.join(dst, paths[i]), readable)
} else {
if (!fs.existsSync(path.join(dst, paths[i]))) {
await fs.mkdirSync(path.join(dst, paths[i]))
}
await this.copyDir(path.join(src, paths[i]), path.join(dst, paths[i]))
}
}
}
async prepareInputs(inputs = {}) {
inputs.name =
ensureString(inputs.functionName, { isOptional: true }) ||
this.state.functionName ||
`DjangoComponent_${random({ length: 6 })}`
inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd()
inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' })
inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString })
inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString })
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
const src = path.join(__dirname, 'component')
const dst = path.join(inputs.codeUri, '.cache')
await this.copyDir(src, dst)
const indexPyFile = await fs.readFileSync(
path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'),
'utf8'
)
const replacedFile = indexPyFile.replace(
eval('/{{django_project}}/g'),
inputs.djangoProjectName
)
await fs.writeFileSync(
path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'),
replacedFile
)
inputs.include = [path.join(inputs.codeUri, '.cache')]
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store')
inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler })
inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime })
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
if (inputs.functionConf) {
inputs.timeout = inputs.functionConf.timeout ? inputs.functionConf.timeout : 3
inputs.memorySize = inputs.functionConf.memorySize ? inputs.functionConf.memorySize : 128
if (inputs.functionConf.environment) {
inputs.environment = inputs.functionConf.environment
}
if (inputs.functionConf.vpcConfig) {
inputs.vpcConfig = inputs.functionConf.vpcConfig
}
}
return inputs
}
async default(inputs = {}) {
if (!inputs.djangoProjectName) {
throw new Error(`'djangoProjectName' is required in serverless.yaml`)
}
inputs = await this.prepareInputs(inputs)
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-django'
const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs)
const apigwParam = {
serviceName: inputs.serviceName,
description: 'Serverless Framework Tencent-Django Component',
serviceId: inputs.serviceId,
region: inputs.region,
protocols: inputs.apigatewayConf.protocols || ['http'],
environment:
inputs.apigatewayConf && inputs.apigatewayConf.environment
? inputs.apigatewayConf.environment
: 'release',
endpoints: [
{
path: '/',
method: 'ANY',
function: {
isIntegratedResponse: true,
functionName: tencentCloudFunctionOutputs.Name
}
}
]
}
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
apigwParam.endpoints[0].usagePlan = inputs.apigatewayConf.usagePlan
}
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth
}
apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-django'
const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam)
const outputs = {
region: inputs.region,
functionName: inputs.name,
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${
tencentApiGatewayOutputs.subDomain
}/${tencentApiGatewayOutputs.environment}/`
}
this.state = outputs
await this.save()
return outputs
}
async remove(inputs = {}) {
const removeInput = {
fromClientRemark: inputs.fromClientRemark || 'tencent-django'
}
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
await tencentCloudFunction.remove(removeInput)
await tencentApiGateway.remove(removeInput)
return {}
}
}
module.exports = TencentDjango