Skip to content

Commit

Permalink
[Elastic Agent] Fix Windows powershell install service script (elasti…
Browse files Browse the repository at this point in the history
…c#20203)

* Fix install service script.

* Add changelog.

* Register as a Windows service and fix issue with reader closer.

* Fix install service script.

* Add changelog.
  • Loading branch information
blakerouse authored Jul 27, 2020
1 parent d199ecf commit 3bacbfd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$ErrorActionPreference = "Stop"

# Delete and stop the service if it already exists.
if (Get-Service {{.BeatName}} -ErrorAction SilentlyContinue) {
$service = Get-WmiObject -Class Win32_Service -Filter "name='{{.BeatName}}'"
Expand All @@ -13,8 +15,5 @@ New-Service -name {{.BeatName}} `
-displayName {{.BeatName | title}} `
-binaryPathName "`"$workdir\{{.BeatName}}.exe`" --path.home `"$workdir`" --path.data `"$workdir\data`" run"

# Attempt to set the service to delayed start using sc config.
Try {
Start-Process -FilePath sc.exe -ArgumentList 'config {{.BeatName}} start= delayed-auto'
}
Catch { Write-Host -f red "An error occured setting the service to delayed start." }
# Start the new service.
Start-Service -name {{.BeatName}}
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
- Fix failing unit tests on windows {pull}20127[20127]
- Improve GRPC stop to be more relaxed {pull}20118[20118]
- Prevent closing closed reader {pull}20214[20214]
- Fix Windows service installation script {pull}20203[20203]

==== New features

Expand Down
2 changes: 2 additions & 0 deletions x-pack/elastic-agent/pkg/agent/application/info/agent_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
return nil, err
}

// reader is closed by this function
cfg, err := config.NewConfigFrom(reader)
if err != nil {
return nil, errors.New(err,
Expand Down Expand Up @@ -126,6 +127,7 @@ func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error {
return err
}

// reader is closed by this function
cfg, err := config.NewConfigFrom(reader)
if err != nil {
return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile),
Expand Down
26 changes: 25 additions & 1 deletion x-pack/elastic-agent/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
package cmd

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/service"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
Expand Down Expand Up @@ -57,12 +60,20 @@ func run(flags *globalFlags, streams *cli.IOStreams) error {
return err
}

// Windows: Mark service as stopped.
// After this is run, the service is considered by the OS to be stopped.
// This must be the first deferred cleanup task (last to execute).
defer service.NotifyTermination()

locker := application.NewAppLocker(paths.Data())
if err := locker.TryLock(); err != nil {
return err
}
defer locker.Unlock()

service.BeforeRun()
defer service.Cleanup()

app, err := application.New(logger, pathConfigFile)
if err != nil {
return err
Expand All @@ -72,11 +83,24 @@ func run(flags *globalFlags, streams *cli.IOStreams) error {
return err
}

// register as a service
stop := make(chan bool)
_, cancel := context.WithCancel(context.Background())
var stopBeat = func() {
close(stop)
}
service.HandleSignals(stopBeat, cancel)

// listen for kill signal
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGQUIT)

<-signals
select {
case <-stop:
break
case <-signals:
break
}

return app.Stop()
}

0 comments on commit 3bacbfd

Please sign in to comment.