From cdb6b179d3b73c3b7bef69cfddc18b54aaef7527 Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Fri, 16 Sep 2022 10:48:14 +0530 Subject: [PATCH] Upload profile zip to SUBNET (#4226) When the `--airgap` flag is not passed, the generated profile zip will be uploaded to SUBNET. --- cmd/license-register.go | 10 ++---- cmd/subnet-utils.go | 4 +++ cmd/support-diag.go | 4 --- cmd/support-profile.go | 69 ++++++++++++++++++++++++++++------------- cmd/support-register.go | 4 --- 5 files changed, 55 insertions(+), 36 deletions(-) diff --git a/cmd/license-register.go b/cmd/license-register.go index c15a3cb0..9627b221 100644 --- a/cmd/license-register.go +++ b/cmd/license-register.go @@ -31,10 +31,6 @@ import ( const licRegisterMsgTag = "licenseRegisterMessage" var licenseRegisterFlags = append([]cli.Flag{ - cli.StringFlag{ - Name: "api-key", - Usage: "SUBNET API key", - }, cli.StringFlag{ Name: "name", Usage: "Specify the name to associate to this MinIO cluster in SUBNET", @@ -170,9 +166,9 @@ func mainLicenseRegister(ctx *cli.Context) error { regInfo := getClusterRegInfo(getAdminInfo(aliasedURL), clusterName) alreadyRegistered := false - apiKey, lic, e := getSubnetCreds(alias) - fatalIf(probe.NewError(e), "Error in fetching subnet credentials") - if len(apiKey) > 0 || len(lic) > 0 { + apiKey, _, e := getSubnetCreds(alias) + fatalIf(probe.NewError(e), "Error in fetching subnet API Key") + if len(apiKey) > 0 { alreadyRegistered = true if len(accAPIKey) == 0 { accAPIKey = apiKey diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index ac59e896..edc35e5e 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -70,6 +70,10 @@ mr/cKCUyBL7rcAvg0zNq1vcSrUSGlAmY3SEDCu3GOKnjG/U4E7+p957ocWSV+mQU Usage: "Development mode - talks to local SUBNET", Hidden: true, }, + cli.StringFlag{ + Name: "api-key", + Usage: "API Key of the account on SUBNET", + }, } ) diff --git a/cmd/support-diag.go b/cmd/support-diag.go index 8dc07187..be67012f 100644 --- a/cmd/support-diag.go +++ b/cmd/support-diag.go @@ -42,10 +42,6 @@ import ( ) var supportDiagFlags = append([]cli.Flag{ - cli.StringFlag{ - Name: "api-key", - Usage: "SUBNET API key", - }, HealthDataTypeFlag{ Name: "test", Usage: "choose specific diagnostics to run [" + options.String() + "]", diff --git a/cmd/support-profile.go b/cmd/support-profile.go index 62b539eb..4b4ab8b5 100644 --- a/cmd/support-profile.go +++ b/cmd/support-profile.go @@ -34,7 +34,7 @@ import ( // profile command flags. var ( - profileFlags = []cli.Flag{ + profileFlags = append([]cli.Flag{ cli.IntFlag{ Name: "duration", Usage: "start profiling for the specified duration in seconds", @@ -45,9 +45,11 @@ var ( Usage: "profiler type, possible values are 'cpu', 'cpuio', 'mem', 'block', 'mutex', 'trace', 'threads' and 'goroutines'", Value: "cpu,mem,block,mutex,threads,goroutines", }, - } + }, subnetCommonFlags...) ) +const profileFile = "profile.zip" + var supportProfileCmd = cli.Command{ Name: "profile", Usage: "generate profile data for debugging", @@ -132,7 +134,7 @@ func moveFile(sourcePath, destPath string) error { return os.Remove(sourcePath) } -func getProfileData(data io.ReadCloser) string { +func saveProfileFile(data io.ReadCloser) { // Create profile zip file tmpFile, e := ioutil.TempFile("", "mc-profile-") fatalIf(probe.NewError(e), "Unable to download profile data.") @@ -145,45 +147,70 @@ func getProfileData(data io.ReadCloser) string { data.Close() tmpFile.Close() - downloadPath := "profile.zip" - downloadedFile := downloadPath + "." + time.Now().Format(dateTimeFormatFilename) + downloadedFile := profileFile + "." + time.Now().Format(dateTimeFormatFilename) - fi, e := os.Stat(downloadPath) + fi, e := os.Stat(profileFile) if e == nil && !fi.IsDir() { - e = moveFile(downloadPath, downloadedFile) + e = moveFile(profileFile, downloadedFile) fatalIf(probe.NewError(e), "Unable to create a backup of profile.zip") } else { if !os.IsNotExist(e) { fatal(probe.NewError(e), "Unable to save profile data") } } - fatalIf(probe.NewError(moveFile(tmpFile.Name(), downloadPath)), "Unable to save profile data") - return downloadPath + fatalIf(probe.NewError(moveFile(tmpFile.Name(), profileFile)), "Unable to save profile data") } // mainSupportProfile is the handle for "mc support profile" command. func mainSupportProfile(ctx *cli.Context) error { // Check for command syntax checkAdminProfileSyntax(ctx) - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) + // Get the alias parameter from cli + aliasedURL := ctx.Args().Get(0) + alias, apiKey := initSubnetConnectivity(ctx, aliasedURL) + + // if `--airgap` is provided do not try to upload to SUBNET. + if !globalAirgapped { + fatalIf(checkURLReachable(subnetBaseURL()).Trace(aliasedURL), "Unable to reach %s to upload MinIO profile file, please use --airgap to upload manually", subnetBaseURL()) + } + + // Create a new MinIO Admin Client + client := getClient(aliasedURL) + + // Main execution + execSupportProfile(ctx, client, alias, apiKey) + return nil +} + +func execSupportProfile(ctx *cli.Context, client *madmin.AdminClient, alias string, apiKey string) { + var reqURL string + var headers map[string]string profilers := ctx.String("type") duration := ctx.Int("duration") - // Create a new MinIO Admin Client - client, err := newAdminClient(aliasedURL) - if err != nil { - fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client") - return nil + if !globalAirgapped { + // Retrieve subnet credentials (login/license) beforehand as + // it can take a long time to fetch the profile data + uploadURL := subnetUploadURL("profile", profileFile) + reqURL, headers = prepareSubnetUploadURL(uploadURL, alias, profileFile, apiKey) } - console.Infof("Profiling '%s' for %d seconds... ", aliasedURL, duration) + console.Infof("Profiling '%s' for %d seconds... ", alias, duration) data, e := client.Profile(globalContext, madmin.ProfilerType(profilers), time.Second*time.Duration(duration)) - fatalIf(probe.NewError(e), "Unable to save profile data") + + saveProfileFile(data) + clr := color.New(color.FgGreen, color.Bold) - clr.Printf("saved successfully at '%s'\n", getProfileData(data)) - return nil + if !globalAirgapped { + _, e := uploadFileToSubnet(alias, profileFile, reqURL, headers) + fatalIf(probe.NewError(e), "Unable to upload profile file to SUBNET portal") + if len(apiKey) > 0 { + setSubnetAPIKey(alias, apiKey) + } + clr.Println("uploaded successfully to SUBNET.") + } else { + clr.Printf("saved successfully at '%s'\n", profileFile) + } } diff --git a/cmd/support-register.go b/cmd/support-register.go index 56c3aa6d..b74fa57c 100644 --- a/cmd/support-register.go +++ b/cmd/support-register.go @@ -23,10 +23,6 @@ import ( ) var supportRegisterFlags = append([]cli.Flag{ - cli.StringFlag{ - Name: "api-key", - Usage: "SUBNET API key", - }, cli.StringFlag{ Name: "name", Usage: "Specify the name to associate to this MinIO cluster in SUBNET",