2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-20 17:45:17 -05:00
|
|
|
|
|
|
|
package madmin
|
|
|
|
|
|
|
|
import (
|
2020-03-20 18:00:44 -04:00
|
|
|
"context"
|
2016-12-20 17:45:17 -05:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-08-27 14:37:47 -04:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2021-03-27 02:24:07 -04:00
|
|
|
"time"
|
2019-08-27 14:37:47 -04:00
|
|
|
|
|
|
|
trace "github.com/minio/minio/pkg/trace"
|
2016-12-20 17:45:17 -05:00
|
|
|
)
|
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
// ServiceRestart - restarts the MinIO cluster
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) ServiceRestart(ctx context.Context) error {
|
|
|
|
return adm.serviceCallAction(ctx, ServiceActionRestart)
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
2016-12-20 17:45:17 -05:00
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
// ServiceStop - stops the MinIO cluster
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) ServiceStop(ctx context.Context) error {
|
|
|
|
return adm.serviceCallAction(ctx, ServiceActionStop)
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceAction - type to restrict service-action values
|
|
|
|
type ServiceAction string
|
2016-12-20 17:45:17 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
const (
|
2019-08-27 14:37:47 -04:00
|
|
|
// ServiceActionRestart represents restart action
|
2019-08-28 18:04:43 -04:00
|
|
|
ServiceActionRestart ServiceAction = "restart"
|
2019-08-27 14:37:47 -04:00
|
|
|
// ServiceActionStop represents stop action
|
|
|
|
ServiceActionStop = "stop"
|
2018-01-22 17:54:55 -05:00
|
|
|
)
|
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
// serviceCallAction - call service restart/update/stop API.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm *AdminClient) serviceCallAction(ctx context.Context, action ServiceAction) error {
|
2019-08-27 14:37:47 -04:00
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("action", string(action))
|
2016-12-20 17:45:17 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Request API to Restart server
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx,
|
|
|
|
http.MethodPost, requestData{
|
|
|
|
relPath: adminAPIPrefix + "/service",
|
|
|
|
queryValues: queryValues,
|
|
|
|
},
|
|
|
|
)
|
2016-12-20 17:45:17 -05:00
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2019-10-15 21:35:41 -04:00
|
|
|
return err
|
2016-12-20 17:45:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2019-10-15 21:35:41 -04:00
|
|
|
return httpRespToErrorResponse(resp)
|
2016-12-20 17:45:17 -05:00
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
|
2019-10-15 21:35:41 -04:00
|
|
|
return nil
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceTraceInfo holds http trace
|
|
|
|
type ServiceTraceInfo struct {
|
|
|
|
Trace trace.Info
|
|
|
|
Err error `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-03-27 02:24:07 -04:00
|
|
|
// ServiceTraceOpts holds tracing options
|
|
|
|
type ServiceTraceOpts struct {
|
2021-04-07 11:16:10 -04:00
|
|
|
All bool // Deprecated
|
|
|
|
|
2021-03-27 02:24:07 -04:00
|
|
|
S3 bool
|
|
|
|
Internal bool
|
|
|
|
Storage bool
|
|
|
|
OS bool
|
|
|
|
OnlyErrors bool
|
|
|
|
Threshold time.Duration
|
|
|
|
}
|
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
// ServiceTrace - listen on http trace notifications.
|
2021-03-27 02:24:07 -04:00
|
|
|
func (adm AdminClient) ServiceTrace(ctx context.Context, opts ServiceTraceOpts) <-chan ServiceTraceInfo {
|
2019-08-27 14:37:47 -04:00
|
|
|
traceInfoCh := make(chan ServiceTraceInfo)
|
|
|
|
// Only success, start a routine to start reading line by line.
|
|
|
|
go func(traceInfoCh chan<- ServiceTraceInfo) {
|
|
|
|
defer close(traceInfoCh)
|
|
|
|
for {
|
|
|
|
urlValues := make(url.Values)
|
2021-03-27 02:24:07 -04:00
|
|
|
urlValues.Set("err", strconv.FormatBool(opts.OnlyErrors))
|
|
|
|
urlValues.Set("threshold", opts.Threshold.String())
|
2021-04-07 11:16:10 -04:00
|
|
|
|
|
|
|
if opts.All {
|
|
|
|
// Deprecated flag
|
|
|
|
urlValues.Set("all", "true")
|
|
|
|
} else {
|
|
|
|
urlValues.Set("s3", strconv.FormatBool(opts.S3))
|
|
|
|
urlValues.Set("internal", strconv.FormatBool(opts.Internal))
|
|
|
|
urlValues.Set("storage", strconv.FormatBool(opts.Storage))
|
|
|
|
urlValues.Set("os", strconv.FormatBool(opts.OS))
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/trace",
|
2019-08-27 14:37:47 -04:00
|
|
|
queryValues: urlValues,
|
|
|
|
}
|
|
|
|
// Execute GET to call trace handler
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
|
2019-08-27 14:37:47 -04:00
|
|
|
if err != nil {
|
|
|
|
closeResponse(resp)
|
2020-08-28 13:04:54 -04:00
|
|
|
traceInfoCh <- ServiceTraceInfo{Err: err}
|
2019-08-27 14:37:47 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
traceInfoCh <- ServiceTraceInfo{Err: httpRespToErrorResponse(resp)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
for {
|
|
|
|
var info trace.Info
|
|
|
|
if err = dec.Decode(&info); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
2020-03-20 18:00:44 -04:00
|
|
|
case <-ctx.Done():
|
2019-08-27 14:37:47 -04:00
|
|
|
return
|
|
|
|
case traceInfoCh <- ServiceTraceInfo{Trace: info}:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(traceInfoCh)
|
|
|
|
|
|
|
|
// Returns the trace info channel, for caller to start reading from.
|
|
|
|
return traceInfoCh
|
2016-12-20 17:45:17 -05:00
|
|
|
}
|