From 58a8a33bf7f514540a5c6b1b957310196defea89 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 9 Mar 2021 15:14:18 +0100 Subject: [PATCH] [Ingest-Manager] Fix windows installer during enroll (#24343) (#24388) [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 b41651723c7..3a888c26442 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 980bb2127f9..60c5f1d28cf 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 3073da25ca4..034e4be7b41 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 }