From 4ed31924797e34be0f11e793c531684f13ffc05a Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 16 Oct 2020 09:05:27 +0200 Subject: [PATCH 1/5] use tempdir within data --- .../pkg/agent/application/paths/paths.go | 12 ++++++++++++ .../pkg/artifact/install/atomic/atomic_installer.go | 4 +++- .../artifact/install/atomic/atomic_installer_test.go | 5 +++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/paths/paths.go b/x-pack/elastic-agent/pkg/agent/application/paths/paths.go index b646f3796ba..69dcd325c68 100644 --- a/x-pack/elastic-agent/pkg/agent/application/paths/paths.go +++ b/x-pack/elastic-agent/pkg/agent/application/paths/paths.go @@ -14,6 +14,10 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release" ) +const ( + tempSubdir = "tmp" +) + var ( topPath string configPath string @@ -29,6 +33,9 @@ func init() { fs.StringVar(&topPath, "path.home", topPath, "Agent root path") fs.StringVar(&configPath, "path.config", configPath, "Config path is the directory Agent looks for its config file") fs.StringVar(&logsPath, "path.logs", logsPath, "Logs path contains Agent log output") + + // create tempdir as it probably don't exists + os.MkdirAll(TempDir(), 0750) } // Top returns the top directory for Elastic Agent, all the versioned @@ -37,6 +44,11 @@ func Top() string { return topPath } +// TempDir returns agent temp dir located within data dir. +func TempDir() string { + return filepath.Join(Data(), tempSubdir) +} + // Home returns a directory where binary lives func Home() string { return versionedHome(topPath) 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 5e26436bfc4..3dc0dbe232a 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 @@ -9,6 +9,8 @@ import ( "io/ioutil" "os" "path/filepath" + + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" ) type embeddedInstaller interface { @@ -31,7 +33,7 @@ func NewInstaller(i embeddedInstaller) (*Installer, error) { // Install performs installation of program in a specific version. func (i *Installer) Install(ctx context.Context, programName, version, installDir string) error { // tar installer uses Dir of installDir to determine location of unpack - tempDir, err := ioutil.TempDir(os.TempDir(), "elastic-agent-install") + tempDir, err := ioutil.TempDir(paths.TempDir(), "elastic-agent-install") if err != nil { return err } diff --git a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go index d6266659b7d..e91877706b8 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go +++ b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go @@ -13,6 +13,7 @@ import ( "sync" "testing" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" "github.com/stretchr/testify/assert" ) @@ -25,7 +26,7 @@ func TestOKInstall(t *testing.T) { assert.NoError(t, err) ctx := context.Background() - installDir := filepath.Join(os.TempDir(), "install_dir") + installDir := filepath.Join(paths.TempDir(), "install_dir") wg.Add(1) go func() { @@ -59,7 +60,7 @@ func TestContextCancelledInstall(t *testing.T) { assert.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) - installDir := filepath.Join(os.TempDir(), "install_dir") + installDir := filepath.Join(paths.TempDir(), "install_dir") wg.Add(1) go func() { From f8a5ad703ec24d683d8147bd948680c5501e9924 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 16 Oct 2020 09:58:30 +0200 Subject: [PATCH 2/5] lazy mkdir --- .../pkg/agent/application/paths/paths.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/paths/paths.go b/x-pack/elastic-agent/pkg/agent/application/paths/paths.go index 69dcd325c68..fca3dbd8828 100644 --- a/x-pack/elastic-agent/pkg/agent/application/paths/paths.go +++ b/x-pack/elastic-agent/pkg/agent/application/paths/paths.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "strings" + "sync" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release" ) @@ -22,6 +23,7 @@ var ( topPath string configPath string logsPath string + tmpCreator sync.Once ) func init() { @@ -33,9 +35,6 @@ func init() { fs.StringVar(&topPath, "path.home", topPath, "Agent root path") fs.StringVar(&configPath, "path.config", configPath, "Config path is the directory Agent looks for its config file") fs.StringVar(&logsPath, "path.logs", logsPath, "Logs path contains Agent log output") - - // create tempdir as it probably don't exists - os.MkdirAll(TempDir(), 0750) } // Top returns the top directory for Elastic Agent, all the versioned @@ -46,7 +45,12 @@ func Top() string { // TempDir returns agent temp dir located within data dir. func TempDir() string { - return filepath.Join(Data(), tempSubdir) + tmpDir := filepath.Join(Data(), tempSubdir) + tmpCreator.Do(func() { + // create tempdir as it probably don't exists + os.MkdirAll(tmpDir, 0750) + }) + return tmpDir } // Home returns a directory where binary lives From ce9554c6ea6ff4f2e20debdfe53eac435f619799 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 16 Oct 2020 10:44:59 +0200 Subject: [PATCH 3/5] fmt --- .../pkg/artifact/install/atomic/atomic_installer_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go index e91877706b8..a0bfa213ca7 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go +++ b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer_test.go @@ -13,8 +13,9 @@ import ( "sync" "testing" - "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" "github.com/stretchr/testify/assert" + + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" ) func TestOKInstall(t *testing.T) { From 6865dda5a307f4c1db6781728af1ffd93309864b Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 16 Oct 2020 10:48:25 +0200 Subject: [PATCH 4/5] changelog --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index deae2522773..cd4064adebc 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -16,7 +16,7 @@ - Include inputs in action store actions {pull}21298[21298] - Fix issue where inputs without processors defined would panic {pull}21628[21628] - Partial extracted beat result in failure to spawn beat {issue}21718[21718] - +- Use local temp instead of system one {pull}21883[21883] ==== New features - Prepare packaging for endpoint and asc files {pull}20186[20186] From f9549dec222adc73a10fecc17e76c2ca25d07254 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 16 Oct 2020 10:49:01 +0200 Subject: [PATCH 5/5] Update CHANGELOG.next.asciidoc --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index cd4064adebc..d01c8a1c7bf 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -17,6 +17,7 @@ - Fix issue where inputs without processors defined would panic {pull}21628[21628] - Partial extracted beat result in failure to spawn beat {issue}21718[21718] - Use local temp instead of system one {pull}21883[21883] + ==== New features - Prepare packaging for endpoint and asc files {pull}20186[20186]