diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index deae25227733..d01c8a1c7bf4 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -16,6 +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 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 b646f3796ba8..fca3dbd88288 100644 --- a/x-pack/elastic-agent/pkg/agent/application/paths/paths.go +++ b/x-pack/elastic-agent/pkg/agent/application/paths/paths.go @@ -10,14 +10,20 @@ import ( "os" "path/filepath" "strings" + "sync" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release" ) +const ( + tempSubdir = "tmp" +) + var ( topPath string configPath string logsPath string + tmpCreator sync.Once ) func init() { @@ -37,6 +43,16 @@ func Top() string { return topPath } +// TempDir returns agent temp dir located within data dir. +func TempDir() string { + 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 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 5e26436bfc41..3dc0dbe232a3 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 d6266659b7d3..a0bfa213ca7e 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 @@ -14,6 +14,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" ) func TestOKInstall(t *testing.T) { @@ -25,7 +27,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 +61,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() {