From ff5ec22d575965aefd0962881df285da12623800 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 21 Jul 2020 11:17:33 +0200 Subject: [PATCH 1/3] unpacking using zip package --- .../pkg/artifact/install/zip/zip_installer.go | 63 ++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) 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 451cd701627f..076f8c4c21e2 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 @@ -7,13 +7,13 @@ package zip import ( "archive/zip" "context" - "fmt" + "io" "os" - "os/exec" "path/filepath" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact" + "github.com/hashicorp/go-multierror" ) const ( @@ -47,7 +47,7 @@ func (i *Installer) Install(_ context.Context, programName, version, installDir os.RemoveAll(installDir) } - if err := i.unzip(artifactPath, programName, version); err != nil { + if err := i.unzip(artifactPath); err != nil { return err } @@ -67,14 +67,59 @@ func (i *Installer) Install(_ context.Context, programName, version, installDir return nil } -func (i *Installer) unzip(artifactPath, programName, version string) error { - if _, err := os.Stat(artifactPath); err != nil { - return errors.New(fmt.Sprintf("artifact for '%s' version '%s' could not be found at '%s'", programName, version, artifactPath), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, artifactPath)) +func (i *Installer) unzip(artifactPath string) error { + r, err := zip.OpenReader(artifactPath) + if err != nil { + return err } + defer r.Close() - powershellArg := fmt.Sprintf("Expand-Archive -LiteralPath \"%s\" -DestinationPath \"%s\"", artifactPath, i.config.InstallPath) - installCmd := exec.Command("powershell", "-command", powershellArg) - return installCmd.Run() + if err := os.MkdirAll(i.config.InstallPath, 0755); err != nil && !os.IsExist(err) { + // failed to create install dir + return err + } + + unpackFile := func(f *zip.File) (err error) { + rc, err := f.Open() + if err != nil { + return err + } + defer func() { + if cerr := rc.Close(); cerr != nil { + err = multierror.Append(err, cerr) + } + }() + + path := filepath.Join(i.config.InstallPath, f.Name) + + if f.FileInfo().IsDir() { + os.MkdirAll(path, f.Mode()) + } else { + os.MkdirAll(filepath.Dir(path), f.Mode()) + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) + if err != nil { + return err + } + defer func() { + if cerr := f.Close(); cerr != nil { + err = multierror.Append(err, cerr) + } + }() + + if _, err = io.Copy(f, rc); err != nil { + return err + } + } + return nil + } + + for _, f := range r.File { + if err := unpackFile(f); err != nil { + return err + } + } + + return nil } // retrieves root directory from zip archive From b281a8b42684b30c6b61690d72749faa37a32996 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 21 Jul 2020 12:49:20 +0200 Subject: [PATCH 2/3] changelog --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 9b3ab9b2ac33..2324ccaf873c 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -51,6 +51,7 @@ - Remove support for logs type and use logfile {pull}19761[19761] - Avoid comparing uncomparable types on enroll {issue}19976[19976] - Fix issues with merging of elastic-agent.yml and fleet.yml {pull}20026[20026] +- Unzip failures on Windows 8/Windows server 2012 {pull}20088[20088] ==== New features From 7720d10cf417f85da18e2f2bce093b26393535e3 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 21 Jul 2020 13:08:48 +0200 Subject: [PATCH 3/3] fmt --- x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 076f8c4c21e2..ffc90f2dce8a 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 @@ -11,9 +11,10 @@ import ( "os" "path/filepath" + "github.com/hashicorp/go-multierror" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact" - "github.com/hashicorp/go-multierror" ) const (