-
Notifications
You must be signed in to change notification settings - Fork 69
/
recipe_detector.go
123 lines (100 loc) · 3.22 KB
/
recipe_detector.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
package recipes
import (
"context"
"sort"
"time"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/install/execution"
"github.com/newrelic/newrelic-cli/internal/install/types"
)
type DetectionStatusProvider interface {
DetectionStatus(context.Context, *types.OpenInstallationRecipe) execution.RecipeStatusType
}
type RecipeDetectionResult struct {
Recipe *types.OpenInstallationRecipe
Status execution.RecipeStatusType
DurationMs int64
}
type RecipeDetectionResults []*RecipeDetectionResult
func (rd *RecipeDetectionResults) GetRecipeDetection(name string) (*RecipeDetectionResult, bool) {
for _, d := range *rd {
if d.Recipe.Name == name {
return d, true
}
}
return nil, false
}
func (rd RecipeDetectionResults) Len() int {
return len(rd)
}
func (rd RecipeDetectionResults) Swap(i, j int) {
rd[i], rd[j] = rd[j], rd[i]
}
func (rd RecipeDetectionResults) Less(i, j int) bool {
return rd[i].Recipe.Name < rd[j].Recipe.Name
}
type RecipeDetector struct {
processEvaluator DetectionStatusProvider
scriptEvaluator DetectionStatusProvider
context context.Context
repo Finder
installerContext *types.InstallerContext
}
func NewRecipeDetector(contex context.Context, repo *RecipeRepository, peval ProcessEvaluatorInterface, ic *types.InstallerContext) *RecipeDetector {
return &RecipeDetector{
processEvaluator: peval,
scriptEvaluator: NewScriptEvaluator(),
context: contex,
repo: repo,
installerContext: ic,
}
}
func (dt *RecipeDetector) GetDetectedRecipes() (RecipeDetectionResults, RecipeDetectionResults, error) {
availableRecipes := RecipeDetectionResults{}
unavailableRecipes := RecipeDetectionResults{}
recipes, err := dt.repo.FindAll()
if err != nil {
return nil, nil, err
}
for _, r := range recipes {
dr := dt.detectRecipe(r)
if dr.Status == execution.RecipeStatusTypes.AVAILABLE {
availableRecipes = append(availableRecipes, dr)
} else {
unavailableRecipes = append(unavailableRecipes, dr)
}
}
sort.Sort(availableRecipes)
return availableRecipes, unavailableRecipes, nil
}
func (dt *RecipeDetector) shouldDiscover(recipe *types.OpenInstallationRecipe) bool {
isTargeted := dt.installerContext.IsRecipeTargeted(recipe.Name)
if len(recipe.PreInstall.DiscoveryMode) == 1 &&
(recipe.PreInstall.DiscoveryMode[0] == types.OpenInstallationDiscoveryModeTypes.TARGETED) {
return isTargeted
}
return true
}
func (dt *RecipeDetector) detectRecipe(recipe *types.OpenInstallationRecipe) *RecipeDetectionResult {
start := time.Now()
if !dt.shouldDiscover(recipe) {
durationMs := time.Since(start).Milliseconds()
return &RecipeDetectionResult{
recipe,
execution.RecipeStatusTypes.NULL,
durationMs,
}
}
status := dt.processEvaluator.DetectionStatus(dt.context, recipe)
durationMs := time.Since(start).Milliseconds()
if status == execution.RecipeStatusTypes.AVAILABLE && recipe.PreInstall.RequireAtDiscovery != "" {
status = dt.scriptEvaluator.DetectionStatus(dt.context, recipe)
durationMs = time.Since(start).Milliseconds()
log.Debugf("ScriptEvaluation for recipe:%s completed in %dms with status:%s", recipe.Name, durationMs, status)
}
return &RecipeDetectionResult{
recipe,
status,
durationMs,
}
}