minio/update-main.go

242 lines
6.6 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"
2015-12-22 20:11:11 -05:00
"io/ioutil"
2015-10-17 18:03:46 -04:00
"net/http"
"runtime"
2015-12-22 20:11:11 -05:00
"strings"
2015-10-17 18:03:46 -04:00
"time"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
2015-10-17 18:03:46 -04:00
)
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 (
2015-12-22 20:11:11 -05:00
minioUpdateStableURL = "https://dl.minio.io/server/minio/release/"
minioUpdateExperimentalURL = "https://dl.minio.io/server/minio/experimental/"
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.")
}
msg, err := colorizeUpdateMessage(u.Download)
fatalIf(err, "Unable to colorize update notice "+msg+".")
2015-11-26 00:06:29 -05:00
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((err), "Unable to marshal into JSON.")
2015-10-17 18:03:46 -04:00
return string(updateMessageJSONBytes)
}
func parseReleaseData(data string) (time.Time, error) {
2015-12-22 20:11:11 -05:00
releaseStr := strings.Fields(data)
if len(releaseStr) < 2 {
return time.Time{}, errors.New("Update data malformed")
2015-12-22 20:11:11 -05:00
}
releaseDate := releaseStr[1]
releaseDateSplits := strings.SplitN(releaseDate, ".", 3)
if len(releaseDateSplits) < 3 {
return time.Time{}, (errors.New("Update data malformed"))
2015-12-22 20:11:11 -05:00
}
if releaseDateSplits[0] != "minio" {
return time.Time{}, (errors.New("Update data malformed, missing minio tag"))
2015-12-22 20:11:11 -05:00
}
// "OFFICIAL" tag is still kept for backward compatibility, we should remove this for the next release.
if releaseDateSplits[1] != "RELEASE" && releaseDateSplits[1] != "OFFICIAL" {
return time.Time{}, (errors.New("Update data malformed, missing RELEASE tag"))
2015-12-22 20:11:11 -05:00
}
dateSplits := strings.SplitN(releaseDateSplits[2], "T", 2)
if len(dateSplits) < 2 {
return time.Time{}, (errors.New("Update data malformed, not in modified RFC3359 form"))
2015-12-22 20:11:11 -05:00
}
dateSplits[1] = strings.Replace(dateSplits[1], "-", ":", -1)
date := strings.Join(dateSplits, "T")
parsedDate, err := time.Parse(time.RFC3339, date)
if err != nil {
return time.Time{}, err
2015-12-22 20:11:11 -05:00
}
return parsedDate, nil
}
2015-11-26 00:06:29 -05:00
// verify updates for releases.
func getReleaseUpdate(updateURL string, noError bool) updateMessage {
// Construct a new update url.
2016-01-27 14:43:26 -05:00
newUpdateURLPrefix := updateURL + "/" + runtime.GOOS + "-" + runtime.GOARCH
newUpdateURL := newUpdateURLPrefix + "/minio.shasum"
// Get the downloadURL.
var downloadURL string
switch runtime.GOOS {
case "windows":
// For windows.
downloadURL = newUpdateURLPrefix + "/minio.exe"
default:
// For all other operating systems.
downloadURL = newUpdateURLPrefix + "/minio"
}
// Initialize update message.
updateMsg := updateMessage{
Download: downloadURL,
Version: minioVersion,
}
// Instantiate a new client with 1 sec timeout.
client := &http.Client{
Timeout: 1 * time.Millisecond,
}
// Fetch new update.
data, err := client.Get(newUpdateURL)
if err != nil && noError {
return updateMsg
}
fatalIf((err), "Unable to read from update URL "+newUpdateURL+".")
// Error out if 'update' command is issued for development based builds.
if minioVersion == "DEVELOPMENT.GOGET" && !noError {
fatalIf((errors.New("")),
"Update mechanism is not supported for go get based binary builds. Please download official releases from https://minio.io/#minio")
2015-10-17 18:03:46 -04:00
}
// Parse current minio version into RFC3339.
current, err := time.Parse(time.RFC3339, minioVersion)
if err != nil && noError {
return updateMsg
}
fatalIf((err), "Unable to parse version string as time.")
2015-10-17 18:03:46 -04:00
// Verify if current minio version is zero.
if current.IsZero() && !noError {
fatalIf((errors.New("")),
"Updates mechanism is not supported for custom builds. Please download official releases from https://minio.io/#minio")
2015-10-17 18:03:46 -04:00
}
// 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
}
fatalIf((errors.New("")), "Failed to retrieve update notice. "+data.Status)
}
}
// Read the response body.
updateBody, err := ioutil.ReadAll(data.Body)
if err != nil && noError {
return updateMsg
}
fatalIf((err), "Failed to retrieve update notice. Please try again later.")
2015-10-17 18:03:46 -04:00
// Parse the date if its valid.
latest, err := parseReleaseData(string(updateBody))
if err != nil && noError {
return updateMsg
}
errMsg := "Failed to retrieve update notice. Please try again later. Please report this issue at https://github.com/minio/minio/issues"
fatalIf(err, errMsg)
2015-10-17 18:03:46 -04:00
// Verify if the date is not zero.
if latest.IsZero() && !noError {
fatalIf((errors.New("")), errMsg)
2015-10-17 18:03:46 -04:00
}
// Is the update latest?.
2015-10-17 18:03:46 -04:00
if latest.After(current) {
2015-11-26 00:06:29 -05:00
updateMsg.Update = true
2015-10-17 18:03:46 -04:00
}
// Return update message.
return 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) {
// Print all errors as they occur.
noError := false
2015-11-26 00:06:29 -05:00
// Check for update.
if ctx.Bool("experimental") {
console.Println(getReleaseUpdate(minioUpdateExperimentalURL, noError))
2015-11-26 00:06:29 -05:00
} else {
console.Println(getReleaseUpdate(minioUpdateStableURL, noError))
2015-10-17 18:03:46 -04:00
}
}