-
Notifications
You must be signed in to change notification settings - Fork 73
/
bundle_installer.go
167 lines (133 loc) · 4.49 KB
/
bundle_installer.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
package install
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/install/execution"
"github.com/newrelic/newrelic-cli/internal/install/recipes"
"github.com/newrelic/newrelic-cli/internal/install/types"
"github.com/newrelic/newrelic-cli/internal/install/ux"
)
type StatusReporter interface {
ReportStatus(status execution.RecipeStatusType, event execution.RecipeStatusEvent)
}
type BundleInstaller struct {
installedRecipes map[string]bool
ctx context.Context
manifest *types.DiscoveryManifest
statusReporter StatusReporter
recipeInstaller RecipeInstaller
prompter Prompter
}
func NewBundleInstaller(ctx context.Context, manifest *types.DiscoveryManifest, recipeInstallerInterface RecipeInstaller, statusReporter StatusReporter) *BundleInstaller {
return &BundleInstaller{
ctx: ctx,
manifest: manifest,
recipeInstaller: recipeInstallerInterface,
statusReporter: statusReporter,
installedRecipes: make(map[string]bool),
prompter: NewPrompter(),
}
}
func NewPrompter() *ux.PromptUIPrompter {
return ux.NewPromptUIPrompter()
}
func (bi *BundleInstaller) InstallStopOnError(bundle *recipes.Bundle, assumeYes bool) error {
bi.reportBundleStatus(bundle)
installableBundleRecipes := bi.getInstallableBundleRecipes(bundle)
if len(installableBundleRecipes) == 0 {
return nil
}
for _, br := range installableBundleRecipes {
err := bi.InstallBundleRecipe(br, assumeYes)
if err != nil {
return err
}
}
return nil
}
func (bi *BundleInstaller) InstallContinueOnError(bundle *recipes.Bundle, assumeYes bool) {
bi.reportBundleStatus(bundle)
installableBundleRecipes := bi.getInstallableBundleRecipes(bundle)
if len(installableBundleRecipes) == 0 {
return
}
if !assumeYes && bundle.IsAdditionalGuided() {
fmt.Println("\nWe've detected additional monitoring that can be configured by installing the following:")
for _, bundleRecipe := range installableBundleRecipes {
fmt.Printf(" %s\n", bundleRecipe.Recipe.DisplayName)
}
fmt.Println()
prompter := ux.NewPromptUIPrompter()
msg := "Continue installing? "
isConfirmed, err := prompter.PromptYesNo(msg)
if err != nil {
log.Debug(err)
isConfirmed = false
}
if !isConfirmed {
for _, additionalRecipe := range installableBundleRecipes {
skippedEvent := execution.NewRecipeStatusEvent(additionalRecipe.Recipe)
bi.statusReporter.ReportStatus(execution.RecipeStatusTypes.SKIPPED, skippedEvent)
}
return
}
}
for _, additionalRecipe := range installableBundleRecipes {
err := bi.InstallBundleRecipe(additionalRecipe, assumeYes)
if err != nil {
log.Debugf("error installing recipe %v: %v", additionalRecipe.Recipe.Name, err)
}
}
}
func (bi *BundleInstaller) reportBundleStatus(bundle *recipes.Bundle) {
for _, recipe := range bundle.BundleRecipes {
if bi.installedRecipes[recipe.Recipe.Name] {
continue
}
for _, ds := range recipe.DetectedStatuses {
e := execution.RecipeStatusEvent{Recipe: *recipe.Recipe, ValidationDurationMs: ds.DurationMs}
bi.statusReporter.ReportStatus(ds.Status, e)
}
}
}
func (bi *BundleInstaller) InstalledRecipesCount() int {
return len(bi.installedRecipes)
}
func (bi *BundleInstaller) InstallBundleRecipe(bundleRecipe *recipes.BundleRecipe, assumeYes bool) error {
var err error
for _, dr := range bundleRecipe.Dependencies {
err = bi.InstallBundleRecipe(dr, assumeYes)
if err != nil {
return err
}
}
recipeName := bundleRecipe.Recipe.Name
if bi.installedRecipes[bundleRecipe.Recipe.Name] {
return nil
}
log.WithFields(log.Fields{
"name": recipeName,
}).Debug("installing recipe")
_, err = bi.recipeInstaller.executeAndValidateWithProgress(bi.ctx, bi.manifest, bundleRecipe.Recipe, assumeYes)
if err != nil {
log.Debugf("Failed while executing and validating with progress for recipe name %s, detail:%s", recipeName, err)
return err
}
bi.installedRecipes[recipeName] = true
log.Debugf("Done executing and validating with progress for recipe name %s.", recipeName)
return nil
}
func (bi *BundleInstaller) getInstallableBundleRecipes(bundle *recipes.Bundle) []*recipes.BundleRecipe {
var bundleRecipes []*recipes.BundleRecipe
for _, bundleRecipe := range bundle.BundleRecipes {
if !bundleRecipe.HasStatus(execution.RecipeStatusTypes.AVAILABLE) {
//Skip if not available
continue
}
if !bi.installedRecipes[bundleRecipe.Recipe.Name] {
bundleRecipes = append(bundleRecipes, bundleRecipe)
}
}
return bundleRecipes
}