storagenode-updater: check binary version on self-update

Check binary version on self-update instead of current process
version to prevent updating already updated binary.
Add info logs to report current version of service beeing
updated.

Change-Id: Id22dee188a99d6d45db925104786f49f5d3a61ae
This commit is contained in:
Yaroslav Vorobiov 2020-10-01 23:36:35 +03:00 committed by Ivan Fraixedes
parent 979ee762ba
commit 25df79a6bf
5 changed files with 31 additions and 16 deletions

View File

@ -33,6 +33,8 @@ var (
// TODO: replace with config value of random bytes in storagenode config.
nodeID storj.NodeID
updaterBinaryPath string
rootCmd = &cobra.Command{
Use: "storagenode-updater",
Short: "Version updater for storage node",
@ -84,6 +86,11 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
}
}
updaterBinaryPath, err = os.Executable()
if err != nil {
zap.L().Fatal("Unable to find storage node updater binary path.")
}
if !fileExists(runCfg.BinaryLocation) {
zap.L().Fatal("Unable to find storage node executable binary.")
}
@ -97,7 +104,10 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
zap.L().Fatal("Empty node ID.")
}
zap.L().Info("Running on version", zap.String("version", version.Build.Version.String()))
zap.L().Info("Running on version",
zap.String("Service", updaterServiceName),
zap.String("Version", version.Build.Version.String()),
)
ctx, _ := process.Ctx(cmd)

View File

@ -7,7 +7,6 @@ package main
import (
"context"
"os"
"go.uber.org/zap"
@ -29,8 +28,7 @@ func loopFunc(ctx context.Context) error {
zap.L().Error("Error updating service.", zap.String("Service", runCfg.ServiceName), zap.Error(err))
}
updaterBinName := os.Args[0]
if err := update(ctx, updaterServiceName, updaterBinName, all.Processes.StoragenodeUpdater); err != nil {
if err := update(ctx, updaterServiceName, updaterBinaryPath, all.Processes.StoragenodeUpdater); err != nil {
// 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))
}

View File

@ -32,8 +32,7 @@ func loopFunc(ctx context.Context) error {
zap.L().Error("Error updating service.", zap.String("Service", runCfg.ServiceName), zap.Error(err))
}
updaterBinName := os.Args[0]
if err := updateSelf(ctx, updaterBinName, all.Processes.StoragenodeUpdater); err != nil {
if err := updateSelf(ctx, updaterBinaryPath, all.Processes.StoragenodeUpdater); err != nil {
// 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))
}
@ -47,7 +46,15 @@ func updateSelf(ctx context.Context, binaryLocation string, ver version.Process)
return errs.Wrap(err)
}
currentVersion := version.Build.Version
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()),
)
// should update
shouldUpdate, reason, err := version.ShouldUpdateVersion(currentVersion, nodeID, ver)

View File

@ -65,7 +65,7 @@ func getServicePID(service string) (int, error) {
out, err := cmd.CombinedOutput()
if err != nil {
return 0, err
return 0, errs.New("Error retrieving service pid: systemctl: %s %v", string(out), err)
}
trimmed := strings.TrimPrefix(string(out), "MainPID=")

View File

@ -19,16 +19,16 @@ func update(ctx context.Context, serviceName, binaryLocation string, ver version
return errs.Wrap(err)
}
var currentVersion version.SemVer
if serviceName == updaterServiceName {
currentVersion = version.Build.Version
} else {
currentVersion, err = binaryVersion(binaryLocation)
if err != nil {
return errs.Wrap(err)
}
currentVersion, err := binaryVersion(binaryLocation)
if err != nil {
return errs.Wrap(err)
}
zap.L().Info("Current binary version",
zap.String("Service", serviceName),
zap.String("Version", currentVersion.String()),
)
// should update
shouldUpdate, reason, err := version.ShouldUpdateVersion(currentVersion, nodeID, ver)
if err != nil {