From 4a899231fc86baa69d2e4007d270f52c28a75166 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Wed, 18 Mar 2020 10:45:53 +0100 Subject: [PATCH 01/12] snapshot installer --- .../agent/pkg/agent/operation/common_test.go | 4 +- .../pkg/agent/operation/operation_install.go | 3 +- .../agent/pkg/artifact/install/installer.go | 24 +++++++- .../install/snapshot/snapshot_installer.go | 56 +++++++++++++++++++ .../pkg/artifact/install/tar/tar_installer.go | 31 +++++----- .../pkg/artifact/install/zip/zip_installer.go | 8 +-- 6 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go diff --git a/x-pack/agent/pkg/agent/operation/common_test.go b/x-pack/agent/pkg/agent/operation/common_test.go index 3985f296173c..5317dc7251dc 100644 --- a/x-pack/agent/pkg/agent/operation/common_test.go +++ b/x-pack/agent/pkg/agent/operation/common_test.go @@ -96,8 +96,8 @@ var _ download.Downloader = &DummyDownloader{} type DummyInstaller struct { } -func (*DummyInstaller) Install(p, v, _ string) error { - return nil +func (*DummyInstaller) Install(p, v, _ string) (string, error) { + return "", nil } var _ install.Installer = &DummyInstaller{} diff --git a/x-pack/agent/pkg/agent/operation/operation_install.go b/x-pack/agent/pkg/agent/operation/operation_install.go index 6f1ac2b12e4a..3982d708434b 100644 --- a/x-pack/agent/pkg/agent/operation/operation_install.go +++ b/x-pack/agent/pkg/agent/operation/operation_install.go @@ -67,5 +67,6 @@ func (o *operationInstall) Run(ctx context.Context, application Application) (er } }() - return o.installer.Install(o.program.BinaryName(), o.program.Version(), o.program.Directory()) + _, err = o.installer.Install(o.program.BinaryName(), o.program.Version(), o.program.Directory()) + return err } diff --git a/x-pack/agent/pkg/artifact/install/installer.go b/x-pack/agent/pkg/artifact/install/installer.go index 7ccb6321918b..7debd4125c48 100644 --- a/x-pack/agent/pkg/artifact/install/installer.go +++ b/x-pack/agent/pkg/artifact/install/installer.go @@ -9,6 +9,7 @@ import ( "runtime" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact" + "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/snapshot" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/tar" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/zip" ) @@ -23,7 +24,7 @@ type Installer interface { // Install installs an artifact and returns // location of the installed program // error if something went wrong - Install(programName, version, installDir string) error + Install(programName, version, installDir string) (string, error) } // NewInstaller returns a correct installer associated with a @@ -36,8 +37,25 @@ func NewInstaller(config *artifact.Config) (Installer, error) { return nil, ErrConfigNotProvided } + var i Installer + var err error + if runtime.GOOS == "windows" { - return zip.NewInstaller(config) + i, err = zip.NewInstaller(config) + } else { + i, err = tar.NewInstaller(config) + } + if err != nil { + return nil, err + } + + if IsSnapshot() { + return snapshot.NewInstaller(i) } - return tar.NewInstaller(config) + + return i, nil +} + +func IsSnapshot() bool { + return true } diff --git a/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go b/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go new file mode 100644 index 000000000000..c32dcfc37027 --- /dev/null +++ b/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go @@ -0,0 +1,56 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package snapshot + +import ( + "os" + "path/filepath" + "strings" +) + +const snapshotIdentifier = "-SNAPSHOT" + +// embeddedInstaller is an interface allowing installation of an artifact +type embeddedInstaller interface { + // Install installs an artifact and returns + // location of the installed program + // error if something went wrong + Install(programName, version, installDir string) (string, error) +} + +// Installer or zip packages +type Installer struct { + installer embeddedInstaller +} + +// NewInstaller creates an installer able to install zip packages +func NewInstaller(installer embeddedInstaller) (*Installer, error) { + return &Installer{ + installer: installer, + }, nil +} + +// Install performs installation of program in a specific version. +// It expects package to be already downloaded. +func (i *Installer) Install(programName, version, installDir string) (string, error) { + artifactPath, err := i.installer.Install(programName, version, installDir) + if err != nil { + return artifactPath, err + } + + if !strings.Contains(filepath.Base(artifactPath), snapshotIdentifier) { + return artifactPath, nil + } + + oldBase := filepath.Base(artifactPath) + newBase := strings.Replace(oldBase, snapshotIdentifier, "", 1) + newPath := strings.Replace(artifactPath, oldBase, newBase, 1) + + if err := os.Rename(artifactPath, newPath); err != nil { + return artifactPath, err + } + + return newPath, nil +} diff --git a/x-pack/agent/pkg/artifact/install/tar/tar_installer.go b/x-pack/agent/pkg/artifact/install/tar/tar_installer.go index 998589bad8ab..044f5ca00e72 100644 --- a/x-pack/agent/pkg/artifact/install/tar/tar_installer.go +++ b/x-pack/agent/pkg/artifact/install/tar/tar_installer.go @@ -31,15 +31,15 @@ func NewInstaller(config *artifact.Config) (*Installer, error) { // Install performs installation of program in a specific version. // It expects package to be already downloaded. -func (i *Installer) Install(programName, version, _ string) error { +func (i *Installer) Install(programName, version, _ string) (string, error) { artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory) if err != nil { - return err + return "", err } f, err := os.Open(artifactPath) if 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)) + 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)) } defer f.Close() @@ -47,13 +47,14 @@ func (i *Installer) Install(programName, version, _ string) error { } -func unpack(r io.Reader, dir string) error { +func unpack(r io.Reader, dir string) (string, error) { zr, err := gzip.NewReader(r) if err != nil { - return errors.New("requires gzip-compressed body", err, errors.TypeFilesystem) + return "", errors.New("requires gzip-compressed body", err, errors.TypeFilesystem) } tr := tar.NewReader(zr) + var rootDir string for { f, err := tr.Next() @@ -61,11 +62,11 @@ func unpack(r io.Reader, dir string) error { break } if err != nil { - return err + return "", err } if !validFileName(f.Name) { - return errors.New("tar contained invalid filename: %q", f.Name, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) + return "", errors.New("tar contained invalid filename: %q", f.Name, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) } rel := filepath.FromSlash(f.Name) abs := filepath.Join(dir, rel) @@ -76,12 +77,12 @@ func unpack(r io.Reader, dir string) error { case mode.IsRegular(): // just to be sure, it should already be created by Dir type if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil { - return errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) + return rootDir, errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) } wf, err := os.OpenFile(abs, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode.Perm()) if err != nil { - return errors.New(err, "TarInstaller: creating file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) + return rootDir, errors.New(err, "TarInstaller: creating file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) } _, err = io.Copy(wf, tr) @@ -89,18 +90,22 @@ func unpack(r io.Reader, dir string) error { err = closeErr } if err != nil { - return fmt.Errorf("TarInstaller: error writing to %s: %v", abs, err) + return rootDir, fmt.Errorf("TarInstaller: error writing to %s: %v", abs, err) } case mode.IsDir(): + if rootDir == "" { + rootDir = abs + } + if err := os.MkdirAll(abs, 0755); err != nil { - return errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) + return rootDir, errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) } default: - return errors.New(fmt.Sprintf("tar file entry %s contained unsupported file type %v", f.Name, mode), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) + return rootDir, errors.New(fmt.Sprintf("tar file entry %s contained unsupported file type %v", f.Name, mode), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) } } - return nil + return rootDir, nil } func validFileName(p string) bool { diff --git a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go index 6e8d36bb9b02..79b8d0ddc1a8 100644 --- a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go @@ -34,18 +34,18 @@ func NewInstaller(config *artifact.Config) (*Installer, error) { // Install performs installation of program in a specific version. // It expects package to be already downloaded. -func (i *Installer) Install(programName, version, installDir string) error { +func (i *Installer) Install(programName, version, installDir string) (string, error) { if err := i.unzip(programName, version, installDir); err != nil { - return err + return "", err } oldPath := filepath.Join(installDir, fmt.Sprintf("%s-%s-windows", programName, version)) newPath := filepath.Join(installDir, strings.Title(programName)) if err := os.Rename(oldPath, newPath); err != nil { - return errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, newPath)) + return oldPath, errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, newPath)) } - return i.runInstall(programName, installDir) + return newPath, i.runInstall(programName, installDir) } func (i *Installer) unzip(programName, version, installPath string) error { From cc0be89df5ffe4ae63a20a674366b85ae0a6a646 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Wed, 18 Mar 2020 11:49:37 +0100 Subject: [PATCH 02/12] a --- x-pack/agent/pkg/artifact/install/installer.go | 10 +--------- x-pack/agent/pkg/artifact/install/tar/tar_installer.go | 9 +++++---- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/x-pack/agent/pkg/artifact/install/installer.go b/x-pack/agent/pkg/artifact/install/installer.go index 7debd4125c48..b117b153be1b 100644 --- a/x-pack/agent/pkg/artifact/install/installer.go +++ b/x-pack/agent/pkg/artifact/install/installer.go @@ -49,13 +49,5 @@ func NewInstaller(config *artifact.Config) (Installer, error) { return nil, err } - if IsSnapshot() { - return snapshot.NewInstaller(i) - } - - return i, nil -} - -func IsSnapshot() bool { - return true + return snapshot.NewInstaller(i) } diff --git a/x-pack/agent/pkg/artifact/install/tar/tar_installer.go b/x-pack/agent/pkg/artifact/install/tar/tar_installer.go index 044f5ca00e72..d1d138c683e9 100644 --- a/x-pack/agent/pkg/artifact/install/tar/tar_installer.go +++ b/x-pack/agent/pkg/artifact/install/tar/tar_installer.go @@ -71,6 +71,11 @@ func unpack(r io.Reader, dir string) (string, error) { rel := filepath.FromSlash(f.Name) abs := filepath.Join(dir, rel) + // find the root dir + if currentDir := filepath.Dir(abs); rootDir == "" || len(filepath.Dir(rootDir)) > len(currentDir) { + rootDir = currentDir + } + fi := f.FileInfo() mode := fi.Mode() switch { @@ -93,10 +98,6 @@ func unpack(r io.Reader, dir string) (string, error) { return rootDir, fmt.Errorf("TarInstaller: error writing to %s: %v", abs, err) } case mode.IsDir(): - if rootDir == "" { - rootDir = abs - } - if err := os.MkdirAll(abs, 0755); err != nil { return rootDir, errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) } From fff0fb8d06ce6a5da64dc734858b047daa8c6315 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Wed, 18 Mar 2020 13:18:26 +0100 Subject: [PATCH 03/12] windows --- .../pkg/artifact/install/zip/zip_installer.go | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go index 79b8d0ddc1a8..4b910b721b5b 100644 --- a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go @@ -35,20 +35,20 @@ func NewInstaller(config *artifact.Config) (*Installer, error) { // Install performs installation of program in a specific version. // It expects package to be already downloaded. func (i *Installer) Install(programName, version, installDir string) (string, error) { - if err := i.unzip(programName, version, installDir); err != nil { + if err := i.unzip(programName, version); err != nil { return "", err } - oldPath := filepath.Join(installDir, fmt.Sprintf("%s-%s-windows", programName, version)) - newPath := filepath.Join(installDir, strings.Title(programName)) + oldPath := filepath.Join(i.config.InstallPath, fmt.Sprintf("%s-%s-windows", programName, version)) + newPath := filepath.Join(i.config.InstallPath, strings.Title(programName)) if err := os.Rename(oldPath, newPath); err != nil { return oldPath, errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, newPath)) } - return newPath, i.runInstall(programName, installDir) + return newPath, i.runInstall(programName, version, installDir) } -func (i *Installer) unzip(programName, version, installPath string) error { +func (i *Installer) unzip(programName, version string) error { artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory) if err != nil { return err @@ -58,14 +58,25 @@ func (i *Installer) unzip(programName, version, installPath string) error { 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)) } - powershellArg := fmt.Sprintf("Expand-Archive -Path \"%s\" -DestinationPath \"%s\"", artifactPath, installPath) + powershellArg := fmt.Sprintf("Expand-Archive -LiteralPath \"%s\" -DestinationPath \"%s\"", artifactPath, i.config.InstallPath) installCmd := exec.Command("powershell", "-command", powershellArg) return installCmd.Run() } -func (i *Installer) runInstall(programName, installPath string) error { - powershellCmd := fmt.Sprintf(powershellCmdTemplate, installPath, programName) +func (i *Installer) runInstall(programName, version, installPath string) error { + alignedPath := alignInstallPath(installPath, version) + powershellCmd := fmt.Sprintf(powershellCmdTemplate, alignedPath, programName) installCmd := exec.Command("powershell", "-command", powershellCmd) return installCmd.Run() } + +func alignInstallPath(installPath, version string) string { + snapshotPath := strings.Replace(installPath, version, fmt.Sprintf("%s-SNAPSHOT", version), 1) + + if _, err := os.Stat(snapshotPath); err == nil { + return snapshotPath + } + + return installPath +} From 04f167f7657cc5ae3d2e868641b69a9998aa93ac Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Wed, 18 Mar 2020 13:45:06 +0100 Subject: [PATCH 04/12] more windows --- x-pack/agent/_meta/agent.yml | 2 +- x-pack/agent/_meta/common.p2.yml | 2 +- x-pack/agent/_meta/common.reference.p2.yml | 2 +- x-pack/agent/agent.reference.yml | 2 +- x-pack/agent/agent.yml | 2 +- .../agent/pkg/artifact/install/zip/zip_installer.go | 12 ++++++------ 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/x-pack/agent/_meta/agent.yml b/x-pack/agent/_meta/agent.yml index 7c36499b8ea0..c17c32405e1f 100644 --- a/x-pack/agent/_meta/agent.yml +++ b/x-pack/agent/_meta/agent.yml @@ -4,7 +4,7 @@ outputs: default: type: elasticsearch - hosts: [127.0.0.1:9200, 127.0.0.1:9300] + hosts: [127.0.0.1:9200] username: elastic password: changeme diff --git a/x-pack/agent/_meta/common.p2.yml b/x-pack/agent/_meta/common.p2.yml index adf61fcd2540..2fc2da9071e3 100644 --- a/x-pack/agent/_meta/common.p2.yml +++ b/x-pack/agent/_meta/common.p2.yml @@ -4,7 +4,7 @@ outputs: default: type: elasticsearch - hosts: [127.0.0.1:9200, 127.0.0.1:9300] + hosts: [127.0.0.1:9200] username: elastic password: changeme diff --git a/x-pack/agent/_meta/common.reference.p2.yml b/x-pack/agent/_meta/common.reference.p2.yml index 9fb2e0a9dbd5..97ac70329c31 100644 --- a/x-pack/agent/_meta/common.reference.p2.yml +++ b/x-pack/agent/_meta/common.reference.p2.yml @@ -4,7 +4,7 @@ outputs: default: type: elasticsearch - hosts: [127.0.0.1:9200, 127.0.0.1:9300] + hosts: [127.0.0.1:9200] username: elastic password: changeme diff --git a/x-pack/agent/agent.reference.yml b/x-pack/agent/agent.reference.yml index e7d6ba45d6e9..53cb79b40b51 100644 --- a/x-pack/agent/agent.reference.yml +++ b/x-pack/agent/agent.reference.yml @@ -9,7 +9,7 @@ outputs: default: type: elasticsearch - hosts: [127.0.0.1:9200, 127.0.0.1:9300] + hosts: [127.0.0.1:9200] username: elastic password: changeme diff --git a/x-pack/agent/agent.yml b/x-pack/agent/agent.yml index 711cdc61f92e..ac4ed658ced7 100644 --- a/x-pack/agent/agent.yml +++ b/x-pack/agent/agent.yml @@ -9,7 +9,7 @@ outputs: default: type: elasticsearch - hosts: [127.0.0.1:9200, 127.0.0.1:9300] + hosts: [127.0.0.1:9200] username: elastic password: changeme diff --git a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go index 4b910b721b5b..9b47c2c6931d 100644 --- a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go @@ -8,7 +8,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "strings" "github.com/elastic/beats/v7/x-pack/agent/pkg/agent/errors" @@ -39,13 +38,14 @@ func (i *Installer) Install(programName, version, installDir string) (string, er return "", err } - oldPath := filepath.Join(i.config.InstallPath, fmt.Sprintf("%s-%s-windows", programName, version)) - newPath := filepath.Join(i.config.InstallPath, strings.Title(programName)) - if err := os.Rename(oldPath, newPath); err != nil { - return oldPath, errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, newPath)) + windowsPath := strings.Replace(installDir, version, fmt.Sprintf("%s-windows", version), 1) + if _, err := os.Stat(windowsPath); err == nil { + if err := os.Rename(windowsPath, installDir); err != nil { + return windowsPath, errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, installDir)) + } } - return newPath, i.runInstall(programName, version, installDir) + return installDir, i.runInstall(programName, version, installDir) } func (i *Installer) unzip(programName, version string) error { From 833f777681f516b1fd5b4d5a7ec73c1e21d9a8f6 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Wed, 18 Mar 2020 14:28:50 +0100 Subject: [PATCH 05/12] changelog --- x-pack/agent/CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/agent/CHANGELOG.asciidoc b/x-pack/agent/CHANGELOG.asciidoc index b543eadb4182..1b19ceb6484e 100644 --- a/x-pack/agent/CHANGELOG.asciidoc +++ b/x-pack/agent/CHANGELOG.asciidoc @@ -13,6 +13,7 @@ ==== Bugfixes - Fixed tests on windows {pull}16922[16922] +- Fixed installers for SNAPSHOTs and windows {pull}17077[17077] ==== New features From 042a54fe1a942751d84ce385803f11c789e306f3 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Wed, 18 Mar 2020 16:47:18 +0100 Subject: [PATCH 06/12] root dir fixed --- .../pkg/artifact/install/zip/zip_installer.go | 64 +++++++++++++------ 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go index 9b47c2c6931d..a986484995c4 100644 --- a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go @@ -5,10 +5,11 @@ package zip import ( + "archive/zip" "fmt" "os" "os/exec" - "strings" + "path/filepath" "github.com/elastic/beats/v7/x-pack/agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact" @@ -34,26 +35,32 @@ func NewInstaller(config *artifact.Config) (*Installer, error) { // Install performs installation of program in a specific version. // It expects package to be already downloaded. func (i *Installer) Install(programName, version, installDir string) (string, error) { - if err := i.unzip(programName, version); err != nil { + artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory) + if err != nil { + return "", err + } + + if err := i.unzip(artifactPath, programName, version); err != nil { + return "", err + } + + rootDir, err := i.getRootDir(artifactPath) + if err != nil { return "", err } - windowsPath := strings.Replace(installDir, version, fmt.Sprintf("%s-windows", version), 1) - if _, err := os.Stat(windowsPath); err == nil { - if err := os.Rename(windowsPath, installDir); err != nil { - return windowsPath, errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, installDir)) + // if root directory is not the same as desired directory rename + // e.g contains `-windows-` or `-SNAPSHOT-` + if rootDir != installDir { + if err := os.Rename(rootDir, installDir); err != nil { + return rootDir, errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, installDir)) } } return installDir, i.runInstall(programName, version, installDir) } -func (i *Installer) unzip(programName, version string) error { - artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory) - if err != nil { - return err - } - +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)) } @@ -64,19 +71,36 @@ func (i *Installer) unzip(programName, version string) error { } func (i *Installer) runInstall(programName, version, installPath string) error { - alignedPath := alignInstallPath(installPath, version) - powershellCmd := fmt.Sprintf(powershellCmdTemplate, alignedPath, programName) - + powershellCmd := fmt.Sprintf(powershellCmdTemplate, installPath, programName) installCmd := exec.Command("powershell", "-command", powershellCmd) + return installCmd.Run() } -func alignInstallPath(installPath, version string) string { - snapshotPath := strings.Replace(installPath, version, fmt.Sprintf("%s-SNAPSHOT", version), 1) +// retrieves root directory from zip archive +func (i *Installer) getRootDir(zipPath string) (dir string, err error) { + defer func() { + if dir != "" { + dir = filepath.Join(i.config.InstallPath, dir) + } + }() - if _, err := os.Stat(snapshotPath); err == nil { - return snapshotPath + zipReader, err := zip.OpenReader(zipPath) + if err != nil { + return "", err + } + defer zipReader.Close() + + var rootDir string + for _, f := range zipReader.File { + if filepath.Base(f.Name) == filepath.Dir(f.Name) { + return f.Name, nil + } + + if currentDir := filepath.Dir(f.Name); rootDir == "" || len(currentDir) < len(rootDir) { + rootDir = currentDir + } } - return installPath + return rootDir, nil } From 1f52f2bbc1b1842c1c55a7e2a6c6aa78dfc6fd66 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 23 Mar 2020 10:47:33 +0100 Subject: [PATCH 07/12] snaphot only snapshot --- dev-tools/packaging/packages.yml | 16 +++--- x-pack/agent/magefile.go | 20 +++++-- .../agent/pkg/agent/operation/common_test.go | 4 +- .../pkg/agent/operation/operation_install.go | 3 +- .../download/localremote/downloader.go | 9 ++- .../artifact/download/snapshot/downloader.go | 32 +++++++++++ .../download/snapshot/downloader_test.go | 55 +++++++++++++++++++ .../artifact/download/snapshot/verifier.go | 29 ++++++++++ .../agent/pkg/artifact/install/installer.go | 9 ++- .../install/snapshot/snapshot_installer.go | 28 ++-------- .../pkg/artifact/install/tar/tar_installer.go | 26 ++++----- .../pkg/artifact/install/zip/zip_installer.go | 12 ++-- x-pack/agent/pkg/basecmd/version/cmd.go | 5 ++ x-pack/agent/pkg/release/version.go | 16 +++++- 14 files changed, 199 insertions(+), 65 deletions(-) create mode 100644 x-pack/agent/pkg/artifact/download/snapshot/downloader.go create mode 100644 x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go create mode 100644 x-pack/agent/pkg/artifact/download/snapshot/verifier.go diff --git a/dev-tools/packaging/packages.yml b/dev-tools/packaging/packages.yml index 3cda43b0f2cb..807bb330350f 100644 --- a/dev-tools/packaging/packages.yml +++ b/dev-tools/packaging/packages.yml @@ -54,10 +54,10 @@ shared: /etc/init.d/{{.BeatServiceName}}: template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/{{.PackageType}}/init.sh.tmpl' mode: 0755 - /etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: + /etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz' mode: 0644 - /etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: + /etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz' mode: 0644 @@ -97,10 +97,10 @@ shared: source: 'agent.yml' mode: 0600 config: true - /etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: + /etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz' mode: 0644 - /etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: + /etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz' mode: 0644 @@ -131,10 +131,10 @@ shared: <<: *common files: <<: *agent_binary_files - 'data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz': + 'data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz': source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz' mode: 0644 - 'data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.tar.gz': + 'data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz': source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz' mode: 0644 @@ -149,10 +149,10 @@ shared: uninstall-service-{{.BeatName}}.ps1: template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/windows/uninstall-service.ps1.tmpl' mode: 0755 - 'data/downloads/filebeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.zip': + 'data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip': source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip' mode: 0644 - 'data/downloads/metricbeat-{{ beat_version }}-{{.GOOS}}-{{.AgentArchName}}.zip': + 'data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip': source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.zip' mode: 0644 diff --git a/x-pack/agent/magefile.go b/x-pack/agent/magefile.go index d653b3bdc572..a9934e941a18 100644 --- a/x-pack/agent/magefile.go +++ b/x-pack/agent/magefile.go @@ -36,6 +36,7 @@ const ( goLicenserRepo = "github.com/elastic/go-licenser" buildDir = "build" metaDir = "_meta" + snapshotEnv = "SNAPSHOT" ) // Aliases for commands required by master makefile @@ -101,6 +102,7 @@ func (Build) GenerateConfig() error { func GolangCrossBuildOSS() error { params := devtools.DefaultGolangCrossBuildArgs() params.InputFiles = []string{"cmd/agent/agent.go"} + params.LDFlags = flagsSet() return devtools.GolangCrossBuild(params) } @@ -110,6 +112,7 @@ func GolangCrossBuild() error { params := devtools.DefaultGolangCrossBuildArgs() params.InputFiles = []string{"cmd/agent/agent.go"} params.OutputDir = "build/golang-crossbuild" + params.LDFlags = flagsSet() if err := devtools.GolangCrossBuild(params); err != nil { return err } @@ -210,7 +213,7 @@ func (Check) License() error { ) } -// Changes run git status --porcelain and return an error if we have changes or uncommited files. +// Changes run git status --porcelain and return an error if we have changes or uncommitted files. func (Check) Changes() error { out, err := sh.Output("git", "status", "--porcelain") if err != nil { @@ -354,14 +357,19 @@ func commitID() string { } func flags() string { + return strings.Join(flagsSet(), " ") +} + +func flagsSet() []string { ts := time.Now().Format(time.RFC3339) commitID := commitID() + isSnapshot, _ := os.LookupEnv(snapshotEnv) - return fmt.Sprintf( - `-X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.buildTime=%s" -X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.commit=%s"`, - ts, - commitID, - ) + return []string{ + fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.buildTime=%s"`, ts), + fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.commit=%s"`, commitID), + fmt.Sprintf(` -X "github.com/elastic/beats/v7/x-pack/agent/pkg/release.snapshot=%s"`, isSnapshot), + } } // Update is an alias for executing fields, dashboards, config, includes. diff --git a/x-pack/agent/pkg/agent/operation/common_test.go b/x-pack/agent/pkg/agent/operation/common_test.go index 5317dc7251dc..3985f296173c 100644 --- a/x-pack/agent/pkg/agent/operation/common_test.go +++ b/x-pack/agent/pkg/agent/operation/common_test.go @@ -96,8 +96,8 @@ var _ download.Downloader = &DummyDownloader{} type DummyInstaller struct { } -func (*DummyInstaller) Install(p, v, _ string) (string, error) { - return "", nil +func (*DummyInstaller) Install(p, v, _ string) error { + return nil } var _ install.Installer = &DummyInstaller{} diff --git a/x-pack/agent/pkg/agent/operation/operation_install.go b/x-pack/agent/pkg/agent/operation/operation_install.go index 3982d708434b..6f1ac2b12e4a 100644 --- a/x-pack/agent/pkg/agent/operation/operation_install.go +++ b/x-pack/agent/pkg/agent/operation/operation_install.go @@ -67,6 +67,5 @@ func (o *operationInstall) Run(ctx context.Context, application Application) (er } }() - _, err = o.installer.Install(o.program.BinaryName(), o.program.Version(), o.program.Directory()) - return err + return o.installer.Install(o.program.BinaryName(), o.program.Version(), o.program.Directory()) } diff --git a/x-pack/agent/pkg/artifact/download/localremote/downloader.go b/x-pack/agent/pkg/artifact/download/localremote/downloader.go index 7faf6a500dd5..0366556e1fec 100644 --- a/x-pack/agent/pkg/artifact/download/localremote/downloader.go +++ b/x-pack/agent/pkg/artifact/download/localremote/downloader.go @@ -10,10 +10,17 @@ import ( "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/composed" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/fs" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/http" + "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/snapshot" + "github.com/elastic/beats/v7/x-pack/agent/pkg/release" ) // NewDownloader creates a downloader which first checks local directory // and then fallbacks to remote if configured. func NewDownloader(config *artifact.Config, downloaders ...download.Downloader) download.Downloader { - return composed.NewDownloader(fs.NewDownloader(config), http.NewDownloader(config)) + d := composed.NewDownloader(fs.NewDownloader(config), http.NewDownloader(config)) + if release.Snapshot() { + return snapshot.NewDownloader(d) + } + + return d } diff --git a/x-pack/agent/pkg/artifact/download/snapshot/downloader.go b/x-pack/agent/pkg/artifact/download/snapshot/downloader.go new file mode 100644 index 000000000000..47b1d4674e73 --- /dev/null +++ b/x-pack/agent/pkg/artifact/download/snapshot/downloader.go @@ -0,0 +1,32 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package snapshot + +import ( + "context" + "fmt" + + "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" +) + +// Downloader has an embedded downloader and tweaks behavior in a way to use +// handle SNAPSHOTs only. +type Downloader struct { + embeddedDownloader download.Downloader +} + +// NewDownloader creates a snapshot downloader out of predefined downloader. +func NewDownloader(downloader download.Downloader) *Downloader { + return &Downloader{ + embeddedDownloader: downloader, + } +} + +// Download fetches the package from configured source. +// Returns absolute path to downloaded package and an error. +func (e *Downloader) Download(ctx context.Context, programName, version string) (string, error) { + version = fmt.Sprintf("%s-SNAPSHOT", version) + return e.embeddedDownloader.Download(ctx, programName, version) +} diff --git a/x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go b/x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go new file mode 100644 index 000000000000..4fd2b7827278 --- /dev/null +++ b/x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go @@ -0,0 +1,55 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package snapshot + +import ( + "context" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" +) + +type SuccDownloader struct { + called bool +} + +func (d *SuccDownloader) Download(ctx context.Context, a, b string) (string, error) { + if strings.HasSuffix(b, "-SNAPSHOT") { + d.called = true + } + return "succ", nil +} +func (d *SuccDownloader) Called() bool { return d.called } + +func TestSnapshotDownloader(t *testing.T) { + testCases := []testCase{ + testCase{ + downloader: &SuccDownloader{}, + checkFunc: func(d CheckableDownloader) bool { return d.Called() }, + }, + } + + for _, tc := range testCases { + d := NewDownloader(tc.downloader) + r, _ := d.Download(nil, "a", "b") + + assert.Equal(t, true, r == "succ") + + assert.True(t, tc.checkFunc(tc.downloader)) + } +} + +type CheckableDownloader interface { + download.Downloader + Called() bool +} + +type testCase struct { + downloader CheckableDownloader + checkFunc func(downloader CheckableDownloader) bool +} diff --git a/x-pack/agent/pkg/artifact/download/snapshot/verifier.go b/x-pack/agent/pkg/artifact/download/snapshot/verifier.go new file mode 100644 index 000000000000..339bb2ae6b2e --- /dev/null +++ b/x-pack/agent/pkg/artifact/download/snapshot/verifier.go @@ -0,0 +1,29 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package snapshot + +import ( + "fmt" + + "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" +) + +// Verifier tweaks embedded verifier to handle Snapshots only. +type Verifier struct { + embeddedVerifier download.Verifier +} + +// NewVerifier creates a snapshot verifier composed out of predefined verifier. +func NewVerifier(verifier download.Verifier) *Verifier { + return &Verifier{ + embeddedVerifier: verifier, + } +} + +// Verify checks the package from configured source. +func (e *Verifier) Verify(programName, version string) (bool, error) { + version = fmt.Sprintf("%s-SNAPSHOT", version) + return e.embeddedVerifier.Verify(programName, version) +} diff --git a/x-pack/agent/pkg/artifact/install/installer.go b/x-pack/agent/pkg/artifact/install/installer.go index b117b153be1b..9ac2195e9a0c 100644 --- a/x-pack/agent/pkg/artifact/install/installer.go +++ b/x-pack/agent/pkg/artifact/install/installer.go @@ -12,6 +12,7 @@ import ( "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/snapshot" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/tar" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/zip" + "github.com/elastic/beats/v7/x-pack/agent/pkg/release" ) var ( @@ -24,7 +25,7 @@ type Installer interface { // Install installs an artifact and returns // location of the installed program // error if something went wrong - Install(programName, version, installDir string) (string, error) + Install(programName, version, installDir string) error } // NewInstaller returns a correct installer associated with a @@ -49,5 +50,9 @@ func NewInstaller(config *artifact.Config) (Installer, error) { return nil, err } - return snapshot.NewInstaller(i) + if release.Snapshot() { + return snapshot.NewInstaller(i) + } + + return i, nil } diff --git a/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go b/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go index c32dcfc37027..374512a82406 100644 --- a/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go +++ b/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go @@ -5,9 +5,7 @@ package snapshot import ( - "os" - "path/filepath" - "strings" + "fmt" ) const snapshotIdentifier = "-SNAPSHOT" @@ -17,7 +15,7 @@ type embeddedInstaller interface { // Install installs an artifact and returns // location of the installed program // error if something went wrong - Install(programName, version, installDir string) (string, error) + Install(programName, version, installDir string) error } // Installer or zip packages @@ -34,23 +32,7 @@ func NewInstaller(installer embeddedInstaller) (*Installer, error) { // Install performs installation of program in a specific version. // It expects package to be already downloaded. -func (i *Installer) Install(programName, version, installDir string) (string, error) { - artifactPath, err := i.installer.Install(programName, version, installDir) - if err != nil { - return artifactPath, err - } - - if !strings.Contains(filepath.Base(artifactPath), snapshotIdentifier) { - return artifactPath, nil - } - - oldBase := filepath.Base(artifactPath) - newBase := strings.Replace(oldBase, snapshotIdentifier, "", 1) - newPath := strings.Replace(artifactPath, oldBase, newBase, 1) - - if err := os.Rename(artifactPath, newPath); err != nil { - return artifactPath, err - } - - return newPath, nil +func (i *Installer) Install(programName, version, installDir string) error { + version = fmt.Sprintf("%s-SNAPSHOT", version) + return i.installer.Install(programName, version, installDir) } diff --git a/x-pack/agent/pkg/artifact/install/tar/tar_installer.go b/x-pack/agent/pkg/artifact/install/tar/tar_installer.go index d1d138c683e9..1b5e28a23ee1 100644 --- a/x-pack/agent/pkg/artifact/install/tar/tar_installer.go +++ b/x-pack/agent/pkg/artifact/install/tar/tar_installer.go @@ -31,15 +31,15 @@ func NewInstaller(config *artifact.Config) (*Installer, error) { // Install performs installation of program in a specific version. // It expects package to be already downloaded. -func (i *Installer) Install(programName, version, _ string) (string, error) { +func (i *Installer) Install(programName, version, _ string) error { artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory) if err != nil { - return "", err + return err } f, err := os.Open(artifactPath) if 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)) + 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)) } defer f.Close() @@ -47,10 +47,10 @@ func (i *Installer) Install(programName, version, _ string) (string, error) { } -func unpack(r io.Reader, dir string) (string, error) { +func unpack(r io.Reader, dir string) error { zr, err := gzip.NewReader(r) if err != nil { - return "", errors.New("requires gzip-compressed body", err, errors.TypeFilesystem) + return errors.New("requires gzip-compressed body", err, errors.TypeFilesystem) } tr := tar.NewReader(zr) @@ -62,11 +62,11 @@ func unpack(r io.Reader, dir string) (string, error) { break } if err != nil { - return "", err + return err } if !validFileName(f.Name) { - return "", errors.New("tar contained invalid filename: %q", f.Name, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) + return errors.New("tar contained invalid filename: %q", f.Name, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) } rel := filepath.FromSlash(f.Name) abs := filepath.Join(dir, rel) @@ -82,12 +82,12 @@ func unpack(r io.Reader, dir string) (string, error) { case mode.IsRegular(): // just to be sure, it should already be created by Dir type if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil { - return rootDir, errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) + return errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) } wf, err := os.OpenFile(abs, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode.Perm()) if err != nil { - return rootDir, errors.New(err, "TarInstaller: creating file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) + return errors.New(err, "TarInstaller: creating file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) } _, err = io.Copy(wf, tr) @@ -95,18 +95,18 @@ func unpack(r io.Reader, dir string) (string, error) { err = closeErr } if err != nil { - return rootDir, fmt.Errorf("TarInstaller: error writing to %s: %v", abs, err) + return fmt.Errorf("TarInstaller: error writing to %s: %v", abs, err) } case mode.IsDir(): if err := os.MkdirAll(abs, 0755); err != nil { - return rootDir, errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) + return errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs)) } default: - return rootDir, errors.New(fmt.Sprintf("tar file entry %s contained unsupported file type %v", f.Name, mode), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) + return errors.New(fmt.Sprintf("tar file entry %s contained unsupported file type %v", f.Name, mode), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name)) } } - return rootDir, nil + return nil } func validFileName(p string) bool { diff --git a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go index a986484995c4..f71b1e6f71a9 100644 --- a/x-pack/agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/agent/pkg/artifact/install/zip/zip_installer.go @@ -34,30 +34,30 @@ func NewInstaller(config *artifact.Config) (*Installer, error) { // Install performs installation of program in a specific version. // It expects package to be already downloaded. -func (i *Installer) Install(programName, version, installDir string) (string, error) { +func (i *Installer) Install(programName, version, installDir string) error { artifactPath, err := artifact.GetArtifactPath(programName, version, i.config.OS(), i.config.Arch(), i.config.TargetDirectory) if err != nil { - return "", err + return err } if err := i.unzip(artifactPath, programName, version); err != nil { - return "", err + return err } rootDir, err := i.getRootDir(artifactPath) if err != nil { - return "", err + return err } // if root directory is not the same as desired directory rename // e.g contains `-windows-` or `-SNAPSHOT-` if rootDir != installDir { if err := os.Rename(rootDir, installDir); err != nil { - return rootDir, errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, installDir)) + return errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, installDir)) } } - return installDir, i.runInstall(programName, version, installDir) + return i.runInstall(programName, version, installDir) } func (i *Installer) unzip(artifactPath, programName, version string) error { diff --git a/x-pack/agent/pkg/basecmd/version/cmd.go b/x-pack/agent/pkg/basecmd/version/cmd.go index da05cfdd4978..530db169b9a9 100644 --- a/x-pack/agent/pkg/basecmd/version/cmd.go +++ b/x-pack/agent/pkg/basecmd/version/cmd.go @@ -19,6 +19,11 @@ func NewCommandWithArgs(streams *cli.IOStreams) *cobra.Command { Use: "version", Short: "Display the version of the agent.", Run: func(_ *cobra.Command, _ []string) { + version := release.Version() + if release.Snapshot() { + version = version + "-SNAPSHOT" + } + fmt.Fprintf( streams.Out, "Agent version is %s (build: %s at %s)\n", diff --git a/x-pack/agent/pkg/release/version.go b/x-pack/agent/pkg/release/version.go index d0e45df3c6cf..946e608c31e0 100644 --- a/x-pack/agent/pkg/release/version.go +++ b/x-pack/agent/pkg/release/version.go @@ -4,7 +4,10 @@ package release -import "time" +import ( + "strconv" + "time" +) // version is the current version of the agent. var version = "8.0.0" @@ -18,7 +21,10 @@ var buildTime = "" // qualifier returns the version qualifier like alpha1. var qualifier = "" -// Commit returns the current build hash or unkown if it was not injected in the build process. +// snapshot is a flag marking build as a snapshot. +var snapshot = "" + +// Commit returns the current build hash or unknown if it was not injected in the build process. func Commit() string { return commit } @@ -39,3 +45,9 @@ func Version() string { } return version + "-" + qualifier } + +// Snapshot returns true if binary was built as snapshot. +func Snapshot() bool { + val, err := strconv.ParseBool(snapshot) + return err == nil && val +} From 87771ffd734d3d63cea5749282a76a1a8389d5b4 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 23 Mar 2020 11:05:15 +0100 Subject: [PATCH 08/12] version cmd --- x-pack/agent/pkg/basecmd/version/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/agent/pkg/basecmd/version/cmd.go b/x-pack/agent/pkg/basecmd/version/cmd.go index 530db169b9a9..b2de26fd1d44 100644 --- a/x-pack/agent/pkg/basecmd/version/cmd.go +++ b/x-pack/agent/pkg/basecmd/version/cmd.go @@ -27,7 +27,7 @@ func NewCommandWithArgs(streams *cli.IOStreams) *cobra.Command { fmt.Fprintf( streams.Out, "Agent version is %s (build: %s at %s)\n", - release.Version(), + version, release.Commit(), release.BuildTime(), ) From 24f3db3d9ad689cee8052cfae3b6e04446286a3f Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 23 Mar 2020 12:07:08 +0100 Subject: [PATCH 09/12] inject env to crossbuild --- dev-tools/mage/crossbuild.go | 1 + 1 file changed, 1 insertion(+) diff --git a/dev-tools/mage/crossbuild.go b/dev-tools/mage/crossbuild.go index 1d8fb72a3377..2c4b302c0673 100644 --- a/dev-tools/mage/crossbuild.go +++ b/dev-tools/mage/crossbuild.go @@ -268,6 +268,7 @@ func (b GolangCrossBuilder) Build() error { "--rm", "--env", "MAGEFILE_VERBOSE="+verbose, "--env", "MAGEFILE_TIMEOUT="+EnvOr("MAGEFILE_TIMEOUT", ""), + "--env", fmt.Sprintf("SNAPSHOT=%v", Snapshot), "-v", repoInfo.RootDir+":"+mountPoint, "-w", workDir, image, From 1452241b9f251555b221c87a979706f8ca4aaff2 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 23 Mar 2020 12:44:26 +0100 Subject: [PATCH 10/12] check snapshot --- x-pack/agent/pkg/agent/operation/operation_fetch.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/x-pack/agent/pkg/agent/operation/operation_fetch.go b/x-pack/agent/pkg/agent/operation/operation_fetch.go index df1556544f2d..c78a83bf947d 100644 --- a/x-pack/agent/pkg/agent/operation/operation_fetch.go +++ b/x-pack/agent/pkg/agent/operation/operation_fetch.go @@ -6,6 +6,7 @@ package operation import ( "context" + "fmt" "os" "github.com/elastic/beats/v7/x-pack/agent/pkg/agent/errors" @@ -13,6 +14,7 @@ import ( "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" "github.com/elastic/beats/v7/x-pack/agent/pkg/core/logger" + "github.com/elastic/beats/v7/x-pack/agent/pkg/release" ) // operationFetch fetches artifact from preconfigured source @@ -52,7 +54,12 @@ func (o *operationFetch) Name() string { // - Fetch does not need to run if package is already present func (o *operationFetch) Check() (bool, error) { downloadConfig := o.operatorConfig.DownloadConfig - fullPath, err := artifact.GetArtifactPath(o.program.BinaryName(), o.program.Version(), downloadConfig.OS(), downloadConfig.Arch(), downloadConfig.TargetDirectory) + version := release.Version() + if release.Snapshot() { + version = fmt.Sprintf("%s-SNAPSHOT", version) + } + + fullPath, err := artifact.GetArtifactPath(o.program.BinaryName(), version, downloadConfig.OS(), downloadConfig.Arch(), downloadConfig.TargetDirectory) if err != nil { return false, err } @@ -62,7 +69,7 @@ func (o *operationFetch) Check() (bool, error) { return true, nil } - o.logger.Infof("%s.%s already exists in %s. Skipping operation %s", o.program.BinaryName(), o.program.Version(), fullPath, o.Name()) + o.logger.Infof("%s.%s already exists in %s. Skipping operation %s", o.program.BinaryName(), version, fullPath, o.Name()) return false, err } From f615a8579e83225ee8f92b001f5cf0fc62642204 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 23 Mar 2020 13:00:02 +0100 Subject: [PATCH 11/12] simplified versiony --- go.sum | 5 +- .../pkg/agent/operation/operation_fetch.go | 11 +--- .../pkg/agent/operation/operator_handlers.go | 8 ++- .../download/localremote/downloader.go | 9 +-- .../artifact/download/snapshot/downloader.go | 32 ----------- .../download/snapshot/downloader_test.go | 55 ------------------- .../artifact/download/snapshot/verifier.go | 29 ---------- .../agent/pkg/artifact/install/installer.go | 18 +----- .../install/snapshot/snapshot_installer.go | 38 ------------- 9 files changed, 15 insertions(+), 190 deletions(-) delete mode 100644 x-pack/agent/pkg/artifact/download/snapshot/downloader.go delete mode 100644 x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go delete mode 100644 x-pack/agent/pkg/artifact/download/snapshot/verifier.go delete mode 100644 x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go diff --git a/go.sum b/go.sum index fbce282b5948..a402db525809 100644 --- a/go.sum +++ b/go.sum @@ -107,8 +107,8 @@ github.com/andrewkroh/goja v0.0.0-20190128172624-dd2ac4456e20 h1:7rj9qZ63knnVo2Z github.com/andrewkroh/goja v0.0.0-20190128172624-dd2ac4456e20/go.mod h1:cI59GRkC2FRaFYtgbYEqMlgnnfvAwXzjojyZKXwklNg= github.com/andrewkroh/sys v0.0.0-20151128191922-287798fe3e43 h1:WFwa9pqou0Nb4DdfBOyaBTH0GqLE74Qwdf61E7ITHwQ= github.com/andrewkroh/sys v0.0.0-20151128191922-287798fe3e43/go.mod h1:tJPYQG4mnMeUtQvQKNkbsFrnmZOg59Qnf8CcctFv5v4= -github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q= github.com/antlr/antlr4 v0.0.0-20200225173536-225249fdaef5 h1:nkZ9axP+MvUFCu8JRN/MCY+DmTfs6lY7hE0QnJbxSdI= github.com/antlr/antlr4 v0.0.0-20200225173536-225249fdaef5/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -215,6 +215,7 @@ github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= github.com/eclipse/paho.mqtt.golang v1.2.1-0.20200121105743-0d940dd29fd2 h1:DW6WrARxK5J+o8uAKCiACi5wy9EK1UzrsCpGBPsKHAA= github.com/eclipse/paho.mqtt.golang v1.2.1-0.20200121105743-0d940dd29fd2/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts= +github.com/elastic/beats v7.6.1+incompatible h1:4iPVpFr8BSJW2fUVi+/tYXQ4v/IYVx0k2PPLTg8cfks= github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3 h1:lnDkqiRFKm0rxdljqrj3lotWinO9+jFmeDXIC4gvIQs= github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3/go.mod h1:aPqzac6AYkipvp4hufTyMj5PDIphF3+At8zr7r51xjY= github.com/elastic/ecs v1.5.0 h1:/VEIBsRU4ecq2+U3RPfKNc6bFyomP6qnthYEcQZu8GU= @@ -730,8 +731,8 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191021144547-ec77196f6094/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191112182307-2180aed22343/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= diff --git a/x-pack/agent/pkg/agent/operation/operation_fetch.go b/x-pack/agent/pkg/agent/operation/operation_fetch.go index c78a83bf947d..df1556544f2d 100644 --- a/x-pack/agent/pkg/agent/operation/operation_fetch.go +++ b/x-pack/agent/pkg/agent/operation/operation_fetch.go @@ -6,7 +6,6 @@ package operation import ( "context" - "fmt" "os" "github.com/elastic/beats/v7/x-pack/agent/pkg/agent/errors" @@ -14,7 +13,6 @@ import ( "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" "github.com/elastic/beats/v7/x-pack/agent/pkg/core/logger" - "github.com/elastic/beats/v7/x-pack/agent/pkg/release" ) // operationFetch fetches artifact from preconfigured source @@ -54,12 +52,7 @@ func (o *operationFetch) Name() string { // - Fetch does not need to run if package is already present func (o *operationFetch) Check() (bool, error) { downloadConfig := o.operatorConfig.DownloadConfig - version := release.Version() - if release.Snapshot() { - version = fmt.Sprintf("%s-SNAPSHOT", version) - } - - fullPath, err := artifact.GetArtifactPath(o.program.BinaryName(), version, downloadConfig.OS(), downloadConfig.Arch(), downloadConfig.TargetDirectory) + fullPath, err := artifact.GetArtifactPath(o.program.BinaryName(), o.program.Version(), downloadConfig.OS(), downloadConfig.Arch(), downloadConfig.TargetDirectory) if err != nil { return false, err } @@ -69,7 +62,7 @@ func (o *operationFetch) Check() (bool, error) { return true, nil } - o.logger.Infof("%s.%s already exists in %s. Skipping operation %s", o.program.BinaryName(), version, fullPath, o.Name()) + o.logger.Infof("%s.%s already exists in %s. Skipping operation %s", o.program.BinaryName(), o.program.Version(), fullPath, o.Name()) return false, err } diff --git a/x-pack/agent/pkg/agent/operation/operator_handlers.go b/x-pack/agent/pkg/agent/operation/operator_handlers.go index 2d583f8a12dd..34022763bcba 100644 --- a/x-pack/agent/pkg/agent/operation/operator_handlers.go +++ b/x-pack/agent/pkg/agent/operation/operator_handlers.go @@ -11,6 +11,7 @@ import ( "github.com/elastic/beats/v7/x-pack/agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact" "github.com/elastic/beats/v7/x-pack/agent/pkg/core/plugin/app" + "github.com/elastic/beats/v7/x-pack/agent/pkg/release" ) type handleFunc func(step configrequest.Step) error @@ -66,7 +67,12 @@ func getProgramFromStepWithTags(step configrequest.Step, artifactConfig *artifac return nil, nil, err } - p := app.NewDescriptor(step.Process, step.Version, artifactConfig, tags) + version := step.Version + if release.Snapshot() { + version = fmt.Sprintf("%s-SNAPSHOT", version) + } + + p := app.NewDescriptor(step.Process, version, artifactConfig, tags) return p, config, nil } diff --git a/x-pack/agent/pkg/artifact/download/localremote/downloader.go b/x-pack/agent/pkg/artifact/download/localremote/downloader.go index 0366556e1fec..7faf6a500dd5 100644 --- a/x-pack/agent/pkg/artifact/download/localremote/downloader.go +++ b/x-pack/agent/pkg/artifact/download/localremote/downloader.go @@ -10,17 +10,10 @@ import ( "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/composed" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/fs" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/http" - "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download/snapshot" - "github.com/elastic/beats/v7/x-pack/agent/pkg/release" ) // NewDownloader creates a downloader which first checks local directory // and then fallbacks to remote if configured. func NewDownloader(config *artifact.Config, downloaders ...download.Downloader) download.Downloader { - d := composed.NewDownloader(fs.NewDownloader(config), http.NewDownloader(config)) - if release.Snapshot() { - return snapshot.NewDownloader(d) - } - - return d + return composed.NewDownloader(fs.NewDownloader(config), http.NewDownloader(config)) } diff --git a/x-pack/agent/pkg/artifact/download/snapshot/downloader.go b/x-pack/agent/pkg/artifact/download/snapshot/downloader.go deleted file mode 100644 index 47b1d4674e73..000000000000 --- a/x-pack/agent/pkg/artifact/download/snapshot/downloader.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package snapshot - -import ( - "context" - "fmt" - - "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" -) - -// Downloader has an embedded downloader and tweaks behavior in a way to use -// handle SNAPSHOTs only. -type Downloader struct { - embeddedDownloader download.Downloader -} - -// NewDownloader creates a snapshot downloader out of predefined downloader. -func NewDownloader(downloader download.Downloader) *Downloader { - return &Downloader{ - embeddedDownloader: downloader, - } -} - -// Download fetches the package from configured source. -// Returns absolute path to downloaded package and an error. -func (e *Downloader) Download(ctx context.Context, programName, version string) (string, error) { - version = fmt.Sprintf("%s-SNAPSHOT", version) - return e.embeddedDownloader.Download(ctx, programName, version) -} diff --git a/x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go b/x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go deleted file mode 100644 index 4fd2b7827278..000000000000 --- a/x-pack/agent/pkg/artifact/download/snapshot/downloader_test.go +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package snapshot - -import ( - "context" - "strings" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" -) - -type SuccDownloader struct { - called bool -} - -func (d *SuccDownloader) Download(ctx context.Context, a, b string) (string, error) { - if strings.HasSuffix(b, "-SNAPSHOT") { - d.called = true - } - return "succ", nil -} -func (d *SuccDownloader) Called() bool { return d.called } - -func TestSnapshotDownloader(t *testing.T) { - testCases := []testCase{ - testCase{ - downloader: &SuccDownloader{}, - checkFunc: func(d CheckableDownloader) bool { return d.Called() }, - }, - } - - for _, tc := range testCases { - d := NewDownloader(tc.downloader) - r, _ := d.Download(nil, "a", "b") - - assert.Equal(t, true, r == "succ") - - assert.True(t, tc.checkFunc(tc.downloader)) - } -} - -type CheckableDownloader interface { - download.Downloader - Called() bool -} - -type testCase struct { - downloader CheckableDownloader - checkFunc func(downloader CheckableDownloader) bool -} diff --git a/x-pack/agent/pkg/artifact/download/snapshot/verifier.go b/x-pack/agent/pkg/artifact/download/snapshot/verifier.go deleted file mode 100644 index 339bb2ae6b2e..000000000000 --- a/x-pack/agent/pkg/artifact/download/snapshot/verifier.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package snapshot - -import ( - "fmt" - - "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/download" -) - -// Verifier tweaks embedded verifier to handle Snapshots only. -type Verifier struct { - embeddedVerifier download.Verifier -} - -// NewVerifier creates a snapshot verifier composed out of predefined verifier. -func NewVerifier(verifier download.Verifier) *Verifier { - return &Verifier{ - embeddedVerifier: verifier, - } -} - -// Verify checks the package from configured source. -func (e *Verifier) Verify(programName, version string) (bool, error) { - version = fmt.Sprintf("%s-SNAPSHOT", version) - return e.embeddedVerifier.Verify(programName, version) -} diff --git a/x-pack/agent/pkg/artifact/install/installer.go b/x-pack/agent/pkg/artifact/install/installer.go index 9ac2195e9a0c..b8860f6b003f 100644 --- a/x-pack/agent/pkg/artifact/install/installer.go +++ b/x-pack/agent/pkg/artifact/install/installer.go @@ -9,10 +9,8 @@ import ( "runtime" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact" - "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/snapshot" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/tar" "github.com/elastic/beats/v7/x-pack/agent/pkg/artifact/install/zip" - "github.com/elastic/beats/v7/x-pack/agent/pkg/release" ) var ( @@ -38,21 +36,9 @@ func NewInstaller(config *artifact.Config) (Installer, error) { return nil, ErrConfigNotProvided } - var i Installer - var err error - if runtime.GOOS == "windows" { - i, err = zip.NewInstaller(config) - } else { - i, err = tar.NewInstaller(config) - } - if err != nil { - return nil, err - } - - if release.Snapshot() { - return snapshot.NewInstaller(i) + return zip.NewInstaller(config) } - return i, nil + return tar.NewInstaller(config) } diff --git a/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go b/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go deleted file mode 100644 index 374512a82406..000000000000 --- a/x-pack/agent/pkg/artifact/install/snapshot/snapshot_installer.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package snapshot - -import ( - "fmt" -) - -const snapshotIdentifier = "-SNAPSHOT" - -// embeddedInstaller is an interface allowing installation of an artifact -type embeddedInstaller interface { - // Install installs an artifact and returns - // location of the installed program - // error if something went wrong - Install(programName, version, installDir string) error -} - -// Installer or zip packages -type Installer struct { - installer embeddedInstaller -} - -// NewInstaller creates an installer able to install zip packages -func NewInstaller(installer embeddedInstaller) (*Installer, error) { - return &Installer{ - installer: installer, - }, nil -} - -// Install performs installation of program in a specific version. -// It expects package to be already downloaded. -func (i *Installer) Install(programName, version, installDir string) error { - version = fmt.Sprintf("%s-SNAPSHOT", version) - return i.installer.Install(programName, version, installDir) -} From e49481fbfac2de0fd1941ab0934f3c47486462c6 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 23 Mar 2020 13:46:49 +0100 Subject: [PATCH 12/12] mage vendor --- go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/go.sum b/go.sum index a402db525809..f2ac7c185e09 100644 --- a/go.sum +++ b/go.sum @@ -215,7 +215,6 @@ github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= github.com/eclipse/paho.mqtt.golang v1.2.1-0.20200121105743-0d940dd29fd2 h1:DW6WrARxK5J+o8uAKCiACi5wy9EK1UzrsCpGBPsKHAA= github.com/eclipse/paho.mqtt.golang v1.2.1-0.20200121105743-0d940dd29fd2/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts= -github.com/elastic/beats v7.6.1+incompatible h1:4iPVpFr8BSJW2fUVi+/tYXQ4v/IYVx0k2PPLTg8cfks= github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3 h1:lnDkqiRFKm0rxdljqrj3lotWinO9+jFmeDXIC4gvIQs= github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3/go.mod h1:aPqzac6AYkipvp4hufTyMj5PDIphF3+At8zr7r51xjY= github.com/elastic/ecs v1.5.0 h1:/VEIBsRU4ecq2+U3RPfKNc6bFyomP6qnthYEcQZu8GU=