github.com/minio/madmin-go/v3@v3.0.51/update-commands.go (about)

     1  //
     2  // Copyright (c) 2015-2024 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"net/http"
    26  	"net/url"
    27  	"strconv"
    28  )
    29  
    30  // ServerPeerUpdateStatus server update peer binary update result
    31  type ServerPeerUpdateStatus struct {
    32  	Host           string                 `json:"host"`
    33  	Err            string                 `json:"err,omitempty"`
    34  	CurrentVersion string                 `json:"currentVersion"`
    35  	UpdatedVersion string                 `json:"updatedVersion"`
    36  	WaitingDrives  map[string]DiskMetrics `json:"waitingDrives,omitempty"`
    37  }
    38  
    39  // ServerUpdateStatusV2 server update status
    40  type ServerUpdateStatusV2 struct {
    41  	DryRun  bool                     `json:"dryRun"`
    42  	Results []ServerPeerUpdateStatus `json:"results,omitempty"`
    43  }
    44  
    45  // ServerUpdateOpts specifies the URL (optionally to download the binary from)
    46  // also allows a dry-run, the new API is idempotent which means you can
    47  // run it as many times as you want and any server that is not upgraded
    48  // automatically does get upgraded eventually to the relevant version.
    49  type ServerUpdateOpts struct {
    50  	UpdateURL string
    51  	DryRun    bool
    52  }
    53  
    54  // ServerUpdateV2 - updates and restarts the MinIO cluster to latest version.
    55  // optionally takes an input URL to specify a custom update binary link
    56  func (adm *AdminClient) ServerUpdateV2(ctx context.Context, opts ServerUpdateOpts) (us ServerUpdateStatusV2, err error) {
    57  	queryValues := url.Values{}
    58  	queryValues.Set("type", "2")
    59  	queryValues.Set("updateURL", opts.UpdateURL)
    60  	queryValues.Set("dry-run", strconv.FormatBool(opts.DryRun))
    61  
    62  	// Request API to Restart server
    63  	resp, err := adm.executeMethod(ctx,
    64  		http.MethodPost, requestData{
    65  			relPath:     adminAPIPrefix + "/update",
    66  			queryValues: queryValues,
    67  		},
    68  	)
    69  	defer closeResponse(resp)
    70  	if err != nil {
    71  		return us, err
    72  	}
    73  
    74  	if resp.StatusCode != http.StatusOK {
    75  		return us, httpRespToErrorResponse(resp)
    76  	}
    77  
    78  	if err = json.NewDecoder(resp.Body).Decode(&us); err != nil {
    79  		return us, err
    80  	}
    81  
    82  	return us, nil
    83  }
    84  
    85  // ServerUpdateStatus - contains the response of service update API
    86  type ServerUpdateStatus struct {
    87  	// Deprecated: this struct is fully deprecated since Jan 2024.
    88  	CurrentVersion string `json:"currentVersion"`
    89  	UpdatedVersion string `json:"updatedVersion"`
    90  }
    91  
    92  // ServerUpdate - updates and restarts the MinIO cluster to latest version.
    93  // optionally takes an input URL to specify a custom update binary link
    94  func (adm *AdminClient) ServerUpdate(ctx context.Context, updateURL string) (us ServerUpdateStatus, err error) {
    95  	queryValues := url.Values{}
    96  	queryValues.Set("updateURL", updateURL)
    97  
    98  	// Request API to Restart server
    99  	resp, err := adm.executeMethod(ctx,
   100  		http.MethodPost, requestData{
   101  			relPath:     adminAPIPrefix + "/update",
   102  			queryValues: queryValues,
   103  		},
   104  	)
   105  	defer closeResponse(resp)
   106  	if err != nil {
   107  		return us, err
   108  	}
   109  
   110  	if resp.StatusCode != http.StatusOK {
   111  		return us, httpRespToErrorResponse(resp)
   112  	}
   113  
   114  	if err = json.NewDecoder(resp.Body).Decode(&us); err != nil {
   115  		return us, err
   116  	}
   117  
   118  	return us, nil
   119  }