Merge pull request #983 from harshavardhana/improve-update

update: Re-write update command.
master
Harshavardhana 9 years ago
commit f35128eec5
  1. 6
      config-version-main.go
  2. 2
      notifier.go
  3. 194
      update-main.go

@ -47,14 +47,12 @@ func mainConfigVersion(ctx *cli.Context) {
// convert interface{} back to its original struct // convert interface{} back to its original struct
newConf := config newConf := config
type Version struct { type Version string
Value string `json:"value"`
}
if globalJSONFlag { if globalJSONFlag {
tB, e := json.Marshal( tB, e := json.Marshal(
struct { struct {
Version Version `json:"version"` Version Version `json:"version"`
}{Version: Version{newConf.Version}}, }{Version: Version(newConf.Version)},
) )
fatalIf(probe.NewError(e), "Unable to construct version string.", nil) fatalIf(probe.NewError(e), "Unable to construct version string.", nil)
Println(string(tB)) Println(string(tB))

@ -81,6 +81,6 @@ func colorizeUpdateMessage(updateString string) (string, *probe.Error) {
sideBar + line2InColor + spacePaddingLine2 + sideBar + "\n" + sideBar + line2InColor + spacePaddingLine2 + sideBar + "\n" +
bottom + "\n" bottom + "\n"
} }
// finally print the message // return the final message
return message, nil return message, nil
} }

@ -22,7 +22,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"runtime" "runtime"
"strings"
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
@ -30,177 +29,152 @@ import (
"github.com/minio/minio-xl/pkg/probe" "github.com/minio/minio-xl/pkg/probe"
) )
// 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.",
},
}
)
// Check for new software updates. // Check for new software updates.
var updateCmd = cli.Command{ var updateCmd = cli.Command{
Name: "update", Name: "update",
Usage: "Check for new software updates.", Usage: "Check for a new software update.",
Action: mainUpdate, Action: mainUpdate,
Flags: updateFlags,
CustomHelpTemplate: `Name: CustomHelpTemplate: `Name:
minio {{.Name}} - {{.Usage}} minio {{.Name}} - {{.Usage}}
USAGE: USAGE:
minio {{.Name}} release minio {{.Name}} [FLAGS]
minio {{.Name}} experimental
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES: EXAMPLES:
1. Check for new official releases 1. Check for any new official release.
$ minio {{.Name}} release $ minio {{.Name}}
2. Check for new experimental releases 2. Check for any new experimental release.
$ minio {{.Name}} experimental $ minio {{.Name}} --experimental
`, `,
} }
// updates container to hold updates json // update URL endpoints.
type updates struct { 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 {
BuildDate string BuildDate string
Platforms map[string]string Platforms map[string]string
} }
// updateMessage container to hold update messages // updateMessage container to hold update messages.
type updateMessage struct { type updateMessage struct {
Status string `json:"status"`
Update bool `json:"update"` Update bool `json:"update"`
Download string `json:"downloadURL"` Download string `json:"downloadURL"`
Version string `json:"version"` Version string `json:"version"`
} }
// String colorized update message // String colorized update message.
func (u updateMessage) String() string { func (u updateMessage) String() string {
if u.Update { if !u.Update {
var msg string updateMessage := color.New(color.FgGreen, color.Bold).SprintfFunc()
if runtime.GOOS == "windows" { return updateMessage("You are already running the most recent version of ‘minio’.")
msg = "Download " + u.Download }
} else { var msg string
msg = "Download " + u.Download if runtime.GOOS == "windows" {
} msg = "Download " + u.Download
msg, err := colorizeUpdateMessage(msg) } else {
fatalIf(err.Trace(msg), "Unable to colorize experimental update notification string ‘"+msg+"’.", nil) msg = "Download " + u.Download
return msg
} }
updateMessage := color.New(color.FgGreen, color.Bold).SprintfFunc() msg, err := colorizeUpdateMessage(msg)
return updateMessage("You are already running the most recent version of ‘minio’.") fatalIf(err.Trace(msg), "Unable to colorize experimental update notification string ‘"+msg+"’.", nil)
return msg
} }
// JSON jsonified update message // JSON jsonified update message.
func (u updateMessage) JSON() string { func (u updateMessage) JSON() string {
u.Status = "success"
updateMessageJSONBytes, err := json.Marshal(u) updateMessageJSONBytes, err := json.Marshal(u)
fatalIf(probe.NewError(err), "Unable to marshal into JSON.", nil) fatalIf(probe.NewError(err), "Unable to marshal into JSON.", nil)
return string(updateMessageJSONBytes) return string(updateMessageJSONBytes)
} }
func getExperimentalUpdate() { // verify updates for releases.
current, e := time.Parse(time.RFC3339, minioVersion) func getReleaseUpdate(updateURL string) {
fatalIf(probe.NewError(e), "Unable to parse Version string as time.", nil) data, e := http.Get(updateURL)
fatalIf(probe.NewError(e), "Unable to read from update URL ‘"+updateURL+"’.", nil)
if current.IsZero() {
fatalIf(probe.NewError(errors.New("")), "Experimental updates are not supported for custom build. Version field is empty. Please download official releases from https://dl.minio.io:9000", nil)
}
resp, err := http.Get(minioExperimentalURL)
fatalIf(probe.NewError(err), "Unable to initalize experimental URL.", nil)
var experimentals updates
decoder := json.NewDecoder(resp.Body)
defer resp.Body.Close()
e = decoder.Decode(&experimentals)
fatalIf(probe.NewError(e), "Unable to decode experimental update notification.", nil)
latest, e := time.Parse(time.RFC3339, experimentals.BuildDate)
fatalIf(probe.NewError(e), "Unable to parse BuildDate.", nil)
if latest.IsZero() {
fatalIf(probe.NewError(errors.New("")), "Unable to validate any experimental update available at this time. Please open an issue at https://github.com/minio/minio/issues", nil)
}
minioExperimentalURLParse, err := url.Parse(minioExperimentalURL)
if err != nil {
fatalIf(probe.NewError(err), "Unable to parse URL: "+minioExperimentalURL, nil)
}
downloadURL := minioExperimentalURLParse.Scheme + "://" +
minioExperimentalURLParse.Host + "/" + experimentals.Platforms[runtime.GOOS+"-"+runtime.GOARCH]
updateMessage := updateMessage{ if minioVersion == "UNOFFICIAL.GOGET" {
Download: downloadURL, fatalIf(probe.NewError(errors.New("")),
Version: minioVersion, "Update mechanism is not supported for ‘go get’ based binary builds. Please download official releases from https://minio.io/#minio", nil)
}
if latest.After(current) {
updateMessage.Update = true
} }
if globalJSONFlag {
Println(updateMessage.JSON())
} else {
Println(updateMessage)
}
}
func getReleaseUpdate() {
current, e := time.Parse(time.RFC3339, minioVersion) current, e := time.Parse(time.RFC3339, minioVersion)
fatalIf(probe.NewError(e), "Unable to parse Version string as time.", nil) fatalIf(probe.NewError(e), "Unable to parse version string as time.", nil)
if current.IsZero() { if current.IsZero() {
fatalIf(probe.NewError(errors.New("")), "Updates not supported for custom build. Version field is empty. Please download official releases from https://dl.minio.io:9000", nil) 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)
} }
resp, err := http.Get(minioUpdateURL) var updates minioUpdates
fatalIf(probe.NewError(err), "Unable to initalize experimental URL.", nil) decoder := json.NewDecoder(data.Body)
e = decoder.Decode(&updates)
var releases updates
decoder := json.NewDecoder(resp.Body)
e = decoder.Decode(&releases)
fatalIf(probe.NewError(e), "Unable to decode update notification.", nil) fatalIf(probe.NewError(e), "Unable to decode update notification.", nil)
latest, e := time.Parse(time.RFC3339, releases.BuildDate) latest, e := time.Parse(time.RFC3339, updates.BuildDate)
fatalIf(probe.NewError(e), "Unable to parse BuildDate.", nil) if e != nil {
latest, e = time.Parse(http.TimeFormat, updates.BuildDate)
fatalIf(probe.NewError(e), "Unable to parse BuildDate.", nil)
}
if latest.IsZero() { if latest.IsZero() {
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) 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)
} }
minioUpdateURLParse, err := url.Parse(minioUpdateURL) updateURLParse, err := url.Parse(updateURL)
if err != nil { if err != nil {
fatalIf(probe.NewError(err), "Unable to parse URL: "+minioUpdateURL, nil) fatalIf(probe.NewError(err), "Unable to parse URL: "+updateURL, nil)
} }
downloadURL := minioUpdateURLParse.Scheme + downloadURL := updateURLParse.Scheme + "://" +
"://" + minioUpdateURLParse.Host + "/" + releases.Platforms[runtime.GOOS+"-"+runtime.GOARCH] updateURLParse.Host + "/" + updates.Platforms[runtime.GOOS+"-"+runtime.GOARCH]
updateMessage := updateMessage{
updateMsg := updateMessage{
Download: downloadURL, Download: downloadURL,
Version: minioVersion, Version: minioVersion,
} }
if latest.After(current) { if latest.After(current) {
updateMessage.Update = true updateMsg.Update = true
} }
if globalJSONFlag { if globalJSONFlag {
Println(updateMessage.JSON()) Println(updateMsg.JSON())
} else { } else {
Println(updateMessage) Println(updateMsg)
}
}
const (
minioUpdateURL = "https://dl.minio.io:9000/updates/minio/updates.json"
minioExperimentalURL = "https://dl.minio.io:9000/updates/minio/experimental.json"
)
func checkUpdateSyntax(ctx *cli.Context) {
if ctx.Args().First() == "help" || !ctx.Args().Present() {
cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code
}
arg := strings.TrimSpace(ctx.Args().First())
if arg != "release" && arg != "experimental" {
fatalIf(probe.NewError(errInvalidArgument), "Unrecognized argument provided.", nil)
} }
} }
// mainUpdate - // main entry point for update command.
func mainUpdate(ctx *cli.Context) { func mainUpdate(ctx *cli.Context) {
checkUpdateSyntax(ctx) // Check for update.
arg := strings.TrimSpace(ctx.Args().First()) if ctx.Bool("experimental") {
switch arg { getReleaseUpdate(minioUpdateExperimentalURL)
case "release": } else {
getReleaseUpdate() getReleaseUpdate(minioUpdateStableURL)
case "experimental":
getExperimentalUpdate()
} }
} }

Loading…
Cancel
Save