diff --git a/.golangci.yml b/.golangci.yml index 71346681f33..ef42c8108e4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -113,6 +113,7 @@ linters-settings: - github.com/dop251/goja_nodejs - github.com/fsnotify/fsnotify - github.com/tonistiigi/fifo + - github.com/leoluk/perflib_exporter/perflib gosimple: # Select the Go version to target. The default is '1.13'. diff --git a/go.mod b/go.mod index f3813217e3b..7e9028c578e 100644 --- a/go.mod +++ b/go.mod @@ -321,6 +321,7 @@ replace ( github.com/Shopify/sarama => github.com/elastic/sarama v1.19.1-0.20220310193331-ebc2b0d8eef3 github.com/dop251/goja => github.com/andrewkroh/goja v0.0.0-20190128172624-dd2ac4456e20 github.com/dop251/goja_nodejs => github.com/dop251/goja_nodejs v0.0.0-20171011081505-adff31b136e6 + github.com/leoluk/perflib_exporter/perflib => github.com/prometheus-community/windows_exporter/pkg/perflib v0.25.1 github.com/tonistiigi/fifo => github.com/containerd/fifo v0.0.0-20190816180239-bda0ff6ed73c ) diff --git a/internal/pkg/agent/appinit/appinit.go b/internal/pkg/agent/appinit/appinit.go new file mode 100644 index 00000000000..e976e7ddc0b --- /dev/null +++ b/internal/pkg/agent/appinit/appinit.go @@ -0,0 +1,36 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package appinit + +import ( + "os" + "sync" + + "github.com/elastic/elastic-agent-libs/service" +) + +var stopSvcChan = make(chan bool) +var once sync.Once +var stopBeat = func() { + close(stopSvcChan) +} + +func init() { + if len(os.Args) > 1 && os.Args[1] == "run" { + once.Do(func() { + var wg sync.WaitGroup + wg.Add(1) + go func() { + wg.Done() + service.ProcessWindowsControlEvents(stopBeat) + }() + wg.Wait() + }) + } +} + +func StopSvcChan() chan bool { + return stopSvcChan +} diff --git a/internal/pkg/agent/cmd/otel.go b/internal/pkg/agent/cmd/otel.go index c3885129f0b..453da4dbb8b 100644 --- a/internal/pkg/agent/cmd/otel.go +++ b/internal/pkg/agent/cmd/otel.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package cmd import ( diff --git a/internal/pkg/agent/cmd/otel_flags.go b/internal/pkg/agent/cmd/otel_flags.go index 6f3905bae16..1058b429425 100644 --- a/internal/pkg/agent/cmd/otel_flags.go +++ b/internal/pkg/agent/cmd/otel_flags.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package cmd import ( diff --git a/internal/pkg/agent/cmd/otel_flags_test.go b/internal/pkg/agent/cmd/otel_flags_test.go index e0b2e52e5ec..43860406af3 100644 --- a/internal/pkg/agent/cmd/otel_flags_test.go +++ b/internal/pkg/agent/cmd/otel_flags_test.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package cmd import ( diff --git a/internal/pkg/agent/cmd/otel_windows.go b/internal/pkg/agent/cmd/otel_windows.go deleted file mode 100644 index 2d741852877..00000000000 --- a/internal/pkg/agent/cmd/otel_windows.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -//go:build windows - -package cmd - -import ( - "github.com/spf13/cobra" - - "github.com/elastic/elastic-agent/internal/pkg/cli" -) - -func newOtelCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command { - return nil -} diff --git a/internal/pkg/agent/cmd/otel_windows_test.go b/internal/pkg/agent/cmd/otel_windows_test.go deleted file mode 100644 index dd84acbe724..00000000000 --- a/internal/pkg/agent/cmd/otel_windows_test.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -//go:build windows - -package cmd - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOtelCommandIsNil(t *testing.T) { - require.Nil(t, newOtelCommandWithArgs(nil, nil)) -} diff --git a/internal/pkg/agent/cmd/run.go b/internal/pkg/agent/cmd/run.go index f5c1db4d498..2ab59c301ef 100644 --- a/internal/pkg/agent/cmd/run.go +++ b/internal/pkg/agent/cmd/run.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/elastic-agent-system-metrics/report" "github.com/elastic/elastic-agent/internal/pkg/agent/vault" + "github.com/elastic/elastic-agent/internal/pkg/agent/appinit" "github.com/elastic/elastic-agent/internal/pkg/agent/application" "github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator" "github.com/elastic/elastic-agent/internal/pkg/agent/application/filelock" @@ -117,6 +118,13 @@ func run(override cfgOverrider, testingMode bool, fleetInitTimeout time.Duration service.WaitExecutionDone() }() + service.BeforeRun() + defer service.Cleanup() + + // register as a service + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + if err := handleUpgrade(); err != nil { return fmt.Errorf("error checking for and handling upgrade: %w", err) } @@ -129,20 +137,7 @@ func run(override cfgOverrider, testingMode bool, fleetInitTimeout time.Duration _ = locker.Unlock() }() - service.BeforeRun() - defer service.Cleanup() - - // register as a service - stop := make(chan bool) - ctx, cancel := context.WithCancel(context.Background()) - stopBeat := func() { - close(stop) - } - - defer cancel() - go service.ProcessWindowsControlEvents(stopBeat) - - return runElasticAgent(ctx, cancel, override, stop, testingMode, fleetInitTimeout, false, nil, modifiers...) + return runElasticAgent(ctx, cancel, override, appinit.StopSvcChan(), testingMode, fleetInitTimeout, false, nil, modifiers...) } func logReturn(l *logger.Logger, err error) error { diff --git a/internal/pkg/agent/cmd/validate.go b/internal/pkg/agent/cmd/validate.go index f87956ee5e2..5ea75e0f3ea 100644 --- a/internal/pkg/agent/cmd/validate.go +++ b/internal/pkg/agent/cmd/validate.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package cmd import ( diff --git a/internal/pkg/agent/cmd/validate_test.go b/internal/pkg/agent/cmd/validate_test.go index b02a1d92cef..326e809748d 100644 --- a/internal/pkg/agent/cmd/validate_test.go +++ b/internal/pkg/agent/cmd/validate_test.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package cmd import ( diff --git a/internal/pkg/otel/components.go b/internal/pkg/otel/components.go index a4e0d370c75..bad2afb68cb 100644 --- a/internal/pkg/otel/components.go +++ b/internal/pkg/otel/components.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package otel import ( diff --git a/internal/pkg/otel/config_file_provider.go b/internal/pkg/otel/config_file_provider.go index 1c2d2697f63..039693ed9c9 100644 --- a/internal/pkg/otel/config_file_provider.go +++ b/internal/pkg/otel/config_file_provider.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package otel import ( diff --git a/internal/pkg/otel/config_file_provider_test.go b/internal/pkg/otel/config_file_provider_test.go index 16478e3ac56..4fbd75da6d0 100644 --- a/internal/pkg/otel/config_file_provider_test.go +++ b/internal/pkg/otel/config_file_provider_test.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package otel import ( diff --git a/internal/pkg/otel/run.go b/internal/pkg/otel/run.go index 5042e7c14d3..46374fee458 100644 --- a/internal/pkg/otel/run.go +++ b/internal/pkg/otel/run.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package otel import ( diff --git a/internal/pkg/otel/run_test.go b/internal/pkg/otel/run_test.go index 983044d5170..484ea5d7fc2 100644 --- a/internal/pkg/otel/run_test.go +++ b/internal/pkg/otel/run_test.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package otel import ( diff --git a/internal/pkg/otel/validate.go b/internal/pkg/otel/validate.go index ce3414536da..924ae784a1f 100644 --- a/internal/pkg/otel/validate.go +++ b/internal/pkg/otel/validate.go @@ -2,8 +2,6 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -//go:build !windows - package otel import ( diff --git a/main.go b/main.go index 98580c51c5b..9adea7ce236 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "os" "time" + _ "github.com/elastic/elastic-agent/internal/pkg/agent/appinit" "github.com/elastic/elastic-agent/internal/pkg/agent/cmd" "github.com/elastic/elastic-agent/pkg/core/process" ) diff --git a/pkg/testing/fixture_install.go b/pkg/testing/fixture_install.go index 1ed50d0add5..9ec43ea44bd 100644 --- a/pkg/testing/fixture_install.go +++ b/pkg/testing/fixture_install.go @@ -190,7 +190,7 @@ func (f *Fixture) installNoPkgManager(ctx context.Context, installOpts *InstallO installArgs = append(installArgs, installOptsArgs...) out, err := f.Exec(ctx, installArgs, opts...) if err != nil { - return out, fmt.Errorf("error running agent install command: %w", err) + return out, fmt.Errorf("error running agent install command: %w with output: %s", err, string(out)) } f.installed = true @@ -245,6 +245,19 @@ func (f *Fixture) installNoPkgManager(ctx context.Context, installOpts *InstallO if err != nil { f.t.Logf("error serializing processes: %s", err) } + + if runtime.GOOS == "windows" { + filePath := filepath.Join(dir, "build", "diagnostics", fmt.Sprintf("TEST-%s-%s-%s-EventLogs.json", sanitizedTestName, f.operatingSystem, f.architecture)) + psCommand := `Get-EventLog -LogName Application -EntryType Error -Newest 1000` + out, err := exec.Command("powershell", "-NoProfile", psCommand).CombinedOutput() + if err != nil { + f.t.Logf("error executing command: %s with output %s", err, string(out)) + } else { + if err := os.WriteFile(filePath, out, 0666); err != nil { + f.t.Logf("error wrting file %s: %s", filePath, err) + } + } + } } }) @@ -603,7 +616,7 @@ func (f *Fixture) uninstallNoPkgManager(ctx context.Context, uninstallOpts *Unin } out, err := f.Exec(ctx, uninstallArgs, opts...) if err != nil { - return out, fmt.Errorf("error running uninstall command: %w", err) + return out, fmt.Errorf("error running uninstall command: %w with output: %s", err, string(out)) } f.installed = false