/
cmdb.go
340 lines (312 loc) · 7.87 KB
/
cmdb.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package cmdb
import (
"github.com/jmoiron/sqlx"
commonDb "github.com/fwtpe/owl-backend/common/db"
sqlxExt "github.com/fwtpe/owl-backend/common/db/sqlx"
"github.com/fwtpe/owl-backend/common/utils"
cmdbModel "github.com/fwtpe/owl-backend/modules/mysqlapi/model"
)
const (
MAINTAIN_PERIOD_BEGIN = 946684800 // Sat, 01 Jan 2000 00:00:00 GMT
MAINTAIN_PERIOD_END = 4292329420 // Thu, 07 Jan 2106 17:43:40 GMT
)
type hostTuple struct {
Hostname string `db:"hostname"`
Ip string `db:"ip"`
MaintainBegin uint32 `db:"maintain_begin"`
MaintainEnd uint32 `db:"maintain_end"`
}
type syncHostTx struct {
hosts []*hostTuple
}
type syncHostGroupTx struct {
groups []*cmdbModel.SyncHostGroup
}
type syncHostgroupContaining struct {
relations map[string][]string
}
func (syncTx *syncHostgroupContaining) InTx(tx *sqlx.Tx) commonDb.TxFinale {
// transform string string relation into temporary memory table
tx.MustExec(`DROP TEMPORARY TABLE IF EXISTS tmp_hostgroup_host`)
tx.MustExec(`
CREATE TEMPORARY TABLE tmp_hostgroup_host (
hh_group_name varchar(255) NOT NULL,
hh_hostname varchar(255) NOT NULL,
PRIMARY KEY(hh_group_name, hh_hostname)
) DEFAULT CHARSET=utf8
`)
txExt := sqlxExt.ToTxExt(tx)
batchStmt := txExt.Preparex(
`
INSERT INTO tmp_hostgroup_host(hh_group_name, hh_hostname)
VALUES (?, ?), (?, ?), (?, ?), (?, ?),
(?, ?), (?, ?), (?, ?), (?, ?),
(?, ?), (?, ?), (?, ?), (?, ?)
`,
)
restStmt := txExt.Preparex(
`
INSERT INTO tmp_hostgroup_host(hh_group_name, hh_hostname)
VALUES (?, ?)
`,
)
for groupName, hosts := range syncTx.relations {
utils.MakeAbstractArray(hosts).BatchProcess(
12,
func(batch interface{}) {
batchStmt.Exec(
utils.FlattenToSlice(
batch,
func(v interface{}) []interface{} {
return []interface{}{
groupName, v,
}
},
)...,
)
},
func(rest interface{}) {
for _, hostName := range rest.([]string) {
restStmt.MustExec(
groupName, hostName,
)
}
},
)
}
/**
* Removes hosts which are not appeared in BOSS
*/
tx.MustExec(`
DELETE gh
FROM grp_host AS gh
INNER JOIN
grp gp
ON gh.grp_id = gp.id
AND gp.come_from = 1
INNER JOIN
host AS hs
ON hs.id = gh.host_id
LEFT OUTER JOIN
tmp_hostgroup_host AS hh
ON hs.hostname = hh.hh_hostname
AND gp.grp_name = hh.hh_group_name
WHERE hh.hh_hostname IS NULL AND
hh.hh_group_name IS NULL
`)
// :~)
/**
* Adds hosts which are appeared in BOSS
*/
tx.MustExec(`
INSERT INTO grp_host (grp_id, host_id)
SELECT gp.id, hs.id
FROM tmp_hostgroup_host AS hh
INNER JOIN
grp gp
ON hh.hh_group_name = gp.grp_name
INNER JOIN
host AS hs
ON hh.hh_hostname = hs.hostname
LEFT OUTER JOIN
grp_host AS gh
ON hs.id = gh.host_id
AND gp.id = gh.grp_id
WHERE gh.host_id IS NULL
AND gh.grp_id IS NULL
`)
// :~)
tx.MustExec(`DROP TEMPORARY TABLE tmp_hostgroup_host`)
return commonDb.TxCommit
}
func (syncTx *syncHostGroupTx) InTx(tx *sqlx.Tx) commonDb.TxFinale {
tx.MustExec(`DROP TEMPORARY TABLE IF EXISTS tempgrp`)
tx.MustExec(`
CREATE TEMPORARY TABLE tempgrp (
grp_name varchar(255) NOT NULL,
create_user varchar(64) NOT NULL,
come_from tinyint(4) NOT NULL,
UNIQUE KEY idx_host_grp_grp_name (grp_name)
) DEFAULT CHARSET=utf8
`)
/*
* multiple insertion with prepared statement
* come_from = 1
* create_user = ?
*/
txExt := sqlxExt.ToTxExt(tx)
batchStmt := txExt.Preparex(
`
INSERT INTO tempgrp (grp_name, create_user, come_from)
VALUES (?, ?, 1), (?, ?, 1), (?, ?, 1), (?, ?, 1),
(?, ?, 1), (?, ?, 1), (?, ?, 1), (?, ?, 1),
(?, ?, 1), (?, ?, 1), (?, ?, 1), (?, ?, 1)
`,
)
restStmt := txExt.PrepareNamed(
`
INSERT INTO tempgrp (grp_name, create_user, come_from)
VALUES (:name, :creator, 1)
`,
)
utils.MakeAbstractArray(syncTx.groups).BatchProcess(
12,
func(batch interface{}) {
batchStmt.MustExec(
utils.FlattenToSlice(
batch,
func(group interface{}) []interface{} {
groupData := group.(*cmdbModel.SyncHostGroup)
return []interface{}{
groupData.Name, groupData.Creator,
}
},
)...,
)
},
func(rest interface{}) {
for _, group := range rest.([]*cmdbModel.SyncHostGroup) {
restStmt.MustExec(group)
}
},
)
// :~)
/*
* update host table from temp_host
*/
tx.MustExec(
`
UPDATE grp INNER JOIN tempgrp
ON grp.grp_name = tempgrp.grp_name
SET grp.create_user = tempgrp.create_user,
grp.come_from = tempgrp.come_from
`)
/*
* insert host table from temp_host
*/
tx.MustExec(
`
INSERT INTO grp(grp_name, create_user, come_from)
SELECT tempgrp.grp_name, tempgrp.create_user, tempgrp.come_from
FROM tempgrp LEFT JOIN grp
ON tempgrp.grp_name = grp.grp_name
WHERE grp.grp_name IS NULL
`)
tx.MustExec(`DROP TEMPORARY TABLE tempgrp`)
return commonDb.TxCommit
}
func api2tuple(hosts []*cmdbModel.SyncHost) []*hostTuple {
var begin uint32
var end uint32
dbData := []*hostTuple{}
for _, h := range hosts {
if h.Activate == 1 {
begin = uint32(0)
end = uint32(0)
} else {
begin = MAINTAIN_PERIOD_BEGIN // Sat, 01 Jan 2000 00:00:00 GMT
end = MAINTAIN_PERIOD_END // Thu, 07 Jan 2106 17:43:40 GMT
}
dbData = append(dbData, &hostTuple{
Hostname: h.Name,
Ip: h.IP,
MaintainBegin: begin,
MaintainEnd: end,
})
}
return dbData
}
func (syncTx *syncHostTx) InTx(tx *sqlx.Tx) commonDb.TxFinale {
tx.MustExec(`DROP TEMPORARY TABLE IF EXISTS temp_host`)
tx.MustExec(`
CREATE TEMPORARY TABLE temp_host (
hostname varchar(255) NOT NULL PRIMARY KEY,
ip varchar(16) NOT NULL,
maintain_begin int(10) unsigned NOT NULL,
maintain_end int(10) unsigned NOT NULL
) DEFAULT CHARSET=utf8
`)
/*
* multiple insertion with prepared statement
*/
txExt := sqlxExt.ToTxExt(tx)
batchStmt := txExt.Preparex(
`
INSERT INTO temp_host (hostname, ip, maintain_begin, maintain_end)
VALUES (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?),
(?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?)
`,
)
restStmt := txExt.PrepareNamed(
`
INSERT INTO temp_host (hostname, ip, maintain_begin, maintain_end)
VALUES (:hostname, :ip, :maintain_begin, :maintain_end)
`,
)
utils.MakeAbstractArray(syncTx.hosts).BatchProcess(
8,
func(batch interface{}) {
batchStmt.Exec(
utils.FlattenToSlice(
batch,
func(host interface{}) []interface{} {
hostData := host.(*hostTuple)
return []interface{}{
hostData.Hostname, hostData.Ip,
hostData.MaintainBegin, hostData.MaintainEnd,
}
},
)...,
)
},
func(rest interface{}) {
for _, host := range rest.([]*hostTuple) {
restStmt.MustExec(host)
}
},
)
// :~)
/*
* update host table from temp_host
*/
tx.MustExec(
`
UPDATE host INNER JOIN temp_host
ON host.hostname = temp_host.hostname
SET host.ip = temp_host.ip,
host.maintain_begin = temp_host.maintain_begin,
host.maintain_end = temp_host.maintain_end
`)
/*
* insert host table from temp_host
*/
tx.MustExec(
`
INSERT INTO host(hostname, ip, maintain_begin, maintain_end)
SELECT temp_host.hostname, temp_host.ip,
temp_host.maintain_begin, temp_host.maintain_end
FROM temp_host LEFT JOIN host
ON temp_host.hostname = host.hostname
WHERE host.hostname IS NULL
`)
tx.MustExec(`DROP TEMPORARY TABLE temp_host`)
return commonDb.TxCommit
}
// Start the Synchronization of CMDB data.
func SyncForHosts(syncData *cmdbModel.SyncForAdding) {
// sync Hosts
txProcessorHost := &syncHostTx{
hosts: api2tuple(syncData.Hosts),
}
DbFacade.NewSqlxDbCtrl().InTx(txProcessorHost)
// sync HostGroups
txProcessorGroup := &syncHostGroupTx{
groups: syncData.Hostgroups,
}
DbFacade.NewSqlxDbCtrl().InTx(txProcessorGroup)
// sync Relations
txProcessorRel := &syncHostgroupContaining{
relations: syncData.Relations,
}
DbFacade.NewSqlxDbCtrl().InTx(txProcessorRel)
}