-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
104 lines (89 loc) · 3.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
const checkModule = require('./scripts/setup');
const cluster = require('cluster');
module.exports = async(app) => {
app.messenger.once('init-job', data => {
if (app.config.ignoreMiddlewareChecker !== true) {
checkModule(app.config.chargeModule);
}
(async() => {
// Mock a context:
const ctx = app.createAnonymousContext();
await ctx.service.token.initEndpoint();
if (app.config.requireMerge) {
console.log('Start merge data....');
const users = await ctx.service.keystone.fetchUsers('RegionOne');
const promises = [];
let index = 0;
const t = await app.model.transaction();
for (index = 0; index < users.users.length; index++) {
const user = users.users[index];
await app.model.Account.findOrCreate({
where: {
"user_id": user.id
},
transaction: t,
defaults: {
"user_id": user.id,
"domain_id": user.domain_id,
}
});
}
const tokenObj = await ctx.service.token.getToken();
const endpoint = tokenObj.endpoint['keystone']['RegionOne'];
const targetRole = await ctx.curl(`${endpoint}/roles?name=${app.config.charge.billing_role}`, {
method: 'GET',
dataType: 'json',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': tokenObj.token,
},
});
if (targetRole.data && targetRole.data.roles.length > 0) {
const roleId = targetRole.data.roles[0].id;
const roles = await ctx.service.keystone.fetchAssignments('RegionOne');
const roleAssignmentDict = new Map();
roles.role_assignments.forEach(r => {
if (r.role && r.role.id === roleId) {
const p = r.scope.project;
roleAssignmentDict.set(p.id, r.user.id);
}
});
const projects = await ctx.service.keystone.fetchProjects('RegionOne');
for (index = 0; index < projects.projects.length; index++) {
const project = projects.projects[index];
await app.model.Project.findOrCreate({
where: {
"project_id": project.id
},
transaction: t,
defaults: {
"user_id": roleAssignmentDict.get(project.id) || null,
"project_id": project.id,
"domain_id": project.domain_id,
"status": "active",
}
});
}
}
t.commit();
}
})().then(res => {
// cluster.worker.exitedAfterDisconnect = true;
// cluster.worker.on('disconnect', () => {
// cluster.worker.isDevReload = true;
// // cluster.worker.kill('SIGTERM');
// process.exit(0);
// });
// cluster.worker.disconnect();
});
});
app.beforeStart(function* () {
app.model.sync();
app.model.Subscription.sync();
});
app.config.coreMiddleware.unshift('debug');
process.on('SIGINT', (...args) => {
console.log('Receive SIGINT signal!', process.pid);
});
}