@@ -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"
2224const 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
2729type 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+
3648func generateAgentID () (string , error ) {
3749 uid , err := uuid .NewV4 ()
3850 if err != nil {
@@ -43,7 +55,8 @@ func generateAgentID() (string, error) {
4355}
4456
4557func 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
6982func 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
112126func 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 {
0 commit comments