Add option to upload health report to Subnet (#3641)
Add an optional text flag `license` to the `subnet health` command. If this flag is passed, upload the generated health report to Subnet by passing the license key as auth.
This commit is contained in:
@@ -17,11 +17,17 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
gojson "encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -31,8 +37,10 @@ import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/klauspost/compress/gzip"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
"github.com/minio/minio/pkg/console"
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var adminHealthFlags = []cli.Flag{
|
||||
@@ -49,6 +57,15 @@ var adminHealthFlags = []cli.Flag{
|
||||
Value: 3600 * time.Second,
|
||||
EnvVar: "MC_HEALTH_DEADLINE,MC_OBD_DEADLINE",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "license",
|
||||
Usage: "Subnet license key",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "dev",
|
||||
Usage: "Development mode",
|
||||
Hidden: true,
|
||||
},
|
||||
}
|
||||
|
||||
var adminSubnetHealthCmd = cli.Command{
|
||||
@@ -73,7 +90,7 @@ EXAMPLES:
|
||||
`,
|
||||
}
|
||||
|
||||
// checkAdminInfoSyntax - validate arguments passed by a user
|
||||
// checkAdminHealthSyntax - validate arguments passed by a user
|
||||
func checkAdminHealthSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) == 0 || len(ctx.Args()) > 1 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "health", 1) // last argument is exit code
|
||||
@@ -81,8 +98,7 @@ func checkAdminHealthSyntax(ctx *cli.Context) {
|
||||
}
|
||||
|
||||
//compress and tar health report output
|
||||
func tarGZ(c HealthReportInfo, alias string) error {
|
||||
filename := fmt.Sprintf("%s-health_%s.json.gz", filepath.Clean(alias), time.Now().Format("20060102150405"))
|
||||
func tarGZ(c HealthReportInfo, filename string) error {
|
||||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -161,11 +177,86 @@ func mainAdminHealth(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
if clusterHealthInfo.GetError() != "" {
|
||||
console.Println(warnText("unable to obtain health information:"), clusterHealthInfo.GetError())
|
||||
e = errors.New(clusterHealthInfo.GetError())
|
||||
fatalIf(probe.NewError(e), "Unable to obtain health information")
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s-health_%s.json.gz", filepath.Clean(aliasedURL), time.Now().Format("20060102150405"))
|
||||
e = tarGZ(clusterHealthInfo, filename)
|
||||
fatalIf(probe.NewError(e), "Unable to create health report file")
|
||||
|
||||
license := ctx.String("license")
|
||||
if len(license) > 0 {
|
||||
e = uploadHealthReport(aliasedURL, filename, license, ctx.Bool("dev"))
|
||||
fatalIf(probe.NewError(e), "Unable to upload health report to Subnet portal")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func uploadHealthReport(alias string, filename string, license string, dev bool) error {
|
||||
uploadURL := subnetUploadURL(alias, filename, license, dev)
|
||||
req, e := subnetUploadReq(uploadURL, filename)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
resp, herr := httpClient(10 * time.Second).Do(req)
|
||||
if herr != nil {
|
||||
return herr
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var respBody []byte
|
||||
respBody, e = ioutil.ReadAll(resp.Body)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
msg := "MinIO Health data was successfully uploaded to Subnet."
|
||||
clusterURL, _ := url.PathUnescape(gjson.Get(string(respBody), "cluster_url").String())
|
||||
if len(clusterURL) > 0 {
|
||||
msg += fmt.Sprintf(" Can be viewed at: %s", clusterURL)
|
||||
}
|
||||
console.Infoln(msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
return tarGZ(clusterHealthInfo, aliasedURL)
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
func subnetUploadReq(url string, filename string) (*http.Request, error) {
|
||||
console.Println(infoText("Uploading health report to subnet"))
|
||||
|
||||
file, _ := os.Open(filename)
|
||||
defer file.Close()
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
part, err := writer.CreateFormFile("file", filepath.Base(file.Name()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = io.Copy(part, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
writer.Close()
|
||||
|
||||
r, _ := http.NewRequest("POST", url, body)
|
||||
r.Header.Add("Content-Type", writer.FormDataContentType())
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func fetchServerHealthInfo(ctx *cli.Context, client *madmin.AdminClient) (madmin.HealthInfo, error) {
|
||||
|
||||
+1
-11
@@ -30,7 +30,6 @@ import (
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/inconshreveable/go-update"
|
||||
"github.com/mattn/go-ieproxy"
|
||||
isatty "github.com/mattn/go-isatty"
|
||||
"github.com/minio/cli"
|
||||
json "github.com/minio/mc/pkg/colorjson"
|
||||
@@ -242,16 +241,7 @@ func downloadReleaseURL(releaseChecksumURL string, timeout time.Duration) (conte
|
||||
}
|
||||
req.Header.Set("User-Agent", getUserAgent())
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: &http.Transport{
|
||||
Proxy: ieproxy.GetProxyFunc(),
|
||||
// need to close connection after usage.
|
||||
DisableKeepAlives: true,
|
||||
},
|
||||
}
|
||||
|
||||
resp, e := client.Do(req)
|
||||
resp, e := httpClient(timeout).Do(req)
|
||||
if e != nil {
|
||||
return content, probe.NewError(e)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -32,6 +33,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-ieproxy"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/encrypt"
|
||||
@@ -448,3 +450,14 @@ func getClient(aliasURL string) *madmin.AdminClient {
|
||||
fatalIf(err, "Unable to initialize admin connection.")
|
||||
return client
|
||||
}
|
||||
|
||||
func httpClient(timeout time.Duration) *http.Client {
|
||||
return &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: &http.Transport{
|
||||
Proxy: ieproxy.GetProxyFunc(),
|
||||
// need to close connection after usage.
|
||||
DisableKeepAlives: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ require (
|
||||
github.com/rjeczalik/notify v0.9.2
|
||||
github.com/rs/xid v1.2.1
|
||||
github.com/shirou/gopsutil/v3 v3.21.1
|
||||
github.com/tidwall/gjson v1.6.8
|
||||
go.uber.org/zap v1.14.1 // indirect
|
||||
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
|
||||
golang.org/x/net v0.0.0-20201216054612-986b41b23924
|
||||
|
||||
@@ -509,7 +509,11 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI=
|
||||
github.com/tidwall/gjson v1.6.8 h1:CTmXMClGYPAmln7652e69B7OLXfTi5ABcPPwjIWUv7w=
|
||||
github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI=
|
||||
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
|
||||
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
|
||||
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/tidwall/sjson v1.0.4/go.mod h1:bURseu1nuBkFpIES5cz6zBtjmYeOQmEESshn7VpF15Y=
|
||||
github.com/tinylib/msgp v1.1.3/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
|
||||
Reference in New Issue
Block a user