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

Cherry-pick #20203 to 7.9: [Elastic Agent] Fix Windows powershell install service script #20252

Merged
merged 1 commit into from
Jul 27, 2020
Merged
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
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]
- Prevent closing closed reader {pull}20214[20214]
- Improve GRPC stop to be more relaxed {pull}20118[20118]
- 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()
}