-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcommand.go
More file actions
304 lines (235 loc) · 9.02 KB
/
Copy pathcommand.go
File metadata and controls
304 lines (235 loc) · 9.02 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package install
import (
"errors"
"fmt"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/http/httpproxy"
"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/v2/pkg/errors"
nrRegion "github.com/newrelic/newrelic-client-go/v2/pkg/region"
)
var (
assumeYes bool
localRecipes string
recipeNames []string
recipePaths []string
tags []string
)
// processRecipeNames validates, extracts recipe names, and sets environment variables.
func processRecipeNames(recipeNames []string) ([]string, error) {
var extractedNames []string
for _, recipe := range recipeNames {
parts := strings.Split(recipe, "@")
if len(parts) < 1 || len(parts) > 2 {
return nil, fmt.Errorf("invalid recipe name format provided: %s", recipe)
}
// Extract the base recipe name
extractedNames = append(extractedNames, parts[0])
// If version is present, set the environment variable
if len(parts) == 2 {
recipeName := parts[0]
version := parts[1]
envVarName := strings.ToUpper(strings.ReplaceAll(recipeName, "-", "_")) + "_VERSION"
err := os.Setenv(envVarName, version)
if err != nil {
return nil, fmt.Errorf("error setting recipe version to environment variable %s: %v", envVarName, err)
}
}
}
return extractedNames, nil
}
// 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 {
extractedRecipeNames, err := processRecipeNames(recipeNames)
if err != nil {
return types.NewDetailError(types.EventTypes.OtherError, err.Error())
}
ic := types.InstallerContext{
AssumeYes: assumeYes,
LocalRecipes: localRecipes,
RecipeNames: extractedRecipeNames,
RecipePaths: recipePaths,
}
ic.SetTags(tags)
logLevel := configAPI.GetLogLevel()
config.InitFileLogger(logLevel)
if err := checkNetwork(); err != nil {
return types.NewDetailError(types.EventTypes.UnableToConnect, err.Error())
}
detailErr := fetchLicenseKey()
if detailErr != nil {
log.Fatal(detailErr)
}
i := NewRecipeInstaller(ic)
//// Do not install both infra and agent controls simultaneously: install only the 'agent-control' if targeted.
//if i.IsRecipeTargeted(types.AgentControlRecipeName) && i.shouldInstallCore() {
// log.Debugf("'%s' is targeted, disabling infra/logs core bundle install\n", types.AgentControlRecipeName)
// i.shouldInstallCore = func() bool { return false }
//}
// 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
}
if errors.Is(err, types.ErrAgentControl) {
return err
}
// For targeted installs, return the error to ensure non-zero exit code
if len(extractedRecipeNames) > 0 {
return err
}
if i.shouldInstallCore() {
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 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(&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")
Command.Flags().StringSliceVarP(&tags, "tag", "", []string{}, "the tags to add during install, can be multiple. Example: --tag tag1:test,tag2:test")
}
func validateProfile() *types.DetailError {
accountID := configAPI.GetActiveProfileAccountID()
apiKey := configAPI.GetActiveProfileString(config.APIKey)
region := configAPI.GetActiveProfileString(config.Region)
if accountID == 0 {
return types.NewDetailError(types.EventTypes.AccountIDMissing, "Account ID is required.")
}
if apiKey == "" {
return types.NewDetailError(types.EventTypes.APIKeyMissing, "User API key is required.")
}
if !utils.IsValidUserAPIKeyFormat(apiKey) {
return types.NewDetailError(types.EventTypes.InvalidUserAPIKeyFormat, `Invalid user API key format detected. Please provide a valid user API key. User API keys usually have a prefix of "NRAK-" or "NRAA-".`)
}
if region == "" {
return types.NewDetailError(types.EventTypes.RegionMissing, "Region is required.")
}
if _, err := nrRegion.Parse(region); err != nil {
return types.NewDetailError(types.EventTypes.InvalidRegion, `Invalid region provided. Valid regions are "US", "EU", or "JP".`)
}
return nil
}
func checkNetwork() error {
if client.NRClient == nil {
return nil
}
err := client.NRClient.TestEndpoints()
proxyConfig := httpproxy.FromEnvironment()
if err == nil {
if IsProxyConfigured() {
if strings.Contains(strings.ToLower(proxyConfig.HTTPSProxy), "http") && !strings.Contains(strings.ToLower(proxyConfig.HTTPSProxy), "https") {
log.Warn("Please ensure the HTTPS_PROXY environment variable is set when using a proxy server.")
log.Warn("New Relic CLI exclusively supports https proxy, not http for security reasons.")
}
}
}
if err != nil {
if IsProxyConfigured() {
proxyConfig := httpproxy.FromEnvironment()
log.Warn("Proxy settings have been configured, but we are still unable to connect to the New Relic platform.")
log.Warn("You may need to adjust your proxy environment variables or configure your proxy to allow the specified domain.")
log.Warn("Current proxy config:")
log.Warnf(" HTTPS_PROXY=%s", proxyConfig.HTTPSProxy)
log.Warnf(" HTTP_PROXY=%s", proxyConfig.HTTPProxy)
log.Warnf(" NO_PROXY=%s", proxyConfig.NoProxy)
} else {
log.Warn("Failed to connect to the New Relic platform.")
log.Warn("If you need to use a proxy, consider setting the HTTPS_PROXY environment variable, then try again.")
}
log.Warn("More information about proxy configuration: https://github.com/newrelic/newrelic-cli/blob/main/docs/GETTING_STARTED.md#using-an-http-proxy")
log.Warn("More information about network requirements: https://docs.newrelic.com/docs/new-relic-solutions/get-started/networks/")
}
return err
}
// Attempt to fetch and set a license key through 3 methods:
// 1. NEW_RELIC_LICENSE_KEY environment variable,
// 2. Active profile config.LicenseKey,
// 3. API call,
// returns an error if all methods fail.
func fetchLicenseKey() *types.DetailError {
var licenseKey string
setLicenseKey := func(licenseKey string) {
os.Setenv("NEW_RELIC_LICENSE_KEY", licenseKey)
// Reinitialize client, overriding fetched values
c, _ := client.NewClient(configAPI.GetActiveProfileName())
client.NRClient = c
log.Debug("using license key: ", utils.Obfuscate(licenseKey))
}
licenseKey = fetchLicenseKeyFromEnvironment()
if licenseKey != "" {
setLicenseKey(licenseKey)
return nil
}
licenseKey = fetchLicenseKeyFromProfile()
if licenseKey != "" {
setLicenseKey(licenseKey)
return nil
}
// fetch licenseKey via API
detailErr := validateProfile()
if detailErr != nil {
log.Fatal(detailErr)
}
accountID := configAPI.GetActiveProfileAccountID()
maxTimeoutSeconds := config.DefaultMaxTimeoutSeconds
licenseKey, err := client.FetchLicenseKey(accountID, config.FlagProfileName, &maxTimeoutSeconds)
if err != nil {
err = fmt.Errorf("could not fetch license key for accountID (%d): %s",
accountID,
err.Error())
return types.NewDetailError(types.EventTypes.UnableToFetchLicenseKey, err.Error())
}
setLicenseKey(licenseKey)
return nil
}
func fetchLicenseKeyFromEnvironment() string {
licenseKey := os.Getenv("NEW_RELIC_LICENSE_KEY")
if licenseKey == "" {
return ""
}
if utils.IsValidLicenseKeyFormat(licenseKey) {
return licenseKey
}
log.Debug("license key provided via NEW_RELIC_LICENSE_KEY is invalid")
return ""
}
func fetchLicenseKeyFromProfile() string {
// fetch licenseKey from active profile
licenseKey := configAPI.GetActiveProfileString(config.LicenseKey)
if licenseKey == "" {
return ""
}
if utils.IsValidLicenseKeyFormat(licenseKey) {
return licenseKey
}
log.Debug("license key provided by config is invalid")
return ""
}