@@ -27,6 +27,7 @@ import (
27
27
28
28
var DEBUG bool = false
29
29
30
+ // Output debug message
30
31
func dbg (v ... interface {}) {
31
32
if DEBUG {
32
33
message := ""
@@ -37,6 +38,9 @@ func dbg(v ...interface{}) {
37
38
}
38
39
}
39
40
41
+ // Prepropess file paths
42
+ // Allow absulute and relative (of pwd) paths with or wothout file:// prefix
43
+ // Return absoulute path of file
40
44
func GetFilePath (name string ) string {
41
45
filePath := name
42
46
// remove file:// prefix
@@ -61,7 +65,9 @@ func GetFilePath(name string) string {
61
65
}
62
66
}
63
67
64
- // Exists reports whether the named file or directory exists.
68
+ // Check file exists
69
+ // Allow absulute and relative (of pwd) paths with or wothout file:// prefix
70
+ // Return boolean value
65
71
func FileExists (name string ) bool {
66
72
filePath := GetFilePath (name )
67
73
if _ , err := os .Stat (filePath ); err != nil {
@@ -72,6 +78,8 @@ func FileExists(name string) bool {
72
78
return true
73
79
}
74
80
81
+ // Parse json data from string to map
82
+ // Return map[string]interface{}
75
83
func ParseJson (jsonData string ) map [string ]interface {} {
76
84
var data map [string ]interface {}
77
85
if err := json .Unmarshal ([]byte (jsonData ), & data ); err != nil {
@@ -82,6 +90,8 @@ func ParseJson(jsonData string) map[string]interface{} {
82
90
}
83
91
}
84
92
93
+ // Load json data from file by path
94
+ // Return map[string]interface{}
85
95
func LoadJsonFile (filePath string ) map [string ]interface {} {
86
96
if FileExists (filePath ) {
87
97
fileContent , err := ioutil .ReadFile (GetFilePath (filePath )) // just pass the file name
@@ -94,6 +104,7 @@ func LoadJsonFile(filePath string) map[string]interface{} {
94
104
return nil
95
105
}
96
106
107
+ // Load data dependencies
97
108
func loadDependencies (data map [string ]interface {}) {
98
109
dep := data ["dependencies" ]
99
110
dependencies := dep .(map [string ]interface {})
@@ -103,6 +114,7 @@ func loadDependencies(data map[string]interface{}) {
103
114
}
104
115
}
105
116
117
+ // Load report templates from files
106
118
func loadTemplates () * template.Template {
107
119
dir , err := filepath .Abs (filepath .Dir (os .Args [0 ]))
108
120
if err != nil {
@@ -134,6 +146,39 @@ func loadTemplates() *template.Template {
134
146
return templates
135
147
}
136
148
149
+ // Prepare raw json data for every host
150
+ func getRawData (data map [string ]interface {}) {
151
+ // for every host get data
152
+ var rawData []interface {}
153
+ hosts := pyraconv .ToInterfaceMap (data ["hosts" ])
154
+ dbg ("Data hosts: " , hosts )
155
+ results := pyraconv .ToInterfaceMap (data ["results" ])
156
+ masterName := pyraconv .ToString (hosts ["master" ])
157
+ masterResults := pyraconv .ToInterfaceMap (results [masterName ])
158
+ masterData := pyraconv .ToInterfaceMap (masterResults ["data" ])
159
+ masterJson , err := json .Marshal (masterData )
160
+ if err == nil {
161
+ masterItem := make (map [string ]interface {})
162
+ masterItem ["host" ] = masterName
163
+ masterItem ["data" ] = string (masterJson )
164
+ rawData = append (rawData , masterItem )
165
+ }
166
+ replicas := pyraconv .ToStringArray (hosts ["replicas" ])
167
+ for _ , host := range replicas {
168
+ hostResults := pyraconv .ToInterfaceMap (results [host ])
169
+ hostData := pyraconv .ToInterfaceMap (hostResults ["data" ])
170
+ hostJson , err := json .Marshal (hostData )
171
+ if err == nil {
172
+ hostItem := make (map [string ]interface {})
173
+ hostItem ["host" ] = host
174
+ hostItem ["data" ] = string (hostJson )
175
+ rawData = append (rawData , hostItem )
176
+ }
177
+ }
178
+ data ["rawData" ] = rawData
179
+ }
180
+
181
+ // Generate md report (file) on base of reportData and save them to file in outputDir
137
182
func generateMdReport (checkId string , reportData map [string ]interface {}, outputDir string ) bool {
138
183
var outputFileName string
139
184
if strings .HasSuffix (strings .ToLower (outputDir ), "/" ) {
@@ -154,24 +199,27 @@ func generateMdReport(checkId string, reportData map[string]interface{}, outputD
154
199
if templates == nil {
155
200
log .Fatal ("Can't load template" )
156
201
}
157
- dbg ("Find" , checkId + ".tpl" )
158
- reporTpl := templates .Lookup (checkId + ".tpl" )
159
- if reporTpl != nil {
160
- err = reporTpl .ExecuteTemplate (f , checkId + ".tpl" , reportData )
161
- if err != nil {
162
- dbg ("Template execute error is" , err )
163
- defer os .Remove (outputFileName )
164
- return false
165
- } else {
166
- return true
167
- }
168
- } else {
202
+ reportFileName := checkId + ".tpl"
203
+ reporTpl := templates .Lookup (reportFileName )
204
+ data := reportData
205
+ if reporTpl == nil {
169
206
dbg ("Template " + checkId + ".tpl not found." )
207
+ getRawData (data )
208
+ reportFileName = "raw.tpl"
209
+ reporTpl = templates .Lookup (reportFileName )
210
+ }
211
+ err = reporTpl .ExecuteTemplate (f , reportFileName , data )
212
+ if err != nil {
213
+ dbg ("Template execute error is" , err )
170
214
defer os .Remove (outputFileName )
171
215
return false
216
+ } else {
217
+ return true
172
218
}
173
219
}
174
220
221
+ // Sort hosts on master and replicas by role and index.
222
+ // Return map {"master":name string, "replicas":[replica1 string, replica2 string]}
175
223
func determineMasterReplica (data map [string ]interface {}) {
176
224
hostRoles := make (map [string ]interface {})
177
225
var sortedReplicas []string
0 commit comments