2019-09-19 17:37:40 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
// Implements support for running the storage node as a Windows Service.
|
|
|
|
//
|
|
|
|
// The Windows Service can be created with sc.exe, e.g.
|
|
|
|
//
|
|
|
|
// sc.exe create storagenode binpath= "C:\Users\MyUser\storagenode.exe run --config-dir C:\Users\MyUser\"
|
|
|
|
//
|
|
|
|
// The --config-dir argument can be omitted if the config.yaml is available at
|
|
|
|
// C:\Windows\System32\config\systemprofile\AppData\Roaming\Storj\Storagenode\config.yaml
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2019-09-24 20:01:36 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-09-19 17:37:40 +01:00
|
|
|
"golang.org/x/sys/windows/svc"
|
|
|
|
|
2020-03-23 19:18:20 +00:00
|
|
|
"storj.io/private/process"
|
2019-09-19 17:37:40 +01:00
|
|
|
)
|
|
|
|
|
2021-11-04 23:08:45 +00:00
|
|
|
func startAsService() bool {
|
2021-01-13 14:08:38 +00:00
|
|
|
isService, err := svc.IsWindowsService()
|
2019-09-19 17:37:40 +01:00
|
|
|
if err != nil {
|
2021-01-13 14:08:38 +00:00
|
|
|
zap.L().Fatal("Failed to determine if session is a service.", zap.Error(err))
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|
2021-01-13 14:08:38 +00:00
|
|
|
if !isService {
|
2021-11-04 23:08:45 +00:00
|
|
|
return false
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the 'run' command is invoked
|
|
|
|
if len(os.Args) < 2 {
|
2021-11-04 23:08:45 +00:00
|
|
|
return false
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if os.Args[1] != "run" {
|
2021-11-04 23:08:45 +00:00
|
|
|
return false
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the Windows Service handler
|
|
|
|
err = svc.Run("storagenode", &service{})
|
|
|
|
if err != nil {
|
2020-04-13 10:31:17 +01:00
|
|
|
zap.L().Fatal("Service failed.", zap.Error(err))
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|
2021-11-04 23:08:45 +00:00
|
|
|
|
|
|
|
return true
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type service struct{}
|
|
|
|
|
|
|
|
func (m *service) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
|
|
|
|
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
|
|
|
|
|
|
|
|
changes <- svc.Status{State: svc.StartPending}
|
|
|
|
|
2019-09-24 20:01:36 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
2019-09-19 17:37:40 +01:00
|
|
|
process.Exec(rootCmd)
|
2019-09-24 20:01:36 +01:00
|
|
|
return nil
|
|
|
|
})
|
2021-11-04 23:22:28 +00:00
|
|
|
defer group.Wait()
|
2019-09-19 17:37:40 +01:00
|
|
|
|
|
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
|
|
|
2021-11-04 23:22:28 +00:00
|
|
|
cmdloop:
|
2019-11-14 08:31:30 +00:00
|
|
|
for c := range r {
|
|
|
|
switch c.Cmd {
|
|
|
|
case svc.Interrogate:
|
2020-04-13 10:31:17 +01:00
|
|
|
zap.L().Info("Interrogate request received.")
|
2019-11-14 08:31:30 +00:00
|
|
|
changes <- c.CurrentStatus
|
|
|
|
case svc.Stop, svc.Shutdown:
|
2020-04-13 10:31:17 +01:00
|
|
|
zap.L().Info("Stop/Shutdown request received.")
|
2021-11-04 23:22:28 +00:00
|
|
|
|
2019-11-14 08:31:30 +00:00
|
|
|
// Cancel the command's root context to cleanup resources
|
|
|
|
_, cancel := process.Ctx(runCmd)
|
|
|
|
cancel()
|
2021-11-04 23:22:28 +00:00
|
|
|
|
|
|
|
changes <- svc.Status{State: svc.StopPending, Accepts: cmdsAccepted}
|
|
|
|
|
|
|
|
break cmdloop
|
2019-11-14 08:31:30 +00:00
|
|
|
default:
|
2020-04-13 10:31:17 +01:00
|
|
|
zap.L().Info("Unexpected control request.", zap.Uint32("Event Type", c.EventType))
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 11:53:36 +01:00
|
|
|
|
|
|
|
return false, 0
|
2019-09-19 17:37:40 +01:00
|
|
|
}
|