Skip to content

Commit

Permalink
feat: 实现云函数的部署;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 28, 2021
1 parent 696ed7f commit 82b0783
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/devops-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ npm start
## TODO

- 【已完成】 部署数据访问策略:写入 app db __deployed__rules, app server 应监听该库之变化(watch)
- ** 部署云函数:写入 app db __deployed__functions, app-server 运行前直接读取即可
- ** 部署应用触发器(新增、修改),应监听该库之变化
- 【已完成】 部署云函数:写入 app db __deployed__functions, app-server 运行前直接读取即可
- ** 调试云函数:调用 app server 提供的调试接口,由 devops server 转发,或者发调试令牌直接调
- ** 部署应用触发器(新增、修改),应监听该库之变化
- 重新考虑云函数调用的问题: devops client 调用需要知道其地址,并且云函数的日志当前只能写入 app db,导致 devops client 无法获取日志

- ** 思考云函数名称是否唯一,是否可修改,是否支持名字空间
- ** 思考触发器如何存储,是否存云函数文档中
Expand Down
51 changes: 45 additions & 6 deletions packages/devops-server/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ async function main() {
// 部署访问策略
await deployAccessPolicy()

// 部署云函数
await deployFunctions()

accessor.close()
app_accessor.close()
}
Expand Down Expand Up @@ -220,12 +223,48 @@ async function deployAccessPolicy() {
const r = await db.collection('__rules').get();
assert.ok(r.ok, `deploy policy: read rules failed`);

const app_coll = app_db.collection(Constants.policy_collection);
const cleared = await app_coll.remove({ multi: true });
assert(cleared.ok, `clear ${Constants.policy_collection} failed`);
const session = app_accessor.conn.startSession()

try {
await session.withTransaction(async () => {
const _db = app_accessor.db
const app_coll = _db.collection(Constants.policy_collection);
const cleared = await app_coll.deleteMany({});
assert(cleared.result.ok, `clear ${Constants.policy_collection} failed`);

const copied = await app_coll.add(r.data, { multi: true });
assert(copied.id, `write ${Constants.policy_collection} failed`);
const inserted = await app_coll.insertMany(r.data);

assert(inserted.result.ok, `write ${Constants.policy_collection} failed`);
console.log('deploy policy succeed')
})
} catch (error) {
await session.endSession()
console.log('deploy policy failed:', error)
}

console.log('deploy policy succeed')
}

// 部署云函数
async function deployFunctions() {
const r = await db.collection('__functions').get();
assert.ok(r.ok, `deploy policy: read functions failed`);

const session = app_accessor.conn.startSession()

try {
await session.withTransaction(async () => {
const _db = app_accessor.db
const app_coll = _db.collection(Constants.function_collection);
const cleared = await app_coll.deleteMany({});
assert(cleared.result.ok, `clear ${Constants.function_collection} failed`);

const inserted = await app_coll.insertMany(r.data);

assert(inserted.result.ok, `write ${Constants.function_collection} failed`);
console.log('deploy functions succeed')
})
} catch (error) {
await session.endSession()
console.log('deploy functions failed:', error)
}
}

0 comments on commit 82b0783

Please sign in to comment.