-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathbeatgen.go
227 lines (195 loc) · 5.6 KB
/
beatgen.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
// 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 beatgen
import (
"bufio"
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
devtools "github.com/elastic/beats/v7/dev-tools/mage"
"github.com/elastic/beats/v7/dev-tools/mage/gotool"
"github.com/elastic/beats/v7/generator/common/beatgen/setup"
)
// ConfigItem represents a value that must be configured for the custom beat
type ConfigItem struct {
Key string
Default func(map[string]string) string
Help string
}
// required user config for a custom beat
// These are specified in env variables with newbeat_*
var configList = []ConfigItem{
{
Key: "project_name",
Help: "Enter the beat name",
Default: func(cfg map[string]string) string {
return "examplebeat"
},
},
{
Key: "github_name",
Help: "Enter your github name",
Default: func(cfg map[string]string) string {
return "your-github-name"
},
},
{
Key: "beat_path",
Help: "Enter the beat path",
Default: func(cfg map[string]string) string {
ghName, _ := cfg["github_name"]
beatName, _ := cfg["project_name"]
return "github.com/" + ghName + "/" + strings.ToLower(beatName)
},
},
{
Key: "full_name",
Help: "Enter your full name",
Default: func(cfg map[string]string) string {
return "Firstname Lastname"
},
},
{
Key: "type",
Help: "Enter the beat type",
Default: func(cfg map[string]string) string {
return "beat"
},
},
{
Key: "beats_revision",
Help: "Enter the github.com/elastic/beats revision",
Default: func(cfg map[string]string) string {
return "master"
},
},
}
// Generate generates a new custom beat
func Generate() error {
cfg, err := getConfig()
if err != nil {
return errors.Wrap(err, "error getting config")
}
beatsModuleName, err := gotool.GetModuleName()
if err != nil {
return err
}
// Make sure the ElasticBeatsDir value is cached
// before changing directory below.
if _, err := devtools.ElasticBeatsDir(); err != nil {
return err
}
err = setup.GenNewBeat(cfg)
if err != nil {
return errors.Wrap(err, "error generating new beat")
}
absBeatPath := filepath.Join(build.Default.GOPATH, "src", cfg["beat_path"])
err = os.Chdir(absBeatPath)
if err != nil {
return errors.Wrap(err, "error changing directory")
}
mg.Deps(setup.InitModule)
err = getConfiguredBeatsRevision(beatsModuleName, cfg["beats_revision"])
if err != nil {
return errors.Wrap(err, "error while getting required beats version")
}
mg.Deps(setup.CopyVendor)
mg.Deps(setup.GitInit)
if cfg["type"] == "metricbeat" {
//This is runV because it'll ask for user input, so we need stdout.
err = sh.RunV("make", "create-metricset")
if err != nil {
return errors.Wrap(err, "error running create-metricset")
}
}
mg.Deps(setup.Update)
mg.Deps(setup.GitAdd)
fmt.Printf("=======================\n")
fmt.Printf("Your custom beat is now available as %s\n", absBeatPath)
fmt.Printf("=======================\n")
return nil
}
func getConfiguredBeatsRevision(beatsModule, revision string) error {
beatsPkg := beatsModule + "@" + revision
return gotool.Get(
gotool.Get.Download(),
gotool.Get.Update(),
gotool.Get.Package(beatsPkg),
)
}
// VendorUpdate updates the vendor directory
func VendorUpdate() error {
err := sh.Rm("./vendor/github.com/elastic/beats")
if err != nil {
return errors.Wrap(err, "error removing vendor dir")
}
devtools.SetElasticBeatsDir(getAbsoluteBeatsPath())
return setup.CopyVendor()
}
// returns a "compleated" config object with everything we need
func getConfig() (map[string]string, error) {
userCfg := make(map[string]string)
for _, cfgVal := range configList {
var cfgKey string
var err error
cfgKey, isSet := getEnvConfig(cfgVal.Key)
if !isSet {
cfgKey, err = getValFromUser(cfgVal.Help, cfgVal.Default(userCfg))
if err != nil {
return userCfg, err
}
}
userCfg[cfgVal.Key] = cfgKey
}
return userCfg, nil
}
func getEnvConfig(cfgKey string) (string, bool) {
EnvKey := fmt.Sprintf("%s_%s", setup.CfgPrefix, strings.ToUpper(cfgKey))
envKey := os.Getenv(EnvKey)
if envKey == "" {
return envKey, false
}
return envKey, true
}
// getValFromUser returns a config object from the user. If they don't enter one, fallback to the default
func getValFromUser(help, def string) (string, error) {
reader := bufio.NewReader(os.Stdin)
// Human-readable prompt
fmt.Printf("%s [%s]: ", help, def)
str, err := reader.ReadString('\n')
if err != nil {
return "", err
}
if str == "\n" {
return def, nil
}
return strings.TrimSpace(str), nil
}
// getAbsoluteBeatsPath tries to infer the "real" non-vendor beats path
func getAbsoluteBeatsPath() string {
beatsImportPath := "github.com/elastic/beats"
gopath := os.Getenv("GOPATH")
if gopath != "" {
return filepath.Join(gopath, "src", beatsImportPath)
}
return filepath.Join(build.Default.GOPATH, "src", beatsImportPath)
}