Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #20088 to 7.9: [Ingest Manager] Fixed unzip on older windows #20109

Merged
merged 2 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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]
- Fix failing unit tests on windows {pull}20127[20127]
- Prevent closing closed reader {pull}20214[20214]
- Improve GRPC stop to be more relaxed {pull}20118[20118]
Expand Down
64 changes: 55 additions & 9 deletions x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package zip
import (
"archive/zip"
"context"
"fmt"
"io"
"os"
"os/exec"
"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"
)
Expand Down Expand Up @@ -47,7 +48,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
}

Expand All @@ -67,14 +68,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()

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
}

powershellArg := fmt.Sprintf("Expand-Archive -LiteralPath \"%s\" -DestinationPath \"%s\"", artifactPath, i.config.InstallPath)
installCmd := exec.Command("powershell", "-command", powershellArg)
return installCmd.Run()
for _, f := range r.File {
if err := unpackFile(f); err != nil {
return err
}
}

return nil
}

// retrieves root directory from zip archive
Expand Down