-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathconfig.go
362 lines (310 loc) · 10.4 KB
/
config.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package mage
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"github.com/magefile/mage/mg"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// Paths to generated config file templates.
var (
shortTemplate = filepath.Join("build", BeatName+".yml.tmpl")
referenceTemplate = filepath.Join("build", BeatName+".reference.yml.tmpl")
dockerTemplate = filepath.Join("build", BeatName+".docker.yml.tmpl")
)
// ConfigFileType is a bitset that indicates what types of config files to
// generate.
type ConfigFileType uint8
// Config file types.
const (
ShortConfigType ConfigFileType = 1 << iota
ReferenceConfigType
DockerConfigType
AllConfigTypes ConfigFileType = 0xFF
)
// IsShort return true if ShortConfigType is set.
func (t ConfigFileType) IsShort() bool { return t&ShortConfigType > 0 }
// IsReference return true if ReferenceConfigType is set.
func (t ConfigFileType) IsReference() bool { return t&ReferenceConfigType > 0 }
// IsDocker return true if DockerConfigType is set.
func (t ConfigFileType) IsDocker() bool { return t&DockerConfigType > 0 }
// ConfigFileParams defines the files that make up each config file.
type ConfigFileParams struct {
Templates []string // List of files or globs to load.
ExtraVars map[string]interface{}
Short, Reference, Docker ConfigParams
}
type ConfigParams struct {
Template string
Deps []interface{}
}
func DefaultConfigFileParams() ConfigFileParams {
return ConfigFileParams{
Templates: []string{LibbeatDir("_meta/config/*.tmpl")},
ExtraVars: map[string]interface{}{},
Short: ConfigParams{
Template: LibbeatDir("_meta/config/default.short.yml.tmpl"),
},
Reference: ConfigParams{
Template: LibbeatDir("_meta/config/default.reference.yml.tmpl"),
},
Docker: ConfigParams{
Template: LibbeatDir("_meta/config/default.docker.yml.tmpl"),
},
}
}
// Config generates config files. Set DEV_OS and DEV_ARCH to change the target
// host for the generated configs. Defaults to linux/amd64.
func Config(types ConfigFileType, args ConfigFileParams, targetDir string) error {
// Short
if types.IsShort() {
file := filepath.Join(targetDir, BeatName+".yml")
if err := makeConfigTemplate(file, 0600, args, ShortConfigType); err != nil {
return errors.Wrap(err, "failed making short config")
}
}
// Reference
if types.IsReference() {
file := filepath.Join(targetDir, BeatName+".reference.yml")
if err := makeConfigTemplate(file, 0644, args, ReferenceConfigType); err != nil {
return errors.Wrap(err, "failed making reference config")
}
}
// Docker
if types.IsDocker() {
file := filepath.Join(targetDir, BeatName+".docker.yml")
if err := makeConfigTemplate(file, 0600, args, DockerConfigType); err != nil {
return errors.Wrap(err, "failed making docker config")
}
}
return nil
}
func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigFileParams, typ ConfigFileType) error {
// Determine what type to build and set some parameters.
var confFile ConfigParams
var tmplParams map[string]interface{}
switch typ {
case ShortConfigType:
confFile = confParams.Short
tmplParams = map[string]interface{}{}
case ReferenceConfigType:
confFile = confParams.Reference
tmplParams = map[string]interface{}{"Reference": true}
case DockerConfigType:
confFile = confParams.Docker
tmplParams = map[string]interface{}{"Docker": true}
default:
panic(errors.Errorf("Invalid config file type: %v", typ))
}
// Build the dependencies.
mg.SerialDeps(confFile.Deps...)
// Set variables that are available in templates.
// Rather than adding more "ExcludeX"/"UseX" options consider overwriting
// one of the libbeat templates in your project by adding a file with the
// same name to your _meta/config directory.
params := map[string]interface{}{
"GOOS": EnvOr("DEV_OS", "linux"),
"GOARCH": EnvOr("DEV_ARCH", "amd64"),
"BeatLicense": BeatLicense,
"Reference": false,
"Docker": false,
"ExcludeConsole": false,
"ExcludeFileOutput": false,
"ExcludeKafka": false,
"ExcludeLogstash": false,
"ExcludeRedis": false,
"UseObserverProcessor": false,
"UseDockerMetadataProcessor": true,
"UseKubernetesMetadataProcessor": false,
"ExcludeDashboards": false,
}
params = joinMaps(params, confParams.ExtraVars, tmplParams)
tmpl := template.New("config").Option("missingkey=error")
funcs := joinMaps(FuncMap, template.FuncMap{
"header": header,
"subheader": subheader,
"indent": indent,
// include is necessary because you cannot pipe 'template' to a function
// since 'template' is an action. This allows you to include a
// template and indent it (e.g. {{ include "x.tmpl" . | indent 4 }}).
"include": func(name string, data interface{}) (string, error) {
buf := bytes.NewBuffer(nil)
if err := tmpl.ExecuteTemplate(buf, name, data); err != nil {
return "", err
}
return buf.String(), nil
},
})
tmpl = tmpl.Funcs(funcs)
fmt.Printf(">> Building %v for %v/%v\n", destination, params["GOOS"], params["GOARCH"])
var err error
for _, templateGlob := range confParams.Templates {
if tmpl, err = tmpl.ParseGlob(templateGlob); err != nil {
return errors.Wrapf(err, "failed to parse config templates in %q", templateGlob)
}
}
data, err := ioutil.ReadFile(confFile.Template)
if err != nil {
return errors.Wrapf(err, "failed to read config template %q", confFile.Template)
}
tmpl, err = tmpl.Parse(string(data))
if err != nil {
return errors.Wrap(err, "failed to parse template")
}
out, err := os.OpenFile(CreateDir(destination), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
if err != nil {
return err
}
defer out.Close()
if err = tmpl.Execute(out, EnvMap(params)); err != nil {
return errors.Wrapf(err, "failed building %v", destination)
}
return nil
}
func header(title string) string {
return makeHeading(title, "=")
}
func subheader(title string) string {
return makeHeading(title, "-")
}
var nonWhitespaceRegex = regexp.MustCompile(`(?m)(^.*\S.*$)`)
// indent pads all non-whitespace lines with the number of spaces specified.
func indent(spaces int, content string) string {
pad := strings.Repeat(" ", spaces)
return nonWhitespaceRegex.ReplaceAllString(content, pad+"$1")
}
func makeHeading(title, separator string) string {
const line = 80
leftEquals := (line - len("# ") - len(title) - 2*len(" ")) / 2
rightEquals := leftEquals + len(title)%2
return "# " + strings.Repeat(separator, leftEquals) + " " + title + " " + strings.Repeat(separator, rightEquals)
}
const moduleConfigTemplate = `
#========================== Modules configuration =============================
{{.BeatName}}.modules:
{{range $mod := .Modules}}
#{{$mod.Dashes}} {{$mod.Title | title}} Module {{$mod.Dashes}}
{{$mod.Config}}
{{- end}}
`
type moduleConfigTemplateData struct {
ID string
Title string
Dashes string
Config string
}
type moduleFieldsYmlData []struct {
Title string `json:"title"`
ShortConfig bool `json:"short_config"`
}
func readModuleFieldsYml(path string) (title string, useShort bool, err error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", false, err
}
var fd moduleFieldsYmlData
if err = yaml.Unmarshal(data, &fd); err != nil {
return "", false, err
}
if len(fd) == 0 {
return "", false, errors.New("module not found in fields.yml")
}
return fd[0].Title, fd[0].ShortConfig, nil
}
// moduleDashes returns a string containing the correct number of dashes '-' to
// center the modules title in the middle of the line surrounded by an equal
// number of dashes on each side.
func moduleDashes(name string) string {
const (
lineLen = 80
headerLen = len("#")
titleSuffixLen = len(" Module ")
)
numDashes := lineLen - headerLen - titleSuffixLen - len(name) - 1
numDashes /= 2
return strings.Repeat("-", numDashes)
}
// GenerateModuleReferenceConfig generates a reference config file and includes
// modules found from the given module dirs.
func GenerateModuleReferenceConfig(out string, moduleDirs ...string) error {
var moduleConfigs []moduleConfigTemplateData
for _, dir := range moduleDirs {
modules, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, modDirInfo := range modules {
if !modDirInfo.IsDir() {
continue
}
name := modDirInfo.Name()
// Get title from fields.yml.
title, _, err := readModuleFieldsYml(filepath.Join(dir, name, "_meta/fields.yml"))
if err != nil {
title = strings.Title(name)
}
// Prioritize config.reference.yml, but fallback to config.yml.
files := []string{
filepath.Join(dir, name, "_meta/config.reference.yml"),
filepath.Join(dir, name, "_meta/config.yml"),
}
var data []byte
for _, f := range files {
data, err = ioutil.ReadFile(f)
if err != nil {
if os.IsNotExist(err) {
continue
}
return err
}
break
}
if data == nil {
continue
}
moduleConfigs = append(moduleConfigs, moduleConfigTemplateData{
ID: name,
Title: title,
Dashes: moduleDashes(title),
Config: string(data),
})
}
}
// Sort them by their module dir name, but put system first.
sort.Slice(moduleConfigs, func(i, j int) bool {
// Bubble system to the top of the list.
if moduleConfigs[i].ID == "system" {
return true
} else if moduleConfigs[j].ID == "system" {
return false
}
return moduleConfigs[i].ID < moduleConfigs[j].ID
})
config := MustExpand(moduleConfigTemplate, map[string]interface{}{
"Modules": moduleConfigs,
})
return ioutil.WriteFile(createDir(out), []byte(config), 0644)
}