Skip to content

Commit

Permalink
Merge pull request #773 from ripienaar/772
Browse files Browse the repository at this point in the history
(#772) support running as a windows service
  • Loading branch information
ripienaar authored Jan 23, 2020
2 parents 4153ed8 + 3a92bb0 commit 9c08e04
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ provisioning_target_provider.go
plugin_*.go
.idea/
*.msi
*.exe
14 changes: 4 additions & 10 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ func (r *serverRunCommand) Configure() error {
return nil
}

func (r *serverRunCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

func (r *serverRunCommand) prepareInstance() (i *server.Instance, err error) {
if r.disableTLS {
c.Config.DisableTLS = true
log.Warn("Running with TLS disabled, not compatible with production use.")
Expand All @@ -106,23 +104,19 @@ func (r *serverRunCommand) Run(wg *sync.WaitGroup) (err error) {

instance, err := server.NewInstance(c)
if err != nil {
log.Errorf("Could not start choria: %s", err)
os.Exit(1)
return nil, fmt.Errorf("could not create Choria Server instance: %s", err)
}

log.Infof("Choria Server version %s starting with config %s", build.Version, c.Config.ConfigFile)

if r.pidFile != "" {
err := ioutil.WriteFile(r.pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644)
if err != nil {
return fmt.Errorf("could not write PID: %s", err)
return nil, fmt.Errorf("could not write PID: %s", err)
}
}

wg.Add(1)
err = instance.Run(ctx, wg)

return err
return instance, nil
}

func init() {
Expand Down
19 changes: 19 additions & 0 deletions cmd/server_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build !windows

package cmd

import (
"sync"
)

func (r *serverRunCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

instance, err := r.prepareInstance()
if err != nil {
return err
}

wg.Add(1)
return instance.Run(ctx, wg)
}
74 changes: 74 additions & 0 deletions cmd/server_run_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"sync"

"github.com/choria-io/go-choria/server"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/windows/svc"
)

type winServiceWrapper struct {
instance *server.Instance
cmd *serverRunCommand
}

func (w *winServiceWrapper) Execute(args []string, changes <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) {
status <- svc.Status{State: svc.StartPending}

var err error

w.instance, err = w.cmd.prepareInstance()
if err != nil {
log.Errorf("failed to create instance of the server: %q", err)
return false, 1
}

wg.Add(1)
go w.instance.Run(ctx, wg)

status <- svc.Status{
State: svc.Running,
Accepts: svc.AcceptStop | svc.AcceptShutdown,
}

loop:
for change := range changes {
switch change.Cmd {
case svc.Interrogate:
status <- change.CurrentStatus

case svc.Stop, svc.Shutdown:
cancel()

break loop
default:
log.Warnf("Unexpected service control sequence received: %q", change.Cmd)
}
}

status <- svc.Status{State: svc.StopPending}

return false, 0
}

func (r *serverRunCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

interactive, err := svc.IsAnInteractiveSession()
if err != nil {
return err
}

if interactive {
instance, err := r.prepareInstance()
if err != nil {
return err
}

wg.Add(1)
return instance.Run(ctx, wg)
}

return svc.Run("choria-server", &winServiceWrapper{cmd: r})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/sirupsen/logrus v1.4.2
github.com/tidwall/gjson v1.3.5
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c
gopkg.in/alecthomas/kingpin.v2 v2.2.6
rsc.io/goversion v1.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ golang.org/x/sys v0.0.0-20191220220014-0732a990476f h1:72l8qCJ1nGxMGH26QVBVIxKd/
golang.org/x/sys v0.0.0-20191220220014-0732a990476f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8 h1:JA8d3MPx/IToSyXZG/RhwYEtfrKO1Fxrqe8KrkiLXKM=
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 9c08e04

Please sign in to comment.