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 #21883 to 7.x: Use local temp instead of system one #21905

Merged
merged 2 commits into from
Oct 21, 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.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
- Fix issue with named pipes on Windows 7 {pull}21931[21931]
- Rename monitoring index from `elastic.agent` to `elastic_agent` {pull}21932[21932]

Expand Down
16 changes: 16 additions & 0 deletions x-pack/elastic-agent/pkg/agent/application/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down