-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathupdate-main.go
557 lines (479 loc) · 15.9 KB
/
update-main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright (c) 2015-2022 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/>.
package cmd
import (
"crypto"
"crypto/tls"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
_ "crypto/sha256" // needed for selfupdate hashers
"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/v3/env"
"github.com/minio/selfupdate"
)
// Check for new software updates.
var updateCmd = cli.Command{
Name: "update",
Usage: "update mc to latest release",
Action: mainUpdate,
OnUsageError: onUsageError,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "json",
Usage: "enable JSON lines formatted output",
},
},
CustomHelpTemplate: `Name:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}}{{if .VisibleFlags}} [FLAGS]{{end}}
{{if .VisibleFlags}}
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}
EXIT STATUS:
0 - you are already running the most recent version
1 - new update was applied successfully
-1 - error in getting update information
EXAMPLES:
1. Check and update mc:
{{.Prompt}} {{.HelpName}}
`,
}
const (
mcReleaseTagTimeLayout = "2006-01-02T15-04-05Z"
mcOSARCH = runtime.GOOS + "-" + runtime.GOARCH
mcReleaseURL = "https://dl.min.io/client/mc/release/" + mcOSARCH + "/"
envMinisignPubKey = "MC_UPDATE_MINISIGN_PUBKEY"
)
// For windows our files have .exe additionally.
var mcReleaseWindowsInfoURL = mcReleaseURL + "mc.exe.sha256sum"
// mcVersionToReleaseTime - parses a standard official release
// mc --version string.
//
// An official binary's version string is the release time formatted
// with RFC3339 (in UTC) - e.g. `2017-09-29T19:16:56Z`
func mcVersionToReleaseTime(version string) (releaseTime time.Time, err *probe.Error) {
var e error
releaseTime, e = time.Parse(time.RFC3339, version)
return releaseTime, probe.NewError(e)
}
// releaseTagToReleaseTime - releaseTag to releaseTime
func releaseTagToReleaseTime(releaseTag string) (releaseTime time.Time, err *probe.Error) {
fields := strings.Split(releaseTag, ".")
if len(fields) < 2 || len(fields) > 4 {
return releaseTime, probe.NewError(fmt.Errorf("%s is not a valid release tag", releaseTag))
}
if fields[0] != "RELEASE" {
return releaseTime, probe.NewError(fmt.Errorf("%s is not a valid release tag", releaseTag))
}
var e error
releaseTime, e = time.Parse(mcReleaseTagTimeLayout, fields[1])
return releaseTime, probe.NewError(e)
}
// getModTime - get the file modification time of `path`
func getModTime(path string) (t time.Time, err *probe.Error) {
var e error
path, e = filepath.EvalSymlinks(path)
if e != nil {
return t, probe.NewError(fmt.Errorf("Unable to get absolute path of %s. %w", path, e))
}
// Version is mc non-standard, we will use mc binary's
// ModTime as release time.
var fi os.FileInfo
fi, e = os.Stat(path)
if e != nil {
return t, probe.NewError(fmt.Errorf("Unable to get ModTime of %s. %w", path, e))
}
// Return the ModTime
return fi.ModTime().UTC(), nil
}
// GetCurrentReleaseTime - returns this process's release time. If it
// is official mc --version, parsed version is returned else mc
// binary's mod time is returned.
func GetCurrentReleaseTime() (releaseTime time.Time, err *probe.Error) {
if releaseTime, err = mcVersionToReleaseTime(Version); err == nil {
return releaseTime, nil
}
// Looks like version is mc non-standard, we use mc
// binary's ModTime as release time:
path, e := os.Executable()
if e != nil {
return releaseTime, probe.NewError(e)
}
return getModTime(path)
}
// IsDocker - returns if the environment mc is running in docker or
// not. The check is a simple file existence check.
//
// https://github.com/moby/moby/blob/master/daemon/initlayer/setup_unix.go#L25
//
// "/.dockerenv": "file",
func IsDocker() bool {
_, e := os.Stat("/.dockerenv")
if os.IsNotExist(e) {
return false
}
return e == nil
}
// IsDCOS returns true if mc is running in DCOS.
func IsDCOS() bool {
// http://mesos.apache.org/documentation/latest/docker-containerizer/
// Mesos docker containerizer sets this value
return os.Getenv("MESOS_CONTAINER_NAME") != ""
}
// IsKubernetes returns true if MinIO is running in kubernetes.
func IsKubernetes() bool {
// Kubernetes env used to validate if we are
// indeed running inside a kubernetes pod
// is KUBERNETES_SERVICE_HOST but in future
// we might need to enhance this.
return os.Getenv("KUBERNETES_SERVICE_HOST") != ""
}
// IsSourceBuild - returns if this binary is a non-official build from
// source code.
func IsSourceBuild() bool {
_, err := mcVersionToReleaseTime(Version)
return err != nil
}
// DO NOT CHANGE USER AGENT STYLE.
// The style should be
//
// mc (<OS>; <ARCH>[; dcos][; kubernetes][; docker][; source]) mc/<VERSION> mc/<RELEASE-TAG> mc/<COMMIT-ID>
//
// Any change here should be discussed by opening an issue at
// https://github.com/minio/mc/issues.
func getUserAgent() string {
userAgentParts := []string{}
// Helper function to concisely append a pair of strings to a
// the user-agent slice.
uaAppend := func(p, q string) {
userAgentParts = append(userAgentParts, p, q)
}
uaAppend("mc (", runtime.GOOS)
uaAppend("; ", runtime.GOARCH)
if IsDCOS() {
uaAppend("; ", "dcos")
}
if IsKubernetes() {
uaAppend("; ", "kubernetes")
}
if IsDocker() {
uaAppend("; ", "docker")
}
if IsSourceBuild() {
uaAppend("; ", "source")
}
uaAppend(") mc/", Version)
uaAppend(" mc/", ReleaseTag)
uaAppend(" mc/", CommitID)
return strings.Join(userAgentParts, "")
}
func downloadReleaseURL(releaseChecksumURL string, timeout time.Duration) (content string, err *probe.Error) {
req, e := http.NewRequest("GET", releaseChecksumURL, nil)
if e != nil {
return content, probe.NewError(e)
}
req.Header.Set("User-Agent", getUserAgent())
resp, e := httpClient(timeout).Do(req)
if e != nil {
return content, probe.NewError(e)
}
if resp == nil {
return content, probe.NewError(fmt.Errorf("No response from server to download URL %s", releaseChecksumURL))
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return content, probe.NewError(fmt.Errorf("Error downloading URL %s. Response: %v", releaseChecksumURL, resp.Status))
}
contentBytes, e := io.ReadAll(resp.Body)
if e != nil {
return content, probe.NewError(fmt.Errorf("Error reading response. %s", err))
}
return string(contentBytes), nil
}
// DownloadReleaseData - downloads release data from mc official server.
func DownloadReleaseData(customReleaseURL string, timeout time.Duration) (data string, err *probe.Error) {
releaseURL := mcReleaseInfoURL
if runtime.GOOS == "windows" {
releaseURL = mcReleaseWindowsInfoURL
}
if customReleaseURL != "" {
releaseURL = customReleaseURL
}
return func() (data string, err *probe.Error) {
data, err = downloadReleaseURL(releaseURL, timeout)
if err == nil {
return data, nil
}
return data, err.Trace(releaseURL)
}()
}
// parseReleaseData - parses release info file content fetched from
// official mc download server.
//
// The expected format is a single line with two words like:
//
// fbe246edbd382902db9a4035df7dce8cb441357d mc.RELEASE.2016-10-07T01-16-39Z
//
// The second word must be `mc.` appended to a standard release tag.
func parseReleaseData(data string) (sha256Hex string, releaseTime time.Time, releaseTag string, err *probe.Error) {
fields := strings.Fields(data)
if len(fields) != 2 {
return sha256Hex, releaseTime, "", probe.NewError(fmt.Errorf("Unknown release data `%s`", data))
}
sha256Hex = fields[0]
releaseInfo := fields[1]
fields = strings.SplitN(releaseInfo, ".", 2)
if len(fields) != 2 {
return sha256Hex, releaseTime, "", probe.NewError(fmt.Errorf("Unknown release information `%s`", releaseInfo))
}
if fields[0] != "mc" {
return sha256Hex, releaseTime, "", probe.NewError(fmt.Errorf("Unknown release `%s`", releaseInfo))
}
releaseTime, err = releaseTagToReleaseTime(fields[1])
if err != nil {
return sha256Hex, releaseTime, fields[1], err.Trace(fields...)
}
return sha256Hex, releaseTime, fields[1], nil
}
func getLatestReleaseTime(customReleaseURL string, timeout time.Duration) (sha256Hex string, releaseTime time.Time, releaseTag string, err *probe.Error) {
data, err := DownloadReleaseData(customReleaseURL, timeout)
if err != nil {
return sha256Hex, releaseTime, releaseTag, err.Trace()
}
return parseReleaseData(data)
}
func getDownloadURL(customReleaseURL, releaseTag string) (downloadURL string) {
// Check if we are docker environment, return docker update command
if IsDocker() {
// Construct release tag name.
return fmt.Sprintf("docker pull minio/mc:%s", releaseTag)
}
if customReleaseURL == "" {
return mcReleaseURL + "archive/mc." + releaseTag
}
u, e := url.Parse(customReleaseURL)
if e != nil {
return mcReleaseURL + "archive/mc." + releaseTag
}
u.Path = path.Dir(u.Path) + "/mc." + releaseTag
return u.String()
}
func getUpdateInfo(customReleaseURL string, timeout time.Duration) (updateMsg, sha256Hex string, currentReleaseTime, latestReleaseTime time.Time, releaseTag string, err *probe.Error) {
currentReleaseTime, err = GetCurrentReleaseTime()
if err != nil {
return updateMsg, sha256Hex, currentReleaseTime, latestReleaseTime, releaseTag, err.Trace()
}
sha256Hex, latestReleaseTime, releaseTag, err = getLatestReleaseTime(customReleaseURL, timeout)
if err != nil {
return updateMsg, sha256Hex, currentReleaseTime, latestReleaseTime, releaseTag, err.Trace()
}
var older time.Duration
var downloadURL string
if latestReleaseTime.After(currentReleaseTime) {
older = latestReleaseTime.Sub(currentReleaseTime)
downloadURL = getDownloadURL(customReleaseURL, releaseTag)
}
return prepareUpdateMessage(downloadURL, older), sha256Hex, currentReleaseTime, latestReleaseTime, releaseTag, nil
}
var (
// Check if we stderr, stdout are dumb terminals, we do not apply
// ansi coloring on dumb terminals.
isTerminal = func() bool {
return isatty.IsTerminal(os.Stdout.Fd()) && isatty.IsTerminal(os.Stderr.Fd())
}
colorCyanBold = func() func(a ...interface{}) string {
if isTerminal() {
color.New(color.FgCyan, color.Bold).SprintFunc()
}
return fmt.Sprint
}()
colorYellowBold = func() func(format string, a ...interface{}) string {
if isTerminal() {
return color.New(color.FgYellow, color.Bold).SprintfFunc()
}
return fmt.Sprintf
}()
colorGreenBold = func() func(format string, a ...interface{}) string {
if isTerminal() {
return color.New(color.FgGreen, color.Bold).SprintfFunc()
}
return fmt.Sprintf
}()
)
func getUpdateTransport(timeout time.Duration) http.RoundTripper {
var updateTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: timeout,
DualStack: true,
}).DialContext,
IdleConnTimeout: timeout,
TLSHandshakeTimeout: timeout,
ExpectContinueTimeout: timeout,
TLSClientConfig: &tls.Config{
RootCAs: globalRootCAs,
},
DisableCompression: true,
}
return updateTransport
}
func getUpdateReaderFromURL(u *url.URL, transport http.RoundTripper) (io.ReadCloser, error) {
clnt := &http.Client{
Transport: transport,
}
req, e := http.NewRequest(http.MethodGet, u.String(), nil)
if e != nil {
return nil, e
}
req.Header.Set("User-Agent", getUserAgent())
resp, e := clnt.Do(req)
if e != nil {
return nil, e
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
}
return newProgressReader(resp.Body, "mc", resp.ContentLength), nil
}
func doUpdate(customReleaseURL, sha256Hex string, latestReleaseTime time.Time, releaseTag string, ok bool) (updateStatusMsg string, err *probe.Error) {
fmtReleaseTime := latestReleaseTime.Format(mcReleaseTagTimeLayout)
if !ok {
updateStatusMsg = colorGreenBold("mc update to version %s canceled.",
releaseTag)
return updateStatusMsg, nil
}
sha256Sum, e := hex.DecodeString(sha256Hex)
if e != nil {
return updateStatusMsg, probe.NewError(e)
}
u, e := url.Parse(getDownloadURL(customReleaseURL, releaseTag))
if err != nil {
return updateStatusMsg, probe.NewError(e)
}
transport := getUpdateTransport(30 * time.Second)
rc, e := getUpdateReaderFromURL(u, transport)
if e != nil {
return updateStatusMsg, probe.NewError(e)
}
defer rc.Close()
opts := selfupdate.Options{
Hash: crypto.SHA256,
Checksum: sha256Sum,
}
minisignPubkey := env.Get(envMinisignPubKey, "")
if minisignPubkey != "" {
v := selfupdate.NewVerifier()
u.Path = path.Dir(u.Path) + "/mc." + releaseTag + ".minisig"
if e = v.LoadFromURL(u.String(), minisignPubkey, transport); e != nil {
return updateStatusMsg, probe.NewError(e)
}
opts.Verifier = v
}
if e := opts.CheckPermissions(); e != nil {
permErrMsg := fmt.Sprintf(" failed with: %s", e)
updateStatusMsg = colorYellowBold("mc update to version RELEASE.%s %s.",
fmtReleaseTime, permErrMsg)
return updateStatusMsg, nil
}
if e = selfupdate.Apply(rc, opts); e != nil {
if re := selfupdate.RollbackError(e); re != nil {
rollBackErr := fmt.Sprintf("Failed to rollback from bad update: %v", re)
updateStatusMsg = colorYellowBold("mc update to version RELEASE.%s %s.", fmtReleaseTime, rollBackErr)
return updateStatusMsg, probe.NewError(e)
}
var pathErr *os.PathError
if errors.As(e, &pathErr) {
pathErrMsg := fmt.Sprintf("Unable to update the binary at %s: %v", filepath.Dir(pathErr.Path), pathErr.Err)
updateStatusMsg = colorYellowBold("mc update to version RELEASE.%s %s.",
fmtReleaseTime, pathErrMsg)
return updateStatusMsg, nil
}
return colorYellowBold(fmt.Sprintf("Error in mc update to version RELEASE.%s %v.", fmtReleaseTime, e)), nil
}
return colorGreenBold("mc updated to version RELEASE.%s successfully.", fmtReleaseTime), nil
}
type updateMessage struct {
Status string `json:"status"`
Message string `json:"message"`
}
// String colorized make bucket message.
func (s updateMessage) String() string {
return s.Message
}
// JSON jsonified make bucket message.
func (s updateMessage) JSON() string {
s.Status = "success"
updateJSONBytes, e := json.MarshalIndent(s, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(updateJSONBytes)
}
func mainUpdate(ctx *cli.Context) {
if len(ctx.Args()) > 1 {
showCommandHelpAndExit(ctx, -1)
}
globalQuiet = ctx.Bool("quiet") || ctx.GlobalBool("quiet")
globalJSON = ctx.Bool("json") || ctx.GlobalBool("json")
customReleaseURL := ctx.Args().Get(0)
updateMsg, sha256Hex, _, latestReleaseTime, releaseTag, err := getUpdateInfo(customReleaseURL, 10*time.Second)
if err != nil {
errorIf(err, "Unable to update ‘mc’.")
os.Exit(-1)
}
// Nothing to update running the latest release.
color.New(color.FgGreen, color.Bold)
if updateMsg == "" {
printMsg(updateMessage{
Status: "success",
Message: colorGreenBold("You are already running the most recent version of ‘mc’."),
})
os.Exit(0)
}
printMsg(updateMessage{
Status: "success",
Message: updateMsg,
})
// Avoid updating mc development, source builds.
if updateMsg != "" {
var updateStatusMsg string
var err *probe.Error
updateStatusMsg, err = doUpdate(customReleaseURL, sha256Hex, latestReleaseTime, releaseTag, true)
if err != nil {
errorIf(err, "Unable to update ‘mc’.")
os.Exit(-1)
}
printMsg(updateMessage{Status: "success", Message: updateStatusMsg})
os.Exit(1)
}
}