update: Change update notifier for new style banner. (#3289)

For binary releases and operating systems it would be

All operating systems.
```
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Minio is 25 days 12 hours 30 minutes old                           ┃
┃ Update: https://dl.minio.io/server/minio/release/linux-amd64/minio ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
```

On docker.
```
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Minio is 25 days 12 hours 32 minutes old ┃
┃ Update: docker pull minio/minio          ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
```
This commit is contained in:
Harshavardhana
2016-11-19 23:20:13 -08:00
committed by GitHub
parent 1c47365445
commit 2c3a2241e7
8 changed files with 289 additions and 64 deletions

View File

@@ -18,7 +18,6 @@ package cmd
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
@@ -74,10 +73,9 @@ const (
// updateMessage container to hold update messages.
type updateMessage struct {
Status string `json:"status"`
Update bool `json:"update"`
Download string `json:"downloadURL"`
Version string `json:"version"`
Update bool `json:"update"`
Download string `json:"downloadURL"`
NewerThan time.Duration `json:"newerThan"`
}
// String colorized update message.
@@ -86,19 +84,10 @@ func (u updateMessage) String() string {
updateMessage := color.New(color.FgGreen, color.Bold).SprintfFunc()
return updateMessage("You are already running the most recent version of minio.")
}
msg := colorizeUpdateMessage(u.Download)
msg := colorizeUpdateMessage(u.Download, u.NewerThan)
return msg
}
// JSON jsonified update message.
func (u updateMessage) JSON() string {
u.Status = "success"
updateMessageJSONBytes, err := json.Marshal(u)
fatalIf((err), "Unable to marshal into JSON.")
return string(updateMessageJSONBytes)
}
func parseReleaseData(data string) (time.Time, error) {
releaseStr := strings.Fields(data)
if len(releaseStr) < 2 {
@@ -149,6 +138,34 @@ func isDocker() bool {
return bytes.Contains(cgroup, []byte("docker"))
}
// Check if the minio server binary was built with source.
func isSourceBuild() bool {
return Version == "DEVELOPMENT.GOGET"
}
// Fetch the current version of the Minio server binary.
func getCurrentMinioVersion() (current time.Time, err error) {
// For development builds we check for binary modTime
// to validate against latest minio server release.
if Version != "DEVELOPMENT.GOGET" {
// Parse current minio version into RFC3339.
current, err = time.Parse(time.RFC3339, Version)
if err != nil {
return time.Time{}, err
}
return current, nil
} // else {
// For all development builds through `go get`.
// fall back to looking for version of the build
// date of the binary itself.
var fi os.FileInfo
fi, err = os.Stat(os.Args[0])
if err != nil {
return time.Time{}, err
}
return fi.ModTime(), nil
}
// verify updates for releases.
func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updateMessage, errMsg string, err error) {
// Construct a new update url.
@@ -157,19 +174,22 @@ func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updat
// 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"
if isDocker() {
downloadURL = "docker pull minio/minio"
} else {
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: Version,
}
// Instantiate a new client with 3 sec timeout.
@@ -177,17 +197,9 @@ func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updat
Timeout: duration,
}
// Parse current minio version into RFC3339.
current, err := time.Parse(time.RFC3339, Version)
current, err := getCurrentMinioVersion()
if err != nil {
errMsg = "Unable to parse version string as time."
return
}
// Verify if current minio version is zero.
if current.IsZero() {
err = errors.New("date should not be zero")
errMsg = "Updates mechanism is not supported for custom builds. Please download official releases from https://minio.io/#minio"
errMsg = "Unable to fetch the current version of Minio server."
return
}
@@ -198,10 +210,22 @@ func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updat
}
userAgentPrefix := func() string {
prefix := "Minio (" + runtime.GOOS + "; " + runtime.GOARCH
// if its a source build.
if isSourceBuild() {
if isDocker() {
prefix = prefix + "; " + "docker; source) "
} else {
prefix = prefix + "; " + "source) "
}
return prefix
} // else { not source.
if isDocker() {
return "Minio (" + runtime.GOOS + "; " + runtime.GOARCH + "; " + "docker) "
prefix = prefix + "; " + "docker) "
} else {
prefix = prefix + ") "
}
return "Minio (" + runtime.GOOS + "; " + runtime.GOARCH + ") "
return prefix
}()
// Set user agent.
@@ -239,13 +263,14 @@ func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updat
// Verify if the date is not zero.
if latest.IsZero() {
err = errors.New("date should not be zero")
err = errors.New("Release date cannot be zero. Please report this issue at https://github.com/minio/minio/issues")
return
}
// Is the update latest?.
if latest.After(current) {
updateMsg.Update = true
updateMsg.NewerThan = latest.Sub(current)
}
// Return update message.
@@ -254,11 +279,6 @@ func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updat
// main entry point for update command.
func mainUpdate(ctx *cli.Context) {
// Error out if 'update' command is issued for development based builds.
if Version == "DEVELOPMENT.GOGET" {
fatalIf(errors.New(""), "Update mechanism is not supported for go get based binary builds. Please download official releases from https://minio.io/#minio")
}
// Check for update.
var updateMsg updateMessage
var errMsg string