/
inventory.go
381 lines (345 loc) · 10.3 KB
/
inventory.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package libhive
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"gopkg.in/inconshreveable/log15.v2"
"gopkg.in/yaml.v3"
)
// Inventory keeps names of clients and simulators.
type Inventory struct {
BaseDir string
Clients map[string]InventoryClient
Simulators map[string]struct{}
}
type InventoryClient struct {
Dockerfiles []string
Meta ClientMetadata
}
// ClientDirectory returns the directory containing the given client's Dockerfile.
// The client name may contain a branch specifier.
func (inv Inventory) ClientDirectory(client ClientDesignator) string {
return filepath.Join(inv.BaseDir, "clients", filepath.FromSlash(client.Client))
}
// SimulatorDirectory returns the directory of containing the given simulator's Dockerfile.
func (inv Inventory) SimulatorDirectory(name string) string {
return filepath.Join(inv.BaseDir, "simulators", filepath.FromSlash(name))
}
// AddClient ensures the given client name is known to the inventory.
// This method exists for unit testing purposes only.
func (inv *Inventory) AddClient(name string, ic *InventoryClient) {
if inv.Clients == nil {
inv.Clients = make(map[string]InventoryClient)
}
var icv InventoryClient
if ic != nil {
icv = *ic
}
inv.Clients[name] = icv
}
// AddSimulator ensures the given simulator name is known to the inventory.
// This method exists for unit testing purposes only.
func (inv *Inventory) AddSimulator(name string) {
if inv.Simulators == nil {
inv.Simulators = make(map[string]struct{})
}
inv.Simulators[name] = struct{}{}
}
// MatchSimulators returns matching simulator names.
func (inv *Inventory) MatchSimulators(expr string) ([]string, error) {
expr = strings.TrimSpace(expr)
if expr == "" {
return nil, nil
}
re, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
var result []string
for sim := range inv.Simulators {
if re.MatchString(sim) {
result = append(result, sim)
}
}
sort.Strings(result)
return result, nil
}
// LoadInventory finds all clients and simulators in basedir.
func LoadInventory(basedir string) (Inventory, error) {
var err error
inv := Inventory{BaseDir: basedir}
inv.Clients, err = findClients(filepath.Join(basedir, "clients"))
if err != nil {
return inv, err
}
inv.Simulators, err = findSimulators(filepath.Join(basedir, "simulators"))
return inv, err
}
func findSimulators(dir string) (map[string]struct{}, error) {
names := make(map[string]struct{})
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
name := info.Name()
// If we hit a dockerfile, add the parent and stop looking in this directory.
if name == "Dockerfile" {
rel, _ := filepath.Rel(dir, filepath.Dir(path))
name := filepath.ToSlash(rel)
names[name] = struct{}{}
return filepath.SkipDir
}
return nil
})
return names, err
}
func findClients(dir string) (map[string]InventoryClient, error) {
clients := make(map[string]InventoryClient)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil || path == dir {
return err
}
rel, err := filepath.Rel(dir, filepath.Dir(path))
if err != nil {
return err
}
clientName := filepath.ToSlash(rel)
// Skip client sub-directories.
if info.IsDir() && path != dir {
if _, ok := clients[clientName]; ok {
return filepath.SkipDir
}
}
// Add Dockerfiles.
file := info.Name()
switch {
case file == "Dockerfile":
clients[clientName] = InventoryClient{
Meta: ClientMetadata{
Roles: []string{"eth1"}, // default role
},
}
case strings.HasPrefix(file, "Dockerfile."):
client, ok := clients[clientName]
if !ok {
log15.Warn(fmt.Sprintf("found %s in directory without Dockerfile", file), "path", filepath.Dir(path))
return nil
}
client.Dockerfiles = append(client.Dockerfiles, strings.TrimPrefix(file, "Dockerfile."))
clients[clientName] = client
case file == "hive.yaml":
client, ok := clients[clientName]
if !ok {
log15.Warn("found hive.yaml in directory without Dockerfile", "path", filepath.Dir(path))
return nil
}
md, err := loadClientMetadata(path)
if err != nil {
return err
}
client.Meta = md
clients[clientName] = client
}
return nil
})
return clients, err
}
func loadClientMetadata(path string) (m ClientMetadata, err error) {
f, err := os.Open(path)
if err != nil {
return m, err
}
defer f.Close()
dec := yaml.NewDecoder(f)
dec.KnownFields(true)
if err := dec.Decode(&m); err != nil {
return m, fmt.Errorf("error in %s: %v", path, err)
}
return m, nil
}
// ClientDesignator specifies a client and build parameters for it.
type ClientDesignator struct {
// Client is the client name.
// This must refer to a subdirectory of clients/
Client string `yaml:"client"`
// Nametag is used in the name of the client image.
// This is for assigning meaningful names to different builds of the same client.
// If unspecified, a default value is chosen to make client names unique.
Nametag string `yaml:"nametag,omitempty"`
// DockerfileExt is the extension of the Docker that should be used to build the
// client. Example: setting this to "git" will build using "Dockerfile.git".
DockerfileExt string `yaml:"dockerfile,omitempty"`
// Arguments passed to the docker build.
BuildArgs map[string]string `yaml:"build_args,omitempty"`
}
func (c ClientDesignator) buildString() string {
var values []string
if c.DockerfileExt != "" {
values = append(values, c.DockerfileExt)
}
if c.BuildArgs["tag"] != "" {
values = append(values, c.BuildArgs["tag"])
}
keys := maps.Keys(c.BuildArgs)
sort.Strings(keys)
for _, k := range keys {
if k == "tag" {
continue
}
values = append(values, k, c.BuildArgs[k])
}
return strings.Join(values, "_")
}
// Dockerfile gives the name of the Dockerfile to use when building the client.
func (c ClientDesignator) Dockerfile() string {
if c.DockerfileExt == "" {
return "Dockerfile"
}
return "Dockerfile." + c.DockerfileExt
}
// Name returns the full client name including nametag.
func (c ClientDesignator) Name() string {
if c.Nametag == "" {
return c.Client
}
return c.Client + "_" + c.Nametag
}
// ParseClientList reads a comma-separated list of client names. Each client name may
// optionally contain a branch/tag specifier separated from the name by underscore, e.g.
// "besu_nightly".
func ParseClientList(inv *Inventory, arg string) ([]ClientDesignator, error) {
var res []ClientDesignator
for _, name := range strings.Split(arg, clientDelimiter) {
c, err := parseClientDesignator(name)
if err != nil {
return nil, err
}
res = append(res, c)
}
if err := validateClients(inv, res); err != nil {
return nil, err
}
return res, nil
}
const (
branchDelimiter = "_" // separates the client name and branch, eg: besu_nightly
clientDelimiter = "," // separates client names in a list
)
// parseClientDesignator parses a client name string.
func parseClientDesignator(fullString string) (ClientDesignator, error) {
var res ClientDesignator
if strings.Count(fullString, branchDelimiter) > 0 {
substrings := strings.Split(fullString, branchDelimiter)
res.Client = strings.Join(substrings[0:len(substrings)-1], "_")
tag := substrings[len(substrings)-1]
if tag == "" {
return res, fmt.Errorf("invalid branch/tag: %s", tag)
}
res.BuildArgs = map[string]string{"tag": tag}
} else {
res.Client = fullString
}
if res.Client == "" {
return res, fmt.Errorf("invalid client name: %s", fullString)
}
return res, nil
}
// ParseClientListYAML reads a YAML document containing a list of clients.
func ParseClientListYAML(inv *Inventory, file io.Reader) ([]ClientDesignator, error) {
var res []ClientDesignator
dec := yaml.NewDecoder(file)
dec.KnownFields(true)
if err := dec.Decode(&res); err != nil {
return nil, fmt.Errorf("unable to parse clients file: %w", err)
}
if err := validateClients(inv, res); err != nil {
return nil, err
}
return res, nil
}
// FilterClients trims the given list to only include clients matching the 'filter list'.
func FilterClients(list []ClientDesignator, filter []string) []ClientDesignator {
accept := make(set[string])
for _, f := range filter {
accept.add(strings.TrimSpace(f))
}
var res []ClientDesignator
for _, c := range list {
if accept.contains(c.Client) || accept.contains(c.Name()) {
res = append(res, c)
}
}
return res
}
var knownBuildArgs = map[string]struct{}{
"tag": {}, // this is the branch/version specifier when pulling the git repo or docker base image
"github": {}, // (for git pull) github repo to clone
"baseimage": {}, // (for dockerhub-based clients) name of the client image
"local_path": {}, // (for builds from local source) path to the source directory
}
func validateClients(inv *Inventory, list []ClientDesignator) error {
occurrences := make(map[string]int)
clientTags := make(map[string]set[string])
for _, c := range list {
occurrences[c.Client]++
// Validate client exists.
ic, ok := inv.Clients[c.Client]
if !ok {
return fmt.Errorf("unknown client %q", c.Client)
}
// Validate DockerfileExt.
if c.DockerfileExt != "" {
if !slices.Contains(ic.Dockerfiles, c.DockerfileExt) {
return fmt.Errorf("client %s doesn't have Dockerfile.%s", c.Client, c.DockerfileExt)
}
}
// Check build arguments.
for key := range c.BuildArgs {
if _, ok := knownBuildArgs[key]; !ok {
log15.Warn(fmt.Sprintf("unknown build arg %q in clients.yaml file", key))
}
}
clientTags[c.Client] = clientTags[c.Client].add(c.BuildArgs["tag"])
}
// Assign nametags.
usednames := make(set[string], len(list))
for i := range list {
c := &list[i]
if occurrences[c.Client] == 1 {
continue
}
if c.Nametag == "" {
// Try assigning nametag based on "tag" argument.
if len(clientTags[c.Client]) == occurrences[c.Client] {
c.Nametag = c.BuildArgs["tag"]
} else {
// Fall back to using all build arguments as nametag.
c.Nametag = c.buildString()
}
}
name := c.Name()
if usednames.contains(name) {
return fmt.Errorf("duplicate client name %q", name)
}
usednames.add(c.Name())
}
return nil
}
type set[X comparable] map[X]struct{}
func (s set[X]) add(x X) set[X] {
if s == nil {
s = make(map[X]struct{})
}
s[x] = struct{}{}
return s
}
func (s set[X]) contains(x X) bool {
_, ok := s[x]
return ok
}