-
Notifications
You must be signed in to change notification settings - Fork 69
/
recipe_filter_runner.go
228 lines (184 loc) · 5.95 KB
/
recipe_filter_runner.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
package recipes
import (
"context"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/install/execution"
"github.com/newrelic/newrelic-cli/internal/install/types"
)
type RecipeFilterRunner struct {
availablilityFilters []RecipeFilterer
userSkippedFilters []RecipeFilterer
installStatus *execution.InstallStatus
}
func NewRecipeFilterRunner(ic types.InstallerContext, s *execution.InstallStatus) *RecipeFilterRunner {
skipFilter := NewSkipFilterer(s)
skipFilter.SkipNames(ic.SkipNames()...)
skipFilter.SkipTypes(ic.SkipTypes()...)
skipFilter.SkipKeywords(ic.SkipKeywords()...)
skipFilter.OnlyNames(ic.RecipeNames...)
return &RecipeFilterRunner{
installStatus: s,
availablilityFilters: []RecipeFilterer{
NewProcessMatchRecipeFilterer(),
NewScriptEvaluationRecipeFilterer(),
},
userSkippedFilters: []RecipeFilterer{
skipFilter,
},
}
}
func (rf *RecipeFilterRunner) RunFilter(ctx context.Context, r *types.OpenInstallationRecipe, m *types.DiscoveryManifest) bool {
for _, f := range rf.availablilityFilters {
filtered := f.Filter(ctx, r, m)
if filtered {
log.Tracef("Filtering out unavailable recipe %s", r.Name)
return true
}
}
if r.HasApplicationTargetType() {
if !r.HasKeyword(types.ApmKeyword) {
rf.installStatus.RecipeRecommended(execution.RecipeStatusEvent{Recipe: *r})
}
} else {
rf.installStatus.RecipeAvailable(*r)
}
for _, f := range rf.userSkippedFilters {
filtered := f.Filter(ctx, r, m)
if filtered {
log.Tracef("Filtering out skipped recipe %s", r.Name)
rf.installStatus.RecipeSkipped(execution.RecipeStatusEvent{Recipe: *r})
return true
}
}
return false
}
func (rf *RecipeFilterRunner) RunFilterAll(ctx context.Context, r []types.OpenInstallationRecipe, m *types.DiscoveryManifest) []types.OpenInstallationRecipe {
results := []types.OpenInstallationRecipe{}
for _, recipe := range r {
filtered := rf.RunFilter(ctx, &recipe, m)
if !filtered {
results = append(results, recipe)
}
}
return results
}
func getRecipeFirstName(r types.OpenInstallationRecipe) string {
if len(r.DisplayName) > 0 {
parts := strings.Split(r.DisplayName, " ")
return parts[0]
}
return r.DisplayName
}
func (rf *RecipeFilterRunner) EnsureDoesNotFilter(ctx context.Context, r []types.OpenInstallationRecipe, m *types.DiscoveryManifest) error {
for _, recipe := range r {
filtered := rf.RunFilter(ctx, &recipe, m)
if filtered {
rf.installStatus.RecipeUnsupported(execution.RecipeStatusEvent{Recipe: recipe})
recipeFirstName := getRecipeFirstName(recipe)
return fmt.Errorf("we couldn’t install the %s. Make sure %s is installed and running on this host and rerun the newrelic-cli command", recipe.DisplayName, recipeFirstName)
}
}
return nil
}
type RecipeFilterer interface {
Filter(context.Context, *types.OpenInstallationRecipe, *types.DiscoveryManifest) bool
}
type ProcessMatchRecipeFilterer struct {
processMatchFinder ProcessMatchFinder
}
func NewProcessMatchRecipeFilterer() *ProcessMatchRecipeFilterer {
return &ProcessMatchRecipeFilterer{
processMatchFinder: NewRegexProcessMatchFinder(),
}
}
func (f *ProcessMatchRecipeFilterer) Filter(ctx context.Context, r *types.OpenInstallationRecipe, m *types.DiscoveryManifest) bool {
matches := f.processMatchFinder.FindMatches(ctx, m.DiscoveredProcesses, *r)
filtered := len(r.ProcessMatch) > 0 && len(matches) == 0
if filtered {
log.Tracef("recipe %s not matching any process", r.Name)
}
return filtered
}
type ScriptEvaluationRecipeFilterer struct {
recipeExecutor execution.RecipeExecutor
}
func NewScriptEvaluationRecipeFilterer() *ScriptEvaluationRecipeFilterer {
recipeExecutor := execution.NewShRecipeExecutor()
return &ScriptEvaluationRecipeFilterer{
recipeExecutor: recipeExecutor,
}
}
func (f *ScriptEvaluationRecipeFilterer) Filter(ctx context.Context, r *types.OpenInstallationRecipe, m *types.DiscoveryManifest) bool {
if err := f.recipeExecutor.ExecutePreInstall(ctx, *r, types.RecipeVars{}); err != nil {
log.Tracef("recipe %s failed script evaluation %s", r.Name, err)
return true
}
return false
}
type SkipFilterer struct {
*execution.InstallStatus
skipNames []string
skipTypes []string
skipKeywords []string
onlyNames []string
}
func NewSkipFilterer(s *execution.InstallStatus) *SkipFilterer {
return &SkipFilterer{
InstallStatus: s,
skipNames: []string{},
skipTypes: []string{},
skipKeywords: []string{},
onlyNames: []string{},
}
}
func (f *SkipFilterer) SkipNames(names ...string) {
f.skipNames = append(f.skipNames, names...)
}
func (f *SkipFilterer) OnlyNames(names ...string) {
f.onlyNames = append(f.onlyNames, names...)
}
func (f *SkipFilterer) SkipTypes(types ...string) {
f.skipTypes = append(f.skipNames, types...)
}
func (f *SkipFilterer) SkipKeywords(keywords ...string) {
f.skipKeywords = append(f.skipNames, keywords...)
}
func (f *SkipFilterer) Filter(ctx context.Context, r *types.OpenInstallationRecipe, m *types.DiscoveryManifest) bool {
if len(f.onlyNames) > 0 {
filtered := true
for _, n := range f.onlyNames {
if strings.EqualFold(strings.TrimSpace(n), strings.TrimSpace(r.Name)) {
filtered = false
}
}
if filtered {
log.Tracef("recipe %s does not match provided names %s", r.Name, f.onlyNames)
return true
}
}
for _, n := range f.skipNames {
if strings.EqualFold(strings.TrimSpace(n), strings.TrimSpace(r.Name)) {
log.Tracef("recipe %s found in skip list %s", r.Name, f.skipNames)
return true
}
}
for _, k := range f.skipKeywords {
if r.HasKeyword(k) {
log.Tracef("recipe %s has keyword %s found in skip list %s", r.Name, k, f.skipKeywords)
return true
}
}
// Infra should never be skipped based on type
if r.Name == types.InfraAgentRecipeName {
return false
}
for _, t := range f.skipTypes {
if r.HasTargetType(types.OpenInstallationTargetType(t)) {
log.Tracef("recipe %s has type %s found in skip list %s", r.Name, t, f.skipTypes)
return true
}
}
return false
}