diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index b41651723c75..3a888c26442b 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -37,6 +37,7 @@ - Fix reloading of log level for services {pull}[24055]24055 - Fix: Successfully installed and enrolled agent running standalone{pull}[24128]24128 - Make installer atomic on windows {pull}[24253]24253 +- Fix windows installer during enroll {pull}[24343]24343 - Fix failing installation on windows 7 {pull}[24387]24387 - Fix capabilities resolution in inspect command {pull}[24346]24346 diff --git a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go index 980bb2127f9d..60c5f1d28cfd 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go @@ -52,6 +52,21 @@ func (i *Installer) Install(ctx context.Context, spec program.Spec, version, ins os.RemoveAll(tempInstallDir) } + // on windows rename is not atomic, let's force it to flush the cache + defer func() { + if runtime.GOOS == "windows" { + if f, err := os.OpenFile(installDir, os.O_RDWR, 0777); err == nil { + f.Sync() + f.Close() + } + + if f, err := os.OpenFile(tempInstallDir, os.O_RDWR, 0777); err == nil { + f.Sync() + f.Close() + } + } + }() + if err := i.installer.Install(ctx, spec, version, tempInstallDir); err != nil { // cleanup unfinished install if rerr := os.RemoveAll(tempInstallDir); rerr != nil { @@ -70,12 +85,5 @@ func (i *Installer) Install(ctx context.Context, spec program.Spec, version, ins return err } - // on windows rename is not atomic, let's force it to flush the cache - if runtime.GOOS == "windows" { - if f, err := os.OpenFile(installDir, os.O_SYNC|os.O_RDWR, 0755); err == nil { - f.Sync() - } - } - return nil } diff --git a/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go index 3073da25ca40..034e4be7b416 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go @@ -69,7 +69,7 @@ func (i *Installer) Install(ctx context.Context, spec program.Spec, version, ins return nil } -func (i *Installer) unzip(ctx context.Context, artifactPath string) error { +func (i *Installer) unzip(_ context.Context, artifactPath string) error { r, err := zip.OpenReader(artifactPath) if err != nil { return err @@ -120,11 +120,6 @@ func (i *Installer) unzip(ctx context.Context, artifactPath string) error { } for _, f := range r.File { - // if we were cancelled in between - if err := ctx.Err(); err != nil { - return err - } - if err := unpackFile(f); err != nil { return err }