From 06b40e9c379ce6fceffff8fb661100cbe193e482 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Thu, 23 Jul 2020 10:10:26 -0400 Subject: [PATCH 1/5] Fix install service script. --- .../windows/install-service-elastic-agent.ps1.tmpl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl b/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl index 58fd5b63b9f..fe037e3b425 100644 --- a/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl +++ b/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl @@ -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}}'" @@ -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}} From 52a39d6018aadd93b5941e87a7fa51506704af98 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Thu, 23 Jul 2020 10:26:49 -0400 Subject: [PATCH 2/5] Add changelog. --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 9eb0f512ae3..41709ceb5d7 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -54,6 +54,7 @@ - Unzip failures on Windows 8/Windows server 2012 {pull}20088[20088] - Fix failing unit tests on windows {pull}20127[20127] - Improve GRPC stop to be more relaxed {pull}20118[20118] +- Fix Windows service installation script {pull}20203[20203] ==== New features From a70e58e1f337949c7c222685feff2971040283b4 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Mon, 27 Jul 2020 11:03:39 -0400 Subject: [PATCH 3/5] Register as a Windows service and fix issue with reader closer. --- .../pkg/agent/application/info/agent_id.go | 10 ++----- x-pack/elastic-agent/pkg/agent/cmd/run.go | 26 ++++++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go b/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go index 60462b7da4f..a93483ca1cd 100644 --- a/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go +++ b/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go @@ -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, @@ -94,10 +95,6 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) { errors.M(errors.MetaKeyPath, agentConfigFile)) } - if err := reader.Close(); err != nil { - return nil, err - } - configMap, err := cfg.ToMapStr() if err != nil { return nil, errors.New(err, @@ -130,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), @@ -137,10 +135,6 @@ func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error { errors.M(errors.MetaKeyPath, agentConfigFile)) } - if err := reader.Close(); err != nil { - return err - } - configMap := make(map[string]interface{}) if err := cfg.Unpack(&configMap); err != nil { return errors.New(err, "failed to unpack stored config to map") diff --git a/x-pack/elastic-agent/pkg/agent/cmd/run.go b/x-pack/elastic-agent/pkg/agent/cmd/run.go index 2f1bf8e0db1..518463af6b1 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/run.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/run.go @@ -5,6 +5,7 @@ package cmd import ( + "context" "fmt" "os" "os/signal" @@ -12,6 +13,8 @@ import ( "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" @@ -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 @@ -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() } From 099f03d971ef540f5279942624d5d78fdaffb171 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Thu, 23 Jul 2020 10:10:26 -0400 Subject: [PATCH 4/5] Fix install service script. --- .../windows/install-service-elastic-agent.ps1.tmpl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl b/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl index 58fd5b63b9f..fe037e3b425 100644 --- a/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl +++ b/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl @@ -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}}'" @@ -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}} From b51847ab6eb7cc674fdc4b3c6292c188107cd6de Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Thu, 23 Jul 2020 10:26:49 -0400 Subject: [PATCH 5/5] Add changelog. --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index d3c08e7167c..1d8d8c2bf83 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -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