2019-08-30 10:02:36 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-17 08:19:56 +01:00
|
|
|
"archive/zip"
|
2019-09-05 22:10:05 +01:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-08-30 10:02:36 +01:00
|
|
|
"context"
|
2019-09-17 08:19:56 +01:00
|
|
|
"errors"
|
2019-09-05 22:10:05 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2019-08-30 10:02:36 +01:00
|
|
|
"os"
|
2019-09-05 22:10:05 +01:00
|
|
|
"os/exec"
|
2019-08-30 10:02:36 +01:00
|
|
|
"os/signal"
|
2019-09-17 08:19:56 +01:00
|
|
|
"path/filepath"
|
2019-09-05 22:10:05 +01:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2019-08-30 10:02:36 +01:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2019-09-05 22:10:05 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-10-21 11:50:59 +01:00
|
|
|
"go.uber.org/zap"
|
2019-08-30 10:02:36 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/common/fpath"
|
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/sync2"
|
2020-03-23 19:18:20 +00:00
|
|
|
"storj.io/private/cfgstruct"
|
|
|
|
"storj.io/private/process"
|
2020-03-23 19:30:31 +00:00
|
|
|
"storj.io/private/version"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/version/checker"
|
2019-08-30 10:02:36 +01:00
|
|
|
)
|
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
const (
|
|
|
|
updaterServiceName = "storagenode-updater"
|
|
|
|
minCheckInterval = time.Minute
|
|
|
|
)
|
2019-10-21 11:50:59 +01:00
|
|
|
|
2019-08-30 10:02:36 +01:00
|
|
|
var (
|
2019-09-19 12:00:26 +01:00
|
|
|
cancel context.CancelFunc
|
2019-10-31 12:27:53 +00:00
|
|
|
// TODO: replace with config value of random bytes in storagenode config.
|
|
|
|
nodeID storj.NodeID
|
2019-09-19 12:00:26 +01:00
|
|
|
|
2019-08-30 10:02:36 +01:00
|
|
|
rootCmd = &cobra.Command{
|
2019-09-19 10:33:56 +01:00
|
|
|
Use: "storagenode-updater",
|
|
|
|
Short: "Version updater for storage node",
|
2019-08-30 10:02:36 +01:00
|
|
|
}
|
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
2019-09-19 10:33:56 +01:00
|
|
|
Short: "Run the storagenode-updater for storage node",
|
2019-09-17 08:19:56 +01:00
|
|
|
Args: cobra.OnlyValidArgs,
|
2019-10-21 11:50:59 +01:00
|
|
|
RunE: cmdRun,
|
2019-08-30 10:02:36 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
runCfg struct {
|
|
|
|
// TODO: check interval default has changed from 6 hours to 15 min.
|
|
|
|
checker.Config
|
|
|
|
Identity identity.Config
|
|
|
|
|
|
|
|
BinaryLocation string `help:"the storage node executable binary location" default:"storagenode.exe"`
|
|
|
|
ServiceName string `help:"storage node OS service name" default:"storagenode"`
|
2019-12-03 13:56:49 +00:00
|
|
|
// deprecated
|
|
|
|
Log string `help:"deprecated, use --log.output" default:""`
|
2019-10-21 11:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
confDir string
|
|
|
|
identityDir string
|
2019-08-30 10:02:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-21 11:50:59 +01:00
|
|
|
// TODO: this will probably generate warnings for mismatched config fields.
|
|
|
|
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")
|
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
2019-08-30 10:02:36 +01:00
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
rootCmd.AddCommand(runCmd)
|
2019-09-17 08:19:56 +01:00
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
process.Bind(runCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2019-08-30 10:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
2019-12-03 13:56:49 +00:00
|
|
|
err = openLog()
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("Error creating new logger: %v", err)
|
|
|
|
}
|
2019-09-17 08:19:56 +01:00
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
if !fileExists(runCfg.BinaryLocation) {
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Fatal("Unable to find storage node executable binary")
|
2019-10-21 11:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ident, err := runCfg.Identity.Load()
|
|
|
|
if err != nil {
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Fatalf("Error loading identity: %v", err)
|
2019-09-05 22:10:05 +01:00
|
|
|
}
|
2019-10-31 12:27:53 +00:00
|
|
|
nodeID = ident.ID
|
|
|
|
if nodeID.IsZero() {
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Fatal("Empty node ID")
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
2019-09-05 22:10:05 +01:00
|
|
|
|
2019-09-19 12:00:26 +01:00
|
|
|
var ctx context.Context
|
2019-10-31 12:27:53 +00:00
|
|
|
ctx, cancel = process.Ctx(cmd)
|
2019-08-30 10:02:36 +01:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
|
|
|
|
signal.Stop(c)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
2019-09-20 15:22:40 +01:00
|
|
|
loopFunc := func(ctx context.Context) (err error) {
|
2019-11-12 12:31:57 +00:00
|
|
|
if err := update(ctx, runCfg.BinaryLocation, runCfg.ServiceName); err != nil {
|
2019-10-31 12:27:53 +00:00
|
|
|
// don't finish loop in case of error just wait for another execution
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Errorf("Error updating %s: %v", runCfg.ServiceName, err)
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 07:26:50 +00:00
|
|
|
updaterBinName := os.Args[0]
|
|
|
|
if err := update(ctx, updaterBinName, updaterServiceName); err != nil {
|
|
|
|
// don't finish loop in case of error just wait for another execution
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Errorf("Error updating %s: %v", updaterServiceName, err)
|
2019-11-18 07:26:50 +00:00
|
|
|
}
|
2019-08-30 10:02:36 +01:00
|
|
|
return nil
|
2019-09-20 15:22:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
switch {
|
|
|
|
case runCfg.CheckInterval <= 0:
|
2019-09-20 15:22:40 +01:00
|
|
|
err = loopFunc(ctx)
|
2019-10-21 11:50:59 +01:00
|
|
|
case runCfg.CheckInterval < minCheckInterval:
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Errorf("Check interval below minimum: %s, setting to %s", runCfg.CheckInterval, minCheckInterval)
|
2019-10-21 11:50:59 +01:00
|
|
|
runCfg.CheckInterval = minCheckInterval
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
loop := sync2.NewCycle(runCfg.CheckInterval)
|
2019-09-20 15:22:40 +01:00
|
|
|
err = loop.Run(ctx, loopFunc)
|
|
|
|
}
|
2019-11-04 09:22:08 +00:00
|
|
|
if err != nil && !errs2.IsCanceled(err) {
|
2019-10-21 11:50:59 +01:00
|
|
|
log.Fatal(err)
|
2019-08-30 10:02:36 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 12:31:57 +00:00
|
|
|
func update(ctx context.Context, binPath, serviceName string) (err error) {
|
2019-10-31 12:27:53 +00:00
|
|
|
if nodeID.IsZero() {
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Fatal("Empty node ID")
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
2019-10-20 08:56:23 +01:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
var currentVersion version.SemVer
|
|
|
|
if serviceName == updaterServiceName {
|
|
|
|
// TODO: find better way to check this binary version
|
|
|
|
currentVersion = version.Build.Version
|
|
|
|
} else {
|
|
|
|
currentVersion, err = binaryVersion(binPath)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
2019-10-20 08:56:23 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
client := checker.New(runCfg.ClientConfig)
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("Downloading versions from %s", runCfg.ServerAddress)
|
2019-11-12 12:31:57 +00:00
|
|
|
processVersion, err := client.Process(ctx, serviceName)
|
2019-10-20 08:56:23 +01:00
|
|
|
if err != nil {
|
2019-10-21 11:50:59 +01:00
|
|
|
return errs.Wrap(err)
|
2019-10-20 08:56:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 12:31:57 +00:00
|
|
|
// TODO: consolidate semver.Version and version.SemVer
|
|
|
|
suggestedVersion, err := processVersion.Suggested.SemVer()
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
2019-10-20 08:56:23 +01:00
|
|
|
|
2019-11-12 12:31:57 +00:00
|
|
|
if currentVersion.Compare(suggestedVersion) >= 0 {
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("%s version is up to date", serviceName)
|
2019-11-12 12:31:57 +00:00
|
|
|
return nil
|
2019-10-20 08:56:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 12:31:57 +00:00
|
|
|
if !version.ShouldUpdate(processVersion.Rollout, nodeID) {
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("New %s version available but not rolled out to this nodeID yet", serviceName)
|
2019-11-12 12:31:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:03:49 +00:00
|
|
|
tempArchive, err := ioutil.TempFile("", serviceName)
|
2019-11-12 12:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("cannot create temporary archive: %v", err)
|
|
|
|
}
|
2019-11-14 14:03:49 +00:00
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err,
|
|
|
|
tempArchive.Close(),
|
|
|
|
os.Remove(tempArchive.Name()),
|
|
|
|
)
|
|
|
|
}()
|
2019-10-20 08:56:23 +01:00
|
|
|
|
2019-11-12 12:31:57 +00:00
|
|
|
downloadURL := parseDownloadURL(processVersion.Suggested.URL)
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("Start downloading %s to %s", downloadURL, tempArchive.Name())
|
2019-11-12 12:31:57 +00:00
|
|
|
err = downloadArchive(ctx, tempArchive, downloadURL)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("Finished downloading %s to %s", downloadURL, tempArchive.Name())
|
2019-10-31 12:27:53 +00:00
|
|
|
|
2019-11-12 12:31:57 +00:00
|
|
|
newVersionPath := prependExtension(binPath, suggestedVersion.String())
|
|
|
|
err = unpackBinary(ctx, tempArchive.Name(), newVersionPath)
|
|
|
|
if err != nil {
|
2019-10-31 12:27:53 +00:00
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
2019-11-12 12:31:57 +00:00
|
|
|
|
|
|
|
// TODO add here recovery even before starting service (if version command cannot be executed)
|
|
|
|
|
|
|
|
downloadedVersion, err := binaryVersion(newVersionPath)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if suggestedVersion.Compare(downloadedVersion) != 0 {
|
|
|
|
return errs.New("invalid version downloaded: wants %s got %s", suggestedVersion.String(), downloadedVersion.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// backup original binary
|
|
|
|
var backupPath string
|
|
|
|
if serviceName == updaterServiceName {
|
|
|
|
// NB: don't include old version number for updater binary backup
|
|
|
|
backupPath = prependExtension(binPath, "old")
|
|
|
|
} else {
|
|
|
|
backupPath = prependExtension(binPath, "old."+currentVersion.String())
|
|
|
|
}
|
|
|
|
if err := os.Rename(binPath, backupPath); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rename new binary to replace original
|
|
|
|
if err := os.Rename(newVersionPath, binPath); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("Restarting service %s", serviceName)
|
2019-11-12 12:31:57 +00:00
|
|
|
err = restartService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: should we try to recover from this?
|
2019-12-03 13:56:49 +00:00
|
|
|
return errs.New("Unable to restart service: %v", err)
|
2019-11-12 12:31:57 +00:00
|
|
|
}
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("Service %s restarted successfully", serviceName)
|
2019-11-12 12:31:57 +00:00
|
|
|
|
|
|
|
// TODO remove old binary ??
|
2019-10-31 12:27:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 12:31:57 +00:00
|
|
|
func prependExtension(path, ext string) string {
|
|
|
|
originalExt := filepath.Ext(path)
|
|
|
|
dir, base := filepath.Split(path)
|
|
|
|
base = base[:len(base)-len(originalExt)]
|
|
|
|
return filepath.Join(dir, base+"."+ext+originalExt)
|
|
|
|
}
|
2019-10-31 12:27:53 +00:00
|
|
|
|
|
|
|
func parseDownloadURL(template string) string {
|
|
|
|
url := strings.Replace(template, "{os}", runtime.GOOS, 1)
|
|
|
|
url = strings.Replace(url, "{arch}", runtime.GOARCH, 1)
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
func binaryVersion(location string) (version.SemVer, error) {
|
2019-10-31 12:27:53 +00:00
|
|
|
out, err := exec.Command(location, "version").CombinedOutput()
|
2019-09-05 22:10:05 +01:00
|
|
|
if err != nil {
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.S().Infof("Command output: %s", out)
|
2019-10-21 11:50:59 +01:00
|
|
|
return version.SemVer{}, err
|
2019-09-05 22:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(out))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
prefix := "Version: "
|
|
|
|
if strings.HasPrefix(line, prefix) {
|
2019-09-20 15:22:40 +01:00
|
|
|
line = line[len(prefix):]
|
2019-10-21 11:50:59 +01:00
|
|
|
return version.NewSemVer(line)
|
2019-09-05 22:10:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-21 11:50:59 +01:00
|
|
|
return version.SemVer{}, errs.New("unable to determine binary version")
|
2019-09-05 22:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func downloadArchive(ctx context.Context, file io.Writer, url string) (err error) {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { err = errs.Combine(err, resp.Body.Close()) }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return errs.New("bad status: %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = sync2.Copy(ctx, file, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-17 08:19:56 +01:00
|
|
|
func unpackBinary(ctx context.Context, archive, target string) (err error) {
|
|
|
|
// TODO support different compression types e.g. tar.gz
|
|
|
|
|
|
|
|
zipReader, err := zip.OpenReader(archive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, zipReader.Close()) }()
|
|
|
|
|
|
|
|
if len(zipReader.File) != 1 {
|
|
|
|
return errors.New("archive should contain only binary file")
|
|
|
|
}
|
|
|
|
|
|
|
|
zipedExec, err := zipReader.File[0].Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, zipedExec.Close()) }()
|
|
|
|
|
|
|
|
newExec, err := os.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.FileMode(0755))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, newExec.Close()) }()
|
|
|
|
|
|
|
|
_, err = sync2.Copy(ctx, newExec, zipedExec)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Combine(err, os.Remove(newExec.Name()))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:10:05 +01:00
|
|
|
func fileExists(filename string) bool {
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return info.Mode().IsRegular()
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:56:49 +00:00
|
|
|
func openLog() error {
|
2019-10-31 12:27:53 +00:00
|
|
|
if runCfg.Log != "" {
|
2019-12-03 13:56:49 +00:00
|
|
|
logPath := runCfg.Log
|
|
|
|
if runtime.GOOS == "windows" && !strings.HasPrefix(logPath, "winfile:///") {
|
|
|
|
logPath = "winfile:///" + logPath
|
|
|
|
}
|
|
|
|
logger, err := process.NewLoggerWithOutputPaths(logPath)
|
2019-10-31 12:27:53 +00:00
|
|
|
if err != nil {
|
2019-12-03 13:56:49 +00:00
|
|
|
return err
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
2019-12-03 13:56:49 +00:00
|
|
|
zap.ReplaceGlobals(logger)
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
2019-12-03 13:56:49 +00:00
|
|
|
return nil
|
2019-08-30 10:02:36 +01:00
|
|
|
}
|
2019-11-15 14:37:29 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
process.Exec(rootCmd)
|
|
|
|
}
|