You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
540 lines
15 KiB
540 lines
15 KiB
9 years ago
|
/*
|
||
6 years ago
|
* MinIO Cloud Storage, (C) 2015, 2016, 2017 MinIO, Inc.
|
||
9 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
8 years ago
|
"bufio"
|
||
7 years ago
|
"context"
|
||
7 years ago
|
"crypto"
|
||
|
"encoding/hex"
|
||
8 years ago
|
"fmt"
|
||
9 years ago
|
"io/ioutil"
|
||
5 years ago
|
"net"
|
||
9 years ago
|
"net/http"
|
||
8 years ago
|
"os"
|
||
8 years ago
|
"path/filepath"
|
||
9 years ago
|
"runtime"
|
||
9 years ago
|
"strings"
|
||
9 years ago
|
"time"
|
||
|
|
||
7 years ago
|
"github.com/inconshreveable/go-update"
|
||
6 years ago
|
xhttp "github.com/minio/minio/cmd/http"
|
||
7 years ago
|
"github.com/minio/minio/cmd/logger"
|
||
7 years ago
|
_ "github.com/minio/sha256-simd" // Needed for sha256 hash verifier.
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
const (
|
||
|
minioReleaseTagTimeLayout = "2006-01-02T15-04-05Z"
|
||
7 years ago
|
minioOSARCH = runtime.GOOS + "-" + runtime.GOARCH
|
||
5 years ago
|
minioReleaseURL = "https://dl.min.io/server/minio/release/" + minioOSARCH + SlashSeparator
|
||
8 years ago
|
)
|
||
8 years ago
|
|
||
7 years ago
|
var (
|
||
|
// Newer official download info URLs appear earlier below.
|
||
|
minioReleaseInfoURLs = []string{
|
||
|
minioReleaseURL + "minio.sha256sum",
|
||
|
minioReleaseURL + "minio.shasum",
|
||
9 years ago
|
}
|
||
7 years ago
|
|
||
|
// For windows our files have .exe additionally.
|
||
|
minioReleaseWindowsInfoURLs = []string{
|
||
|
minioReleaseURL + "minio.exe.sha256sum",
|
||
|
minioReleaseURL + "minio.exe.shasum",
|
||
|
}
|
||
7 years ago
|
)
|
||
9 years ago
|
|
||
7 years ago
|
// minioVersionToReleaseTime - parses a standard official release
|
||
6 years ago
|
// MinIO version string.
|
||
7 years ago
|
//
|
||
|
// 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
|
||
6 years ago
|
// an official MinIO release tag.
|
||
7 years ago
|
//
|
||
|
// 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) {
|
||
5 years ago
|
fields := strings.Split(releaseTag, ".")
|
||
|
if len(fields) < 2 || len(fields) > 3 {
|
||
7 years ago
|
return releaseTime, fmt.Errorf("%s is not a valid release tag", releaseTag)
|
||
|
}
|
||
5 years ago
|
if fields[0] != "RELEASE" {
|
||
|
return releaseTime, fmt.Errorf("%s is not a valid release tag", releaseTag)
|
||
|
}
|
||
|
return time.Parse(minioReleaseTagTimeLayout, fields[1])
|
||
7 years ago
|
}
|
||
|
|
||
|
// 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 {
|
||
|
return t, fmt.Errorf("Unable to get absolute path of %s. %s", path, err)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// Version is minio non-standard, we will use minio binary's
|
||
|
// ModTime as release time.
|
||
|
fi, err := os.Stat(absPath)
|
||
9 years ago
|
if err != nil {
|
||
7 years ago
|
return t, fmt.Errorf("Unable to get ModTime of %s. %s", absPath, err)
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
// Return the ModTime
|
||
|
return fi.ModTime().UTC(), nil
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
// 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.
|
||
8 years ago
|
func GetCurrentReleaseTime() (releaseTime time.Time, err error) {
|
||
7 years ago
|
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])
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
// IsDocker - returns if the environment minio is running in docker or
|
||
|
// not. The check is a simple file existence check.
|
||
|
//
|
||
8 years ago
|
// https://github.com/moby/moby/blob/master/daemon/initlayer/setup_unix.go#L25
|
||
|
//
|
||
|
// "/.dockerenv": "file",
|
||
|
//
|
||
7 years ago
|
func IsDocker() bool {
|
||
7 years ago
|
_, err := os.Stat("/.dockerenv")
|
||
7 years ago
|
if os.IsNotExist(err) {
|
||
|
return false
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// Log error, as we will not propagate it to caller
|
||
7 years ago
|
logger.LogIf(context.Background(), err)
|
||
8 years ago
|
|
||
7 years ago
|
return err == nil
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// IsDCOS returns true if minio is running in DCOS.
|
||
|
func IsDCOS() bool {
|
||
|
// http://mesos.apache.org/documentation/latest/docker-containerizer/
|
||
|
// Mesos docker containerizer sets this value
|
||
|
return os.Getenv("MESOS_CONTAINER_NAME") != ""
|
||
|
}
|
||
|
|
||
|
// IsKubernetes returns true if minio is running in kubernetes.
|
||
8 years ago
|
func IsKubernetes() bool {
|
||
|
// Kubernetes env used to validate if we are
|
||
|
// indeed running inside a kubernetes pod
|
||
|
// is KUBERNETES_SERVICE_HOST but in future
|
||
|
// we might need to enhance this.
|
||
|
return os.Getenv("KUBERNETES_SERVICE_HOST") != ""
|
||
|
}
|
||
|
|
||
7 years ago
|
// 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")
|
||
|
if os.IsNotExist(err) {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// Log error, as we will not propagate it to caller
|
||
7 years ago
|
logger.LogIf(context.Background(), err)
|
||
7 years ago
|
|
||
|
return err == nil
|
||
|
}
|
||
|
|
||
6 years ago
|
// MinIO Helm chart uses DownwardAPIFile to write pod label info to /podinfo/labels
|
||
8 years ago
|
// 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)
|
||
7 years ago
|
if err != nil {
|
||
6 years ago
|
// Log errors and return "" as MinIO can be deployed
|
||
7 years ago
|
// without Helm charts as well.
|
||
|
if !os.IsNotExist(err) {
|
||
7 years ago
|
reqInfo := (&logger.ReqInfo{}).AppendTags("helmInfoFilePath", helmInfoFilePath)
|
||
|
ctx := logger.SetReqInfo(context.Background(), reqInfo)
|
||
|
logger.LogIf(ctx, err)
|
||
7 years ago
|
}
|
||
8 years ago
|
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 ""
|
||
|
}
|
||
|
|
||
7 years ago
|
// IsSourceBuild - returns if this binary is a non-official build from
|
||
|
// source code.
|
||
8 years ago
|
func IsSourceBuild() bool {
|
||
7 years ago
|
_, err := minioVersionToReleaseTime(Version)
|
||
|
return err != nil
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// DO NOT CHANGE USER AGENT STYLE.
|
||
|
// The style should be
|
||
8 years ago
|
//
|
||
6 years ago
|
// MinIO (<OS>; <ARCH>[; <MODE>][; dcos][; kubernetes][; docker][; source]) MinIO/<VERSION> MinIO/<RELEASE-TAG> MinIO/<COMMIT-ID> [MinIO/universe-<PACKAGE-NAME>] [MinIO/helm-<HELM-VERSION>]
|
||
8 years ago
|
//
|
||
7 years ago
|
// Any change here should be discussed by opening an issue at
|
||
|
// https://github.com/minio/minio/issues.
|
||
8 years ago
|
func getUserAgent(mode string) string {
|
||
7 years ago
|
|
||
|
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)
|
||
|
}
|
||
|
|
||
6 years ago
|
uaAppend("MinIO (", runtime.GOOS)
|
||
7 years ago
|
uaAppend("; ", runtime.GOARCH)
|
||
8 years ago
|
if mode != "" {
|
||
7 years ago
|
uaAppend("; ", mode)
|
||
8 years ago
|
}
|
||
8 years ago
|
if IsDCOS() {
|
||
7 years ago
|
uaAppend("; ", "dcos")
|
||
8 years ago
|
}
|
||
8 years ago
|
if IsKubernetes() {
|
||
7 years ago
|
uaAppend("; ", "kubernetes")
|
||
8 years ago
|
}
|
||
8 years ago
|
if IsDocker() {
|
||
7 years ago
|
uaAppend("; ", "docker")
|
||
8 years ago
|
}
|
||
7 years ago
|
if IsBOSH() {
|
||
|
uaAppend("; ", "bosh")
|
||
|
}
|
||
8 years ago
|
if IsSourceBuild() {
|
||
7 years ago
|
uaAppend("; ", "source")
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
6 years ago
|
uaAppend(") MinIO/", Version)
|
||
|
uaAppend(" MinIO/", ReleaseTag)
|
||
|
uaAppend(" MinIO/", CommitID)
|
||
8 years ago
|
if IsDCOS() {
|
||
|
universePkgVersion := os.Getenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION")
|
||
|
// On DC/OS environment try to the get universe package version.
|
||
|
if universePkgVersion != "" {
|
||
6 years ago
|
uaAppend(" MinIO/universe-", universePkgVersion)
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
if IsKubernetes() {
|
||
|
// In Kubernetes environment, try to fetch the helm package version
|
||
|
helmChartVersion := getHelmVersion("/podinfo/labels")
|
||
|
if helmChartVersion != "" {
|
||
6 years ago
|
uaAppend(" MinIO/helm-", helmChartVersion)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
pcfTileVersion := os.Getenv("MINIO_PCF_TILE_VERSION")
|
||
|
if pcfTileVersion != "" {
|
||
6 years ago
|
uaAppend(" MinIO/pcf-tile-", pcfTileVersion)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
return strings.Join(userAgentParts, "")
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
7 years ago
|
func downloadReleaseURL(releaseChecksumURL string, timeout time.Duration, mode string) (content string, err error) {
|
||
5 years ago
|
req, err := http.NewRequest(http.MethodGet, releaseChecksumURL, nil)
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
return content, AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
req.Header.Set("User-Agent", getUserAgent(mode))
|
||
9 years ago
|
|
||
5 years ago
|
client := &http.Client{Transport: getUpdateTransport(timeout)}
|
||
8 years ago
|
resp, err := client.Do(req)
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
if isNetworkOrHostDown(err) {
|
||
|
return content, AdminError{
|
||
|
Code: AdminUpdateURLNotReachable,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusServiceUnavailable,
|
||
|
}
|
||
|
}
|
||
|
return content, AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
8 years ago
|
}
|
||
|
if resp == nil {
|
||
5 years ago
|
return content, AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: fmt.Sprintf("No response from server to download URL %s", releaseChecksumURL),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
8 years ago
|
}
|
||
6 years ago
|
defer xhttp.DrainBody(resp.Body)
|
||
8 years ago
|
|
||
8 years ago
|
if resp.StatusCode != http.StatusOK {
|
||
5 years ago
|
return content, AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: fmt.Sprintf("Error downloading URL %s. Response: %v", releaseChecksumURL, resp.Status),
|
||
|
StatusCode: resp.StatusCode,
|
||
|
}
|
||
8 years ago
|
}
|
||
7 years ago
|
contentBytes, err := ioutil.ReadAll(resp.Body)
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
return content, AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: fmt.Sprintf("Error reading response. %s", err),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
return string(contentBytes), nil
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// DownloadReleaseData - downloads release data from minio official server.
|
||
8 years ago
|
func DownloadReleaseData(timeout time.Duration, mode string) (data string, err error) {
|
||
7 years ago
|
releaseURLs := minioReleaseInfoURLs
|
||
|
if runtime.GOOS == globalWindowsOSName {
|
||
|
releaseURLs = minioReleaseWindowsInfoURLs
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
7 years ago
|
return func() (data string, err error) {
|
||
|
for _, url := range releaseURLs {
|
||
|
data, err = downloadReleaseURL(url, timeout, mode)
|
||
|
if err == nil {
|
||
|
return data, nil
|
||
|
}
|
||
|
}
|
||
5 years ago
|
return data, err
|
||
7 years ago
|
}()
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
// parseReleaseData - parses release info file content fetched from
|
||
|
// official minio download server.
|
||
|
//
|
||
|
// The expected format is a single line with two words like:
|
||
|
//
|
||
5 years ago
|
// fbe246edbd382902db9a4035df7dce8cb441357d minio.RELEASE.2016-10-07T01-16-39Z.<hotfix_optional>
|
||
7 years ago
|
//
|
||
|
// The second word must be `minio.` appended to a standard release tag.
|
||
7 years ago
|
func parseReleaseData(data string) (sha256Hex string, releaseTime time.Time, err error) {
|
||
5 years ago
|
defer func() {
|
||
|
if err != nil {
|
||
|
err = AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
8 years ago
|
fields := strings.Fields(data)
|
||
|
if len(fields) != 2 {
|
||
|
err = fmt.Errorf("Unknown release data `%s`", data)
|
||
7 years ago
|
return sha256Hex, releaseTime, err
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
sha256Hex = fields[0]
|
||
8 years ago
|
releaseInfo := fields[1]
|
||
7 years ago
|
|
||
5 years ago
|
// Split release of style minio.RELEASE.2019-08-21T19-40-07Z.<hotfix>
|
||
|
nfields := strings.SplitN(releaseInfo, ".", 2)
|
||
|
if len(nfields) != 2 {
|
||
8 years ago
|
err = fmt.Errorf("Unknown release information `%s`", releaseInfo)
|
||
7 years ago
|
return sha256Hex, releaseTime, err
|
||
9 years ago
|
}
|
||
5 years ago
|
if nfields[0] != "minio" {
|
||
7 years ago
|
err = fmt.Errorf("Unknown release `%s`", releaseInfo)
|
||
7 years ago
|
return sha256Hex, releaseTime, err
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
releaseTime, err = releaseTagToReleaseTime(nfields[1])
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
err = fmt.Errorf("Unknown release tag format. %s", err)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
return sha256Hex, releaseTime, err
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
const updateTimeout = 10 * time.Second
|
||
|
|
||
|
func getUpdateTransport(timeout time.Duration) http.RoundTripper {
|
||
|
var updateTransport http.RoundTripper = &http.Transport{
|
||
|
Proxy: http.ProxyFromEnvironment,
|
||
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||
|
dialer := &net.Dialer{
|
||
|
Timeout: timeout,
|
||
|
KeepAlive: timeout,
|
||
|
DualStack: true,
|
||
|
}
|
||
|
return dialer.DialContext(ctx, network, addr)
|
||
|
},
|
||
|
IdleConnTimeout: timeout,
|
||
|
TLSHandshakeTimeout: timeout,
|
||
|
ExpectContinueTimeout: timeout,
|
||
|
DisableCompression: true,
|
||
|
}
|
||
|
return updateTransport
|
||
|
}
|
||
|
|
||
7 years ago
|
func getLatestReleaseTime(timeout time.Duration, mode string) (sha256Hex string, releaseTime time.Time, err error) {
|
||
8 years ago
|
data, err := DownloadReleaseData(timeout, mode)
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
return sha256Hex, releaseTime, err
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
return parseReleaseData(data)
|
||
|
}
|
||
|
|
||
8 years ago
|
const (
|
||
|
// Kubernetes deployment doc link.
|
||
6 years ago
|
kubernetesDeploymentDoc = "https://docs.min.io/docs/deploy-minio-on-kubernetes"
|
||
8 years ago
|
|
||
|
// Mesos deployment doc link.
|
||
6 years ago
|
mesosDeploymentDoc = "https://docs.min.io/docs/deploy-minio-on-dc-os"
|
||
8 years ago
|
)
|
||
8 years ago
|
|
||
7 years ago
|
func getDownloadURL(releaseTag string) (downloadURL string) {
|
||
8 years ago
|
// Check if we are in DCOS environment, return
|
||
|
// deployment guide for update procedures.
|
||
|
if IsDCOS() {
|
||
|
return mesosDeploymentDoc
|
||
|
}
|
||
|
|
||
8 years ago
|
// 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
|
||
8 years ago
|
if IsDocker() {
|
||
8 years ago
|
// Construct release tag name.
|
||
7 years ago
|
return fmt.Sprintf("docker pull minio/minio:%s", releaseTag)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// For binary only installations, we return link to the latest binary.
|
||
8 years ago
|
if runtime.GOOS == "windows" {
|
||
|
return minioReleaseURL + "minio.exe"
|
||
|
}
|
||
|
|
||
|
return minioReleaseURL + "minio"
|
||
|
}
|
||
9 years ago
|
|
||
7 years ago
|
func getUpdateInfo(timeout time.Duration, mode string) (updateMsg string, sha256Hex string, currentReleaseTime, latestReleaseTime time.Time, err error) {
|
||
8 years ago
|
currentReleaseTime, err = GetCurrentReleaseTime()
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
return updateMsg, sha256Hex, currentReleaseTime, latestReleaseTime, err
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
7 years ago
|
sha256Hex, latestReleaseTime, err = getLatestReleaseTime(timeout, mode)
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
return updateMsg, sha256Hex, currentReleaseTime, latestReleaseTime, err
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
var older time.Duration
|
||
|
var downloadURL string
|
||
8 years ago
|
if latestReleaseTime.After(currentReleaseTime) {
|
||
|
older = latestReleaseTime.Sub(currentReleaseTime)
|
||
7 years ago
|
downloadURL = getDownloadURL(releaseTimeToReleaseTag(latestReleaseTime))
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
7 years ago
|
return prepareUpdateMessage(downloadURL, older), sha256Hex, currentReleaseTime, latestReleaseTime, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func doUpdate(updateURL, sha256Hex, mode string) (err error) {
|
||
7 years ago
|
var sha256Sum []byte
|
||
|
sha256Sum, err = hex.DecodeString(sha256Hex)
|
||
|
if err != nil {
|
||
5 years ago
|
return AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
clnt := &http.Client{Transport: getUpdateTransport(30 * time.Second)}
|
||
5 years ago
|
req, err := http.NewRequest(http.MethodGet, updateURL, nil)
|
||
5 years ago
|
if err != nil {
|
||
|
return AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
req.Header.Set("User-Agent", getUserAgent(mode))
|
||
|
|
||
|
resp, err := clnt.Do(req)
|
||
7 years ago
|
if err != nil {
|
||
5 years ago
|
if isNetworkOrHostDown(err) {
|
||
|
return AdminError{
|
||
|
Code: AdminUpdateURLNotReachable,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusServiceUnavailable,
|
||
|
}
|
||
|
}
|
||
|
return AdminError{
|
||
|
Code: AdminUpdateUnexpectedFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
|
}
|
||
7 years ago
|
}
|
||
6 years ago
|
defer xhttp.DrainBody(resp.Body)
|
||
7 years ago
|
|
||
|
// FIXME: add support for gpg verification as well.
|
||
6 years ago
|
if err = update.Apply(resp.Body,
|
||
7 years ago
|
update.Options{
|
||
|
Hash: crypto.SHA256,
|
||
|
Checksum: sha256Sum,
|
||
|
},
|
||
6 years ago
|
); err != nil {
|
||
5 years ago
|
if os.IsPermission(err) {
|
||
|
return AdminError{
|
||
|
Code: AdminUpdateApplyFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusForbidden,
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
return AdminError{
|
||
|
Code: AdminUpdateApplyFailure,
|
||
|
Message: err.Error(),
|
||
|
StatusCode: http.StatusInternalServerError,
|
||
7 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
return nil
|
||
9 years ago
|
}
|