Skip to content

Commit

Permalink
feat(fix): 新增触发器远程推送部署,远程推送改为保持 _id 一致的方式;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 7, 2021
1 parent c18c266 commit 655792c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
23 changes: 20 additions & 3 deletions packages/devops-server/src/api/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Constants } from "../constants"
import { Globals } from "../lib/globals"
import { compileTs2js } from 'cloud-function-engine/dist/utils'
import { CloudFunctionStruct } from "cloud-function-engine"
import { ObjectId } from 'mongodb'
import * as assert from 'assert'
const db = Globals.sys_db

Expand Down Expand Up @@ -110,17 +111,19 @@ export async function deployFunctions(functions: CloudFunctionStruct[]) {
}

async function _deployOneFunction(func: CloudFunctionStruct) {

await _deleteFunctionWithSameNameButNotId(func)

const db = Globals.sys_accessor.db
const r = await db.collection('__functions').findOne({ name: func.name })
const r = await db.collection('__functions').findOne({ _id: new ObjectId(func._id) })

const data = {
...func
}

delete data['_id']

// if exists function
if (r) {
delete data['_id']
const ret = await db.collection('__functions').updateOne({ _id: r._id }, {
$set: data
})
Expand All @@ -132,4 +135,18 @@ async function _deployOneFunction(func: CloudFunctionStruct) {
// if new function
const ret = await db.collection('__functions').insertOne(data as any)
assert(ret.insertedId, `deploy: add function ${func.name} occurred error`)
}

/**
* 删除本地 _id 不同,但 name 相同的云函数(若存在)
* @param func
*/
async function _deleteFunctionWithSameNameButNotId(func: CloudFunctionStruct) {
const db = Globals.sys_accessor.db
await db.collection('__functions').findOneAndDelete({
_id: {
$ne: new ObjectId(func._id)
},
name: func.name
})
}
22 changes: 20 additions & 2 deletions packages/devops-server/src/api/rules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'assert'
import { Constants } from '../constants'
import { Globals } from "../lib/globals"
import { ObjectId } from 'mongodb'

const db = Globals.sys_db
export interface RuleDocument {
Expand Down Expand Up @@ -87,17 +88,20 @@ export async function deployPolicies(policies) {
}

async function _deployOnePolicy(policy: any) {

await _deletePolicyWithSameNameButNotId(policy)

const db = Globals.sys_accessor.db
const r = await db.collection('__policies').findOne({ name: policy.name })
const r = await db.collection('__policies').findOne({ _id: new ObjectId(policy._id) })

const data = {
...policy
}

delete data['_id']

// if exists
if (r) {
delete data['_id']
const ret = await db.collection('__policies').updateOne({ _id: r._id }, {
$set: data
})
Expand All @@ -109,4 +113,18 @@ async function _deployOnePolicy(policy: any) {
// if new
const ret = await db.collection('__policies').insertOne(data as any)
assert(ret.insertedId, `deploy: add policy ${policy.name} occurred error`)
}

/**
* 删除本地 _id 不同,但 name 相同的策略(若存在)
* @param func
*/
async function _deletePolicyWithSameNameButNotId(policy: any) {
const db = Globals.sys_accessor.db
await db.collection('__policies').findOneAndDelete({
_id: {
$ne: new ObjectId(policy._id)
},
name: policy.name
})
}
60 changes: 59 additions & 1 deletion packages/devops-server/src/api/trigger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

import { Constants } from "../constants"
import { Globals } from "../lib/globals"
import { ObjectId } from 'mongodb'
import * as assert from 'assert'

const db = Globals.sys_db
const logger = Globals.logger

/**
* 请求触发器列表
Expand Down Expand Up @@ -35,7 +38,7 @@ export async function getTriggerById(id: string) {
* 发布触发器
* 实为将 sys db __triggers 集合,复制其数据至 app db 中
*/
export async function publishTriggers() {
export async function publishTriggers() {
const logger = Globals.logger

const app_accessor = Globals.app_accessor
Expand All @@ -54,4 +57,59 @@ export async function getTriggerById(id: string) {
} finally {
await session.endSession()
}
}


/**
* 部署触发器
* 应用远程推送过来的部署请求
*/
export async function deployTriggers(triggers: any[]) {
assert.ok(triggers)
assert.ok(triggers instanceof Array)
const logger = Globals.logger

const accessor = Globals.sys_accessor

const data = triggers
const session = accessor.conn.startSession()

try {
await session.withTransaction(async () => {
for (const func of data) {
await _deployOneTrigger(func)
}
})
} catch (error) {
logger.error(error)
throw error
} finally {
await session.endSession()
}
}

async function _deployOneTrigger(trigger: any) {

const db = Globals.sys_accessor.db
const r = await db.collection('__triggers').findOne({ _id: new ObjectId(trigger._id) })

const data = {
...trigger
}

logger.debug('deploy trigger: ', data, r)
// if exists function
if (r) {
delete data['_id']
const ret = await db.collection('__triggers').updateOne({ _id: r._id }, {
$set: data
})

assert(ret.matchedCount, `deploy: update trigger ${trigger.name} occurred error`)
return
}

// if new function
const ret = await db.collection('__triggers').insertOne(data as any)
assert(ret.insertedId, `deploy: add trigger ${trigger.name} occurred error`)
}
13 changes: 12 additions & 1 deletion packages/devops-server/src/router/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { checkPermission } from '../../api/permission'
import { Globals } from '../../lib/globals'
import { getToken, parseToken } from '../../lib/utils/token'
import { deployPolicies, publishAccessPolicy } from '../../api/rules'
import { deployTriggers, publishTriggers } from '../../api/trigger'

export const DeployRouter = express.Router()
const logger = Globals.logger
Expand Down Expand Up @@ -67,7 +68,7 @@ DeployRouter.post('/create-token', async (req, res) => {
* 接收部署请求
*/
DeployRouter.post('/in', async (req, res) => {
const { policies, functions, comment } = req.body
const { policies, functions, comment, triggers } = req.body
if (!policies && !functions) {
return res.status(422).send('invalid polices & functions')
}
Expand Down Expand Up @@ -116,6 +117,7 @@ DeployRouter.post('/in', async (req, res) => {
status: 'pending', // 'pending' | 'deployed' | 'canceled'
type: 'function',
data: functions,
triggers,
comment,
created_at: Date.now()
}
Expand Down Expand Up @@ -159,11 +161,20 @@ DeployRouter.post('/apply', async (req, res) => {
const type = deploy_request.type
assert.ok(['function', 'policy'].includes(type))

// deploy functions
if (type === 'function') {
await deployFunctions(deploy_request.data)

// deploy triggers if had
if (deploy_request.triggers) {
await deployTriggers(deploy_request.triggers)
await publishTriggers()
}

await publishFunctions()
}

// deploy policies
if (type === 'policy') {
await deployPolicies(deploy_request.data)
await publishAccessPolicy()
Expand Down

0 comments on commit 655792c

Please sign in to comment.