Skip to content

Commit

Permalink
Merge pull request #1475 from luthermonson/svc-manager-win
Browse files Browse the repository at this point in the history
Update service watching logic for windows service manager
  • Loading branch information
luthermonson authored Jul 30, 2021
2 parents fdbedca + 8b1b7a9 commit 3ccdda2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ replace (
)

require (
github.com/Freman/eventloghook v0.0.0-20191003051739-e4d803b6b48b
github.com/Microsoft/hcsshim v0.8.20
github.com/containerd/continuity v0.1.0
github.com/google/go-containerregistry v0.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBp
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Freman/eventloghook v0.0.0-20191003051739-e4d803b6b48b h1:IltY1fRcdIshI/c8KOdmaO8P4lBwDXHJYPymMisvvDs=
github.com/Freman/eventloghook v0.0.0-20191003051739-e4d803b6b48b/go.mod h1:VGwG8f2pQ8SAFjTSH3PEDmLdlvi0XTd7a4C4AZn+pVw=
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317 h1:JhyuWIqYrstW7KHMjk/fTqU0xtMpBOHuiTA2FVc7L4E=
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317/go.mod h1:DF8FZRxMHMGv/vP2lQP6h+dYzzjpuRn24VeRiYn3qjQ=
github.com/JeffAshton/win_pdh v0.0.0-20161109143554-76bb4ee9f0ab h1:UKkYhof1njT1/xq4SEg5z+VpTgjmNeHwPGRQl7takDI=
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/cmds/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmds
import (
"github.com/rancher/k3s/pkg/cli/cmds"
"github.com/rancher/rke2/pkg/rke2"
"github.com/rancher/rke2/pkg/windows"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -80,5 +81,8 @@ func agentSubcommands() cli.Commands {
func AgentRun(clx *cli.Context) error {
validateCloudProviderName(clx)
validateProfile(clx, "agent")
if err := windows.StartService(); err != nil {
return err
}
return rke2.Agent(clx, config)
}
6 changes: 3 additions & 3 deletions pkg/cli/cmds/agent_service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/rancher/k3s/pkg/cli/cmds"
"github.com/rancher/k3s/pkg/version"
"github.com/urfave/cli"
"golang.org/x/sys/windows"
syswin "golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc/mgr"
)

Expand Down Expand Up @@ -78,7 +78,7 @@ func addWindowService(serviceName, config string) error {
defer m.Disconnect()

s, err := m.CreateService(serviceName, p, mgr.Config{
ServiceType: windows.SERVICE_WIN32_OWN_PROCESS,
ServiceType: syswin.SERVICE_WIN32_OWN_PROCESS,
StartType: mgr.StartAutomatic,
ErrorControl: mgr.ErrorNormal,
DisplayName: version.Program,
Expand Down Expand Up @@ -112,7 +112,7 @@ func addWindowService(serviceName, config string) error {
}

lpInfo := serviceFailureActions{ResetPeriod: uint32(30), ActionsCount: uint32(1), Actions: uintptr(unsafe.Pointer(&t[0]))}
return windows.ChangeServiceConfig2(s.Handle, serviceConfigFailureActions, (*byte)(unsafe.Pointer(&lpInfo)))
return syswin.ChangeServiceConfig2(s.Handle, serviceConfigFailureActions, (*byte)(unsafe.Pointer(&lpInfo)))
}

func deleteWindowsService(serviceName string) error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/windows/service_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !windows

package windows

func StartService() error {
return nil
}
71 changes: 71 additions & 0 deletions pkg/windows/service_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// +build windows

package windows

import (
"os"
"time"

"github.com/Freman/eventloghook"
"github.com/rancher/k3s/pkg/version"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/eventlog"
)

type service struct{}

var Service = &service{}

func (h *service) Execute(_ []string, requests <-chan svc.ChangeRequest, statuses chan<- svc.Status) (bool, uint32) {
statuses <- svc.Status{State: svc.StartPending}
statuses <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
for c := range requests {
switch c.Cmd {
case svc.Interrogate:
statuses <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
statuses <- svc.Status{State: svc.StopPending}
logrus.Info("Windows Service is shutting down in 5s")
time.Sleep(5 * time.Second)
return false, 0
}
}
return false, 0
}

func StartService() error {
if ok, err := svc.IsWindowsService(); err != nil || !ok {
return err
}

elog, err := eventlog.Open(version.Program)
if err != nil {
return err
}
logrus.AddHook(eventloghook.NewHook(elog))

stop := make(chan struct{})
go watchService(stop)
go func() {
defer close(stop)
if err := svc.Run(version.Program, Service); err != nil {
logrus.Fatalf("Windows Service error, exiting: %s", err)
}
}()

return nil
}

func watchService(stop chan struct{}) {
<-stop // pause for service to be stopped
ok, err := svc.IsWindowsService()
if err != nil {
logrus.Warnf("Error trying to determine if running as a Windows Service: %s", err)
}

if ok {
logrus.Infof("Windows Service is shutting down")
os.Exit(0)
}
}

0 comments on commit 3ccdda2

Please sign in to comment.