Skip to content

Commit 661bd44

Browse files
committed
feat: support scf publish version and traffic setup
1 parent 31bc271 commit 661bd44

File tree

6 files changed

+69
-37
lines changed

6 files changed

+69
-37
lines changed

example/serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
org: orgDemo # (optional) serverless dashboard org. default is the first org you created during #.
22
app: appDemo # (optional) serverless dashboard app. default is the same as the name property.
33
stage: dev # (optional) serverless dashboard stage. default is dev.
4-
component: flask # (required) name of the component. In that case, it's flask.
4+
component: flask@dev # (required) name of the component. In that case, it's flask.
55
name: flashDemo # (required) name of your flash component instance.
66

77
inputs:

serverless.component.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: flask
22
version: 0.0.1
33
author: Tencent Cloud, Inc
44
org: Tencent Cloud, Inc
5-
description: Deploys a serverless Flask application onto Tencent SCF and Tencent APIGateway.
5+
description: Deploy a serverless Flask application onto Tencent SCF and APIGateway.
66
keywords: tencent, serverless, flask
77
repo: https://github.com/serverless-components/tencent-flask
8-
readme: https://github.com/serverless-components/tencent-flask/tree/v2/README.md
8+
readme: https://github.com/serverless-components/tencent-flask/tree/master/README.md
99
license: MIT
1010
main: ./src

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const CONFIGS = {
88
timeout: 3,
99
memorySize: 128,
1010
namespace: 'default',
11-
description: 'Function created by serverless component'
11+
description: 'Created by Serverless Component'
1212
}
1313

1414
module.exports = CONFIGS

src/package.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
{
2-
"name": "@serverless/flask",
3-
"main": "./serverless.js",
4-
"publishConfig": {
5-
"access": "public"
6-
},
7-
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1",
9-
"lint": "eslint . --fix --cache"
10-
},
11-
"author": "Tencent Cloud, Inc.",
12-
"license": "MIT",
132
"dependencies": {
143
"download": "^8.0.0",
15-
"tencent-component-toolkit": "^1.11.4",
4+
"tencent-component-toolkit": "^1.12.9",
165
"type": "^2.0.0"
176
}
187
}

src/serverless.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ class ServerlessComponent extends Component {
5151
...(this.state[curRegion] ? this.state[curRegion] : {}),
5252
...outputs[curRegion]
5353
}
54+
55+
// default version is $LATEST
56+
outputs[curRegion].lastVersion = scfOutput.LastVersion
57+
? scfOutput.LastVersion
58+
: this.state.lastVersion || '$LATEST'
59+
60+
// default traffic is 1.0, it can also be 0, so we should compare to undefined
61+
outputs[curRegion].traffic = scfOutput.Traffic
62+
? scfOutput.Traffic
63+
: this.state.traffic !== undefined
64+
? this.state.traffic
65+
: 1
66+
67+
if (outputs[curRegion].traffic !== 1 && scfOutput.ConfigTrafficVersion) {
68+
outputs[curRegion].configTrafficVersion = scfOutput.ConfigTrafficVersion
69+
this.state.configTrafficVersion = scfOutput.ConfigTrafficVersion
70+
}
71+
72+
this.state.lastVersion = outputs[curRegion].lastVersion
73+
this.state.traffic = outputs[curRegion].traffic
5474
}
5575
uploadCodeHandler.push(funcDeployer())
5676
}

src/utils.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ const download = require('download')
88
const { TypeError } = require('tencent-component-toolkit/src/utils/error')
99
const CONFIGS = require('./config')
1010

11-
/*
12-
* Pauses execution for the provided miliseconds
13-
*
14-
* @param ${number} wait - number of miliseconds to wait
15-
*/
16-
const sleep = async (wait) => new Promise((resolve) => setTimeout(() => resolve(), wait))
17-
1811
/*
1912
* Generates a random id
2013
*/
@@ -23,18 +16,28 @@ const generateId = () =>
2316
.toString(36)
2417
.substring(6)
2518

26-
const getDirFiles = async (dirPath) => {
27-
const targetPath = path.resolve(dirPath)
28-
const files = fs.readdirSync(targetPath)
29-
const temp = {}
30-
files.forEach((file) => {
31-
temp[file] = path.join(targetPath, file)
32-
})
33-
return temp
19+
const getType = (obj) => {
20+
return Object.prototype.toString.call(obj).slice(8, -1)
21+
}
22+
23+
const validateTraffic = (num) => {
24+
if (getType(num) !== 'Number') {
25+
throw new TypeError(
26+
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
27+
'traffic must be a number'
28+
)
29+
}
30+
if (num < 0 || num > 1) {
31+
throw new TypeError(
32+
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
33+
'traffic must be a number between 0 and 1'
34+
)
35+
}
36+
return true
3437
}
3538

3639
const getCodeZipPath = async (instance, inputs) => {
37-
console.log(`Packaging ${CONFIGS.compNameFullname} application...`)
40+
console.log(`Packaging ${CONFIGS.compFullname} application...`)
3841

3942
// unzip source zip file
4043
let zipPath
@@ -43,7 +46,7 @@ const getCodeZipPath = async (instance, inputs) => {
4346
const downloadPath = `/tmp/${generateId()}`
4447
const filename = 'template'
4548

46-
console.log(`Installing Default ${CONFIGS.compNameFullname} App...`)
49+
console.log(`Installing Default ${CONFIGS.compFullname} App...`)
4750
try {
4851
await download(CONFIGS.templateUrl, downloadPath, {
4952
filename: `${filename}.zip`
@@ -59,6 +62,16 @@ const getCodeZipPath = async (instance, inputs) => {
5962
return zipPath
6063
}
6164

65+
const getDirFiles = async (dirPath) => {
66+
const targetPath = path.resolve(dirPath)
67+
const files = fs.readdirSync(targetPath)
68+
const temp = {}
69+
files.forEach((file) => {
70+
temp[file] = path.join(targetPath, file)
71+
})
72+
return temp
73+
}
74+
6275
/**
6376
* Upload code to COS
6477
* @param {Component} instance serverless component instance
@@ -235,8 +248,18 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
235248
fromClientRemark,
236249
layers: ensureIterable(tempFunctionConf.layers ? tempFunctionConf.layers : inputs.layers, {
237250
default: []
238-
})
251+
}),
252+
publish: inputs.publish,
253+
traffic: inputs.traffic,
254+
lastVersion: instance.state.lastVersion
239255
}
256+
257+
// validate traffic
258+
if (inputs.traffic !== undefined) {
259+
validateTraffic(inputs.traffic)
260+
}
261+
functionConf.needSetTraffic = inputs.traffic !== undefined && functionConf.lastVersion
262+
240263
functionConf.tags = ensureObject(tempFunctionConf.tags ? tempFunctionConf.tags : inputs.tag, {
241264
default: null
242265
})
@@ -267,7 +290,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
267290

268291
// 对apigw inputs进行标准化
269292
const apigatewayConf = inputs.apigatewayConf ? inputs.apigatewayConf : {}
270-
apigatewayConf.isDisabled = apigatewayConf.isDisabled === true
293+
apigatewayConf.isDisabled = inputs.apigatewayConf === true
271294
apigatewayConf.fromClientRemark = fromClientRemark
272295
apigatewayConf.serviceName = inputs.serviceName
273296
apigatewayConf.description = `Serverless Framework Tencent-${capitalString(
@@ -286,7 +309,8 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
286309
function: {
287310
isIntegratedResponse: apigatewayConf.isIntegratedResponse === false ? false : true,
288311
functionName: functionConf.name,
289-
functionNamespace: functionConf.namespace
312+
functionNamespace: functionConf.namespace,
313+
functionQualifier: functionConf.needSetTraffic ? '$DEFAULT' : '$LATEST'
290314
}
291315
}
292316
]
@@ -371,7 +395,6 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
371395

372396
module.exports = {
373397
generateId,
374-
sleep,
375398
uploadCodeToCos,
376399
mergeJson,
377400
capitalString,

0 commit comments

Comments
 (0)