Skip to content

Commit

Permalink
feat: 支持 devops 初始化时创建云函数预置触发器
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 3, 2021
1 parent f00ddba commit c1b1dde
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 1 addition & 3 deletions packages/devops-server/init/functions/init-app-rbac/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* 本函数会默认创建 'App:ready' 事件触发器,应用启动并初始化完成后被自动调用。
*
* 本函数可用于初始化应用必要的一些配置、数据,通常不需要删除此云函数,也不要开启 HTTP 调用。
* 本函数可用于初始化一套 RBAC 必要的数据,通常不需要删除此云函数,也不要开启 HTTP 调用。
*/

import cloud from '@/cloud-sdk'
Expand Down
1 change: 1 addition & 0 deletions packages/devops-server/init/functions/initializer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const db = cloud.database()
exports.main = async function (ctx) {

const r = await cloud.invoke('init-app-rbac', {})
console.log(r.logs)
if (r.data === 'ok') {
console.log('init rbac: ok')
}
Expand Down
11 changes: 10 additions & 1 deletion packages/devops-server/init/functions/initializer/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@
"description": "",
"enableHTTP": false,
"reserved": true,
"tags": ["初始化", "预置"]
"tags": ["初始化", "预置"],
"triggers": [
{
"name": "监听应用启动就绪事件",
"type": "event",
"event": "App:ready",
"desc": "",
"status": 1
}
]
}
32 changes: 31 additions & 1 deletion packages/devops-server/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { Constants } = require('../dist/constants')
const { Globals } = require('../dist/lib/globals')
const { publishFunctions } = require('../dist/api/function')
const { publishAccessPolicy } = require('../dist/api/rules')
const { publishTriggers } = require('../dist/api/trigger')
const appAdminRules = require('./policies/app-admin.json')
const appUserRules = require('./policies/app-user.json')

Expand Down Expand Up @@ -44,6 +45,9 @@ async function main() {
// 部署云函数
await publishFunctions().then(() => console.log('functions deployed'))

// 部署触发器
await publishTriggers().then(() => console.log('triggers deployed'))

sys_accessor.close()
app_accessor.close()
}
Expand Down Expand Up @@ -208,13 +212,19 @@ async function createBuiltinFunctions() {
const funcs = await loader.getFunctions()
for (const func of funcs) {
try {
const triggers = func.triggers || []
const data = {
...func,
status: 1,
created_at: Date.now(),
updated_at: Date.now()
}
await db.collection('__functions').add(data)
delete data['triggers']
const r = await db.collection('__functions').add(data)

if (triggers.length) {
await createTriggers(r.id, triggers)
}
} catch (error) {
if (error.code == 11000) {
console.log('functions already exists: ' + func.name)
Expand All @@ -225,4 +235,24 @@ async function createBuiltinFunctions() {
}

return true
}

/**
* 创建触发器
*/
async function createTriggers(func_id, triggers) {
assert.ok(func_id, 'invalid func_id')
assert.ok(triggers.length, 'no triggers found')

for (const tri of triggers) {
const data = {
...tri,
created_at: Date.now(),
updated_at: Date.now(),
func_id: func_id
}
await db.collection('__triggers').add(data)
}

console.log(`triggers of func[${func_id}] created`)
}

0 comments on commit c1b1dde

Please sign in to comment.