Skip to content

Commit 3bacbfd

Browse files
authored
[Elastic Agent] Fix Windows powershell install service script (elastic#20203)
* Fix install service script. * Add changelog. * Register as a Windows service and fix issue with reader closer. * Fix install service script. * Add changelog.
1 parent d199ecf commit 3bacbfd

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
$ErrorActionPreference = "Stop"
2+
13
# Delete and stop the service if it already exists.
24
if (Get-Service {{.BeatName}} -ErrorAction SilentlyContinue) {
35
$service = Get-WmiObject -Class Win32_Service -Filter "name='{{.BeatName}}'"
@@ -13,8 +15,5 @@ New-Service -name {{.BeatName}} `
1315
-displayName {{.BeatName | title}} `
1416
-binaryPathName "`"$workdir\{{.BeatName}}.exe`" --path.home `"$workdir`" --path.data `"$workdir\data`" run"
1517

16-
# Attempt to set the service to delayed start using sc config.
17-
Try {
18-
Start-Process -FilePath sc.exe -ArgumentList 'config {{.BeatName}} start= delayed-auto'
19-
}
20-
Catch { Write-Host -f red "An error occured setting the service to delayed start." }
18+
# Start the new service.
19+
Start-Service -name {{.BeatName}}

x-pack/elastic-agent/CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
- Fix failing unit tests on windows {pull}20127[20127]
5656
- Improve GRPC stop to be more relaxed {pull}20118[20118]
5757
- Prevent closing closed reader {pull}20214[20214]
58+
- Fix Windows service installation script {pull}20203[20203]
5859

5960
==== New features
6061

x-pack/elastic-agent/pkg/agent/application/info/agent_id.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
8686
return nil, err
8787
}
8888

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

130+
// reader is closed by this function
129131
cfg, err := config.NewConfigFrom(reader)
130132
if err != nil {
131133
return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile),

x-pack/elastic-agent/pkg/agent/cmd/run.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
package cmd
66

77
import (
8+
"context"
89
"fmt"
910
"os"
1011
"os/signal"
1112
"syscall"
1213

1314
"github.com/spf13/cobra"
1415

16+
"github.com/elastic/beats/v7/libbeat/service"
17+
1518
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application"
1619
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
1720
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
@@ -57,12 +60,20 @@ func run(flags *globalFlags, streams *cli.IOStreams) error {
5760
return err
5861
}
5962

63+
// Windows: Mark service as stopped.
64+
// After this is run, the service is considered by the OS to be stopped.
65+
// This must be the first deferred cleanup task (last to execute).
66+
defer service.NotifyTermination()
67+
6068
locker := application.NewAppLocker(paths.Data())
6169
if err := locker.TryLock(); err != nil {
6270
return err
6371
}
6472
defer locker.Unlock()
6573

74+
service.BeforeRun()
75+
defer service.Cleanup()
76+
6677
app, err := application.New(logger, pathConfigFile)
6778
if err != nil {
6879
return err
@@ -72,11 +83,24 @@ func run(flags *globalFlags, streams *cli.IOStreams) error {
7283
return err
7384
}
7485

86+
// register as a service
87+
stop := make(chan bool)
88+
_, cancel := context.WithCancel(context.Background())
89+
var stopBeat = func() {
90+
close(stop)
91+
}
92+
service.HandleSignals(stopBeat, cancel)
93+
7594
// listen for kill signal
7695
signals := make(chan os.Signal, 1)
7796
signal.Notify(signals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGQUIT)
7897

79-
<-signals
98+
select {
99+
case <-stop:
100+
break
101+
case <-signals:
102+
break
103+
}
80104

81105
return app.Stop()
82106
}

0 commit comments

Comments
 (0)