From 4db3c6cd61b35a31809b7ef36c4db7837293042d Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 5 Mar 2021 15:54:35 +0100 Subject: [PATCH] [Ingest-Manager] Fix windows installer during enroll (#24343) [Ingest-Manager] Fix windows installer during enroll (#24343) --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + .../install/atomic/atomic_installer.go | 22 +++++++++++++------ .../pkg/artifact/install/zip/zip_installer.go | 7 +----- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 4ceaff77f464..25c2d5dad11b 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 ==== New features 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 }