Skip to content

Commit 8e91aa7

Browse files
committed
[Elastic-Agent] Follow home path for all config files (#18161)
* fleet.yml at home * action_store * changelog
1 parent a1ee6fd commit 8e91aa7

5 files changed

Lines changed: 29 additions & 24 deletions

File tree

x-pack/elastic-agent/CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@
4242
- Allow CLI overrides of paths {pull}17781[17781]
4343
- Enable Filebeat input: S3, Azureeventhub, cloudfoundry, httpjson, netflow, o365audit. {pull}17909[17909]
4444
- Use data subfolder as default for process logs {pull}17960[17960]
45+
- Follow home path for all config files {pull}18161[18161]

x-pack/elastic-agent/pkg/agent/application/config.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,13 @@ import (
88
"fmt"
99
"time"
1010

11-
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info"
1211
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
1312
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
1413
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/kibana"
1514
fleetreporter "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/reporter/fleet"
1615
logreporter "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/reporter/log"
1716
)
1817

19-
// TODO(ph) correctly setup global path.
20-
func fleetAgentConfigPath() string {
21-
return info.AgentConfigFile
22-
}
23-
24-
// TODO(ph) correctly setup with global path.
25-
func fleetActionStoreFile() string {
26-
return info.AgentActionStoreFile
27-
}
28-
2918
// Config define the configuration of the Agent.
3019
type Config struct {
3120
Management *config.Config `config:"management"`

x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func NewEnrollCmd(
9191
store := storage.NewReplaceOnSuccessStore(
9292
configPath,
9393
DefaultAgentFleetConfig,
94-
storage.NewEncryptedDiskStore(fleetAgentConfigPath(), []byte("")),
94+
storage.NewEncryptedDiskStore(info.AgentConfigFile(), []byte("")),
9595
)
9696

9797
return NewEnrollCmdWithStore(

x-pack/elastic-agent/pkg/agent/application/info/agent_id.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ import (
88
"bytes"
99
"fmt"
1010
"io"
11+
"path/filepath"
1112

1213
"github.com/gofrs/uuid"
1314
"gopkg.in/yaml.v2"
1415

16+
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
1517
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
1618
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/storage"
1719
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
1820
)
1921

20-
// AgentConfigFile is a name of file used to store agent information
21-
const AgentConfigFile = "fleet.yml"
22+
// defaultAgentConfigFile is a name of file used to store agent information
23+
const defaultAgentConfigFile = "fleet.yml"
2224
const agentInfoKey = "agent_info"
2325

24-
// AgentActionStoreFile is the file that will contains the action that can be replayed after restart.
25-
const AgentActionStoreFile = "action_store.yml"
26+
// defaultAgentActionStoreFile is the file that will contains the action that can be replayed after restart.
27+
const defaultAgentActionStoreFile = "action_store.yml"
2628

2729
type persistentAgentInfo struct {
2830
ID string `json:"ID" yaml:"ID" config:"ID"`
@@ -33,6 +35,16 @@ type ioStore interface {
3335
Load() (io.ReadCloser, error)
3436
}
3537

38+
// AgentConfigFile is a name of file used to store agent information
39+
func AgentConfigFile() string {
40+
return filepath.Join(paths.Home(), defaultAgentConfigFile)
41+
}
42+
43+
// AgentActionStoreFile is the file that will contains the action that can be replayed after restart.
44+
func AgentActionStoreFile() string {
45+
return filepath.Join(paths.Home(), defaultAgentActionStoreFile)
46+
}
47+
3648
func generateAgentID() (string, error) {
3749
uid, err := uuid.NewV4()
3850
if err != nil {
@@ -43,7 +55,8 @@ func generateAgentID() (string, error) {
4355
}
4456

4557
func loadAgentInfo(forceUpdate bool) (*persistentAgentInfo, error) {
46-
s := storage.NewEncryptedDiskStore(AgentConfigFile, []byte(""))
58+
agentConfigFile := AgentConfigFile()
59+
s := storage.NewEncryptedDiskStore(agentConfigFile, []byte(""))
4760

4861
agentinfo, err := getInfoFromStore(s)
4962
if err != nil {
@@ -67,6 +80,7 @@ func loadAgentInfo(forceUpdate bool) (*persistentAgentInfo, error) {
6780
}
6881

6982
func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
83+
agentConfigFile := AgentConfigFile()
7084
reader, err := s.Load()
7185
if err != nil {
7286
return nil, err
@@ -75,9 +89,9 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
7589
cfg, err := config.NewConfigFrom(reader)
7690
if err != nil {
7791
return nil, errors.New(err,
78-
fmt.Sprintf("fail to read configuration %s for the agent", AgentConfigFile),
92+
fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile),
7993
errors.TypeFilesystem,
80-
errors.M(errors.MetaKeyPath, AgentConfigFile))
94+
errors.M(errors.MetaKeyPath, agentConfigFile))
8195
}
8296

8397
if err := reader.Close(); err != nil {
@@ -110,16 +124,17 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
110124
}
111125

112126
func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error {
127+
agentConfigFile := AgentConfigFile()
113128
reader, err := s.Load()
114129
if err != nil {
115130
return err
116131
}
117132

118133
cfg, err := config.NewConfigFrom(reader)
119134
if err != nil {
120-
return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", AgentConfigFile),
135+
return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile),
121136
errors.TypeFilesystem,
122-
errors.M(errors.MetaKeyPath, AgentConfigFile))
137+
errors.M(errors.MetaKeyPath, agentConfigFile))
123138
}
124139

125140
if err := reader.Close(); err != nil {

x-pack/elastic-agent/pkg/agent/application/managed_mode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func newManaged(
6868
return nil, err
6969
}
7070

71-
path := fleetAgentConfigPath()
71+
path := info.AgentConfigFile()
7272

7373
// TODO(ph): Define the encryption password.
7474
store := storage.NewEncryptedDiskStore(path, []byte(""))
@@ -138,9 +138,9 @@ func newManaged(
138138
batchedAcker := newLazyAcker(acker)
139139

140140
// Create the action store that will persist the last good policy change on disk.
141-
actionStore, err := newActionStore(log, storage.NewDiskStore(fleetActionStoreFile()))
141+
actionStore, err := newActionStore(log, storage.NewDiskStore(info.AgentActionStoreFile()))
142142
if err != nil {
143-
return nil, errors.New(err, fmt.Sprintf("fail to read action store '%s'", fleetActionStoreFile()))
143+
return nil, errors.New(err, fmt.Sprintf("fail to read action store '%s'", info.AgentActionStoreFile()))
144144
}
145145
actionAcker := newActionStoreAcker(batchedAcker, actionStore)
146146

0 commit comments

Comments
 (0)