Skip to content

Commit ded2e99

Browse files
[Ingest Manager] Fixed unzip on older windows (#20088)
* unpacking using zip package * changelog * fmt
1 parent bca0adc commit ded2e99

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

x-pack/elastic-agent/CHANGELOG.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
- Remove support for logs type and use logfile {pull}19761[19761]
5252
- Avoid comparing uncomparable types on enroll {issue}19976[19976]
5353
- Fix issues with merging of elastic-agent.yml and fleet.yml {pull}20026[20026]
54+
- Unzip failures on Windows 8/Windows server 2012 {pull}20088[20088]
5455

5556
==== New features
5657

x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go

+55-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ package zip
77
import (
88
"archive/zip"
99
"context"
10-
"fmt"
10+
"io"
1111
"os"
12-
"os/exec"
1312
"path/filepath"
1413

14+
"github.com/hashicorp/go-multierror"
15+
1516
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
1617
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact"
1718
)
@@ -47,7 +48,7 @@ func (i *Installer) Install(_ context.Context, programName, version, installDir
4748
os.RemoveAll(installDir)
4849
}
4950

50-
if err := i.unzip(artifactPath, programName, version); err != nil {
51+
if err := i.unzip(artifactPath); err != nil {
5152
return err
5253
}
5354

@@ -67,14 +68,59 @@ func (i *Installer) Install(_ context.Context, programName, version, installDir
6768
return nil
6869
}
6970

70-
func (i *Installer) unzip(artifactPath, programName, version string) error {
71-
if _, err := os.Stat(artifactPath); err != nil {
72-
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))
71+
func (i *Installer) unzip(artifactPath string) error {
72+
r, err := zip.OpenReader(artifactPath)
73+
if err != nil {
74+
return err
75+
}
76+
defer r.Close()
77+
78+
if err := os.MkdirAll(i.config.InstallPath, 0755); err != nil && !os.IsExist(err) {
79+
// failed to create install dir
80+
return err
81+
}
82+
83+
unpackFile := func(f *zip.File) (err error) {
84+
rc, err := f.Open()
85+
if err != nil {
86+
return err
87+
}
88+
defer func() {
89+
if cerr := rc.Close(); cerr != nil {
90+
err = multierror.Append(err, cerr)
91+
}
92+
}()
93+
94+
path := filepath.Join(i.config.InstallPath, f.Name)
95+
96+
if f.FileInfo().IsDir() {
97+
os.MkdirAll(path, f.Mode())
98+
} else {
99+
os.MkdirAll(filepath.Dir(path), f.Mode())
100+
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
101+
if err != nil {
102+
return err
103+
}
104+
defer func() {
105+
if cerr := f.Close(); cerr != nil {
106+
err = multierror.Append(err, cerr)
107+
}
108+
}()
109+
110+
if _, err = io.Copy(f, rc); err != nil {
111+
return err
112+
}
113+
}
114+
return nil
73115
}
74116

75-
powershellArg := fmt.Sprintf("Expand-Archive -LiteralPath \"%s\" -DestinationPath \"%s\"", artifactPath, i.config.InstallPath)
76-
installCmd := exec.Command("powershell", "-command", powershellArg)
77-
return installCmd.Run()
117+
for _, f := range r.File {
118+
if err := unpackFile(f); err != nil {
119+
return err
120+
}
121+
}
122+
123+
return nil
78124
}
79125

80126
// retrieves root directory from zip archive

0 commit comments

Comments
 (0)