Checking updates should send a proper user-agent.

This commit is contained in:
Harshavardhana
2016-10-13 20:52:09 -07:00
parent 29a2ffef5d
commit 8c59a41668
2 changed files with 86 additions and 52 deletions

View File

@@ -21,6 +21,7 @@ import (
"os" "os"
"sort" "sort"
"strings" "strings"
"time"
"github.com/minio/cli" "github.com/minio/cli"
"github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/console"
@@ -167,13 +168,16 @@ func Main() {
// Do not print update messages, if quiet flag is set. // Do not print update messages, if quiet flag is set.
if !globalQuiet { if !globalQuiet {
if strings.HasPrefix(ReleaseTag, "RELEASE.") && c.Args().Get(0) != "update" { if strings.HasPrefix(ReleaseTag, "RELEASE.") && c.Args().Get(0) != "update" {
updateMsg := getReleaseUpdate(minioUpdateStableURL, true) updateMsg, _, err := getReleaseUpdate(minioUpdateStableURL, time.Second*1)
if err != nil {
// Ignore all network related errors.
return nil
}
if updateMsg.Update { if updateMsg.Update {
console.Println(updateMsg) console.Println(updateMsg)
} }
} }
} }
return nil return nil
} }

View File

@@ -17,10 +17,13 @@
package cmd package cmd
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"runtime" "runtime"
"strings" "strings"
"time" "time"
@@ -33,10 +36,6 @@ import (
// command specific flags. // command specific flags.
var ( var (
updateFlags = []cli.Flag{ updateFlags = []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "Help for update.",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "experimental, E", Name: "experimental, E",
Usage: "Check experimental update.", Usage: "Check experimental update.",
@@ -49,7 +48,7 @@ var updateCmd = cli.Command{
Name: "update", Name: "update",
Usage: "Check for a new software update.", Usage: "Check for a new software update.",
Action: mainUpdate, Action: mainUpdate,
Flags: updateFlags, Flags: append(updateFlags, globalFlags...),
CustomHelpTemplate: `Name: CustomHelpTemplate: `Name:
minio {{.Name}} - {{.Usage}} minio {{.Name}} - {{.Usage}}
@@ -114,7 +113,8 @@ func parseReleaseData(data string) (time.Time, error) {
if releaseDateSplits[0] != "minio" { if releaseDateSplits[0] != "minio" {
return time.Time{}, (errors.New("Update data malformed, missing minio tag")) return time.Time{}, (errors.New("Update data malformed, missing minio tag"))
} }
// "OFFICIAL" tag is still kept for backward compatibility, we should remove this for the next release. // "OFFICIAL" tag is still kept for backward compatibility.
// We should remove this for the next release.
if releaseDateSplits[1] != "RELEASE" && releaseDateSplits[1] != "OFFICIAL" { if releaseDateSplits[1] != "RELEASE" && releaseDateSplits[1] != "OFFICIAL" {
return time.Time{}, (errors.New("Update data malformed, missing RELEASE tag")) return time.Time{}, (errors.New("Update data malformed, missing RELEASE tag"))
} }
@@ -132,8 +132,28 @@ func parseReleaseData(data string) (time.Time, error) {
return parsedDate, nil return parsedDate, nil
} }
// User Agent should always following the below style.
// Please open an issue to discuss any new changes here.
//
// Minio (OS; ARCH) APP/VER APP/VER
var (
userAgentSuffix = "Minio/" + Version + " " + "Minio/" + ReleaseTag + " " + "Minio/" + CommitID
userAgentPrefix = "Minio (" + runtime.GOOS + "; " + runtime.GOARCH + ") "
userAgent = userAgentPrefix + userAgentSuffix
)
// Check if the operating system is a docker container.
func isDocker() bool {
cgroup, err := ioutil.ReadFile("/proc/self/cgroup")
if err != nil && os.IsNotExist(err) {
return false
}
fatalIf(err, "Unable to read `cgroup` file.")
return bytes.Contains(cgroup, []byte("docker"))
}
// verify updates for releases. // verify updates for releases.
func getReleaseUpdate(updateURL string, noError bool) updateMessage { func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updateMessage, errMsg string, err error) {
// Construct a new update url. // Construct a new update url.
newUpdateURLPrefix := updateURL + "/" + runtime.GOOS + "-" + runtime.GOARCH newUpdateURLPrefix := updateURL + "/" + runtime.GOOS + "-" + runtime.GOARCH
newUpdateURL := newUpdateURLPrefix + "/minio.shasum" newUpdateURL := newUpdateURLPrefix + "/minio.shasum"
@@ -150,71 +170,73 @@ func getReleaseUpdate(updateURL string, noError bool) updateMessage {
} }
// Initialize update message. // Initialize update message.
updateMsg := updateMessage{ updateMsg = updateMessage{
Download: downloadURL, Download: downloadURL,
Version: Version, Version: Version,
} }
// Instantiate a new client with 3 sec timeout. // Instantiate a new client with 3 sec timeout.
client := &http.Client{ client := &http.Client{
Timeout: 3 * time.Second, Timeout: duration,
}
// 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 Version == "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")
} }
// Parse current minio version into RFC3339. // Parse current minio version into RFC3339.
current, err := time.Parse(time.RFC3339, Version) current, err := time.Parse(time.RFC3339, Version)
if err != nil && noError { if err != nil {
return updateMsg errMsg = "Unable to parse version string as time."
return
} }
fatalIf((err), "Unable to parse version string as time.")
// Verify if current minio version is zero. // Verify if current minio version is zero.
if current.IsZero() && !noError { if current.IsZero() {
fatalIf((errors.New("")), err = errors.New("date should not be zero")
"Updates mechanism is not supported for custom builds. Please download official releases from https://minio.io/#minio") errMsg = "Updates mechanism is not supported for custom builds. Please download official releases from https://minio.io/#minio"
return
}
// Initialize new request.
req, err := http.NewRequest("GET", newUpdateURL, nil)
if err != nil {
return
}
// Set user agent.
req.Header.Set("User-Agent", userAgent+" "+fmt.Sprintf("Docker/%t", isDocker()))
// Fetch new update.
resp, err := client.Do(req)
if err != nil {
return
} }
// Verify if we have a valid http response i.e http.StatusOK. // Verify if we have a valid http response i.e http.StatusOK.
if data != nil { if resp != nil {
if data.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
// Return quickly if noError is set. errMsg = "Failed to retrieve update notice."
if noError { err = errors.New("http status : " + resp.Status)
return updateMsg return
}
fatalIf((errors.New("")), "Failed to retrieve update notice. "+data.Status)
} }
} }
// Read the response body. // Read the response body.
updateBody, err := ioutil.ReadAll(data.Body) updateBody, err := ioutil.ReadAll(resp.Body)
if err != nil && noError { if err != nil {
return updateMsg errMsg = "Failed to retrieve update notice. Please try again later."
return
} }
fatalIf((err), "Failed to retrieve update notice. Please try again later.")
errMsg = "Failed to retrieve update notice. Please try again later. Please report this issue at https://github.com/minio/minio/issues"
// Parse the date if its valid. // Parse the date if its valid.
latest, err := parseReleaseData(string(updateBody)) latest, err := parseReleaseData(string(updateBody))
if err != nil && noError { if err != nil {
return updateMsg return
} }
errMsg := "Failed to retrieve update notice. Please try again later. Please report this issue at https://github.com/minio/minio/issues"
fatalIf(err, errMsg)
// Verify if the date is not zero. // Verify if the date is not zero.
if latest.IsZero() && !noError { if latest.IsZero() {
fatalIf((errors.New("")), errMsg) err = errors.New("date should not be zero")
return
} }
// Is the update latest?. // Is the update latest?.
@@ -223,18 +245,26 @@ func getReleaseUpdate(updateURL string, noError bool) updateMessage {
} }
// Return update message. // Return update message.
return updateMsg return updateMsg, "", nil
} }
// main entry point for update command. // main entry point for update command.
func mainUpdate(ctx *cli.Context) { func mainUpdate(ctx *cli.Context) {
// Print all errors as they occur. // Error out if 'update' command is issued for development based builds.
noError := false 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. // Check for update.
var updateMsg updateMessage
var errMsg string
var err error
var secs = time.Second * 3
if ctx.Bool("experimental") { if ctx.Bool("experimental") {
console.Println(getReleaseUpdate(minioUpdateExperimentalURL, noError)) updateMsg, errMsg, err = getReleaseUpdate(minioUpdateExperimentalURL, secs)
} else { } else {
console.Println(getReleaseUpdate(minioUpdateStableURL, noError)) updateMsg, errMsg, err = getReleaseUpdate(minioUpdateStableURL, secs)
} }
fatalIf(err, errMsg)
console.Println(updateMsg)
} }