You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
6.5 KiB
241 lines
6.5 KiB
9 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
9 years ago
|
"io/ioutil"
|
||
9 years ago
|
"net/http"
|
||
|
"runtime"
|
||
9 years ago
|
"strings"
|
||
9 years ago
|
"time"
|
||
|
|
||
|
"github.com/fatih/color"
|
||
|
"github.com/minio/cli"
|
||
9 years ago
|
"github.com/minio/mc/pkg/console"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
// 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.",
|
||
|
},
|
||
|
}
|
||
|
)
|
||
|
|
||
9 years ago
|
// Check for new software updates.
|
||
|
var updateCmd = cli.Command{
|
||
|
Name: "update",
|
||
9 years ago
|
Usage: "Check for a new software update.",
|
||
9 years ago
|
Action: mainUpdate,
|
||
9 years ago
|
Flags: updateFlags,
|
||
9 years ago
|
CustomHelpTemplate: `Name:
|
||
|
minio {{.Name}} - {{.Usage}}
|
||
|
|
||
|
USAGE:
|
||
9 years ago
|
minio {{.Name}} [FLAGS]
|
||
9 years ago
|
|
||
9 years ago
|
FLAGS:
|
||
|
{{range .Flags}}{{.}}
|
||
|
{{end}}
|
||
9 years ago
|
EXAMPLES:
|
||
9 years ago
|
1. Check for any new official release.
|
||
|
$ minio {{.Name}}
|
||
9 years ago
|
|
||
9 years ago
|
2. Check for any new experimental release.
|
||
|
$ minio {{.Name}} --experimental
|
||
9 years ago
|
`,
|
||
|
}
|
||
|
|
||
9 years ago
|
// update URL endpoints.
|
||
|
const (
|
||
9 years ago
|
minioUpdateStableURL = "https://dl.minio.io/server/minio/release"
|
||
|
minioUpdateExperimentalURL = "https://dl.minio.io/server/minio/experimental"
|
||
9 years ago
|
)
|
||
|
|
||
|
// updateMessage container to hold update messages.
|
||
9 years ago
|
type updateMessage struct {
|
||
9 years ago
|
Status string `json:"status"`
|
||
9 years ago
|
Update bool `json:"update"`
|
||
|
Download string `json:"downloadURL"`
|
||
|
Version string `json:"version"`
|
||
|
}
|
||
|
|
||
9 years ago
|
// String colorized update message.
|
||
9 years ago
|
func (u updateMessage) String() string {
|
||
9 years ago
|
if !u.Update {
|
||
|
updateMessage := color.New(color.FgGreen, color.Bold).SprintfFunc()
|
||
|
return updateMessage("You are already running the most recent version of ‘minio’.")
|
||
|
}
|
||
9 years ago
|
msg := colorizeUpdateMessage(u.Download)
|
||
9 years ago
|
return msg
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// JSON jsonified update message.
|
||
9 years ago
|
func (u updateMessage) JSON() string {
|
||
9 years ago
|
u.Status = "success"
|
||
9 years ago
|
updateMessageJSONBytes, err := json.Marshal(u)
|
||
9 years ago
|
fatalIf((err), "Unable to marshal into JSON.")
|
||
9 years ago
|
|
||
|
return string(updateMessageJSONBytes)
|
||
|
}
|
||
|
|
||
9 years ago
|
func parseReleaseData(data string) (time.Time, error) {
|
||
9 years ago
|
releaseStr := strings.Fields(data)
|
||
|
if len(releaseStr) < 2 {
|
||
9 years ago
|
return time.Time{}, errors.New("Update data malformed")
|
||
9 years ago
|
}
|
||
|
releaseDate := releaseStr[1]
|
||
|
releaseDateSplits := strings.SplitN(releaseDate, ".", 3)
|
||
|
if len(releaseDateSplits) < 3 {
|
||
9 years ago
|
return time.Time{}, (errors.New("Update data malformed"))
|
||
9 years ago
|
}
|
||
|
if releaseDateSplits[0] != "minio" {
|
||
9 years ago
|
return time.Time{}, (errors.New("Update data malformed, missing minio tag"))
|
||
9 years ago
|
}
|
||
9 years ago
|
// "OFFICIAL" tag is still kept for backward compatibility, we should remove this for the next release.
|
||
|
if releaseDateSplits[1] != "RELEASE" && releaseDateSplits[1] != "OFFICIAL" {
|
||
9 years ago
|
return time.Time{}, (errors.New("Update data malformed, missing RELEASE tag"))
|
||
9 years ago
|
}
|
||
|
dateSplits := strings.SplitN(releaseDateSplits[2], "T", 2)
|
||
|
if len(dateSplits) < 2 {
|
||
9 years ago
|
return time.Time{}, (errors.New("Update data malformed, not in modified RFC3359 form"))
|
||
9 years ago
|
}
|
||
|
dateSplits[1] = strings.Replace(dateSplits[1], "-", ":", -1)
|
||
|
date := strings.Join(dateSplits, "T")
|
||
|
|
||
9 years ago
|
parsedDate, err := time.Parse(time.RFC3339, date)
|
||
|
if err != nil {
|
||
|
return time.Time{}, err
|
||
9 years ago
|
}
|
||
|
return parsedDate, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// verify updates for releases.
|
||
9 years ago
|
func getReleaseUpdate(updateURL string, noError bool) updateMessage {
|
||
|
// Construct a new update url.
|
||
9 years ago
|
newUpdateURLPrefix := updateURL + "/" + runtime.GOOS + "-" + runtime.GOARCH
|
||
|
newUpdateURL := newUpdateURLPrefix + "/minio.shasum"
|
||
9 years ago
|
|
||
|
// Get the downloadURL.
|
||
|
var downloadURL string
|
||
|
switch runtime.GOOS {
|
||
9 years ago
|
case "windows":
|
||
|
// For windows.
|
||
8 years ago
|
downloadURL = newUpdateURLPrefix + "/minio.exe?update=yes"
|
||
9 years ago
|
default:
|
||
|
// For all other operating systems.
|
||
8 years ago
|
downloadURL = newUpdateURLPrefix + "/minio?update=yes"
|
||
9 years ago
|
}
|
||
|
|
||
|
// Initialize update message.
|
||
|
updateMsg := updateMessage{
|
||
|
Download: downloadURL,
|
||
8 years ago
|
Version: Version,
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Instantiate a new client with 3 sec timeout.
|
||
9 years ago
|
client := &http.Client{
|
||
9 years ago
|
Timeout: 3 * time.Second,
|
||
9 years ago
|
}
|
||
|
|
||
|
// Fetch new update.
|
||
9 years ago
|
data, err := client.Get(newUpdateURL)
|
||
|
if err != nil && noError {
|
||
9 years ago
|
return updateMsg
|
||
|
}
|
||
9 years ago
|
fatalIf((err), "Unable to read from update URL ‘"+newUpdateURL+"’.")
|
||
9 years ago
|
|
||
9 years ago
|
// Error out if 'update' command is issued for development based builds.
|
||
8 years ago
|
if Version == "DEVELOPMENT.GOGET" && !noError {
|
||
9 years ago
|
fatalIf((errors.New("")),
|
||
9 years ago
|
"Update mechanism is not supported for ‘go get’ based binary builds. Please download official releases from https://minio.io/#minio")
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Parse current minio version into RFC3339.
|
||
8 years ago
|
current, err := time.Parse(time.RFC3339, Version)
|
||
9 years ago
|
if err != nil && noError {
|
||
9 years ago
|
return updateMsg
|
||
|
}
|
||
9 years ago
|
fatalIf((err), "Unable to parse version string as time.")
|
||
9 years ago
|
|
||
9 years ago
|
// Verify if current minio version is zero.
|
||
|
if current.IsZero() && !noError {
|
||
9 years ago
|
fatalIf((errors.New("")),
|
||
9 years ago
|
"Updates mechanism is not supported for custom builds. Please download official releases from https://minio.io/#minio")
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Verify if we have a valid http response i.e http.StatusOK.
|
||
|
if data != nil {
|
||
|
if data.StatusCode != http.StatusOK {
|
||
|
// Return quickly if noError is set.
|
||
|
if noError {
|
||
|
return updateMsg
|
||
|
}
|
||
9 years ago
|
fatalIf((errors.New("")), "Failed to retrieve update notice. "+data.Status)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Read the response body.
|
||
9 years ago
|
updateBody, err := ioutil.ReadAll(data.Body)
|
||
|
if err != nil && noError {
|
||
9 years ago
|
return updateMsg
|
||
|
}
|
||
9 years ago
|
fatalIf((err), "Failed to retrieve update notice. Please try again later.")
|
||
9 years ago
|
|
||
9 years ago
|
// Parse the date if its valid.
|
||
|
latest, err := parseReleaseData(string(updateBody))
|
||
|
if err != nil && noError {
|
||
|
return updateMsg
|
||
|
}
|
||
9 years ago
|
errMsg := "Failed to retrieve update notice. Please try again later. Please report this issue at https://github.com/minio/minio/issues"
|
||
|
fatalIf(err, errMsg)
|
||
9 years ago
|
|
||
9 years ago
|
// Verify if the date is not zero.
|
||
|
if latest.IsZero() && !noError {
|
||
9 years ago
|
fatalIf((errors.New("")), errMsg)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Is the update latest?.
|
||
9 years ago
|
if latest.After(current) {
|
||
9 years ago
|
updateMsg.Update = true
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// Return update message.
|
||
|
return updateMsg
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// main entry point for update command.
|
||
9 years ago
|
func mainUpdate(ctx *cli.Context) {
|
||
9 years ago
|
// Print all errors as they occur.
|
||
|
noError := false
|
||
|
|
||
9 years ago
|
// Check for update.
|
||
|
if ctx.Bool("experimental") {
|
||
9 years ago
|
console.Println(getReleaseUpdate(minioUpdateExperimentalURL, noError))
|
||
9 years ago
|
} else {
|
||
9 years ago
|
console.Println(getReleaseUpdate(minioUpdateStableURL, noError))
|
||
9 years ago
|
}
|
||
|
}
|