Skip to content

Commit

Permalink
feat: support scf publish version and traffic setup
Browse files Browse the repository at this point in the history
  • Loading branch information
yugasun committed Jun 29, 2020
1 parent 1b273d8 commit e6676e1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,32 @@

### 1. 安装

通过 npm 安装最新版本的 Serverless Framework
```
通过 npm 安装最新版本的 Serverless Framework

```bash
$ npm install -g serverless
```

### 2. 创建

创建并进入一个全新目录:
```

```bash
$ mkdir tencent-scf && cd tencent-scf
```

通过如下命令和模板链接,快速创建一个 SCF 应用:
```

```bash
$ serverless create --template-url https://github.com/serverless-components/tencent-scf/tree/v2/example
$ cd example
```

下载完毕后,目录结构如下所示:

```
|- src
| └── index.py
| └── index.js
└── serverless.yml
```

Expand All @@ -72,7 +76,7 @@ $ cd example

以下是腾讯云 SCF 组件的 `serverless.yml`完整配置说明:

```yml
```yaml
# serverless.yml

component: scf # (必填) 引用 component 的名称,当前用到的是 tencent-scf 组件
Expand Down Expand Up @@ -102,7 +106,7 @@ inputs:
method: GET
```
点此查看[全量配置及配置说明](https://github.com/serverless-components/tencent-scf/blob/v2/doc/serverless.yaml)
点此查看[全量配置及配置说明](./docs/config.md)
当你根据该配置文件更新配置字段后,再次运行 `serverless deploy` 或者 `serverless` 就可以更新配置到云端。

Expand All @@ -120,15 +124,15 @@ inputs:

在`serverless.yml`文件所在的目录下,通过如下命令查看部署状态:

```
```bash
$ serverless info
```

### 7. 移除

在`serverless.yml`文件所在的目录下,通过以下命令移除部署 SCF 应用。移除后该组件会对应删除云上部署时所创建的所有相关资源。

```
```bash
$ serverless remove
```

Expand All @@ -138,7 +142,7 @@ $ serverless remove

当前默认支持 CLI 扫描二维码登录,如您希望配置持久的环境变量/秘钥信息,也可以本地创建 `.env` 文件

```console
```bash
$ touch .env # 腾讯云的配置信息
```

Expand All @@ -148,7 +152,7 @@ $ touch .env # 腾讯云的配置信息

如果已有腾讯云账号,可以在[API 密钥管理](https://console.cloud.tencent.com/cam/capi)中获取 `SecretId` 和`SecretKey`.

```
```dotenv
# .env
TENCENT_SECRET_ID=123
TENCENT_SECRET_KEY=123
Expand Down
4 changes: 3 additions & 1 deletion doc/configure.md → docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ inputs:
tags:
key1: value1
key2: value2 # tags 的key value
traffic: 0.9 # 配置默认流量中 $LATEST 版本比重:0 - 1
events: # 触发器
- timer: # 定时触发器
name: timer
Expand Down Expand Up @@ -98,7 +99,7 @@ inputs:
desc: mytest
function:
isIntegratedResponse: TRUE
functionQualifier: $LATEST
functionQualifier: $LATEST # 当配置 traffic 时,需要配置为 $DEFAUlt
usagePlan:
usagePlanId: 1111
usagePlanName: slscmp
Expand Down Expand Up @@ -169,6 +170,7 @@ inputs:
| eip | 否 | false | 固定出口 IP。默认为 false,即不启用 |
| tags | 否 | | 标签设置。可设置多对 key-value 的键值对 |
| events | 否 | | 触发器数组。支持以下几种触发器:timer(定时触发器)、apigw(网关触发器)、cos(COS 触发器)、cmq(CMQ Topic 触发器)、ckafka(CKafka 触发器)配置参数参考触发器。 |
| taffic | 否 | 1 | 配置默认流量中 `$LATEST` 版本比重,取值范围:0 ~ 1,比如 80%,可配置成 0.8。注意如果皮遏制灰度流量,需要配置对应的 API 网关触发器的 enpoints 的 `function.functionQualifier` 参数为 `$DEFAULT` (默认流量) |

### 执行目录

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"commitlint": "commitlint -f HEAD@{15}",
"lint": "eslint --ext .js,.ts,.tsx .",
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
"prettier": "prettier --check **/*.{css,html,js,json,md,yaml,yml}",
"prettier:fix": "prettier --write **/*.{css,html,js,json,md,yaml,yml}",
"prettier": "prettier --check '**/*.{css,html,js,json,md,yaml,yml}'",
"prettier:fix": "prettier --write '**/*.{css,html,js,json,md,yaml,yml}'",
"release": "semantic-release",
"release-local": "node -r dotenv/config node_modules/semantic-release/bin/semantic-release --no-ci --dry-run",
"check-dependencies": "npx npm-check --skip-unused --update"
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "Apache",
"dependencies": {
"download": "^8.0.0",
"tencent-component-toolkit": "^1.11.4",
"tencent-component-toolkit": "^1.12.2",
"type": "^2.0.0"
}
}
12 changes: 11 additions & 1 deletion src/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ServerlessComponent extends Component {
if (!tmpSecrets || !tmpSecrets.TmpSecretId) {
throw new TypeError(
'CREDENTIAL',
'Cannot get secretId/Key, your account could be sub-account or does not have access, please check if SLS_QcsRole role exists in your account, and visit https://console.cloud.tencent.com/cam to bind this role to your account.'
`Cannot get secretId/Key, your account could be sub-account and does not have the access to use SLS_QcsRole, please make sure the role exists first, then visit https://cloud.tencent.com/document/product/1154/43006, follow the instructions to bind the role to your account.`
)
}

Expand Down Expand Up @@ -63,6 +63,16 @@ class ServerlessComponent extends Component {
MemorySize: scfOutput.MemorySize
}

if (scfOutput.LastVersion) {
outputs.LastVersion = scfOutput.LastVersion
this.state.lastVersion = scfOutput.LastVersion
}

if (scfOutput.Traffic) {
outputs.Traffic = scfOutput.Traffic
this.state.functionTraffic = scfOutput.Traffic
}

// handle apigw event outputs
if (existApigwTrigger) {
const stateApigw = {}
Expand Down
21 changes: 21 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ const generateId = () =>
.toString(36)
.substring(6)

const getType = (obj) => {
return Object.prototype.toString.call(obj).slice(8, -1)
}

const validateTraffic = (num) => {
if (getType(num) !== 'Number') {
throw new TypeError('PARAMETER_SCF_TRAFFIC', 'traffic must be a number')
}
if (num < 0 || num > 1) {
throw new TypeError('PARAMETER_SCF_TRAFFIC', 'traffic must be a number between 0 and 1')
}
return true
}

/**
* get default template zip file path
*/
Expand Down Expand Up @@ -136,6 +150,13 @@ const prepareInputs = async (instance, credentials, appId, inputs) => {
return event
})

// validate traffic config
if (inputs.traffic !== undefined) {
validateTraffic(inputs.traffic)
}

inputs.lastVersion = instance.state.lastVersion

return {
useDefault,
existApigwTrigger,
Expand Down

0 comments on commit e6676e1

Please sign in to comment.