github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-update.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "bufio" 22 "fmt" 23 "os" 24 "strings" 25 26 "github.com/fatih/color" 27 "github.com/jedib0t/go-pretty/v6/table" 28 "github.com/jedib0t/go-pretty/v6/text" 29 "github.com/minio/cli" 30 json "github.com/minio/colorjson" 31 "github.com/minio/madmin-go/v3" 32 "github.com/minio/mc/pkg/probe" 33 "github.com/minio/pkg/v2/console" 34 ) 35 36 var adminUpdateFlags = []cli.Flag{ 37 cli.BoolFlag{ 38 Name: "yes, y", 39 Usage: "Confirms the server update", 40 }, 41 } 42 43 var adminServerUpdateCmd = cli.Command{ 44 Name: "update", 45 Usage: "update all MinIO servers", 46 Action: mainAdminServerUpdate, 47 OnUsageError: onUsageError, 48 Before: setGlobalsFromContext, 49 Flags: append(adminUpdateFlags, globalFlags...), 50 CustomHelpTemplate: `NAME: 51 {{.HelpName}} - {{.Usage}} 52 53 USAGE: 54 {{.HelpName}} TARGET 55 56 FLAGS: 57 {{range .VisibleFlags}}{{.}} 58 {{end}} 59 EXAMPLES: 60 1. Update MinIO server represented by its alias 'play'. 61 {{.Prompt}} {{.HelpName}} play/ 62 63 2. Update all MinIO servers in a distributed setup, represented by its alias 'mydist'. 64 {{.Prompt}} {{.HelpName}} mydist/ 65 `, 66 } 67 68 // serverUpdateMessage is container for ServerUpdate success and failure messages. 69 type serverUpdateMessage struct { 70 Status string `json:"status"` 71 ServerURL string `json:"serverURL"` 72 ServerUpdateStatus madmin.ServerUpdateStatusV2 `json:"serverUpdateStatus"` 73 } 74 75 // String colorized serverUpdate message. 76 func (s serverUpdateMessage) String() string { 77 var rows []table.Row 78 for _, peerRes := range s.ServerUpdateStatus.Results { 79 errStr := fmt.Sprintf("upgraded server from %s to %s: %s", peerRes.CurrentVersion, peerRes.UpdatedVersion, tickCell) 80 if peerRes.Err != "" { 81 errStr = peerRes.Err 82 } else if len(peerRes.WaitingDrives) > 0 { 83 errStr = fmt.Sprintf("%d drives are hung, process was upgraded. However OS reboot is recommended.", len(peerRes.WaitingDrives)) 84 } 85 rows = append(rows, table.Row{peerRes.Host, errStr}) 86 } 87 88 t := table.NewWriter() 89 var s1 strings.Builder 90 s1.WriteString("Server update request sent successfully `" + s.ServerURL + "`\n") 91 92 t.SetOutputMirror(&s1) 93 t.SetColumnConfigs([]table.ColumnConfig{{Align: text.AlignCenter}}) 94 95 t.AppendHeader(table.Row{"Host", "Status"}) 96 t.AppendRows(rows) 97 t.SetStyle(table.StyleLight) 98 t.Render() 99 100 return console.Colorize("ServiceRestart", s1.String()) 101 } 102 103 // JSON jsonified server update message. 104 func (s serverUpdateMessage) JSON() string { 105 serverUpdateJSONBytes, e := json.MarshalIndent(s, "", " ") 106 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 107 108 return string(serverUpdateJSONBytes) 109 } 110 111 // checkAdminServerUpdateSyntax - validate all the passed arguments 112 func checkAdminServerUpdateSyntax(ctx *cli.Context) { 113 if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 { 114 showCommandHelpAndExit(ctx, 1) // last argument is exit code 115 } 116 } 117 118 func mainAdminServerUpdate(ctx *cli.Context) error { 119 // Validate serivce update syntax. 120 checkAdminServerUpdateSyntax(ctx) 121 122 // Set color. 123 console.SetColor("ServerUpdate", color.New(color.FgGreen, color.Bold)) 124 125 // Get the alias parameter from cli 126 args := ctx.Args() 127 aliasedURL := args.Get(0) 128 129 client, err := newAdminClient(aliasedURL) 130 fatalIf(err, "Unable to initialize admin connection.") 131 132 updateURL := args.Get(1) 133 134 autoConfirm := ctx.Bool("yes") 135 136 if isTerminal() && !autoConfirm { 137 fmt.Printf("You are about to upgrade *MinIO Server*, please confirm [y/N]: ") 138 answer, e := bufio.NewReader(os.Stdin).ReadString('\n') 139 fatalIf(probe.NewError(e), "Unable to parse user input.") 140 answer = strings.TrimSpace(answer) 141 if answer = strings.ToLower(answer); answer != "y" && answer != "yes" { 142 fmt.Println("Upgrade aborted!") 143 return nil 144 } 145 } 146 147 // Update the specified MinIO server, optionally also 148 // with the provided update URL. 149 us, e := client.ServerUpdateV2(globalContext, madmin.ServerUpdateOpts{ 150 DryRun: ctx.Bool("dry-run"), 151 UpdateURL: updateURL, 152 }) 153 fatalIf(probe.NewError(e), "Unable to update the server.") 154 155 // Success.. 156 printMsg(serverUpdateMessage{ 157 Status: "success", 158 ServerURL: aliasedURL, 159 ServerUpdateStatus: us, 160 }) 161 return nil 162 }