Skip to content

Commit

Permalink
[Elastic-Agent] Follow home path for all config files (#18161)
Browse files Browse the repository at this point in the history
* fleet.yml at home

* action_store

* changelog
  • Loading branch information
michalpristas committed May 4, 2020
1 parent 4bf5d62 commit 1c6b278
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@
- Use data subfolder as default for process logs {pull}17960[17960]
- Do not require unnecessary configuration {pull}18003[18003]
- Enable debug log level for Metricbeat and Filebeat when run under the Elastic Agent. {pull}17935[17935]
- Follow home path for all config files {pull}18161[18161]
11 changes: 0 additions & 11 deletions x-pack/elastic-agent/pkg/agent/application/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@ import (
"fmt"
"time"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/kibana"
fleetreporter "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/reporter/fleet"
logreporter "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/reporter/log"
)

// TODO(ph) correctly setup global path.
func fleetAgentConfigPath() string {
return info.AgentConfigFile
}

// TODO(ph) correctly setup with global path.
func fleetActionStoreFile() string {
return info.AgentActionStoreFile
}

// Config define the configuration of the Agent.
type Config struct {
Management *config.Config `config:"management"`
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewEnrollCmd(
store := storage.NewReplaceOnSuccessStore(
configPath,
DefaultAgentFleetConfig,
storage.NewEncryptedDiskStore(fleetAgentConfigPath(), []byte("")),
storage.NewEncryptedDiskStore(info.AgentConfigFile(), []byte("")),
)

return NewEnrollCmdWithStore(
Expand Down
33 changes: 24 additions & 9 deletions x-pack/elastic-agent/pkg/agent/application/info/agent_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import (
"bytes"
"fmt"
"io"
"path/filepath"

"github.com/gofrs/uuid"
"gopkg.in/yaml.v2"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/storage"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
)

// AgentConfigFile is a name of file used to store agent information
const AgentConfigFile = "fleet.yml"
// defaultAgentConfigFile is a name of file used to store agent information
const defaultAgentConfigFile = "fleet.yml"
const agentInfoKey = "agent_info"

// AgentActionStoreFile is the file that will contains the action that can be replayed after restart.
const AgentActionStoreFile = "action_store.yml"
// defaultAgentActionStoreFile is the file that will contains the action that can be replayed after restart.
const defaultAgentActionStoreFile = "action_store.yml"

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

// AgentConfigFile is a name of file used to store agent information
func AgentConfigFile() string {
return filepath.Join(paths.Home(), defaultAgentConfigFile)
}

// AgentActionStoreFile is the file that will contains the action that can be replayed after restart.
func AgentActionStoreFile() string {
return filepath.Join(paths.Home(), defaultAgentActionStoreFile)
}

func generateAgentID() (string, error) {
uid, err := uuid.NewV4()
if err != nil {
Expand All @@ -43,7 +55,8 @@ func generateAgentID() (string, error) {
}

func loadAgentInfo(forceUpdate bool) (*persistentAgentInfo, error) {
s := storage.NewEncryptedDiskStore(AgentConfigFile, []byte(""))
agentConfigFile := AgentConfigFile()
s := storage.NewEncryptedDiskStore(agentConfigFile, []byte(""))

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

func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
agentConfigFile := AgentConfigFile()
reader, err := s.Load()
if err != nil {
return nil, err
Expand All @@ -75,9 +89,9 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) {
cfg, err := config.NewConfigFrom(reader)
if err != nil {
return nil, errors.New(err,
fmt.Sprintf("fail to read configuration %s for the agent", AgentConfigFile),
fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, AgentConfigFile))
errors.M(errors.MetaKeyPath, agentConfigFile))
}

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

func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error {
agentConfigFile := AgentConfigFile()
reader, err := s.Load()
if err != nil {
return err
}

cfg, err := config.NewConfigFrom(reader)
if err != nil {
return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", AgentConfigFile),
return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, AgentConfigFile))
errors.M(errors.MetaKeyPath, agentConfigFile))
}

if err := reader.Close(); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions x-pack/elastic-agent/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newManaged(
return nil, err
}

path := fleetAgentConfigPath()
path := info.AgentConfigFile()

// TODO(ph): Define the encryption password.
store := storage.NewEncryptedDiskStore(path, []byte(""))
Expand Down Expand Up @@ -145,9 +145,9 @@ func newManaged(
batchedAcker := newLazyAcker(acker)

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

Expand Down

0 comments on commit 1c6b278

Please sign in to comment.