storagenode-updater/linux: restart systemd service

Change-Id: I8ac25ecc41323ec0d5abf8ee65276c1d7a59f74d
This commit is contained in:
Yaroslav Vorobiov 2020-07-09 16:02:34 +03:00 committed by Egon Elbre
parent 61b17f1214
commit 4783c3e1d3
4 changed files with 86 additions and 4 deletions

View File

@ -19,6 +19,7 @@ import (
"storj.io/common/sync2"
"storj.io/private/cfgstruct"
"storj.io/private/process"
"storj.io/private/version"
_ "storj.io/storj/private/version" // This attaches version information during release builds.
"storj.io/storj/storagenode"
)
@ -52,7 +53,7 @@ var (
runCfg struct {
storagenode.Config
BinaryLocation string `help:"the storage node executable binary location" default:"storagenode.exe"`
BinaryLocation string `help:"the storage node executable binary location" default:"storagenode"`
ServiceName string `help:"storage node OS service name" default:"storagenode"`
// deprecated
Log string `help:"deprecated, use --log.output" default:""`
@ -96,6 +97,8 @@ 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()))
ctx, _ := process.Ctx(cmd)
switch {

View File

@ -1,7 +1,7 @@
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
// +build unittest !windows
// +build !service
package main

View File

@ -0,0 +1,79 @@
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
// +build linux,service
package main
import (
"context"
"os"
"os/exec"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
)
func cmdRestart(cmd *cobra.Command, args []string) error {
return nil
}
func restartService(ctx context.Context, service, binaryLocation, newVersionPath, backupPath string) error {
if err := os.Rename(binaryLocation, backupPath); err != nil {
return errs.Wrap(err)
}
if err := os.Rename(newVersionPath, binaryLocation); err != nil {
return errs.Combine(err, os.Rename(backupPath, binaryLocation), os.Remove(newVersionPath))
}
if service == updaterServiceName {
os.Exit(1)
}
if err := stopProcess(service); err != nil {
err = errs.New("error stopping %s service: %v", service, err)
return errs.Combine(err, os.Rename(backupPath, binaryLocation))
}
return nil
}
func stopProcess(service string) (err error) {
pid, err := getServicePID(service)
if err != nil {
return err
}
p, err := os.FindProcess(pid)
if err != nil {
return err
}
return p.Signal(os.Interrupt)
}
func getServicePID(service string) (int, error) {
args := []string{
"show",
"--property=MainPID",
service,
}
cmd := exec.Command("systemctl", args...)
out, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
trimmed := strings.TrimPrefix(string(out), "MainPID=")
pid, err := strconv.Atoi(trimmed)
if err != nil {
return 0, err
}
return pid, nil
}

View File

@ -1,7 +1,7 @@
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
// +build windows,!unittest
// +build windows,service
package main
@ -20,7 +20,7 @@ import (
"storj.io/private/process"
)
var unrecoverableErr = errs.Class("unable to recoverrecover binary from backup")
var unrecoverableErr = errs.Class("unable to recover binary from backup")
func cmdRestart(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)