2020-04-01 12:59:34 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/common/fpath"
|
2021-09-01 15:51:19 +01:00
|
|
|
"storj.io/common/identity"
|
2020-04-01 12:59:34 +01:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/sync2"
|
|
|
|
"storj.io/private/cfgstruct"
|
|
|
|
"storj.io/private/process"
|
2020-07-09 14:02:34 +01:00
|
|
|
"storj.io/private/version"
|
2020-04-01 12:59:34 +01:00
|
|
|
_ "storj.io/storj/private/version" // This attaches version information during release builds.
|
2021-09-01 15:51:19 +01:00
|
|
|
"storj.io/storj/private/version/checker"
|
2020-04-01 12:59:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
updaterServiceName = "storagenode-updater"
|
|
|
|
minCheckInterval = time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// TODO: replace with config value of random bytes in storagenode config.
|
|
|
|
nodeID storj.NodeID
|
|
|
|
|
2020-10-01 21:36:35 +01:00
|
|
|
updaterBinaryPath string
|
|
|
|
|
2020-04-01 12:59:34 +01:00
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "storagenode-updater",
|
|
|
|
Short: "Version updater for storage node",
|
|
|
|
}
|
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "Run the storagenode-updater for storage node",
|
|
|
|
Args: cobra.OnlyValidArgs,
|
|
|
|
RunE: cmdRun,
|
|
|
|
}
|
|
|
|
restartCmd = &cobra.Command{
|
|
|
|
Use: "restart-service <new binary path>",
|
|
|
|
Short: "Restart service with the new binary",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: cmdRestart,
|
|
|
|
}
|
2020-12-09 22:44:42 +00:00
|
|
|
shouldUpdateCmd = &cobra.Command{
|
|
|
|
Use: "should-update <service>",
|
|
|
|
Short: "Check if service should be updated to suggested version",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: cmdShouldUpdate,
|
|
|
|
}
|
2020-04-01 12:59:34 +01:00
|
|
|
|
|
|
|
runCfg struct {
|
2021-09-01 15:51:19 +01:00
|
|
|
Identity identity.Config
|
|
|
|
Version checker.Config
|
2020-04-01 12:59:34 +01:00
|
|
|
|
2020-07-09 14:02:34 +01:00
|
|
|
BinaryLocation string `help:"the storage node executable binary location" default:"storagenode"`
|
2020-04-01 12:59:34 +01:00
|
|
|
ServiceName string `help:"storage node OS service name" default:"storagenode"`
|
|
|
|
// deprecated
|
|
|
|
Log string `help:"deprecated, use --log.output" default:""`
|
|
|
|
}
|
|
|
|
|
|
|
|
confDir string
|
|
|
|
identityDir string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
|
|
|
defaultConfDir := fpath.ApplicationDir("storj", "storagenode")
|
|
|
|
defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "storagenode")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for storagenode configuration")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", defaultIdentityDir, "main directory for storagenode identity credentials")
|
|
|
|
|
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
rootCmd.AddCommand(restartCmd)
|
2020-12-09 22:44:42 +00:00
|
|
|
rootCmd.AddCommand(shouldUpdateCmd)
|
2020-04-01 12:59:34 +01:00
|
|
|
|
|
|
|
process.Bind(runCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
process.Bind(restartCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2020-12-09 22:44:42 +00:00
|
|
|
process.Bind(shouldUpdateCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2020-04-01 12:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
if runCfg.Log != "" {
|
|
|
|
if err = openLog(runCfg.Log); err != nil {
|
|
|
|
zap.L().Error("Error creating new logger.", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 21:36:35 +01:00
|
|
|
updaterBinaryPath, err = os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Fatal("Unable to find storage node updater binary path.")
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:59:34 +01:00
|
|
|
if !fileExists(runCfg.BinaryLocation) {
|
|
|
|
zap.L().Fatal("Unable to find storage node executable binary.")
|
|
|
|
}
|
|
|
|
|
|
|
|
ident, err := runCfg.Identity.Load()
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Fatal("Error loading identity.", zap.Error(err))
|
|
|
|
}
|
|
|
|
nodeID = ident.ID
|
|
|
|
if nodeID.IsZero() {
|
|
|
|
zap.L().Fatal("Empty node ID.")
|
|
|
|
}
|
|
|
|
|
2020-10-01 21:36:35 +01:00
|
|
|
zap.L().Info("Running on version",
|
|
|
|
zap.String("Service", updaterServiceName),
|
|
|
|
zap.String("Version", version.Build.Version.String()),
|
|
|
|
)
|
2020-07-09 14:02:34 +01:00
|
|
|
|
2020-04-01 12:59:34 +01:00
|
|
|
ctx, _ := process.Ctx(cmd)
|
|
|
|
|
|
|
|
switch {
|
2020-06-08 22:25:30 +01:00
|
|
|
case runCfg.Version.CheckInterval <= 0:
|
2020-04-01 12:59:34 +01:00
|
|
|
err = loopFunc(ctx)
|
2020-06-08 22:25:30 +01:00
|
|
|
case runCfg.Version.CheckInterval < minCheckInterval:
|
2020-04-01 12:59:34 +01:00
|
|
|
zap.L().Error("Check interval below minimum. Overriding it minimum.",
|
2020-06-08 22:25:30 +01:00
|
|
|
zap.Stringer("Check Interval", runCfg.Version.CheckInterval),
|
2020-04-01 12:59:34 +01:00
|
|
|
zap.Stringer("Minimum Check Interval", minCheckInterval),
|
|
|
|
)
|
2020-06-08 22:25:30 +01:00
|
|
|
runCfg.Version.CheckInterval = minCheckInterval
|
2020-04-01 12:59:34 +01:00
|
|
|
fallthrough
|
|
|
|
default:
|
2020-06-08 22:25:30 +01:00
|
|
|
loop := sync2.NewCycle(runCfg.Version.CheckInterval)
|
2020-04-01 12:59:34 +01:00
|
|
|
err = loop.Run(ctx, loopFunc)
|
|
|
|
}
|
|
|
|
if err != nil && !errs2.IsCanceled(err) {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:44:42 +00:00
|
|
|
func cmdShouldUpdate(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
ctx, _ := process.Ctx(cmd)
|
|
|
|
service := args[0]
|
|
|
|
|
|
|
|
switch service {
|
|
|
|
case "storagenode", "storagenode-updater":
|
|
|
|
default: // do not decide if other than above mentioned processes should be updated
|
|
|
|
zap.L().Error("Process is not allowed", zap.String("service", service))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ident, err := runCfg.Identity.Load()
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Fatal("Error loading identity.", zap.Error(err))
|
|
|
|
}
|
|
|
|
nodeID = ident.ID
|
|
|
|
if nodeID.IsZero() {
|
|
|
|
zap.L().Fatal("Empty node ID.")
|
|
|
|
}
|
|
|
|
|
|
|
|
ver, err := checker.New(runCfg.Version.ClientConfig).Process(ctx, service)
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Fatal("Error retrieving version info.", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
var shouldUpdate bool
|
|
|
|
|
|
|
|
if runCfg.BinaryLocation != "" && fileExists(runCfg.BinaryLocation) {
|
|
|
|
currentVersion, err := binaryVersion(runCfg.BinaryLocation)
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Fatal("Error retrieving binary version.", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
updateVersion, _, err := version.ShouldUpdateVersion(currentVersion, nodeID, ver)
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("Error on should update version",
|
|
|
|
zap.String("service", service),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldUpdate = !updateVersion.IsZero()
|
|
|
|
} else {
|
|
|
|
shouldUpdate = version.ShouldUpdate(ver.Rollout, nodeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldUpdate {
|
|
|
|
zap.L().Info("Service should be updated", zap.String("service", service))
|
|
|
|
os.Exit(0)
|
|
|
|
} else {
|
|
|
|
zap.L().Info("Service should not be updated", zap.String("service", service))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:59:34 +01:00
|
|
|
func fileExists(filename string) bool {
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return info.Mode().IsRegular()
|
|
|
|
}
|
|
|
|
|
|
|
|
func openLog(logPath string) error {
|
|
|
|
if runtime.GOOS == "windows" && !strings.HasPrefix(logPath, "winfile:///") {
|
|
|
|
logPath = "winfile:///" + logPath
|
|
|
|
}
|
|
|
|
|
2020-09-25 15:47:42 +01:00
|
|
|
logger, err := process.NewLoggerWithOutputPaths("storagenode-updater", logPath)
|
2020-04-01 12:59:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-11 04:40:46 +01:00
|
|
|
zap.ReplaceGlobals(logger.With(zap.String("Process", updaterServiceName)))
|
2020-04-01 12:59:34 +01:00
|
|
|
return nil
|
|
|
|
}
|