2020-04-01 12:59:34 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/private/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
func update(ctx context.Context, serviceName, binaryLocation string, ver version.Process) error {
|
|
|
|
suggestedVersion, err := ver.Suggested.SemVer()
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-10-01 21:36:35 +01:00
|
|
|
currentVersion, err := binaryVersion(binaryLocation)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
2020-04-01 12:59:34 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 21:36:35 +01:00
|
|
|
zap.L().Info("Current binary version",
|
|
|
|
zap.String("Service", serviceName),
|
|
|
|
zap.String("Version", currentVersion.String()),
|
|
|
|
)
|
|
|
|
|
2020-04-01 12:59:34 +01:00
|
|
|
// should update
|
2020-06-05 23:09:34 +01:00
|
|
|
shouldUpdate, reason, err := version.ShouldUpdateVersion(currentVersion, nodeID, ver)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
2020-04-01 12:59:34 +01:00
|
|
|
}
|
2020-06-05 23:09:34 +01:00
|
|
|
if !shouldUpdate {
|
|
|
|
zap.L().Info(reason, zap.String("Service", serviceName))
|
2020-04-01 12:59:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newVersionPath := prependExtension(binaryLocation, ver.Suggested.Version)
|
|
|
|
|
|
|
|
if err = downloadBinary(ctx, parseDownloadURL(ver.Suggested.URL), newVersionPath); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadedVersion, err := binaryVersion(newVersionPath)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Combine(errs.Wrap(err), os.Remove(newVersionPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
if suggestedVersion.Compare(downloadedVersion) != 0 {
|
|
|
|
err := errs.New("invalid version downloaded: wants %s got %s",
|
|
|
|
suggestedVersion.String(),
|
|
|
|
downloadedVersion.String(),
|
|
|
|
)
|
|
|
|
return errs.Combine(err, os.Remove(newVersionPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
var backupPath string
|
|
|
|
if serviceName == updaterServiceName {
|
|
|
|
// NB: don't include old version number for updater binary backup
|
|
|
|
backupPath = prependExtension(binaryLocation, "old")
|
|
|
|
} else {
|
|
|
|
backupPath = prependExtension(binaryLocation, "old."+currentVersion.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
zap.L().Info("Restarting service.", zap.String("Service", serviceName))
|
|
|
|
|
|
|
|
if err = restartService(ctx, serviceName, binaryLocation, newVersionPath, backupPath); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
zap.L().Info("Service restarted successfully.", zap.String("Service", serviceName))
|
|
|
|
return nil
|
|
|
|
}
|