Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 244 windows #247

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions backends/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package backends

import (
"bytes"
"os/exec"
"reflect"
"time"

"github.com/Graylog2/collector-sidecar/api/graylog"
"github.com/Graylog2/collector-sidecar/common"
"github.com/Graylog2/collector-sidecar/system"
"os/exec"
)

type Backend struct {
Expand Down Expand Up @@ -88,16 +90,40 @@ func (b *Backend) ValidatePreconditions() bool {

func (b *Backend) ValidateConfigurationFile() (bool, string) {
if b.ValidationParameters == nil {
log.Errorf("[%s] No parameters configured to validate configuration!", b.Name)
log.Errorf("[%s] No parameters for validating the configuration file are configured!", b.Name)
return false, ""
}

output, err := exec.Command(b.ExecutablePath, b.ValidationParameters...).CombinedOutput()
if err != nil {
soutput := string(output)
log.Errorf("[%s] Error during configuration validation: %s %s", b.Name, soutput, err)
return false, soutput
cmd := exec.Command(b.ExecutablePath, b.ValidationParameters...)

var combinedOutputBuffer bytes.Buffer
cmd.Stdout = &combinedOutputBuffer
cmd.Stderr = &combinedOutputBuffer

if err := cmd.Start(); err != nil {
log.Errorf("[%s] Couldn't start validation command: %s %s", b.Name, string(combinedOutputBuffer.Bytes()), err)
return false, string(combinedOutputBuffer.Bytes())
}

return true, ""
done := make(chan error)
go func() {
done <- cmd.Wait()
}()

select {
case <-time.After(time.Duration(30) * time.Second):
if err := cmd.Process.Kill(); err != nil {
log.Errorf("[%s] Failed to kill validation process: %s", b.Name, err)
return false, err.Error()
}
log.Errorf("[%s] Timeout reached for validation command.", b.Name)
return false, "Unable to validate configuration, timeout reached."
case err := <-done:
if err != nil {
close(done)
log.Errorf("[%s] Error during configuration validation: %s %s", b.Name, string(combinedOutputBuffer.Bytes()), err)
return false, string(combinedOutputBuffer.Bytes())
}
return true, ""
}
}
38 changes: 19 additions & 19 deletions daemon/svc_runner_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ func (r *SvcRunner) Name() string {
func (r *SvcRunner) Running() bool {
m, err := mgr.Connect()
if err != nil {
backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
return false
}
defer m.Disconnect()

s, err := m.OpenService(r.serviceName)
// service exist so we only update the properties
if err != nil {
backends.SetStatusLogErrorf(r.name, "Can't get status of service %s cause it doesn't exist: %v", r.serviceName, err)
r.backend.SetStatusLogErrorf("Can't get status of service %s cause it doesn't exist: %v", r.serviceName, err)
return false
}
defer s.Close()

status, err := s.Query()
if err != nil {
backends.SetStatusLogErrorf(r.name, "Can't query status of service %s: %v", r.serviceName, err)
r.backend.SetStatusLogErrorf("Can't query status of service %s: %v", r.serviceName, err)
}

return status.State == svc.Running
Expand All @@ -107,8 +107,8 @@ func (r *SvcRunner) SetDaemon(d *DaemonConfig) {
r.daemon = d
}

func (r *SvcRunner) GetBackend() backends.Backend {
return r.backend
func (r *SvcRunner) GetBackend() *backends.Backend {
return &r.backend
}

func (r *SvcRunner) SetBackend(b backends.Backend) {
Expand All @@ -120,12 +120,12 @@ func (r *SvcRunner) SetBackend(b backends.Backend) {
func (r *SvcRunner) ValidateBeforeStart() error {
execPath, err := exec.LookPath(r.exec)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to find collector executable %s", r.exec)
return r.backend.SetStatusLogErrorf("Failed to find collector executable %s", r.exec)
}

m, err := mgr.Connect()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
return r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
}
defer m.Disconnect()

Expand All @@ -147,21 +147,21 @@ func (r *SvcRunner) ValidateBeforeStart() error {
}
err = s.UpdateConfig(currentConfig)
if err != nil {
backends.SetStatusLogErrorf(r.name, "Failed to update service: %v", err)
r.backend.SetStatusLogErrorf("Failed to update service: %v", err)
}
// service needs to be created
} else {
s, err = m.CreateService(r.serviceName,
execPath,
serviceConfig)
if err != nil {
backends.SetStatusLogErrorf(r.name, "Failed to install service: %v", err)
r.backend.SetStatusLogErrorf("Failed to install service: %v", err)
}
defer s.Close()
err = eventlog.InstallAsEventCreate(r.serviceName, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
s.Delete()
backends.SetStatusLogErrorf(r.name, "SetupEventLogSource() failed: %v", err)
r.backend.SetStatusLogErrorf("SetupEventLogSource() failed: %v", err)
}
}

Expand All @@ -184,7 +184,7 @@ func (r *SvcRunner) startSupervisor() {
continue
}

backends.SetStatusLogErrorf(r.name, "Backend finished unexpectedly, sending restart signal")
r.backend.SetStatusLogErrorf("Backend finished unexpectedly, sending restart signal")
r.Restart()
}
}()
Expand All @@ -201,19 +201,19 @@ func (r *SvcRunner) start() error {

m, err := mgr.Connect()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
return r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
}
defer m.Disconnect()

ws, err := m.OpenService(r.serviceName)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not access service: %v", err)
return r.backend.SetStatusLogErrorf("Could not access service: %v", err)
}
defer ws.Close()

err = ws.Start("is", "manual-started")
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not start service: %v", err)
return r.backend.SetStatusLogErrorf("Could not start service: %v", err)
}

r.setSupervised(true)
Expand All @@ -235,30 +235,30 @@ func (r *SvcRunner) stop() error {

m, err := mgr.Connect()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
return r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
}
defer m.Disconnect()

ws, err := m.OpenService(r.serviceName)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not access service: %v", err)
return r.backend.SetStatusLogErrorf("Could not access service: %v", err)
}
defer ws.Close()

status, err := ws.Control(svc.Stop)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not send stop control: %v", err)
return r.backend.SetStatusLogErrorf("Could not send stop control: %v", err)
}

timeout := time.Now().Add(10 * time.Second)
for status.State != svc.Stopped {
if timeout.Before(time.Now()) {
return backends.SetStatusLogErrorf(r.name, "Timeout waiting for service to go to stopped state: %v", err)
return r.backend.SetStatusLogErrorf("Timeout waiting for service to go to stopped state: %v", err)
}
time.Sleep(300 * time.Millisecond)
status, err = ws.Query()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not retrieve service status: %v", err)
return r.backend.SetStatusLogErrorf("Could not retrieve service status: %v", err)
}
}

Expand Down