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-05 22:10:05 +01:00
|
|
|
"encoding/json"
|
2019-09-17 08:19:56 +01:00
|
|
|
"errors"
|
2019-08-30 10:02:36 +01:00
|
|
|
"fmt"
|
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-08-30 10:02:36 +01:00
|
|
|
|
|
|
|
"storj.io/storj/internal/sync2"
|
2019-09-05 22:10:05 +01:00
|
|
|
"storj.io/storj/internal/version"
|
2019-08-30 10:02:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
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-08-30 10:02:36 +01:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
err = cmdRun(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
interval string
|
|
|
|
versionURL string
|
|
|
|
binaryLocation string
|
2019-09-17 08:19:56 +01:00
|
|
|
snServiceName string
|
|
|
|
logPath string
|
2019-08-30 10:02:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
|
|
|
|
runCmd.Flags().StringVar(&interval, "interval", "06h", "interval for checking the new version")
|
|
|
|
runCmd.Flags().StringVar(&versionURL, "version-url", "https://version.storj.io/release/", "version server URL")
|
|
|
|
runCmd.Flags().StringVar(&binaryLocation, "binary-location", "storagenode.exe", "the storage node executable binary location")
|
2019-09-17 08:19:56 +01:00
|
|
|
|
|
|
|
runCmd.Flags().StringVar(&snServiceName, "service-name", "storagenode", "storage node OS service name")
|
|
|
|
runCmd.Flags().StringVar(&logPath, "log", "", "path to log file, if empty standard output will be used")
|
2019-08-30 10:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
2019-09-17 08:19:56 +01:00
|
|
|
logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("error opening log file: %v", err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, logFile.Close()) }()
|
|
|
|
|
|
|
|
log.SetOutput(logFile)
|
|
|
|
|
2019-09-05 22:10:05 +01:00
|
|
|
if !fileExists(binaryLocation) {
|
|
|
|
return errs.New("unable to find storage node executable binary")
|
|
|
|
}
|
|
|
|
|
2019-08-30 10:02:36 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
|
|
|
|
signal.Stop(c)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
loopInterval, err := time.ParseDuration(interval)
|
|
|
|
if err != nil {
|
2019-09-05 22:10:05 +01:00
|
|
|
return errs.New("unable to parse interval parameter: %v", err)
|
2019-08-30 10:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loop := sync2.NewCycle(loopInterval)
|
2019-09-05 22:10:05 +01:00
|
|
|
|
|
|
|
update := func(ctx context.Context) (err error) {
|
|
|
|
currentVersion, err := binaryVersion(binaryLocation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println("downloading versions from", versionURL)
|
|
|
|
suggestedVersion, downloadURL, err := suggestedVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadURL = strings.Replace(downloadURL, "{os}", runtime.GOOS, 1)
|
|
|
|
downloadURL = strings.Replace(downloadURL, "{arch}", runtime.GOARCH, 1)
|
|
|
|
|
|
|
|
if currentVersion.Compare(suggestedVersion) < 0 {
|
|
|
|
tempArchive, err := ioutil.TempFile(os.TempDir(), "storagenode")
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("cannot create temporary archive: %v", err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, os.Remove(tempArchive.Name())) }()
|
|
|
|
|
|
|
|
log.Println("start downloading", downloadURL, "to", tempArchive.Name())
|
|
|
|
err = downloadArchive(ctx, tempArchive, downloadURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println("finished downloading", downloadURL, "to", tempArchive.Name())
|
|
|
|
|
2019-09-17 08:19:56 +01:00
|
|
|
extension := filepath.Ext(binaryLocation)
|
|
|
|
if extension != "" {
|
|
|
|
extension = "." + extension
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Dir(binaryLocation)
|
|
|
|
backupExec := filepath.Join(dir, "storagenode.old."+currentVersion.String()+extension)
|
|
|
|
|
|
|
|
if err = os.Rename(binaryLocation, backupExec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = unpackBinary(ctx, tempArchive.Name(), binaryLocation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadedVersion, err := binaryVersion(binaryLocation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if suggestedVersion.Compare(downloadedVersion) != 0 {
|
|
|
|
return errs.New("invalid version downloaded: wants %s got %s", suggestedVersion.String(), downloadedVersion.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("restarting service", snServiceName)
|
|
|
|
err = restartSNService(snServiceName)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("unable to restart service: %v", err)
|
|
|
|
}
|
|
|
|
log.Println("service", snServiceName, "restarted successfully")
|
|
|
|
|
|
|
|
// TODO remove old binary ??
|
|
|
|
} else {
|
|
|
|
log.Println("storage node version is up to date")
|
2019-09-05 22:10:05 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 10:02:36 +01:00
|
|
|
err = loop.Run(ctx, func(ctx context.Context) (err error) {
|
2019-09-05 22:10:05 +01:00
|
|
|
if err := update(ctx); err != nil {
|
|
|
|
// don't finish loop in case of error just wait for another execution
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2019-08-30 10:02:36 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != context.Canceled {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:10:05 +01:00
|
|
|
func binaryVersion(location string) (version.SemVer, error) {
|
|
|
|
out, err := exec.Command(location, "version").Output()
|
|
|
|
if err != nil {
|
|
|
|
return version.SemVer{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(out))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
prefix := "Version: "
|
|
|
|
if strings.HasPrefix(line, prefix) {
|
|
|
|
return version.NewSemVer(line[len(prefix):])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return version.SemVer{}, errs.New("unable to determine binary version")
|
|
|
|
}
|
|
|
|
|
|
|
|
func suggestedVersion() (ver version.SemVer, url string, err error) {
|
|
|
|
resp, err := http.Get(versionURL)
|
|
|
|
if err != nil {
|
|
|
|
return ver, url, err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, resp.Body.Close()) }()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return ver, url, err
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:01:36 +01:00
|
|
|
var response version.AllowedVersions
|
2019-09-05 22:10:05 +01:00
|
|
|
err = json.Unmarshal(body, &response)
|
|
|
|
if err != nil {
|
|
|
|
return ver, url, err
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestedVersion := response.Processes.Storagenode.Suggested
|
|
|
|
ver, err = version.NewSemVer(suggestedVersion.Version)
|
|
|
|
if err != nil {
|
|
|
|
return ver, url, err
|
|
|
|
}
|
|
|
|
return ver, suggestedVersion.URL, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func restartSNService(name string) error {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
// TODO how run this as one command `net stop servicename && net start servicename`?
|
|
|
|
_, err := exec.Command("net", "stop", name).Output()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = exec.Command("net", "start", name).Output()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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-08-30 10:02:36 +01:00
|
|
|
func main() {
|
|
|
|
_ = rootCmd.Execute()
|
|
|
|
}
|