2015-10-17 18:03:46 -04:00
|
|
|
|
/*
|
2017-02-15 03:31:00 -05:00
|
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc.
|
2015-10-17 18:03:46 -04:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
|
package cmd
|
2015-10-17 18:03:46 -04:00
|
|
|
|
|
|
|
|
|
import (
|
2016-10-12 21:09:08 -04:00
|
|
|
|
"bytes"
|
2017-02-15 03:31:00 -05:00
|
|
|
|
"fmt"
|
2015-12-22 20:11:11 -05:00
|
|
|
|
"io/ioutil"
|
2015-10-17 18:03:46 -04:00
|
|
|
|
"net/http"
|
2016-10-12 21:09:08 -04:00
|
|
|
|
"os"
|
2015-10-17 18:03:46 -04:00
|
|
|
|
"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"
|
2016-03-04 04:50:59 -05:00
|
|
|
|
"github.com/minio/mc/pkg/console"
|
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,
|
2017-02-15 03:31:00 -05:00
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
cli.BoolFlag{
|
|
|
|
|
Name: "help, h",
|
|
|
|
|
Usage: "Show this help.",
|
|
|
|
|
},
|
|
|
|
|
cli.BoolFlag{
|
|
|
|
|
Name: "quiet",
|
|
|
|
|
Usage: "Disable any update messages.",
|
|
|
|
|
},
|
|
|
|
|
},
|
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}}
|
2017-02-15 03:31:00 -05:00
|
|
|
|
EXIT STATUS:
|
|
|
|
|
0 - You are already running the most recent version.
|
|
|
|
|
1 - New update is available.
|
|
|
|
|
-1 - Error in getting update information.
|
2015-11-26 00:06:29 -05:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
VERSION:
|
|
|
|
|
` + Version + `{{"\n"}}`,
|
2015-10-17 18:03:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
const releaseTagTimeLayout = "2006-01-02T15-04-05Z"
|
2015-10-17 18:03:46 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
const minioReleaseURL = "https://dl.minio.io/server/minio/release/" + runtime.GOOS + "-" + runtime.GOARCH + "/"
|
|
|
|
|
|
|
|
|
|
func getCurrentReleaseTime(minioVersion, minioBinaryPath string) (releaseTime time.Time, err error) {
|
|
|
|
|
if releaseTime, err = time.Parse(time.RFC3339, minioVersion); err == nil {
|
|
|
|
|
return releaseTime, err
|
2015-12-22 20:11:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
// Looks like version is minio non-standard, we use minio binary's ModTime as release time.
|
|
|
|
|
fi, err := os.Stat(minioBinaryPath)
|
2016-04-29 17:24:10 -04:00
|
|
|
|
if err != nil {
|
2017-02-15 03:31:00 -05:00
|
|
|
|
err = fmt.Errorf("Unable to get ModTime of %s. %s", minioBinaryPath, err)
|
|
|
|
|
} else {
|
|
|
|
|
releaseTime = fi.ModTime().UTC()
|
2015-12-22 20:11:11 -05:00
|
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
|
|
|
|
|
|
return releaseTime, err
|
2015-12-22 20:11:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
// GetCurrentReleaseTime - returns this process's release time. If it is official minio version,
|
|
|
|
|
// parsed version is returned else minio binary's mod time is returned.
|
|
|
|
|
func GetCurrentReleaseTime() (releaseTime time.Time, err error) {
|
|
|
|
|
return getCurrentReleaseTime(Version, os.Args[0])
|
|
|
|
|
}
|
2016-10-12 21:09:08 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
func isDocker(cgroupFile string) (bool, error) {
|
|
|
|
|
cgroup, err := ioutil.ReadFile(cgroupFile)
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
err = nil
|
2016-10-12 21:09:08 -04:00
|
|
|
|
}
|
2016-11-24 19:06:49 -05:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
return bytes.Contains(cgroup, []byte("docker")), err
|
2016-10-12 21:09:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
// IsDocker - returns if the environment is docker or not.
|
|
|
|
|
func IsDocker() bool {
|
|
|
|
|
found, err := isDocker("/proc/self/cgroup")
|
2016-11-20 02:20:13 -05:00
|
|
|
|
if err != nil {
|
2017-02-15 03:31:00 -05:00
|
|
|
|
console.Fatalf("Error in docker check: %s", err)
|
2016-11-20 02:20:13 -05:00
|
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
|
|
|
|
|
|
return found
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isSourceBuild(minioVersion string) bool {
|
|
|
|
|
_, err := time.Parse(time.RFC3339, minioVersion)
|
|
|
|
|
return err != nil
|
2016-11-20 02:20:13 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
// IsSourceBuild - returns if this binary is made from source or not.
|
|
|
|
|
func IsSourceBuild() bool {
|
|
|
|
|
return isSourceBuild(Version)
|
|
|
|
|
}
|
2016-03-24 20:20:49 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
// DO NOT CHANGE USER AGENT STYLE.
|
|
|
|
|
// The style should be
|
|
|
|
|
// Minio (<OS>; <ARCH>[; docker][; source]) Minio/<VERSION> Minio/<RELEASE-TAG> Minio/<COMMIT-ID>
|
|
|
|
|
//
|
|
|
|
|
// For any change here should be discussed by openning an issue at https://github.com/minio/minio/issues.
|
|
|
|
|
func getUserAgent() string {
|
|
|
|
|
userAgent := "Minio (" + runtime.GOOS + "; " + runtime.GOARCH
|
|
|
|
|
if IsDocker() {
|
|
|
|
|
userAgent += "; docker"
|
|
|
|
|
}
|
|
|
|
|
if IsSourceBuild() {
|
|
|
|
|
userAgent += "; source"
|
2016-03-24 20:20:49 -04:00
|
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
|
userAgent += ") " + " Minio/" + Version + " Minio/" + ReleaseTag + " Minio/" + CommitID
|
|
|
|
|
|
|
|
|
|
return userAgent
|
|
|
|
|
}
|
2016-03-24 20:20:49 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
func downloadReleaseData(releaseChecksumURL string, timeout time.Duration) (data string, err error) {
|
|
|
|
|
req, err := http.NewRequest("GET", releaseChecksumURL, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return data, err
|
2016-03-24 20:20:49 -04:00
|
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
|
req.Header.Set("User-Agent", getUserAgent())
|
2016-03-24 20:20:49 -04:00
|
|
|
|
|
|
|
|
|
client := &http.Client{
|
2017-02-15 03:31:00 -05:00
|
|
|
|
Timeout: timeout,
|
2016-03-24 20:20:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
resp, err := client.Do(req)
|
2016-08-19 11:09:05 -04:00
|
|
|
|
if err != nil {
|
2017-02-15 03:31:00 -05:00
|
|
|
|
return data, err
|
|
|
|
|
}
|
|
|
|
|
if resp == nil {
|
|
|
|
|
return data, fmt.Errorf("No response from server to download URL %s", releaseChecksumURL)
|
2016-08-19 11:09:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
return data, fmt.Errorf("Error downloading URL %s. Response: %v", releaseChecksumURL, resp.Status)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataBytes, err := ioutil.ReadAll(resp.Body)
|
2016-10-12 21:09:08 -04:00
|
|
|
|
if err != nil {
|
2017-02-15 03:31:00 -05:00
|
|
|
|
return data, fmt.Errorf("Error reading response. %s", err)
|
2016-10-12 21:09:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
data = string(dataBytes)
|
|
|
|
|
return data, err
|
|
|
|
|
}
|
2016-10-24 16:44:15 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
// DownloadReleaseData - downloads release data from minio official server.
|
|
|
|
|
func DownloadReleaseData(timeout time.Duration) (data string, err error) {
|
|
|
|
|
return downloadReleaseData(minioReleaseURL+"minio.shasum", timeout)
|
|
|
|
|
}
|
2016-10-12 21:09:08 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
func parseReleaseData(data string) (releaseTime time.Time, err error) {
|
|
|
|
|
fields := strings.Fields(data)
|
|
|
|
|
if len(fields) != 2 {
|
|
|
|
|
err = fmt.Errorf("Unknown release data `%s`", data)
|
|
|
|
|
return releaseTime, err
|
2015-10-17 18:03:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
releaseInfo := fields[1]
|
|
|
|
|
if fields = strings.Split(releaseInfo, "."); len(fields) != 3 {
|
|
|
|
|
err = fmt.Errorf("Unknown release information `%s`", releaseInfo)
|
|
|
|
|
return releaseTime, err
|
2016-03-24 20:20:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
if !(fields[0] == "minio" && fields[1] == "RELEASE") {
|
|
|
|
|
err = fmt.Errorf("Unknown release '%s'", releaseInfo)
|
|
|
|
|
return releaseTime, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
releaseTime, err = time.Parse(releaseTagTimeLayout, fields[2])
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = fmt.Errorf("Unknown release time format. %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return releaseTime, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getLatestReleaseTime(timeout time.Duration) (releaseTime time.Time, err error) {
|
|
|
|
|
data, err := DownloadReleaseData(timeout)
|
2016-08-19 11:09:05 -04:00
|
|
|
|
if err != nil {
|
2017-02-15 03:31:00 -05:00
|
|
|
|
return releaseTime, err
|
2016-03-24 20:20:49 -04:00
|
|
|
|
}
|
2016-08-19 11:09:05 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
return parseReleaseData(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDownloadURL() (downloadURL string) {
|
|
|
|
|
if IsDocker() {
|
|
|
|
|
return "docker pull minio/minio"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
return minioReleaseURL + "minio.exe"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return minioReleaseURL + "minio"
|
|
|
|
|
}
|
2015-10-17 18:03:46 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
func getUpdateInfo(timeout time.Duration) (older time.Duration, downloadURL string, err error) {
|
|
|
|
|
currentReleaseTime, err := GetCurrentReleaseTime()
|
2016-08-19 11:09:05 -04:00
|
|
|
|
if err != nil {
|
2017-02-15 03:31:00 -05:00
|
|
|
|
return older, downloadURL, err
|
2016-03-24 20:20:49 -04:00
|
|
|
|
}
|
2015-10-17 18:03:46 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
latestReleaseTime, err := getLatestReleaseTime(timeout)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return older, downloadURL, err
|
2015-10-17 18:03:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
if latestReleaseTime.After(currentReleaseTime) {
|
|
|
|
|
older = latestReleaseTime.Sub(currentReleaseTime)
|
|
|
|
|
downloadURL = getDownloadURL()
|
2015-10-17 18:03:46 -04:00
|
|
|
|
}
|
2016-03-24 20:20:49 -04:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
return older, downloadURL, nil
|
2015-10-17 18:03:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mainUpdate(ctx *cli.Context) {
|
2017-02-15 03:31:00 -05:00
|
|
|
|
if len(ctx.Args()) != 0 {
|
|
|
|
|
cli.ShowCommandHelpAndExit(ctx, "update", -1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quiet := ctx.Bool("quiet") || ctx.GlobalBool("quiet")
|
|
|
|
|
quietPrintln := func(args ...interface{}) {
|
|
|
|
|
if !quiet {
|
|
|
|
|
console.Println(args...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
older, downloadURL, err := getUpdateInfo(10 * time.Second)
|
|
|
|
|
if err != nil {
|
|
|
|
|
quietPrintln(err)
|
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2016-11-28 15:15:36 -05:00
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
if older != time.Duration(0) {
|
|
|
|
|
quietPrintln(colorizeUpdateMessage(downloadURL, older))
|
|
|
|
|
os.Exit(1)
|
2016-11-23 18:48:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
|
colorSprintf := color.New(color.FgGreen, color.Bold).SprintfFunc()
|
|
|
|
|
quietPrintln(colorSprintf("You are already running the most recent version of ‘minio’."))
|
|
|
|
|
os.Exit(0)
|
2015-10-17 18:03:46 -04:00
|
|
|
|
}
|