2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-10-17 18:03:46 -04:00
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2015-10-17 18:03:46 -04:00
|
|
|
|
|
|
|
import (
|
2017-06-24 16:17:28 -04:00
|
|
|
"bufio"
|
2017-12-15 15:33:42 -05:00
|
|
|
"crypto"
|
2020-06-03 16:18:54 -04:00
|
|
|
"crypto/tls"
|
2017-12-15 15:33:42 -05:00
|
|
|
"encoding/hex"
|
2019-09-12 16:03:42 -04:00
|
|
|
"errors"
|
2017-02-15 03:31:00 -05:00
|
|
|
"fmt"
|
2020-07-23 11:03:31 -04:00
|
|
|
"io"
|
2015-12-22 20:11:11 -05:00
|
|
|
"io/ioutil"
|
2015-10-17 18:03:46 -04:00
|
|
|
"net/http"
|
2020-07-23 11:03:31 -04:00
|
|
|
"net/url"
|
2016-10-12 21:09:08 -04:00
|
|
|
"os"
|
2020-07-23 11:03:31 -04:00
|
|
|
"path"
|
2017-02-21 04:32:05 -05:00
|
|
|
"path/filepath"
|
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"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-05-28 18:17:01 -04:00
|
|
|
"github.com/minio/pkg/env"
|
2021-06-14 17:54:37 -04:00
|
|
|
xnet "github.com/minio/pkg/net"
|
2020-07-23 11:03:31 -04:00
|
|
|
"github.com/minio/selfupdate"
|
2015-10-17 18:03:46 -04:00
|
|
|
)
|
|
|
|
|
2017-02-21 04:32:05 -05:00
|
|
|
const (
|
|
|
|
minioReleaseTagTimeLayout = "2006-01-02T15-04-05Z"
|
2017-12-15 15:33:42 -05:00
|
|
|
minioOSARCH = runtime.GOOS + "-" + runtime.GOARCH
|
2019-08-06 15:08:58 -04:00
|
|
|
minioReleaseURL = "https://dl.min.io/server/minio/release/" + minioOSARCH + SlashSeparator
|
2020-07-23 11:03:31 -04:00
|
|
|
|
|
|
|
envMinisignPubKey = "MINIO_UPDATE_MINISIGN_PUBKEY"
|
|
|
|
updateTimeout = 10 * time.Second
|
2017-02-21 04:32:05 -05:00
|
|
|
)
|
2017-02-15 03:31:00 -05:00
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
var (
|
2018-04-13 14:51:03 -04:00
|
|
|
// For windows our files have .exe additionally.
|
2020-07-23 11:03:31 -04:00
|
|
|
minioReleaseWindowsInfoURL = minioReleaseURL + "minio.exe.sha256sum"
|
2017-10-09 19:12:13 -04:00
|
|
|
)
|
2015-12-22 20:11:11 -05:00
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
// minioVersionToReleaseTime - parses a standard official release
|
2019-04-09 14:39:42 -04:00
|
|
|
// MinIO version string.
|
2017-10-09 19:12:13 -04:00
|
|
|
//
|
|
|
|
// An official binary's version string is the release time formatted
|
|
|
|
// with RFC3339 (in UTC) - e.g. `2017-09-29T19:16:56Z`
|
|
|
|
func minioVersionToReleaseTime(version string) (releaseTime time.Time, err error) {
|
|
|
|
return time.Parse(time.RFC3339, version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// releaseTimeToReleaseTag - converts a time to a string formatted as
|
2019-04-09 14:39:42 -04:00
|
|
|
// an official MinIO release tag.
|
2017-10-09 19:12:13 -04:00
|
|
|
//
|
|
|
|
// An official minio release tag looks like:
|
|
|
|
// `RELEASE.2017-09-29T19-16-56Z`
|
|
|
|
func releaseTimeToReleaseTag(releaseTime time.Time) string {
|
|
|
|
return "RELEASE." + releaseTime.Format(minioReleaseTagTimeLayout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// releaseTagToReleaseTime - reverse of `releaseTimeToReleaseTag()`
|
|
|
|
func releaseTagToReleaseTime(releaseTag string) (releaseTime time.Time, err error) {
|
2019-08-28 18:04:43 -04:00
|
|
|
fields := strings.Split(releaseTag, ".")
|
|
|
|
if len(fields) < 2 || len(fields) > 3 {
|
2017-10-09 19:12:13 -04:00
|
|
|
return releaseTime, fmt.Errorf("%s is not a valid release tag", releaseTag)
|
|
|
|
}
|
2019-08-28 18:04:43 -04:00
|
|
|
if fields[0] != "RELEASE" {
|
|
|
|
return releaseTime, fmt.Errorf("%s is not a valid release tag", releaseTag)
|
|
|
|
}
|
|
|
|
return time.Parse(minioReleaseTagTimeLayout, fields[1])
|
2017-10-09 19:12:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// getModTime - get the file modification time of `path`
|
|
|
|
func getModTime(path string) (t time.Time, err error) {
|
|
|
|
// Convert to absolute path
|
|
|
|
absPath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
2019-12-02 12:28:01 -05:00
|
|
|
return t, fmt.Errorf("Unable to get absolute path of %s. %w", path, err)
|
2017-02-21 04:32:05 -05:00
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
// Version is minio non-standard, we will use minio binary's
|
|
|
|
// ModTime as release time.
|
|
|
|
fi, err := os.Stat(absPath)
|
2016-04-29 17:24:10 -04:00
|
|
|
if err != nil {
|
2019-12-02 12:28:01 -05:00
|
|
|
return t, fmt.Errorf("Unable to get ModTime of %s. %w", absPath, err)
|
2015-12-22 20:11:11 -05:00
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
// Return the ModTime
|
|
|
|
return fi.ModTime().UTC(), nil
|
2015-12-22 20:11:11 -05:00
|
|
|
}
|
|
|
|
|
2017-10-09 19:12:13 -04: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.
|
2017-02-15 03:31:00 -05:00
|
|
|
func GetCurrentReleaseTime() (releaseTime time.Time, err error) {
|
2017-10-09 19:12:13 -04:00
|
|
|
if releaseTime, err = minioVersionToReleaseTime(Version); err == nil {
|
|
|
|
return releaseTime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks like version is minio non-standard, we use minio
|
|
|
|
// binary's ModTime as release time:
|
|
|
|
return getModTime(os.Args[0])
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
2016-10-12 21:09:08 -04:00
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
// IsDocker - returns if the environment minio is running in docker or
|
|
|
|
// not. The check is a simple file existence check.
|
|
|
|
//
|
2017-06-12 20:33:21 -04:00
|
|
|
// https://github.com/moby/moby/blob/master/daemon/initlayer/setup_unix.go#L25
|
|
|
|
//
|
|
|
|
// "/.dockerenv": "file",
|
|
|
|
//
|
2017-10-09 19:12:13 -04:00
|
|
|
func IsDocker() bool {
|
2020-05-22 11:40:59 -04:00
|
|
|
if env.Get("MINIO_CI_CD", "") == "" {
|
2019-12-10 23:28:22 -05:00
|
|
|
_, err := os.Stat("/.dockerenv")
|
2020-11-23 11:36:49 -05:00
|
|
|
if osIsNotExist(err) {
|
2019-12-10 23:28:22 -05:00
|
|
|
return false
|
|
|
|
}
|
2016-10-12 21:09:08 -04:00
|
|
|
|
2019-12-10 23:28:22 -05:00
|
|
|
// Log error, as we will not propagate it to caller
|
2020-04-09 12:30:02 -04:00
|
|
|
logger.LogIf(GlobalContext, err)
|
2017-02-15 03:31:00 -05:00
|
|
|
|
2019-12-10 23:28:22 -05:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
return false
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:33:21 -04:00
|
|
|
// IsDCOS returns true if minio is running in DCOS.
|
|
|
|
func IsDCOS() bool {
|
2020-05-22 11:40:59 -04:00
|
|
|
if env.Get("MINIO_CI_CD", "") == "" {
|
2019-12-10 23:28:22 -05:00
|
|
|
// http://mesos.apache.org/documentation/latest/docker-containerizer/
|
|
|
|
// Mesos docker containerizer sets this value
|
|
|
|
return env.Get("MESOS_CONTAINER_NAME", "") != ""
|
|
|
|
}
|
|
|
|
return false
|
2017-06-12 20:33:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsKubernetes returns true if minio is running in kubernetes.
|
2017-06-09 05:42:12 -04:00
|
|
|
func IsKubernetes() bool {
|
2020-05-22 11:40:59 -04:00
|
|
|
if env.Get("MINIO_CI_CD", "") == "" {
|
2019-12-10 23:28:22 -05:00
|
|
|
// Kubernetes env used to validate if we are
|
|
|
|
// indeed running inside a kubernetes pod
|
2020-09-09 20:21:39 -04:00
|
|
|
// is KUBERNETES_SERVICE_HOST
|
|
|
|
// https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/kubelet_pods.go#L541
|
2019-12-10 23:28:22 -05:00
|
|
|
return env.Get("KUBERNETES_SERVICE_HOST", "") != ""
|
|
|
|
}
|
|
|
|
return false
|
2017-06-09 05:42:12 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:53:45 -04:00
|
|
|
// IsBOSH returns true if minio is deployed from a bosh package
|
|
|
|
func IsBOSH() bool {
|
|
|
|
// "/var/vcap/bosh" exists in BOSH deployed instance.
|
|
|
|
_, err := os.Stat("/var/vcap/bosh")
|
2020-11-23 11:36:49 -05:00
|
|
|
if osIsNotExist(err) {
|
2017-10-26 21:53:45 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log error, as we will not propagate it to caller
|
2020-04-09 12:30:02 -04:00
|
|
|
logger.LogIf(GlobalContext, err)
|
2017-10-26 21:53:45 -04:00
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// MinIO Helm chart uses DownwardAPIFile to write pod label info to /podinfo/labels
|
2017-06-24 16:17:28 -04:00
|
|
|
// More info: https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#store-pod-fields
|
|
|
|
// Check if this is Helm package installation and report helm chart version
|
|
|
|
func getHelmVersion(helmInfoFilePath string) string {
|
|
|
|
// Read the file exists.
|
|
|
|
helmInfoFile, err := os.Open(helmInfoFilePath)
|
2017-10-09 19:12:13 -04:00
|
|
|
if err != nil {
|
2019-04-09 14:39:42 -04:00
|
|
|
// Log errors and return "" as MinIO can be deployed
|
2017-10-09 19:12:13 -04:00
|
|
|
// without Helm charts as well.
|
2020-11-23 11:36:49 -05:00
|
|
|
if !osIsNotExist(err) {
|
2018-04-05 18:04:40 -04:00
|
|
|
reqInfo := (&logger.ReqInfo{}).AppendTags("helmInfoFilePath", helmInfoFilePath)
|
2020-04-09 12:30:02 -04:00
|
|
|
ctx := logger.SetReqInfo(GlobalContext, reqInfo)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2017-10-09 19:12:13 -04:00
|
|
|
}
|
2017-06-24 16:17:28 -04:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(helmInfoFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
if strings.Contains(scanner.Text(), "chart=") {
|
|
|
|
helmChartVersion := strings.TrimPrefix(scanner.Text(), "chart=")
|
|
|
|
// remove quotes from the chart version
|
|
|
|
return strings.Trim(helmChartVersion, `"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
// IsSourceBuild - returns if this binary is a non-official build from
|
|
|
|
// source code.
|
2017-02-15 03:31:00 -05:00
|
|
|
func IsSourceBuild() bool {
|
2017-10-09 19:12:13 -04:00
|
|
|
_, err := minioVersionToReleaseTime(Version)
|
|
|
|
return err != nil
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
2016-03-24 20:20:49 -04:00
|
|
|
|
2021-04-08 20:51:59 -04:00
|
|
|
// IsPCFTile returns if server is running in PCF
|
|
|
|
func IsPCFTile() bool {
|
|
|
|
return env.Get("MINIO_PCF_TILE_VERSION", "") != ""
|
|
|
|
}
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
// DO NOT CHANGE USER AGENT STYLE.
|
|
|
|
// The style should be
|
2017-06-09 05:42:12 -04:00
|
|
|
//
|
2019-04-09 14:39:42 -04:00
|
|
|
// MinIO (<OS>; <ARCH>[; <MODE>][; dcos][; kubernetes][; docker][; source]) MinIO/<VERSION> MinIO/<RELEASE-TAG> MinIO/<COMMIT-ID> [MinIO/universe-<PACKAGE-NAME>] [MinIO/helm-<HELM-VERSION>]
|
2017-02-15 03:31:00 -05:00
|
|
|
//
|
2017-10-09 19:12:13 -04:00
|
|
|
// Any change here should be discussed by opening an issue at
|
|
|
|
// https://github.com/minio/minio/issues.
|
2017-03-16 15:21:58 -04:00
|
|
|
func getUserAgent(mode string) string {
|
2017-10-09 19:12:13 -04:00
|
|
|
|
|
|
|
userAgentParts := []string{}
|
|
|
|
// Helper function to concisely append a pair of strings to a
|
|
|
|
// the user-agent slice.
|
|
|
|
uaAppend := func(p, q string) {
|
|
|
|
userAgentParts = append(userAgentParts, p, q)
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
uaAppend("MinIO (", runtime.GOOS)
|
2017-10-09 19:12:13 -04:00
|
|
|
uaAppend("; ", runtime.GOARCH)
|
2017-03-16 15:21:58 -04:00
|
|
|
if mode != "" {
|
2017-10-09 19:12:13 -04:00
|
|
|
uaAppend("; ", mode)
|
2017-03-16 15:21:58 -04:00
|
|
|
}
|
2017-06-12 20:33:21 -04:00
|
|
|
if IsDCOS() {
|
2017-10-09 19:12:13 -04:00
|
|
|
uaAppend("; ", "dcos")
|
2017-06-12 20:33:21 -04:00
|
|
|
}
|
2017-06-09 05:42:12 -04:00
|
|
|
if IsKubernetes() {
|
2017-10-09 19:12:13 -04:00
|
|
|
uaAppend("; ", "kubernetes")
|
2017-06-09 05:42:12 -04:00
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
if IsDocker() {
|
2017-10-09 19:12:13 -04:00
|
|
|
uaAppend("; ", "docker")
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
2017-10-26 21:53:45 -04:00
|
|
|
if IsBOSH() {
|
|
|
|
uaAppend("; ", "bosh")
|
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
if IsSourceBuild() {
|
2017-10-09 19:12:13 -04:00
|
|
|
uaAppend("; ", "source")
|
2016-03-24 20:20:49 -04:00
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
uaAppend(") MinIO/", Version)
|
|
|
|
uaAppend(" MinIO/", ReleaseTag)
|
|
|
|
uaAppend(" MinIO/", CommitID)
|
2017-06-12 20:33:21 -04:00
|
|
|
if IsDCOS() {
|
2019-10-04 13:35:33 -04:00
|
|
|
universePkgVersion := env.Get("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION", "")
|
2017-06-12 20:33:21 -04:00
|
|
|
// On DC/OS environment try to the get universe package version.
|
|
|
|
if universePkgVersion != "" {
|
2019-04-09 14:39:42 -04:00
|
|
|
uaAppend(" MinIO/universe-", universePkgVersion)
|
2017-06-12 20:33:21 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-24 16:17:28 -04:00
|
|
|
|
|
|
|
if IsKubernetes() {
|
|
|
|
// In Kubernetes environment, try to fetch the helm package version
|
|
|
|
helmChartVersion := getHelmVersion("/podinfo/labels")
|
|
|
|
if helmChartVersion != "" {
|
2019-04-09 14:39:42 -04:00
|
|
|
uaAppend(" MinIO/helm-", helmChartVersion)
|
2017-06-24 16:17:28 -04:00
|
|
|
}
|
2020-09-09 20:21:39 -04:00
|
|
|
// In Kubernetes environment, try to fetch the Operator, VSPHERE plugin version
|
|
|
|
opVersion := env.Get("MINIO_OPERATOR_VERSION", "")
|
|
|
|
if opVersion != "" {
|
|
|
|
uaAppend(" MinIO/operator-", opVersion)
|
|
|
|
}
|
|
|
|
vsphereVersion := env.Get("MINIO_VSPHERE_PLUGIN_VERSION", "")
|
|
|
|
if vsphereVersion != "" {
|
|
|
|
uaAppend(" MinIO/vsphere-plugin-", vsphereVersion)
|
|
|
|
}
|
2017-06-24 16:17:28 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 20:51:59 -04:00
|
|
|
if IsPCFTile() {
|
|
|
|
pcfTileVersion := env.Get("MINIO_PCF_TILE_VERSION", "")
|
|
|
|
if pcfTileVersion != "" {
|
|
|
|
uaAppend(" MinIO/pcf-tile-", pcfTileVersion)
|
|
|
|
}
|
2017-10-26 21:53:45 -04:00
|
|
|
}
|
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
return strings.Join(userAgentParts, "")
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
2016-03-24 20:20:49 -04:00
|
|
|
|
2020-07-23 11:03:31 -04:00
|
|
|
func downloadReleaseURL(u *url.URL, timeout time.Duration, mode string) (content string, err error) {
|
|
|
|
var reader io.ReadCloser
|
|
|
|
if u.Scheme == "https" || u.Scheme == "http" {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return content, AdminError{
|
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
req.Header.Set("User-Agent", getUserAgent(mode))
|
2016-03-24 20:20:49 -04:00
|
|
|
|
2020-07-23 11:03:31 -04:00
|
|
|
client := &http.Client{Transport: getUpdateTransport(timeout)}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2020-10-29 12:52:11 -04:00
|
|
|
if xnet.IsNetworkOrHostDown(err, false) {
|
2020-07-23 11:03:31 -04:00
|
|
|
return content, AdminError{
|
|
|
|
Code: AdminUpdateURLNotReachable,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusServiceUnavailable,
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
return content, AdminError{
|
2020-07-23 11:03:31 -04:00
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
2019-08-27 14:37:47 -04:00
|
|
|
Message: err.Error(),
|
2020-07-23 11:03:31 -04:00
|
|
|
StatusCode: http.StatusInternalServerError,
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
if resp == nil {
|
|
|
|
return content, AdminError{
|
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: fmt.Sprintf("No response from server to download URL %s", u),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
reader = resp.Body
|
|
|
|
defer xhttp.DrainBody(resp.Body)
|
2016-08-19 11:09:05 -04:00
|
|
|
|
2020-07-23 11:03:31 -04:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return content, AdminError{
|
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: fmt.Sprintf("Error downloading URL %s. Response: %v", u, resp.Status),
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reader, err = os.Open(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return content, AdminError{
|
|
|
|
Code: AdminUpdateURLNotReachable,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusServiceUnavailable,
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
|
|
|
|
contentBytes, err := ioutil.ReadAll(reader)
|
2016-10-12 21:09:08 -04:00
|
|
|
if err != nil {
|
2019-08-27 14:37:47 -04:00
|
|
|
return content, AdminError{
|
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: fmt.Sprintf("Error reading response. %s", err),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
2016-10-12 21:09:08 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
return string(contentBytes), nil
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
2016-10-24 16:44:15 -04:00
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
// parseReleaseData - parses release info file content fetched from
|
|
|
|
// official minio download server.
|
|
|
|
//
|
|
|
|
// The expected format is a single line with two words like:
|
|
|
|
//
|
2019-08-28 18:04:43 -04:00
|
|
|
// fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z.<hotfix_optional>
|
2017-10-09 19:12:13 -04:00
|
|
|
//
|
|
|
|
// The second word must be `minio.` appended to a standard release tag.
|
2021-04-08 12:51:11 -04:00
|
|
|
func parseReleaseData(data string) (sha256Sum []byte, releaseTime time.Time, releaseInfo string, err error) {
|
2019-08-28 18:04:43 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = AdminError{
|
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-02-15 03:31:00 -05:00
|
|
|
fields := strings.Fields(data)
|
|
|
|
if len(fields) != 2 {
|
|
|
|
err = fmt.Errorf("Unknown release data `%s`", data)
|
2021-04-08 12:51:11 -04:00
|
|
|
return sha256Sum, releaseTime, releaseInfo, err
|
2020-07-23 11:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sha256Sum, err = hex.DecodeString(fields[0])
|
|
|
|
if err != nil {
|
2021-04-08 12:51:11 -04:00
|
|
|
return sha256Sum, releaseTime, releaseInfo, err
|
2015-10-17 18:03:46 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:51:11 -04:00
|
|
|
releaseInfo = fields[1]
|
2017-10-09 19:12:13 -04:00
|
|
|
|
2019-08-28 18:04:43 -04:00
|
|
|
// Split release of style minio.RELEASE.2019-08-21T19-40-07Z.<hotfix>
|
|
|
|
nfields := strings.SplitN(releaseInfo, ".", 2)
|
|
|
|
if len(nfields) != 2 {
|
2017-02-15 03:31:00 -05:00
|
|
|
err = fmt.Errorf("Unknown release information `%s`", releaseInfo)
|
2021-04-08 12:51:11 -04:00
|
|
|
return sha256Sum, releaseTime, releaseInfo, err
|
2016-03-24 20:20:49 -04:00
|
|
|
}
|
2019-08-28 18:04:43 -04:00
|
|
|
if nfields[0] != "minio" {
|
2017-10-09 19:12:13 -04:00
|
|
|
err = fmt.Errorf("Unknown release `%s`", releaseInfo)
|
2021-04-08 12:51:11 -04:00
|
|
|
return sha256Sum, releaseTime, releaseInfo, err
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
|
|
|
|
2019-08-28 18:04:43 -04:00
|
|
|
releaseTime, err = releaseTagToReleaseTime(nfields[1])
|
2017-02-15 03:31:00 -05:00
|
|
|
if err != nil {
|
2019-12-02 12:28:01 -05:00
|
|
|
err = fmt.Errorf("Unknown release tag format. %w", err)
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:51:11 -04:00
|
|
|
return sha256Sum, releaseTime, releaseInfo, err
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
func getUpdateTransport(timeout time.Duration) http.RoundTripper {
|
|
|
|
var updateTransport http.RoundTripper = &http.Transport{
|
2020-07-10 15:08:14 -04:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2020-07-03 13:03:41 -04:00
|
|
|
DialContext: xhttp.NewCustomDialContext(timeout),
|
2019-08-27 14:37:47 -04:00
|
|
|
IdleConnTimeout: timeout,
|
|
|
|
TLSHandshakeTimeout: timeout,
|
|
|
|
ExpectContinueTimeout: timeout,
|
2020-06-03 16:18:54 -04:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: globalRootCAs,
|
|
|
|
},
|
|
|
|
DisableCompression: true,
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
|
|
|
return updateTransport
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:03:31 -04:00
|
|
|
func getLatestReleaseTime(u *url.URL, timeout time.Duration, mode string) (sha256Sum []byte, releaseTime time.Time, err error) {
|
|
|
|
data, err := downloadReleaseURL(u, timeout, mode)
|
2016-08-19 11:09:05 -04:00
|
|
|
if err != nil {
|
2020-07-23 11:03:31 -04:00
|
|
|
return sha256Sum, releaseTime, err
|
2016-03-24 20:20:49 -04:00
|
|
|
}
|
2016-08-19 11:09:05 -04:00
|
|
|
|
2021-04-08 12:51:11 -04:00
|
|
|
sha256Sum, releaseTime, _, err = parseReleaseData(data)
|
|
|
|
return
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:33:21 -04:00
|
|
|
const (
|
|
|
|
// Kubernetes deployment doc link.
|
2019-04-09 14:39:42 -04:00
|
|
|
kubernetesDeploymentDoc = "https://docs.min.io/docs/deploy-minio-on-kubernetes"
|
2017-06-12 20:33:21 -04:00
|
|
|
|
|
|
|
// Mesos deployment doc link.
|
2019-04-09 14:39:42 -04:00
|
|
|
mesosDeploymentDoc = "https://docs.min.io/docs/deploy-minio-on-dc-os"
|
2017-06-12 20:33:21 -04:00
|
|
|
)
|
2017-06-09 05:42:12 -04:00
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
func getDownloadURL(releaseTag string) (downloadURL string) {
|
2017-06-12 20:33:21 -04:00
|
|
|
// Check if we are in DCOS environment, return
|
|
|
|
// deployment guide for update procedures.
|
|
|
|
if IsDCOS() {
|
|
|
|
return mesosDeploymentDoc
|
|
|
|
}
|
|
|
|
|
2017-06-09 05:42:12 -04:00
|
|
|
// Check if we are in kubernetes environment, return
|
|
|
|
// deployment guide for update procedures.
|
|
|
|
if IsKubernetes() {
|
|
|
|
return kubernetesDeploymentDoc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are docker environment, return docker update command
|
2017-02-15 03:31:00 -05:00
|
|
|
if IsDocker() {
|
2017-06-09 05:42:12 -04:00
|
|
|
// Construct release tag name.
|
2021-09-01 21:48:26 -04:00
|
|
|
return fmt.Sprintf("podman pull quay.io/minio/minio:%s", releaseTag)
|
2017-02-15 03:31:00 -05:00
|
|
|
}
|
|
|
|
|
2017-10-09 19:12:13 -04:00
|
|
|
// For binary only installations, we return link to the latest binary.
|
2017-02-15 03:31:00 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return minioReleaseURL + "minio.exe"
|
|
|
|
}
|
|
|
|
|
|
|
|
return minioReleaseURL + "minio"
|
|
|
|
}
|
2015-10-17 18:03:46 -04:00
|
|
|
|
2020-07-23 11:03:31 -04:00
|
|
|
func getUpdateReaderFromFile(u *url.URL) (io.ReadCloser, error) {
|
|
|
|
r, err := os.Open(u.Path)
|
2017-02-15 03:31:00 -05:00
|
|
|
if err != nil {
|
2020-07-23 11:03:31 -04:00
|
|
|
return nil, AdminError{
|
2019-08-28 18:04:43 -04:00
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
return r, nil
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
|
2020-07-23 11:03:31 -04:00
|
|
|
func getUpdateReaderFromURL(u *url.URL, transport http.RoundTripper, mode string) (io.ReadCloser, error) {
|
2020-06-03 16:18:54 -04:00
|
|
|
clnt := &http.Client{
|
2020-07-23 11:03:31 -04:00
|
|
|
Transport: transport,
|
2020-06-03 16:18:54 -04:00
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
2019-08-27 14:37:47 -04:00
|
|
|
if err != nil {
|
2020-07-23 11:03:31 -04:00
|
|
|
return nil, AdminError{
|
2019-08-27 14:37:47 -04:00
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
2017-12-15 15:33:42 -05:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
req.Header.Set("User-Agent", getUserAgent(mode))
|
|
|
|
|
|
|
|
resp, err := clnt.Do(req)
|
2017-12-15 15:33:42 -05:00
|
|
|
if err != nil {
|
2020-10-29 12:52:11 -04:00
|
|
|
if xnet.IsNetworkOrHostDown(err, false) {
|
2020-07-23 11:03:31 -04:00
|
|
|
return nil, AdminError{
|
2019-08-27 14:37:47 -04:00
|
|
|
Code: AdminUpdateURLNotReachable,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusServiceUnavailable,
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
return nil, AdminError{
|
2019-08-27 14:37:47 -04:00
|
|
|
Code: AdminUpdateUnexpectedFailure,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
2017-12-15 15:33:42 -05:00
|
|
|
}
|
2020-07-23 11:03:31 -04:00
|
|
|
return resp.Body, nil
|
|
|
|
}
|
2017-12-15 15:33:42 -05:00
|
|
|
|
2021-04-08 12:51:11 -04:00
|
|
|
func doUpdate(u *url.URL, lrTime time.Time, sha256Sum []byte, releaseInfo string, mode string) (err error) {
|
2020-07-23 11:03:31 -04:00
|
|
|
transport := getUpdateTransport(30 * time.Second)
|
|
|
|
var reader io.ReadCloser
|
|
|
|
if u.Scheme == "https" || u.Scheme == "http" {
|
|
|
|
reader, err = getUpdateReaderFromURL(u, transport, mode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reader, err = getUpdateReaderFromFile(u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := selfupdate.Options{
|
|
|
|
Hash: crypto.SHA256,
|
|
|
|
Checksum: sha256Sum,
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:57:21 -04:00
|
|
|
if err := opts.CheckPermissions(); err != nil {
|
|
|
|
return AdminError{
|
|
|
|
Code: AdminUpdateApplyFailure,
|
|
|
|
Message: fmt.Sprintf("server update failed with: %s, do not restart the servers yet", err),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:03:31 -04:00
|
|
|
minisignPubkey := env.Get(envMinisignPubKey, "")
|
|
|
|
if minisignPubkey != "" {
|
|
|
|
v := selfupdate.NewVerifier()
|
2021-04-08 12:51:11 -04:00
|
|
|
u.Path = path.Dir(u.Path) + slashSeparator + releaseInfo + ".minisig"
|
2020-07-23 11:03:31 -04:00
|
|
|
if err = v.LoadFromURL(u.String(), minisignPubkey, transport); err != nil {
|
|
|
|
return AdminError{
|
|
|
|
Code: AdminUpdateApplyFailure,
|
|
|
|
Message: fmt.Sprintf("signature loading failed for %v with %v", u, err),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opts.Verifier = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = selfupdate.Apply(reader, opts); err != nil {
|
|
|
|
if rerr := selfupdate.RollbackError(err); rerr != nil {
|
2019-08-27 14:37:47 -04:00
|
|
|
return AdminError{
|
|
|
|
Code: AdminUpdateApplyFailure,
|
2019-09-12 16:03:42 -04:00
|
|
|
Message: fmt.Sprintf("Failed to rollback from bad update: %v", rerr),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var pathErr *os.PathError
|
|
|
|
if errors.As(err, &pathErr) {
|
|
|
|
return AdminError{
|
|
|
|
Code: AdminUpdateApplyFailure,
|
|
|
|
Message: fmt.Sprintf("Unable to update the binary at %s: %v",
|
|
|
|
filepath.Dir(pathErr.Path), pathErr.Err),
|
2019-08-27 14:37:47 -04:00
|
|
|
StatusCode: http.StatusForbidden,
|
|
|
|
}
|
2019-07-08 20:46:04 -04:00
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
return AdminError{
|
|
|
|
Code: AdminUpdateApplyFailure,
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
2017-12-15 15:33:42 -05:00
|
|
|
}
|
|
|
|
}
|
2019-08-27 14:37:47 -04:00
|
|
|
|
|
|
|
return nil
|
2015-10-17 18:03:46 -04:00
|
|
|
}
|