cleanup 'admin health' command (#3862)

- reach subnet, upon failure provide `--airgap` command line
  to allow them to upload report manually.

- use a proper httpClient when reaching out to subnet
  with a 10sec timeout, and also support custom proxies
  with self signed certs.

- also fix some error messages and cleanup help with correct
  wording and grammar.
This commit is contained in:
Harshavardhana
2021-11-12 09:34:29 -08:00
committed by GitHub
parent b882f3e484
commit b2ff347c69
4 changed files with 110 additions and 86 deletions
+55 -45
View File
@@ -47,15 +47,16 @@ import (
var adminHealthFlags = append([]cli.Flag{
HealthDataTypeFlag{
Name: "test",
Usage: "choose health tests to run [" + fullOptions.String() + "]",
Usage: "choose specific health report(s) to run [" + fullOptions.String() + "]",
Value: nil,
EnvVar: "MC_HEALTH_TEST,MC_OBD_TEST",
Hidden: true,
},
cli.DurationFlag{
Name: "deadline",
Usage: "maximum duration that health tests should be allowed to run",
Usage: "maximum duration MinIO health report should be allowed to run",
Value: 1 * time.Hour,
Hidden: true,
EnvVar: "MC_HEALTH_DEADLINE,MC_OBD_DEADLINE",
},
cli.StringFlag{
@@ -64,19 +65,19 @@ var adminHealthFlags = append([]cli.Flag{
},
cli.IntFlag{
Name: "schedule",
Usage: "Schedule automatic generation and upload of health reports to SUBNET based on the specified number of days (e.g. --schedule 2 is every 2 days)",
Usage: "schedule automatic upload of MinIO health report(s) to SUBNET based on number of days (e.g. --schedule 2 is every 2 days)",
Value: 0,
},
cli.BoolFlag{
Name: "full",
Usage: "Include long running tests (takes longer to generate the report)",
Usage: "include long running health report(s) (takes longer to generate the report)",
Hidden: false,
},
}, subnetCommonFlags...)
var adminSubnetHealthCmd = cli.Command{
Name: "health",
Usage: "run health check for SUBNET",
Usage: "generate MinIO health report for SUBNET",
OnUsageError: onUsageError,
Action: mainSubnetHealth,
Before: setGlobalsFromContext,
@@ -91,13 +92,16 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Upload health diagnostics for the MinIO cluster with alias 'play' (https://play.min.io by default) to SUBNET
1. Upload MinIO health report for 'play' (https://play.min.io by default) to SUBNET
{{.Prompt}} {{.HelpName}} play
2. Upload health diagnostics for the MinIO cluster with alias 'play' (https://play.min.io by default) to SUBNET using the proxy https://192.168.1.3:3128
2. Upload MinIO health report for alias 'play' (https://play.min.io by default) to SUBNET proxying via https://192.168.1.3:3128
{{.Prompt}} {{.HelpName}} play --subnet-proxy https://192.168.1.3:3128
3. Periodically upload health diagnostics for the MinIO cluster with alias 'play' (https://play.min.io by default) to SUBNET every 2 days
3. Schedule periodic upload of MinIO health report for alias 'play' (https://play.min.io by default) to SUBNET every 2 days
{{.Prompt}} {{.HelpName}} play --schedule 2
4. Generate health diagnostics report for the MinIO cluster with alias 'play' (https://play.min.io by default) to and output them to the current working directory
4. Generate MinIO health report for alias 'play' (https://play.min.io by default) save and upload to SUBNET manually
{{.Prompt}} {{.HelpName}} play --airgap
`,
}
@@ -109,7 +113,7 @@ func checkAdminHealthSyntax(ctx *cli.Context) {
}
}
//compress and tar health report output
//compress and tar MinIO health output
func tarGZ(healthInfo interface{}, version string, filename string, showMessages bool) error {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
@@ -137,13 +141,13 @@ func tarGZ(healthInfo interface{}, version string, filename string, showMessages
if showMessages {
warningMsgBoundary := "*********************************************************************************"
warning := warnText(" WARNING!!")
warningContents := infoText(` ** THIS FILE MAY CONTAIN SENSITIVE INFORMATION ABOUT YOUR ENVIRONMENT **
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)
console.Infoln("Health data saved at", filename)
console.Infoln("MinIO health report saved at", filename)
}
return nil
@@ -173,15 +177,17 @@ func mainSubnetHealth(ctx *cli.Context) error {
license, schedule, name, offline := fetchSubnetUploadFlags(ctx)
// license should be provided for us to reach subnet
// if `--offline` is provided do not need to reach out.
uploadToSubnet := !offline
if uploadToSubnet && !subnetReachable() {
console.Fatalln(subnetNotReachableMsg())
if uploadToSubnet {
fatalIf(checkURLReachable(subnetBaseURL()).Trace(aliasedURL), "Unable to reach %s to upload MinIO health report, please use --airgap to upload manually", subnetBaseURL())
}
uploadPeriodically := schedule != 0
e := validateFlags(uploadToSubnet, uploadPeriodically, name)
fatalIf(probe.NewError(e), "Invalid flags.")
fatalIf(probe.NewError(e), "unable to parse input values")
// Create a new MinIO Admin Client
client := getClient(aliasedURL)
@@ -189,6 +195,7 @@ func mainSubnetHealth(ctx *cli.Context) error {
if len(name) == 0 {
name = alias
}
// Main execution
execAdminHealth(ctx, client, alias, license, name, uploadToSubnet)
@@ -198,6 +205,7 @@ func mainSubnetHealth(ctx *cli.Context) error {
sleepDuration := time.Hour * 24 * time.Duration(schedule)
console.Infoln("Waiting for", sleepDuration, "before running health diagnostics again.")
time.Sleep(sleepDuration)
execAdminHealth(ctx, client, alias, license, name, uploadToSubnet)
}
}
@@ -205,8 +213,7 @@ func mainSubnetHealth(ctx *cli.Context) error {
}
func fetchSubnetUploadFlags(ctx *cli.Context) (string, int, string, bool) {
// license flag is passed when the health data
// is to be uploadeD to SUBNET
// license info to upload to subnet.
license := ctx.String("license")
// non-zero schedule means that health diagnostics
@@ -217,7 +224,7 @@ func fetchSubnetUploadFlags(ctx *cli.Context) (string, int, string, bool) {
// subnet as the name of the cluster
name := ctx.String("name")
// If set, the health report file will not be uploaded
// If set, the MinIO health file will not be uploaded
// to subnet and will only be saved locally.
offline := ctx.Bool("airgap") || ctx.Bool("offline")
@@ -272,16 +279,11 @@ func execAdminHealth(ctx *cli.Context, client *madmin.AdminClient, alias string,
}
e = tarGZ(healthInfo, version, filename, !uploadToSubnet)
fatalIf(probe.NewError(e), "Unable to create health report file")
fatalIf(probe.NewError(e), "Unable to save MinIO health report")
if uploadToSubnet {
e = uploadHealthReport(alias, filename, reqURL, headers)
if e == nil {
// Delete the report after successful upload
deleteFile(filename)
}
fatalIf(probe.NewError(e), "Unable to upload health report to SUBNET portal")
fatalIf(probe.NewError(e), "Unable to upload MinIO health report to SUBNET portal")
}
}
@@ -290,14 +292,14 @@ func prepareHealthUploadURL(alias string, clusterName string, filename string, l
clusterName = alias
}
uploadURL := subnetHealthUploadURL()
if len(license) == 0 {
license = getSubnetLicenseFromConfig(alias)
}
uploadURL := subnetHealthUploadURL()
reqURL, headers, e := subnetURLWithAuth(uploadURL, license)
fatalIf(probe.NewError(e), "Unable to get SUBNET auth")
fatalIf(probe.NewError(e).Trace(uploadURL), "Unable to fetch SUBNET authentication")
reqURL = fmt.Sprintf("%s&clustername=%s&filename=%s", reqURL, clusterName, filename)
return reqURL, headers
@@ -309,43 +311,51 @@ func uploadHealthReport(alias string, filename string, reqURL string, headers ma
return e
}
resp, herr := subnetExecReq(req, headers)
if herr != nil {
return herr
resp, e := subnetReqDo(req, headers)
if e != nil {
return e
}
// extract license from response and set it in minio config
subnetLic := gjson.Parse(resp).Get("license").String()
subnetLic := gjson.Get(resp, "license").String()
if len(subnetLic) > 0 {
setSubnetLicenseConfig(alias, subnetLic)
}
msg := "MinIO Health data was successfully uploaded to SUBNET."
// Delete the report after successful upload
deleteFile(filename)
msg := "MinIO health report was successfully uploaded to SUBNET."
clusterURL, _ := url.PathUnescape(gjson.Get(resp, "cluster_url").String())
if len(clusterURL) > 0 {
msg += fmt.Sprintf(" Can be viewed at: %s", clusterURL)
msg += fmt.Sprintf(" Please click here to view our analysis: %s", clusterURL)
}
console.Infoln(msg)
return nil
}
func subnetUploadReq(url string, filename string) (*http.Request, error) {
file, _ := os.Open(filename)
file, e := os.Open(filename)
if e != nil {
return nil, e
}
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
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, e := writer.CreateFormFile("file", filepath.Base(file.Name()))
if e != nil {
return nil, e
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
defer writer.Close()
if _, e = io.Copy(part, file); e != nil {
return nil, e
}
writer.Close()
r, _ := http.NewRequest("POST", url, body)
r, e := http.NewRequest(http.MethodPost, url, &body)
if e != nil {
return nil, e
}
r.Header.Add("Content-Type", writer.FormDataContentType())
return r, nil
+16 -13
View File
@@ -46,11 +46,13 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Register the MinIO cluster with alias 'play' to SUBNET, using the alias as the cluster name.
1. Register MinIO cluster at alias 'play' on SUBNET, using alias as the cluster name.
{{.Prompt}} {{.HelpName}} play
2. Register the MinIO cluster with alias 'play' to SUBNET using the name "play-cluster".
2. Register MinIO cluster at alias 'play' on SUBNET, using the name "play-cluster".
{{.Prompt}} {{.HelpName}} play --name play-cluster
3. Register the MinIO cluster with alias 'play' to SUBNET, using the proxy https://192.168.1.3:3128
3. Register MinIO cluster at alias 'play' on SUBNET, using the proxy https://192.168.1.3:3128
{{.Prompt}} {{.HelpName}} play --subnet-proxy https://192.168.1.3:3128
`,
}
@@ -105,15 +107,15 @@ type SubnetMFAReq struct {
func mainAdminRegister(ctx *cli.Context) error {
checkAdminRegisterSyntax(ctx)
offlineMode := ctx.Bool("airgap") || ctx.Bool("offline")
if !offlineMode && !subnetReachable() {
console.Fatalln(subnetNotReachableMsg())
}
// Get the alias parameter from cli
aliasedURL := ctx.Args().Get(0)
alias, _ := url2Alias(aliasedURL)
offline := ctx.Bool("airgap") || ctx.Bool("offline")
if !offline {
fatalIf(checkURLReachable(subnetBaseURL()).Trace(aliasedURL), "Unable to reach %s register", subnetBaseURL())
}
// Create a new MinIO Admin Client
client := getClient(aliasedURL)
@@ -124,19 +126,20 @@ func mainAdminRegister(ctx *cli.Context) error {
clusterName := ctx.String("name")
if len(clusterName) == 0 {
clusterName = alias
} else {
if offline {
fatalIf(errInvalidArgument(), "'--name' is not allowed in airgapped mode")
}
}
regInfo := getClusterRegInfo(admInfo, clusterName)
if offlineMode {
if offline {
registerOffline(regInfo, alias)
} else {
registerOnline(regInfo, alias, clusterName)
}
msg := fmt.Sprintln("Cluster", alias, "successfully registered on SUBNET.")
console.Infoln(msg)
console.Infoln(fmt.Sprintln("Cluster", clusterName, "successfully registered on SUBNET."))
return nil
}
+32 -26
View File
@@ -100,13 +100,21 @@ func subnetMFAURL() string {
return subnetBaseURL() + "/api/auth/mfa-login"
}
func urlReachable(url string) bool {
r, e := http.Head(url)
return e == nil && r.StatusCode == http.StatusOK
}
func subnetReachable() bool {
return urlReachable(subnetBaseURL())
func checkURLReachable(url string) *probe.Error {
clnt := httpClient(10 * time.Second)
req, e := http.NewRequest(http.MethodHead, url, nil)
if e != nil {
return probe.NewError(e).Trace(url)
}
resp, e := clnt.Do(req)
if e != nil {
return probe.NewError(e).Trace(url)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return probe.NewError(errors.New(resp.Status)).Trace(url)
}
return nil
}
func subnetURLWithAuth(reqURL string, license string) (string, map[string]string, error) {
@@ -137,7 +145,7 @@ func subnetAuthHeaders(authToken string) map[string]string {
return map[string]string{"Authorization": "Bearer " + authToken}
}
func execReq(req *http.Request) (*http.Response, error) {
func httpDo(req *http.Request) (*http.Response, error) {
client := httpClient(10 * time.Second)
if globalSubnetProxyURL != nil {
client.Transport.(*http.Transport).Proxy = http.ProxyURL(globalSubnetProxyURL)
@@ -145,7 +153,7 @@ func execReq(req *http.Request) (*http.Response, error) {
return client.Do(req)
}
func subnetExecReq(r *http.Request, headers map[string]string) (string, error) {
func subnetReqDo(r *http.Request, headers map[string]string) (string, error) {
for k, v := range headers {
r.Header.Add(k, v)
}
@@ -155,7 +163,7 @@ func subnetExecReq(r *http.Request, headers map[string]string) (string, error) {
r.Header.Add("Content-Type", "application/json")
}
resp, e := execReq(r)
resp, e := httpDo(r)
if e != nil {
return "", e
}
@@ -174,17 +182,23 @@ func subnetExecReq(r *http.Request, headers map[string]string) (string, error) {
}
func subnetGetReq(reqURL string, headers map[string]string) (string, error) {
r, _ := http.NewRequest("GET", reqURL, nil)
return subnetExecReq(r, headers)
}
func subnetPostReq(reqURL string, payload interface{}, headers map[string]string) (string, error) {
body, _ := json.Marshal(payload)
r, e := http.NewRequest("POST", reqURL, bytes.NewBuffer(body))
r, e := http.NewRequest(http.MethodGet, reqURL, nil)
if e != nil {
return "", e
}
return subnetExecReq(r, headers)
return subnetReqDo(r, headers)
}
func subnetPostReq(reqURL string, payload interface{}, headers map[string]string) (string, error) {
body, e := json.Marshal(payload)
if e != nil {
return "", e
}
r, e := http.NewRequest(http.MethodPost, reqURL, bytes.NewReader(body))
if e != nil {
return "", e
}
return subnetReqDo(r, headers)
}
func getSubnetLicenseFromConfig(alias string) string {
@@ -410,11 +424,3 @@ JkO2PfyyAYEO/5dBlPh1Undu9WQl6J7B
_, e = lv.Verify(lic)
return e
}
func subnetNotReachableMsg() string {
errMsg := "https://subnet.min.io is not reachable"
if globalSubnetProxyURL != nil {
errMsg += " using proxy " + globalSubnetProxyURL.String()
}
return errMsg
}
+7 -2
View File
@@ -457,8 +457,13 @@ func httpClient(timeout time.Duration) *http.Client {
Timeout: timeout,
Transport: &http.Transport{
Proxy: ieproxy.GetProxyFunc(),
// need to close connection after usage.
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{
RootCAs: globalRootCAs,
// Can't use SSLv3 because of POODLE and BEAST
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
// Can't use TLSv1.1 because of RC4 cipher usage
MinVersion: tls.VersionTLS12,
},
},
}
}