Skip to content

Commit

Permalink
[Agent] Allow CLI paths override (#17781) (#18169)
Browse files Browse the repository at this point in the history
[Agent] Allow CLI paths override (#17781)
  • Loading branch information
michalpristas authored May 4, 2020
1 parent 65e7348 commit e107dc4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 27 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 @@ -37,4 +37,5 @@
- Display the stability of the agent at enroll and start. {pull}17336[17336]
- Expose stream.* variables in events {pull}17468[17468]
- Monitoring configuration reloadable {pull}17855[17855]
- Allow CLI overrides of paths {pull}17781[17781]
- Enable Filebeat input: S3, Azureeventhub, cloudfoundry, httpjson, netflow, o365audit. {pull}17909[17909]
12 changes: 8 additions & 4 deletions x-pack/elastic-agent/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ import (
"flag"
"testing"

// Just using this a place holder.
"github.com/elastic/beats/v7/x-pack/filebeat/cmd"
"github.com/spf13/cobra"
)

var systemTest *bool

func init() {
testing.Init()

cmd := &cobra.Command{
Use: "elastic-agent [subcommand]",
}

systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
}

// Test started when the test binary is started. Only calls main.
Expand Down
41 changes: 36 additions & 5 deletions x-pack/elastic-agent/pkg/agent/application/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,64 @@ import (
"os"
"path/filepath"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
)

var (
homePath string
dataPath string
homePath string
dataPath string
overwrites *common.Config
)

func init() {
homePath = retrieveExecutablePath()
dataPath = retrieveDataPath()
overwrites = common.NewConfig()
common.ConfigOverwriteFlag(nil, overwrites, "path.home", "path.home", "", "Agent root path")
common.ConfigOverwriteFlag(nil, overwrites, "path.data", "path.data", "", "Data path contains Agent managed binaries")
}

// HomePath returns home path where.
func HomePath() string {
if val, err := overwrites.String("path.home", -1); err == nil {
return val
}

return homePath
}

// DataPath returns data path where.
func DataPath() string {
if val, err := overwrites.String("path.data", -1); err == nil {
return val
}

return dataPath
}

// InjectAgentConfig injects config to a provided configuration.
func InjectAgentConfig(c *config.Config) error {
globalConfig := AgentGlobalConfig()
globalConfig := agentGlobalConfig()
if err := c.Merge(globalConfig); err != nil {
return errors.New("failed to inject agent global config", err, errors.TypeConfig)
}

return injectOverwrites(c)
}

func injectOverwrites(c *config.Config) error {
if err := c.Merge(overwrites); err != nil {
return errors.New("failed to inject agent overwrites", err, errors.TypeConfig)
}

return nil
}

// AgentGlobalConfig gets global config used for resolution of variables inside configuration
// agentGlobalConfig gets global config used for resolution of variables inside configuration
// such as ${path.data}.
func AgentGlobalConfig() map[string]interface{} {
func agentGlobalConfig() map[string]interface{} {
return map[string]interface{}{
"path": map[string]interface{}{
"data": dataPath,
Expand Down
18 changes: 9 additions & 9 deletions x-pack/elastic-agent/pkg/agent/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
package cmd

import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/basecmd"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
)

var defaultConfig = "elastic-agent.yml"
const defaultConfig = "elastic-agent.yml"

type globalFlags struct {
PathConfigFile string
PathConfig string
PathData string
PathHome string
PathLogs string
PathConfigFile string
FlagStrictPerms bool
}

// Config returns path which identifies configuration file.
func (f *globalFlags) Config() string {
if len(f.PathConfigFile) == 0 {
return filepath.Join(f.PathHome, defaultConfig)
return filepath.Join(application.HomePath(), defaultConfig)
}
return f.PathConfigFile
}
Expand All @@ -50,11 +50,11 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {

flags := &globalFlags{}

cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.home"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.data"))

cmd.PersistentFlags().StringVarP(&flags.PathConfigFile, "", "c", defaultConfig, fmt.Sprintf(`Configuration file, relative to path.config (default "%s")`, defaultConfig))
cmd.PersistentFlags().StringVarP(&flags.PathHome, "path.home", "", "", "Home path")
cmd.PersistentFlags().StringVarP(&flags.PathConfig, "path.config", "", "${path.home}", "Configuration path")
cmd.PersistentFlags().StringVarP(&flags.PathData, "path.data", "", "${path.home}/data", "Data path")
cmd.PersistentFlags().StringVarP(&flags.PathLogs, "path.logs", "", "${path.home}/logs", "Logs path")
cmd.PersistentFlags().BoolVarP(&flags.FlagStrictPerms, "strict.perms", "", true, "Strict permission checking on config files")

// Add version.
Expand Down
10 changes: 5 additions & 5 deletions x-pack/elastic-agent/pkg/agent/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func newEnrollCommandWithArgs(flags *globalFlags, _ []string, streams *cli.IOStr

func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args []string) error {
warn.PrintNotGA(streams.Out)

config, err := config.LoadYAML(flags.PathConfigFile)
pathConfigFile := flags.Config()
config, err := config.LoadYAML(pathConfigFile)
if err != nil {
return errors.New(err,
fmt.Sprintf("could not read configuration file %s", flags.PathConfigFile),
fmt.Sprintf("could not read configuration file %s", pathConfigFile),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, flags.PathConfigFile))
errors.M(errors.MetaKeyPath, pathConfigFile))
}

force, _ := cmd.Flags().GetBool("force")
Expand Down Expand Up @@ -95,7 +95,7 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args
c, err := application.NewEnrollCmd(
logger,
&options,
flags.PathConfigFile,
pathConfigFile,
)

if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions x-pack/elastic-agent/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ func newRunCommandWithArgs(flags *globalFlags, _ []string, streams *cli.IOStream
}

func run(flags *globalFlags, streams *cli.IOStreams) error {
config, err := config.LoadYAML(flags.PathConfigFile)
pathConfigFile := flags.Config()
config, err := config.LoadYAML(pathConfigFile)
if err != nil {
return errors.New(err,
fmt.Sprintf("could not read configuration file %s", flags.PathConfigFile),
fmt.Sprintf("could not read configuration file %s", pathConfigFile),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, flags.PathConfigFile))
errors.M(errors.MetaKeyPath, pathConfigFile))
}

logger, err := logger.NewFromConfig(config)
if err != nil {
return err
}

app, err := application.New(logger, flags.PathConfigFile)
app, err := application.New(logger, pathConfigFile)
if err != nil {
return err
}
Expand Down

0 comments on commit e107dc4

Please sign in to comment.