2016-12-20 17:45:17 -05:00
|
|
|
/*
|
2019-08-28 18:04:43 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016-2019 MinIO, Inc.
|
2016-12-20 17:45:17 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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("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))
|
|
|
|
urlValues.Set("threshold", opts.Threshold.String())
|
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
|
|
|
}
|