-
Notifications
You must be signed in to change notification settings - Fork 73
/
command.go
122 lines (99 loc) · 3.69 KB
/
command.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
package install
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/install/types"
"github.com/newrelic/newrelic-cli/internal/utils"
nrErrors "github.com/newrelic/newrelic-client-go/pkg/errors"
)
var (
assumeYes bool
localRecipes string
recipeNames []string
recipePaths []string
testMode bool
)
// Command represents the install command.
var Command = &cobra.Command{
Use: "install",
Short: "Install New Relic.",
PreRun: client.RequireClient,
RunE: func(cmd *cobra.Command, args []string) error {
ic := types.InstallerContext{
AssumeYes: assumeYes,
LocalRecipes: localRecipes,
RecipeNames: recipeNames,
RecipePaths: recipePaths,
}
logLevel := configAPI.GetLogLevel()
config.InitFileLogger(logLevel)
err := assertProfileIsValid()
if err != nil {
log.Fatal(err)
return nil
}
i := NewRecipeInstaller(ic, client.NRClient)
// Run the install.
if err := i.Install(); err != nil {
if err == types.ErrInterrupt {
return nil
}
if _, ok := err.(*types.UpdateRequiredError); ok {
return nil
}
if e, ok := err.(*nrErrors.PaymentRequiredError); ok {
return e
}
fallbackErrorMsg := fmt.Sprintf("\nWe encountered an issue during the installation: %s.", err)
fallbackHelpMsg := "If this problem persists, visit the documentation and support page for additional help here at https://docs.newrelic.com/docs/infrastructure/install-infrastructure-agent/get-started/requirements-infrastructure-agent/"
// In the extremely rare case we run into an uncaught error (e.g. no recipes found),
// we need to output something to user to sinc we probably haven't displayed anything yet.
fmt.Println(fallbackErrorMsg)
fmt.Println(fallbackHelpMsg)
fmt.Print("\n\n")
log.Debug(fallbackErrorMsg)
}
return nil
},
}
func assertProfileIsValid() error {
accountID := configAPI.GetActiveProfileAccountID()
if accountID == 0 {
return fmt.Errorf("accountID is required")
}
if configAPI.GetActiveProfileString(config.APIKey) == "" {
return fmt.Errorf("API key is required")
}
if configAPI.GetActiveProfileString(config.Region) == "" {
return fmt.Errorf("region is required")
}
licenseKey, err := client.FetchLicenseKey(accountID, config.FlagProfileName)
if err != nil {
return fmt.Errorf("could not fetch license key for account %d: %s", accountID, err)
}
if licenseKey != configAPI.GetActiveProfileString(config.LicenseKey) {
os.Setenv("NEW_RELIC_LICENSE_KEY", licenseKey)
log.Debugf("using license key %s", utils.Obfuscate(licenseKey))
}
// Reinitialize client, overriding fetched values
c, err := client.NewClient(configAPI.GetActiveProfileName())
if err != nil {
// An error was encountered initializing the client. This may not be a
// problem since many commands don't require the use of an initialized client
log.Debugf("error initializing client: %s", err)
}
client.NRClient = c
return nil
}
func init() {
Command.Flags().StringSliceVarP(&recipePaths, "recipePath", "c", []string{}, "the path to a recipe file to install")
Command.Flags().StringSliceVarP(&recipeNames, "recipe", "n", []string{}, "the name of a recipe to install")
Command.Flags().BoolVarP(&testMode, "testMode", "t", false, "fakes operations for UX testing")
Command.Flags().BoolVarP(&assumeYes, "assumeYes", "y", false, "use \"yes\" for all questions during install")
Command.Flags().StringVarP(&localRecipes, "localRecipes", "", "", "a path to local recipes to load instead of service other fetching")
}