minio/update-main.go

181 lines
4.8 KiB
Go
Raw Normal View History

2015-10-17 18:03:46 -04:00
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* 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 main
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"runtime"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/minio-xl/pkg/probe"
)
2015-11-26 00:06:29 -05:00
// command specific flags.
var (
updateFlags = []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "Help for update.",
},
cli.BoolFlag{
Name: "experimental, E",
Usage: "Check experimental update.",
},
}
)
2015-10-17 18:03:46 -04:00
// Check for new software updates.
var updateCmd = cli.Command{
Name: "update",
2015-11-26 00:06:29 -05:00
Usage: "Check for a new software update.",
2015-10-17 18:03:46 -04:00
Action: mainUpdate,
2015-11-26 00:06:29 -05:00
Flags: updateFlags,
2015-10-17 18:03:46 -04:00
CustomHelpTemplate: `Name:
minio {{.Name}} - {{.Usage}}
USAGE:
2015-11-26 00:06:29 -05:00
minio {{.Name}} [FLAGS]
2015-10-17 18:03:46 -04:00
2015-11-26 00:06:29 -05:00
FLAGS:
{{range .Flags}}{{.}}
{{end}}
2015-10-17 18:03:46 -04:00
EXAMPLES:
2015-11-26 00:06:29 -05:00
1. Check for any new official release.
$ minio {{.Name}}
2015-10-17 18:03:46 -04:00
2015-11-26 00:06:29 -05:00
2. Check for any new experimental release.
$ minio {{.Name}} --experimental
2015-10-17 18:03:46 -04:00
`,
}
2015-11-26 00:06:29 -05:00
// update URL endpoints.
const (
minioUpdateStableURL = "https://dl.minio.io:9000/updates/updates.json"
minioUpdateExperimentalURL = "https://dl.minio.io:9000/updates/experimental.json"
)
// minioUpdates container to hold updates json.
type minioUpdates struct {
2015-10-17 18:03:46 -04:00
BuildDate string
Platforms map[string]string
}
2015-11-26 00:06:29 -05:00
// updateMessage container to hold update messages.
2015-10-17 18:03:46 -04:00
type updateMessage struct {
2015-11-26 00:06:29 -05:00
Status string `json:"status"`
2015-10-17 18:03:46 -04:00
Update bool `json:"update"`
Download string `json:"downloadURL"`
Version string `json:"version"`
}
2015-11-26 00:06:29 -05:00
// String colorized update message.
2015-10-17 18:03:46 -04:00
func (u updateMessage) String() string {
2015-11-26 00:06:29 -05:00
if !u.Update {
updateMessage := color.New(color.FgGreen, color.Bold).SprintfFunc()
return updateMessage("You are already running the most recent version of minio.")
}
var msg string
if runtime.GOOS == "windows" {
msg = "Download " + u.Download
} else {
msg = "Download " + u.Download
2015-10-17 18:03:46 -04:00
}
2015-11-26 00:06:29 -05:00
msg, err := colorizeUpdateMessage(msg)
fatalIf(err.Trace(msg), "Unable to colorize experimental update notification string "+msg+".", nil)
return msg
2015-10-17 18:03:46 -04:00
}
2015-11-26 00:06:29 -05:00
// JSON jsonified update message.
2015-10-17 18:03:46 -04:00
func (u updateMessage) JSON() string {
2015-11-26 00:06:29 -05:00
u.Status = "success"
2015-10-17 18:03:46 -04:00
updateMessageJSONBytes, err := json.Marshal(u)
fatalIf(probe.NewError(err), "Unable to marshal into JSON.", nil)
return string(updateMessageJSONBytes)
}
2015-11-26 00:06:29 -05:00
// verify updates for releases.
func getReleaseUpdate(updateURL string) {
data, e := http.Get(updateURL)
fatalIf(probe.NewError(e), "Unable to read from update URL "+updateURL+".", nil)
2015-11-26 00:06:29 -05:00
if minioVersion == "UNOFFICIAL.GOGET" {
fatalIf(probe.NewError(errors.New("")),
"Update mechanism is not supported for go get based binary builds. Please download official releases from https://minio.io/#minio", nil)
2015-10-17 18:03:46 -04:00
}
current, e := time.Parse(time.RFC3339, minioVersion)
2015-11-26 00:06:29 -05:00
fatalIf(probe.NewError(e), "Unable to parse version string as time.", nil)
2015-10-17 18:03:46 -04:00
if current.IsZero() {
2015-11-26 00:06:29 -05:00
fatalIf(probe.NewError(errors.New("")),
"Updates not supported for custom builds. Version field is empty. Please download official releases from https://minio.io/#minio", nil)
2015-10-17 18:03:46 -04:00
}
2015-11-26 00:06:29 -05:00
var updates minioUpdates
decoder := json.NewDecoder(data.Body)
e = decoder.Decode(&updates)
2015-10-17 18:03:46 -04:00
fatalIf(probe.NewError(e), "Unable to decode update notification.", nil)
2015-11-26 00:06:29 -05:00
latest, e := time.Parse(time.RFC3339, updates.BuildDate)
if e != nil {
latest, e = time.Parse(http.TimeFormat, updates.BuildDate)
fatalIf(probe.NewError(e), "Unable to parse BuildDate.", nil)
}
2015-10-17 18:03:46 -04:00
if latest.IsZero() {
2015-11-26 00:06:29 -05:00
fatalIf(probe.NewError(errors.New("")),
"Unable to validate any update available at this time. Please open an issue at https://github.com/minio/minio/issues", nil)
2015-10-17 18:03:46 -04:00
}
2015-11-26 00:06:29 -05:00
updateURLParse, err := url.Parse(updateURL)
2015-10-17 18:03:46 -04:00
if err != nil {
2015-11-26 00:06:29 -05:00
fatalIf(probe.NewError(err), "Unable to parse URL: "+updateURL, nil)
2015-10-17 18:03:46 -04:00
}
2015-11-26 00:06:29 -05:00
downloadURL := updateURLParse.Scheme + "://" +
updateURLParse.Host + "/" + updates.Platforms[runtime.GOOS+"-"+runtime.GOARCH]
updateMsg := updateMessage{
2015-10-17 18:03:46 -04:00
Download: downloadURL,
Version: minioVersion,
}
if latest.After(current) {
2015-11-26 00:06:29 -05:00
updateMsg.Update = true
2015-10-17 18:03:46 -04:00
}
if globalJSONFlag {
2015-11-26 00:06:29 -05:00
Println(updateMsg.JSON())
2015-10-17 18:03:46 -04:00
} else {
2015-11-26 00:06:29 -05:00
Println(updateMsg)
2015-10-17 18:03:46 -04:00
}
}
2015-11-26 00:06:29 -05:00
// main entry point for update command.
2015-10-17 18:03:46 -04:00
func mainUpdate(ctx *cli.Context) {
2015-11-26 00:06:29 -05:00
// Check for update.
if ctx.Bool("experimental") {
getReleaseUpdate(minioUpdateExperimentalURL)
} else {
getReleaseUpdate(minioUpdateStableURL)
2015-10-17 18:03:46 -04:00
}
}