-
Notifications
You must be signed in to change notification settings - Fork 69
/
recipe_repository.go
286 lines (246 loc) · 7.29 KB
/
recipe_repository.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
package recipes
import (
"math"
"regexp"
"sort"
"strings"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/install/types"
"github.com/newrelic/newrelic-cli/internal/utils"
)
var (
kernelArch = "KernelArch"
kernelVersion = "KernelVersion"
oS = "OS"
platform = "Platform"
platformFamily = "PlatformFamily"
platformVersion = "PlatformVersion"
)
type Finder interface {
FindAll() ([]*types.OpenInstallationRecipe, error)
}
type RecipeRepository struct {
RecipeLoaderFunc func() ([]*types.OpenInstallationRecipe, error)
loadedRecipes []*types.OpenInstallationRecipe
filteredRecipes []*types.OpenInstallationRecipe
discoveryManifest *types.DiscoveryManifest
logMatchFinder LogMatchFinderDefinition
}
type recipeMatch struct {
matchCount int
recipe *types.OpenInstallationRecipe
}
// NewRecipeRepository returns a new instance of types.RecipeRepository.
func NewRecipeRepository(loaderFunc func() ([]*types.OpenInstallationRecipe, error), manifest *types.DiscoveryManifest) *RecipeRepository {
return newRecipeRepository(loaderFunc, manifest, NewLogMatchFinder())
}
func newRecipeRepository(loaderFunc func() ([]*types.OpenInstallationRecipe, error), manifest *types.DiscoveryManifest, logMatchFinder LogMatchFinderDefinition) *RecipeRepository {
rr := RecipeRepository{
RecipeLoaderFunc: loaderFunc,
loadedRecipes: nil,
filteredRecipes: nil,
discoveryManifest: manifest,
logMatchFinder: logMatchFinder,
}
return &rr
}
func (rf *RecipeRepository) FindRecipeByName(name string) *types.OpenInstallationRecipe {
recipes, err := rf.FindAll()
if err != nil {
log.Error(err)
return nil
}
for _, r := range recipes {
if strings.EqualFold(r.Name, name) {
log.Debugf("Found recipe name %v", r.Name)
return r
}
}
return nil
}
func (rf *RecipeRepository) FindRecipes(excludingRecipes []*types.OpenInstallationRecipe) []*types.OpenInstallationRecipe {
results := []*types.OpenInstallationRecipe{}
recipes, err := rf.FindAll()
if err != nil {
log.Error(err)
return nil
}
var found bool
for _, r := range recipes {
found = false
for _, er := range excludingRecipes {
if strings.EqualFold(r.Name, er.Name) {
found = true
break
}
}
if !found {
results = append(results, r)
}
}
return results
}
// Enriching recipe
func (rf *RecipeRepository) enrichLogRecipe() {
for _, recipe := range rf.filteredRecipes {
if recipe.Name == types.LoggingRecipeName {
logMatches := rf.logMatchFinder.GetPaths(utils.SignalCtx, rf.filteredRecipes)
var discoveredLogFilesString string
if len(logMatches) > 0 {
discoveredLogFiles := []string{}
for _, logMatch := range logMatches {
discoveredLogFiles = append(discoveredLogFiles, logMatch.File)
}
log.WithFields(log.Fields{
"logMatches": len(logMatches),
}).Debug("filtered log matches")
discoveredLogFilesString = strings.Join(discoveredLogFiles, ",")
}
recipe.SetRecipeVar("NR_DISCOVERED_LOG_FILES", discoveredLogFilesString)
break
}
}
}
func (rf *RecipeRepository) FindAll() ([]*types.OpenInstallationRecipe, error) {
if rf.filteredRecipes != nil {
return rf.filteredRecipes, nil
}
if rf.loadedRecipes == nil {
recipes, err := rf.RecipeLoaderFunc()
if err != nil {
return nil, err
}
log.Debugf("Loaded %d recipes", len(recipes))
rf.loadedRecipes = recipes
}
rf.filteredRecipes = filterRecipes(rf.loadedRecipes, rf.discoveryManifest)
rf.enrichLogRecipe()
return rf.filteredRecipes, nil
}
func filterRecipes(loadedRecipes []*types.OpenInstallationRecipe, m *types.DiscoveryManifest) []*types.OpenInstallationRecipe {
results := []*types.OpenInstallationRecipe{}
matchRecipes := make(map[string][]recipeMatch)
hostMap := getHostMap(m)
log.Debugf("Find all available out of %d recipes for host %+v", len(loadedRecipes), hostMap)
for _, recipe := range loadedRecipes {
matchTargetCount := []int{}
for _, rit := range recipe.InstallTargets {
matchCount := 0
for k, v := range getRecipeTargetMap(rit) {
if v == "" {
continue
}
isValueMatching := matchRecipeCriteria(hostMap, k, v)
if isValueMatching {
log.Tracef("matching recipe %s field name %s and value %s using hostMap %+v", recipe.Name, k, v, hostMap)
matchCount++
} else {
log.Tracef("recipe %s defines %s=%s but input did not provide a match using hostMap %+v", recipe.Name, k, v, hostMap)
matchCount = -1
break
}
}
if matchCount >= 0 {
matchTargetCount = append(matchTargetCount, matchCount)
}
}
if len(recipe.InstallTargets) == 0 || len(matchTargetCount) > 0 {
maxMatchTargetCount := 0
if len(matchTargetCount) > 0 {
maxMatchTargetCount = mathMax(matchTargetCount)
}
log.Tracef("Recipe InstallTargetsCount %d and maxMatchCount %d", len(recipe.InstallTargets), maxMatchTargetCount)
match := recipeMatch{
recipe: recipe,
matchCount: maxMatchTargetCount,
}
if _, ok := matchRecipes[recipe.Name]; !ok {
matches := []recipeMatch{match}
matchRecipes[recipe.Name] = matches
} else {
matchRecipes[recipe.Name] = append(matchRecipes[recipe.Name], match)
}
}
}
if len(matchRecipes) > 0 {
keys := []string{}
unorderedResults := map[string]*types.OpenInstallationRecipe{}
for _, matches := range matchRecipes {
if len(matches) > 0 {
match := findMaxMatch(matches)
singleRecipe := match.recipe
log.Tracef("Add result for recipe name %s with targets %+v", match.recipe, match.recipe.InstallTargets)
key := singleRecipe.GetOrderKey()
keys = append(keys, key)
unorderedResults[key] = singleRecipe
}
}
sort.Strings(keys)
for _, k := range keys {
recipe := unorderedResults[k]
results = append(results, recipe)
}
}
return results
}
func findMaxMatch(matches []recipeMatch) recipeMatch {
var result *recipeMatch
for _, match := range matches {
if result == nil {
result = &recipeMatch{
recipe: match.recipe,
matchCount: match.matchCount,
}
} else {
if match.matchCount > result.matchCount {
result = &recipeMatch{
recipe: match.recipe,
matchCount: match.matchCount,
}
}
}
}
return *result
}
func mathMax(numbers []int) int {
result := math.MinInt32
for _, number := range numbers {
if number > result {
result = number
}
}
return result
}
func matchRecipeCriteria(hostMap map[string]string, rkey string, rvalue string) bool {
if val, ok := hostMap[rkey]; ok {
if len(rvalue) > 0 && rvalue[0] == '(' {
if regex, error := regexp.Compile(rvalue); error == nil {
return regex.MatchString(val)
}
}
return strings.EqualFold(val, rvalue)
}
return false
}
func getHostMap(m *types.DiscoveryManifest) map[string]string {
hostMap := map[string]string{
kernelArch: m.KernelArch,
kernelVersion: m.KernelVersion,
oS: m.OS,
platform: m.Platform,
platformFamily: m.PlatformFamily,
platformVersion: m.PlatformVersion,
}
return hostMap
}
func getRecipeTargetMap(rit types.OpenInstallationRecipeInstallTarget) map[string]string {
targetMap := map[string]string{
kernelArch: rit.KernelArch,
kernelVersion: rit.KernelVersion,
oS: string(rit.Os),
platform: string(rit.Platform),
platformFamily: string(rit.PlatformFamily),
platformVersion: rit.PlatformVersion,
}
return targetMap
}