2020-04-01 12:59:34 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
// +build windows,!unittest
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/private/version"
|
|
|
|
"storj.io/storj/private/version/checker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// loopFunc is func that is run by the update cycle.
|
|
|
|
func loopFunc(ctx context.Context) error {
|
2020-06-08 22:25:30 +01:00
|
|
|
zap.L().Info("Downloading versions.", zap.String("Server Address", runCfg.Version.ServerAddress))
|
2020-04-01 12:59:34 +01:00
|
|
|
|
2020-06-08 22:25:30 +01:00
|
|
|
all, err := checker.New(runCfg.Version.ClientConfig).All(ctx)
|
2020-04-01 12:59:34 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("Error retrieving version info.", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := update(ctx, runCfg.ServiceName, runCfg.BinaryLocation, all.Processes.Storagenode); err != nil {
|
|
|
|
// don't finish loop in case of error just wait for another execution
|
|
|
|
zap.L().Error("Error updating service.", zap.String("Service", runCfg.ServiceName), zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2020-10-01 21:36:35 +01:00
|
|
|
if err := updateSelf(ctx, updaterBinaryPath, all.Processes.StoragenodeUpdater); err != nil {
|
2020-04-01 12:59:34 +01:00
|
|
|
// don't finish loop in case of error just wait for another execution
|
|
|
|
zap.L().Error("Error updating service.", zap.String("Service", updaterServiceName), zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSelf(ctx context.Context, binaryLocation string, ver version.Process) error {
|
2020-10-01 21:36:35 +01:00
|
|
|
currentVersion, err := binaryVersion(binaryLocation)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
zap.L().Info("Current binary version",
|
|
|
|
zap.String("Service", updaterServiceName),
|
|
|
|
zap.String("Version", currentVersion.String()),
|
|
|
|
)
|
2020-04-01 12:59:34 +01:00
|
|
|
|
|
|
|
// should update
|
2021-11-01 09:57:59 +00:00
|
|
|
newVersion, reason, err := version.ShouldUpdateVersion(currentVersion, nodeID, ver)
|
2020-06-05 23:09:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
2020-04-01 12:59:34 +01:00
|
|
|
}
|
2021-11-01 09:57:59 +00:00
|
|
|
if newVersion.IsZero() {
|
2020-06-05 23:09:34 +01:00
|
|
|
zap.L().Info(reason, zap.String("Service", updaterServiceName))
|
2020-04-01 12:59:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-01 09:57:59 +00:00
|
|
|
newVersionPath := prependExtension(binaryLocation, newVersion.Version)
|
2020-04-01 12:59:34 +01:00
|
|
|
|
2021-11-01 09:57:59 +00:00
|
|
|
if err = downloadBinary(ctx, parseDownloadURL(newVersion.URL), newVersionPath); err != nil {
|
2020-04-01 12:59:34 +01:00
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadedVersion, err := binaryVersion(newVersionPath)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Combine(errs.Wrap(err), os.Remove(newVersionPath))
|
|
|
|
}
|
|
|
|
|
2021-11-01 09:57:59 +00:00
|
|
|
newSemVer, err := newVersion.SemVer()
|
|
|
|
if err != nil {
|
|
|
|
return errs.Combine(err, os.Remove(newVersionPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
if newSemVer.Compare(downloadedVersion) != 0 {
|
|
|
|
err := errs.New("invalid version downloaded: wants %s got %s", newVersion.Version, downloadedVersion)
|
2020-04-01 12:59:34 +01:00
|
|
|
return errs.Combine(err, os.Remove(newVersionPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
zap.L().Info("Restarting service.", zap.String("Service", updaterServiceName))
|
|
|
|
return restartSelf(binaryLocation, newVersionPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func restartSelf(bin, newbin string) error {
|
|
|
|
args := []string{
|
|
|
|
"restart-service",
|
|
|
|
"--binary-location", bin,
|
|
|
|
"--service-name", updaterServiceName,
|
|
|
|
newbin,
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec.Command(bin, args...).Start()
|
|
|
|
}
|