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

[Agent] Allow CLI paths override #17781

Merged
merged 11 commits into from
Apr 28, 2020
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
- Introduced `mage demo` command {pull}17312[17312]
- Display the stability of the agent at enroll and start. {pull}17336[17336]
- Expose stream.* variables in events {pull}17468[17468]
- Allow CLI overrides of paths {pull}17781[17781]
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.SettingFlag(nil, "E", "Configuration overwrite")
common.ConfigOverwriteFlag(nil, overwrites, "path.home", "path.home", "", "Agent root path")
common.ConfigOverwriteFlag(nil, overwrites, "path.data", "path.data", "", "Data path, agent usually look for beats here")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename Data path, agent usually look for beats here to Data path contains Agent managed binary")

}

// 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
19 changes: 10 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,12 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {

flags := &globalFlags{}

cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("E"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see code to make this work?

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