Skip to content

Commit

Permalink
start of changing argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
leehinman committed Oct 17, 2024
1 parent a706c79 commit 6d0ff13
Show file tree
Hide file tree
Showing 22 changed files with 120 additions and 69 deletions.
2 changes: 2 additions & 0 deletions auditbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/elastic/beats/v7/auditbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
)

Expand All @@ -39,6 +40,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(*testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
2 changes: 2 additions & 0 deletions filebeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

fbcmd "github.com/elastic/beats/v7/filebeat/cmd"
inputs "github.com/elastic/beats/v7/filebeat/input/default-inputs"
"github.com/elastic/beats/v7/libbeat/cfgfile"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
)
Expand All @@ -45,6 +46,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
if err := fbCommand.Execute(); err != nil {
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions heartbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/elastic/beats/v7/heartbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
)

Expand All @@ -38,6 +39,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(_ *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
96 changes: 67 additions & 29 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,61 @@
package cfgfile

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

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/fleetmode"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
)

// Command line flags.
// Evil package level globals
var (
// The default config cannot include the beat name as it is not initialized
// when this variable is created. See ChangeDefaultCfgfileFlag which should
// be called prior to flags.Parse().
configfiles = config.StringArrFlag(nil, "c", "beat.yml", "Configuration file, relative to path.config")
overwrites = config.SettingFlag(nil, "E", "Configuration overwrite")

// Additional default settings, that must be available for variable expansion
defaults = config.MustNewConfigFrom(map[string]interface{}{
"path": map[string]interface{}{
"home": ".", // to be initialized by beat
"config": "${path.home}",
"data": fmt.Sprint("${path.home}", string(os.PathSeparator), "data"),
"logs": fmt.Sprint("${path.home}", string(os.PathSeparator), "logs"),
},
})

// home-path CLI flag (initialized in init)
homePath *string
configPath *string
once sync.Once
configfiles *config.StringsFlag
overwrites *config.C
defaults *config.C
homePath *string
configPath *string
dataPath *string
logsPath *string
)

func init() {
// add '-path.x' options overwriting paths in 'overwrites' config
makePathFlag := func(name, usage string) *string {
return config.ConfigOverwriteFlag(nil, overwrites, name, name, "", usage)
}
func Initialize() {
once.Do(func() {
// The default config cannot include the beat name as
// it is not initialized when this variable is
// created. See ChangeDefaultCfgfileFlag which should
// be called prior to flags.Parse().
configfiles = config.StringArrFlag(nil, "c", "beat.yml", "Configuration file, relative to path.config")
overwrites = config.SettingFlag(nil, "E", "Configuration overwrite")
defaults = config.MustNewConfigFrom(map[string]interface{}{
"path": map[string]interface{}{
"home": ".", // to be initialized by beat
"config": "${path.home}",
"data": fmt.Sprint("${path.home}", string(os.PathSeparator), "data"),
"logs": fmt.Sprint("${path.home}", string(os.PathSeparator), "logs"),
},
})
homePath = config.ConfigOverwriteFlag(nil, overwrites, "path.home", "path.home", "", "Home path")
configPath = config.ConfigOverwriteFlag(nil, overwrites, "path.config", "path.config", "", "Configuration path")
dataPath = config.ConfigOverwriteFlag(nil, overwrites, "path.data", "path.data", "", "Data path")
logsPath = config.ConfigOverwriteFlag(nil, overwrites, "path.logs", "path.logs", "", "Logs path")
})
}

homePath = makePathFlag("path.home", "Home path")
configPath = makePathFlag("path.config", "Configuration path")
makePathFlag("path.data", "Data path")
makePathFlag("path.logs", "Logs path")
func ConvertFlagsForBackwardsCompatibility() {
// backwards compatibility workaround, convert -flags to --flags:
for i, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 {
os.Args[1+i] = "-" + arg
}
}
}

// OverrideChecker checks if a config should be overwritten.
Expand All @@ -76,6 +88,7 @@ type ConditionalOverride struct {
// ChangeDefaultCfgfileFlag replaces the value and default value for the `-c`
// flag so that it reflects the beat name.
func ChangeDefaultCfgfileFlag(beatName string) error {
Initialize()
configfiles.SetDefault(beatName + ".yml")
return nil
}
Expand All @@ -97,7 +110,10 @@ func GetDefaultCfgfile() string {
}

// HandleFlags adapts default config settings based on command line flags.
// This also stores if -E management.enabled=true was set on command line
// to determine if running the Beat under agent.
func HandleFlags() error {
Initialize()
// default for the home path is the binary location
home, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
Expand All @@ -114,6 +130,27 @@ func HandleFlags() error {
common.PrintConfigDebugf(overwrites, "CLI setting overwrites (-E flag):")
}

// Enable check to see if beat is running under Agent
// This is stored in a package so the modules which don't have
// access to the config can check this value.
type management struct {
Enabled bool `config:"management.enabled"`
}
var managementSettings management
cfgFlag := flag.Lookup("E")
if cfgFlag == nil {
fleetmode.SetAgentMode(false)
return nil
}
cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag)
cliCfg := cfgObject.Config()

err = cliCfg.Unpack(&managementSettings)
if err != nil {
fleetmode.SetAgentMode(false)
return nil //nolint:nilerr // unpacking failing isn't an error for this case
}
fleetmode.SetAgentMode(managementSettings.Enabled)
return nil
}

Expand Down Expand Up @@ -222,6 +259,7 @@ func SetConfigPath(path string) {

// GetPathConfig returns ${path.config}. If ${path.config} is not set, ${path.home} is returned.
func GetPathConfig() string {
Initialize()
if *configPath != "" {
return *configPath
} else if *homePath != "" {
Expand Down
6 changes: 4 additions & 2 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,9 +974,11 @@ func (b *Beat) Setup(settings Settings, bt beat.Creator, setup SetupSettings) er
}())
}

// handleFlags parses the command line flags. It invokes the HandleFlags
// callback if implemented by the Beat.
// handleFlags converts -flag to --flags, parses the command line
// flags, and it invokes the HandleFlags callback if implemented by
// the Beat.
func (b *Beat) handleFlags() error {
cfgfile.ConvertFlagsForBackwardsCompatibility()
flag.Parse()
return cfgfile.HandleFlags()
}
Expand Down
11 changes: 1 addition & 10 deletions libbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -33,15 +32,6 @@ import (
"github.com/elastic/beats/v7/libbeat/outputs/elasticsearch"
)

func init() {
// backwards compatibility workaround, convert -flags to --flags:
for i, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 {
os.Args[1+i] = "-" + arg
}
}
}

// BeatsRootCmd handles all application command line interface, parses user
// flags and runs subcommands
type BeatsRootCmd struct {
Expand Down Expand Up @@ -76,6 +66,7 @@ func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings
rootCmd.Use = settings.Name

// Due to a dependence upon the beat name, the default config file path
cfgfile.Initialize()
err := cfgfile.ChangeDefaultCfgfileFlag(settings.Name)
if err != nil {
panic(fmt.Errorf("failed to set default config file path: %w", err))
Expand Down
39 changes: 12 additions & 27 deletions libbeat/common/fleetmode/fleet_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,18 @@

package fleetmode

import (
"flag"

"github.com/elastic/elastic-agent-libs/config"
)
var managementEnabled bool

// SetAgentMode stores if the Beat is running under Elastic Agent.
// Normally this is called when the command line flags are parsed.
// This is stored as a package level variable because some components
// (like filebeat/metricbeat modules) don't have access to the
// configuration information to determine this on their own.
func SetAgentMode(enabled bool) {
managementEnabled = enabled
}

// Enabled checks to see if filebeat/metricbeat is running under Agent
// The management setting is stored in the main Beat runtime object, but we can't see that from a module
// So instead we check the CLI flags, since Agent starts filebeat/metricbeat with "-E", "management.enabled=true"
// Enabled returns true if the Beat is running under Elastic Agent.
func Enabled() bool {
type management struct {
Enabled bool `config:"management.enabled"`
}
var managementSettings management

cfgFlag := flag.Lookup("E")
if cfgFlag == nil {
return false
}

cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag)
cliCfg := cfgObject.Config()

err := cliCfg.Unpack(&managementSettings)
if err != nil {
return false
}

return managementSettings.Enabled
return managementEnabled
}
2 changes: 2 additions & 0 deletions libbeat/libbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
)

Expand All @@ -36,6 +37,7 @@ func init() {

// Test started when the test binary is started
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"flag"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
"github.com/elastic/beats/v7/metricbeat/cmd"
)
Expand All @@ -38,6 +39,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
2 changes: 2 additions & 0 deletions packetbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"flag"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
"github.com/elastic/beats/v7/packetbeat/cmd"
)
Expand All @@ -38,6 +39,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(*testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
2 changes: 2 additions & 0 deletions winlogbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"flag"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
"github.com/elastic/beats/v7/winlogbeat/cmd"
)
Expand All @@ -38,6 +39,7 @@ func init() {
// TestSystem is the function called when the test binary is started.
// Only calls main.
func TestSystem(*testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/agentbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/spf13/cobra"
)

Expand All @@ -27,6 +28,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
if err := abCommand.Execute(); err != nil {
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions x-pack/auditbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"flag"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
"github.com/elastic/beats/v7/x-pack/auditbeat/cmd"
)
Expand All @@ -26,6 +27,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(*testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/filebeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
fbcmd "github.com/elastic/beats/v7/x-pack/filebeat/cmd"
Expand All @@ -29,6 +30,7 @@ func init() {

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
if err := fbCommand.Execute(); err != nil {
os.Exit(1)
Expand Down
Loading

0 comments on commit 6d0ff13

Please sign in to comment.