storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/service-commands.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2016-2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package madmin
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"net/http"
    24  	"net/url"
    25  	"strconv"
    26  	"time"
    27  
    28  	trace "storj.io/minio/pkg/trace"
    29  )
    30  
    31  // ServiceRestart - restarts the MinIO cluster
    32  func (adm *AdminClient) ServiceRestart(ctx context.Context) error {
    33  	return adm.serviceCallAction(ctx, ServiceActionRestart)
    34  }
    35  
    36  // ServiceStop - stops the MinIO cluster
    37  func (adm *AdminClient) ServiceStop(ctx context.Context) error {
    38  	return adm.serviceCallAction(ctx, ServiceActionStop)
    39  }
    40  
    41  // ServiceAction - type to restrict service-action values
    42  type ServiceAction string
    43  
    44  const (
    45  	// ServiceActionRestart represents restart action
    46  	ServiceActionRestart ServiceAction = "restart"
    47  	// ServiceActionStop represents stop action
    48  	ServiceActionStop = "stop"
    49  )
    50  
    51  // serviceCallAction - call service restart/update/stop API.
    52  func (adm *AdminClient) serviceCallAction(ctx context.Context, action ServiceAction) error {
    53  	queryValues := url.Values{}
    54  	queryValues.Set("action", string(action))
    55  
    56  	// Request API to Restart server
    57  	resp, err := adm.executeMethod(ctx,
    58  		http.MethodPost, requestData{
    59  			relPath:     adminAPIPrefix + "/service",
    60  			queryValues: queryValues,
    61  		},
    62  	)
    63  	defer closeResponse(resp)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	if resp.StatusCode != http.StatusOK {
    69  		return httpRespToErrorResponse(resp)
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // ServiceTraceInfo holds http trace
    76  type ServiceTraceInfo struct {
    77  	Trace trace.Info
    78  	Err   error `json:"-"`
    79  }
    80  
    81  // ServiceTraceOpts holds tracing options
    82  type ServiceTraceOpts struct {
    83  	All bool // Deprecated
    84  
    85  	S3         bool
    86  	Internal   bool
    87  	Storage    bool
    88  	OS         bool
    89  	OnlyErrors bool
    90  	Threshold  time.Duration
    91  }
    92  
    93  // ServiceTrace - listen on http trace notifications.
    94  func (adm AdminClient) ServiceTrace(ctx context.Context, opts ServiceTraceOpts) <-chan ServiceTraceInfo {
    95  	traceInfoCh := make(chan ServiceTraceInfo)
    96  	// Only success, start a routine to start reading line by line.
    97  	go func(traceInfoCh chan<- ServiceTraceInfo) {
    98  		defer close(traceInfoCh)
    99  		for {
   100  			urlValues := make(url.Values)
   101  			urlValues.Set("err", strconv.FormatBool(opts.OnlyErrors))
   102  			urlValues.Set("threshold", opts.Threshold.String())
   103  
   104  			if opts.All {
   105  				// Deprecated flag
   106  				urlValues.Set("all", "true")
   107  			} else {
   108  				urlValues.Set("s3", strconv.FormatBool(opts.S3))
   109  				urlValues.Set("internal", strconv.FormatBool(opts.Internal))
   110  				urlValues.Set("storage", strconv.FormatBool(opts.Storage))
   111  				urlValues.Set("os", strconv.FormatBool(opts.OS))
   112  			}
   113  			reqData := requestData{
   114  				relPath:     adminAPIPrefix + "/trace",
   115  				queryValues: urlValues,
   116  			}
   117  			// Execute GET to call trace handler
   118  			resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
   119  			if err != nil {
   120  				closeResponse(resp)
   121  				traceInfoCh <- ServiceTraceInfo{Err: err}
   122  				return
   123  			}
   124  
   125  			if resp.StatusCode != http.StatusOK {
   126  				traceInfoCh <- ServiceTraceInfo{Err: httpRespToErrorResponse(resp)}
   127  				return
   128  			}
   129  
   130  			dec := json.NewDecoder(resp.Body)
   131  			for {
   132  				var info trace.Info
   133  				if err = dec.Decode(&info); err != nil {
   134  					break
   135  				}
   136  				select {
   137  				case <-ctx.Done():
   138  					return
   139  				case traceInfoCh <- ServiceTraceInfo{Trace: info}:
   140  				}
   141  			}
   142  		}
   143  	}(traceInfoCh)
   144  
   145  	// Returns the trace info channel, for caller to start reading from.
   146  	return traceInfoCh
   147  }