-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataGenerator.js
90 lines (69 loc) · 3.35 KB
/
DataGenerator.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
const path = require('path')
const fs = require('fs')
const Message = require('./Message.js')
const Group = require('./Group.js')
const Host = require('./Host.js')
const ResourceHelper = require('./ResourceHelper.js')
const Inventory = require('./Inventory.js')
const TreeBuilder = require('./TreeBuilder.js')
const colorG = "\x1b[32m"
const colorB = "\x1b[34m"
const colorR = "\x1b[31m"
const colorRst = "\x1b[0m"
class DataGenerator {
static parseInventoriesAndGenerateData(inventoryConfigFilename) {
var data = {}
data["hosts"] = []
data["groups"] = []
data["messages"] = []
var config = JSON.parse( fs.readFileSync( inventoryConfigFilename, 'utf8') )
var inventoryConfig = config['inventory-config']
var inventories = []
for (var index in inventoryConfig) {
var inv = new Inventory(inventoryConfig[index])
// console.dir(inv)
if (Message.isLoggingEnabled()) console.log("%sParse%s hosts file ('%s%s%s'): '%s%s%s' for inventory '%s%s%s'",
colorG,colorRst, colorG,inv.hostsFileFormat,colorRst, colorB,inv.dir,colorRst, colorB,inv.name,colorRst)
if (inv.isIniHostsFileFormat()) {
inv.flatGroupList = Group.generateFlatGroupListFromIniFile(inv)
inv.flatHostList = Host.generateHostListFromIniFile(inv)
} else if (inv.isYamlHostsFileFormat()) {
inv.flatGroupList = Group.generateFlatGroupListFromYamlFile(inv)
inv.flatHostList = Host.generateHostListFromYamlFile(inv)
} else {
Message.create("error", "parser", "parser", inv.name, "parsing error: hosts file format from inventory '"+inv.name+"' unknown.")
}
inventories.push(inv)
data["hosts"].push.apply(data["hosts"], Object.values(inv.flatHostList))
data["groups"].push.apply(data["groups"], Object.values(inv.flatGroupList))
}
data["ui-config"] = config['ui-config']
data['inventories'] = inventories
data["messages"].push.apply(data["messages"], Message.getAllCreatedMessages())
data["trees"] = TreeBuilder.build(data["groups"], data["hosts"])
inventories.forEach(inv => ResourceHelper.addAllShortcuts(inv))
// remove unneeded and duplicated entries
inventories.forEach(inv => {
delete inv['flatHostList']
delete inv['flatGroupList']
inv['tree'] = []
data['trees'].forEach(n => {
if(n.inventory == inv.name) inv['tree'].push(n)
})
})
delete data['trees']
delete data['hosts']
delete data['groups']
// console.dir(inventories)
// console.dir(data["hosts"])
// console.dir(data["groups"])
// console.dir(data["trees"])
var targetDir = path.join("webpage", "generated-data")
if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir)
var targetFile = path.join(targetDir, "data.js")
fs.writeFileSync(targetFile, "var data = " + JSON.stringify(data))
console.log(colorG+"Completed file generation: %s"+colorRst, targetFile)
return data
}
}
module.exports = DataGenerator