diff --git a/cmd/admin-subnet-health.go b/cmd/admin-subnet-health.go index c42b019c..9e559277 100644 --- a/cmd/admin-subnet-health.go +++ b/cmd/admin-subnet-health.go @@ -61,6 +61,15 @@ var adminHealthFlags = []cli.Flag{ Name: "license", Usage: "Subnet license key", }, + cli.StringFlag{ + Name: "name", + Usage: "Cluster name to be saved in subnet on 1st upload", + }, + cli.IntFlag{ + Name: "schedule", + Usage: "Schedule of uploading to subnet in no of days", + Value: 0, + }, cli.StringFlag{ Name: "subnet-proxy", Usage: "HTTP(S) proxy URL to be used along with license flag", @@ -102,17 +111,13 @@ func checkAdminHealthSyntax(ctx *cli.Context) { } //compress and tar health report output -func tarGZ(c HealthReportInfo, filename string) error { +func tarGZ(c HealthReportInfo, filename string, showMessages bool) error { f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0666) if err != nil { return err } defer f.Close() - defer func() { - console.Infoln("Health data saved at", filename) - }() - gzWriter := gzip.NewWriter(f) defer gzWriter.Close() @@ -134,14 +139,17 @@ func tarGZ(c HealthReportInfo, filename string) error { return err } - warningMsgBoundary := "*********************************************************************************" - warning := warnText(" WARNING!!") - warningContents := infoText(` ** THIS FILE MAY CONTAIN SENSITIVE INFORMATION ABOUT YOUR ENVIRONMENT ** + if showMessages { + warningMsgBoundary := "*********************************************************************************" + warning := warnText(" WARNING!!") + warningContents := infoText(` ** THIS FILE MAY CONTAIN SENSITIVE INFORMATION ABOUT YOUR ENVIRONMENT ** ** PLEASE INSPECT CONTENTS BEFORE SHARING IT ON ANY PUBLIC FORUM **`) - warningMsgHeader := infoText(warningMsgBoundary) - warningMsgTrailer := infoText(warningMsgBoundary) - console.Printf("%s\n%s\n%s\n%s\n", warningMsgHeader, warning, warningContents, warningMsgTrailer) + warningMsgHeader := infoText(warningMsgBoundary) + warningMsgTrailer := infoText(warningMsgBoundary) + console.Printf("%s\n%s\n%s\n%s\n", warningMsgHeader, warning, warningContents, warningMsgTrailer) + console.Infoln("Health data saved at", filename) + } return nil } @@ -168,16 +176,86 @@ func mainAdminHealth(ctx *cli.Context) error { args := ctx.Args() aliasedURL := args.Get(0) + license, schedule, dev, name := fetchSubnetUploadFlags(ctx) + if len(name) == 0 { + name = aliasedURL + } + + uploadToSubnet := len(license) > 0 + uploadPeriodically := schedule != 0 + + e := validateFlags(uploadToSubnet, uploadPeriodically, dev, name) + fatalIf(probe.NewError(e), "Invalid flags.") + // Create a new MinIO Admin Client client, err := newAdminClient(aliasedURL) fatalIf(err, "Unable to initialize admin connection.") + // Main execution + execAdminHealth(ctx, client, aliasedURL, license, name, dev) + + if uploadToSubnet && uploadPeriodically { + // Periodic upload to subnet + for { + sleepDuration := time.Hour * 24 * time.Duration(schedule) + console.Infoln("Waiting for", sleepDuration, "before running health diagnostics again.") + time.Sleep(sleepDuration) + execAdminHealth(ctx, client, aliasedURL, license, name, dev) + } + } + return nil +} + +func fetchSubnetUploadFlags(ctx *cli.Context) (string, int, bool, string) { + // license flag is passed when the health data + // is to be uploadeD to Subnet + license := ctx.String("license") + + // non-zero schedule means that health diagnostics + // are to be run periodically and uploaded to subnet + schedule := ctx.Int("schedule") + + // If set (along with --license), the health data will + // be uploaded to a local (devenv) subnet server + dev := ctx.Bool("dev") + + // If set (along with --license), this will be passed to + // subnet as the name of the cluster + name := ctx.String("name") + + return license, schedule, dev, name +} + +func validateFlags(uploadToSubnet bool, uploadPeriodically bool, dev bool, name string) error { + if uploadToSubnet { + if globalJSON { + return errors.New("--json and --license should not be passed together") + } + return nil + } + + if dev { + return errors.New("--dev is applicable only when --license is also passed") + } + + if uploadPeriodically { + return errors.New("--schedule is applicable only when --license is also passed") + } + + if len(name) > 0 { + return errors.New("--name is applicable only when --license is also passed") + } + + return nil +} + +func execAdminHealth(ctx *cli.Context, client *madmin.AdminClient, aliasedURL string, license string, clusterName string, dev bool) { healthInfo, e := fetchServerHealthInfo(ctx, client) clusterHealthInfo := MapHealthInfoToV1(healthInfo, e) if globalJSON { printMsg(clusterHealthInfo) - return nil + return } if clusterHealthInfo.GetError() != "" { @@ -186,26 +264,32 @@ func mainAdminHealth(ctx *cli.Context) error { } filename := fmt.Sprintf("%s-health_%s.json.gz", filepath.Clean(aliasedURL), time.Now().Format("20060102150405")) - e = tarGZ(clusterHealthInfo, filename) + + uploadToSubnet := len(license) > 0 + e = tarGZ(clusterHealthInfo, filename, !uploadToSubnet) fatalIf(probe.NewError(e), "Unable to create health report file") - license := ctx.String("license") - if len(license) > 0 { + if uploadToSubnet { var proxyURL *url.URL if value := ctx.String("subnet-proxy"); value != "" { proxyURL, e = url.Parse(value) fatalIf(probe.NewError(e), "Unable to parse subnet-proxy flag") } - e = uploadHealthReport(aliasedURL, filename, license, ctx.Bool("dev"), proxyURL) + e = uploadHealthReport(aliasedURL, clusterName, filename, license, dev, proxyURL) + if e == nil { + // Delete the report after successful upload + deleteFile(filename) + } fatalIf(probe.NewError(e), "Unable to upload health report to Subnet portal") } - - return nil } -func uploadHealthReport(alias string, filename string, license string, dev bool, proxyURL *url.URL) error { - uploadURL := subnetUploadURL(alias, filename, license, dev) +func uploadHealthReport(alias string, clusterName string, filename string, license string, dev bool, proxyURL *url.URL) error { + if len(clusterName) == 0 { + clusterName = alias + } + uploadURL := subnetUploadURL(clusterName, filename, license, dev) req, e := subnetUploadReq(uploadURL, filename) if e != nil { return e @@ -240,13 +324,14 @@ func uploadHealthReport(alias string, filename string, license string, dev bool, return fmt.Errorf("Upload to subnet failed with status code %d: %s", resp.StatusCode, respBody) } -func subnetUploadURL(alias string, filename string, license string, dev bool) string { +func subnetUploadURL(clusterName string, filename string, license string, dev bool) string { const apiPath = "/api/auth/health_reports" baseURL := "https://subnet.min.io" if dev { baseURL = "http://localhost:9000" } - return fmt.Sprintf("%s%s?license=%s&clustername=%s&filename=%s", baseURL, apiPath, license, alias, filename) + url := fmt.Sprintf("%s%s?license=%s&clustername=%s&filename=%s", baseURL, apiPath, license, clusterName, filename) + return url } func subnetUploadReq(url string, filename string) (*http.Request, error) {