Skip to content

Commit

Permalink
Remove deprecated Init(), pass instance.Settings around. (#10721)
Browse files Browse the repository at this point in the history
* Remove deprecated Init(), pass instance.Settings around.

Ensure settings from rootCmd are respected also when calling `export`
and `setup`.

fixes #10720
  • Loading branch information
simitt authored Feb 14, 2019
1 parent f988136 commit d83bb4d
Show file tree
Hide file tree
Showing 28 changed files with 108 additions and 137 deletions.
3 changes: 2 additions & 1 deletion auditbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/elastic/beats/auditbeat/core"
"github.com/elastic/beats/libbeat/cmd"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/metricbeat/beater"
"github.com/elastic/beats/metricbeat/mb/module"
)
Expand All @@ -46,6 +47,6 @@ func init() {
),
)
var runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError)
RootCmd = cmd.GenRootCmdWithRunFlags(Name, "", create, runFlags)
RootCmd = cmd.GenRootCmdWithSettings(create, instance.Settings{RunFlags: runFlags, Name: Name})
RootCmd.AddCommand(ShowCmd)
}
3 changes: 2 additions & 1 deletion docs/devguide/newbeat.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,12 @@ import (
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cmd"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/kimjmin/countbeat/beater"
)
var RootCmd = cmd.GenRootCmd("countbeat", "", beater.New)
var RootCmd = cmd.GenRootCmdWithSettings(beater.New, instance.Settings{Name: "countbeat"})
func main() {
if err := RootCmd.Execute(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions filebeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/elastic/beats/filebeat/beater"

cmd "github.com/elastic/beats/libbeat/cmd"
"github.com/elastic/beats/libbeat/cmd/instance"
)

// Name of this beat
Expand All @@ -37,8 +38,7 @@ func init() {
var runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("once"))
runFlags.AddGoFlag(flag.CommandLine.Lookup("modules"))

RootCmd = cmd.GenRootCmdWithRunFlags(Name, "", beater.New, runFlags)
RootCmd = cmd.GenRootCmdWithSettings(beater.New, instance.Settings{RunFlags: runFlags, Name: Name})
RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("M"))
RootCmd.TestCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
RootCmd.SetupCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
Expand Down
3 changes: 2 additions & 1 deletion generator/beat/{beat}/cmd/root.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"{beat_path}/beater"

cmd "github.com/elastic/beats/libbeat/cmd"
"github.com/elastic/beats/libbeat/cmd/instance"
)

// Name of this beat
var Name = "{beat}"

// RootCmd to handle beats cli
var RootCmd = cmd.GenRootCmd(Name, "", beater.New)
var RootCmd = cmd.GenRootCmdWithSettings(beater.New, instance.Settings{Name: Name})
3 changes: 2 additions & 1 deletion heartbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd
import (
// register default heartbeat monitors
_ "github.com/elastic/beats/heartbeat/monitors/defaults"
"github.com/elastic/beats/libbeat/cmd/instance"

"github.com/elastic/beats/heartbeat/beater"
cmd "github.com/elastic/beats/libbeat/cmd"
Expand All @@ -29,4 +30,4 @@ import (
var Name = "heartbeat"

// RootCmd to handle beats cli
var RootCmd = cmd.GenRootCmd(Name, "", beater.New)
var RootCmd = cmd.GenRootCmdWithSettings(beater.New, instance.Settings{Name: Name})
3 changes: 2 additions & 1 deletion journalbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"github.com/elastic/beats/journalbeat/beater"

cmd "github.com/elastic/beats/libbeat/cmd"
"github.com/elastic/beats/libbeat/cmd/instance"
)

// Name of this beat
var Name = "journalbeat"

// RootCmd to handle beats cli
var RootCmd = cmd.GenRootCmd(Name, "", beater.New)
var RootCmd = cmd.GenRootCmdWithSettings(beater.New, instance.Settings{Name: Name})
4 changes: 3 additions & 1 deletion libbeat/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"fmt"
"os"

"github.com/elastic/beats/libbeat/cmd/instance"

"github.com/spf13/cobra"
)

func genCompletionCmd(name, version string, rootCmd *BeatsRootCmd) *cobra.Command {
func genCompletionCmd(_ instance.Settings, rootCmd *BeatsRootCmd) *cobra.Command {
completionCmd := cobra.Command{
Use: "completion SHELL",
Short: "Output shell completion code for the specified shell (bash and zsh only by the moment)",
Expand Down
12 changes: 6 additions & 6 deletions libbeat/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import (
"github.com/elastic/beats/libbeat/cmd/instance"
)

func genExportCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func genExportCmd(settings instance.Settings) *cobra.Command {
exportCmd := &cobra.Command{
Use: "export",
Short: "Export current config or index template",
}

exportCmd.AddCommand(export.GenExportConfigCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenTemplateConfigCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenIndexPatternConfigCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenDashboardCmd(name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenGetILMPolicyCmd(settings, name, idxPrefix, beatVersion))
exportCmd.AddCommand(export.GenExportConfigCmd(settings))
exportCmd.AddCommand(export.GenTemplateConfigCmd(settings))
exportCmd.AddCommand(export.GenIndexPatternConfigCmd(settings))
exportCmd.AddCommand(export.GenDashboardCmd(settings))
exportCmd.AddCommand(export.GenGetILMPolicyCmd(settings))

return exportCmd
}
8 changes: 4 additions & 4 deletions libbeat/cmd/export/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ import (
)

// GenExportConfigCmd write to stdout the current configuration in the YAML format.
func GenExportConfigCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func GenExportConfigCmd(settings instance.Settings) *cobra.Command {
return &cobra.Command{
Use: "config",
Short: "Export current config to stdout",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
return exportConfig(settings, name, idxPrefix, beatVersion)
return exportConfig(settings)
}),
}
}

func exportConfig(settings instance.Settings, name, idxPrefix, beatVersion string) error {
b, err := instance.NewBeat(name, idxPrefix, beatVersion)
func exportConfig(settings instance.Settings) error {
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
return fmt.Errorf("error initializing beat: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions libbeat/cmd/export/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// GenDashboardCmd is the command used to export a dashboard.
func GenDashboardCmd(name, idxPrefix, beatVersion string) *cobra.Command {
func GenDashboardCmd(settings instance.Settings) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "dashboard",
Short: "Export defined dashboard to stdout",
Expand All @@ -40,12 +40,12 @@ func GenDashboardCmd(name, idxPrefix, beatVersion string) *cobra.Command {
yml, _ := cmd.Flags().GetString("yml")
decode, _ := cmd.Flags().GetBool("decode")

b, err := instance.NewBeat(name, idxPrefix, beatVersion)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating beat: %s\n", err)
os.Exit(1)
}
err = b.Init()
err = b.InitWithSettings(settings)
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions libbeat/cmd/export/ilm_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
)

// GenGetILMPolicyCmd is the command used to export the ilm policy.
func GenGetILMPolicyCmd(settings instance.Settings, name, idxPrefix, version string) *cobra.Command {
func GenGetILMPolicyCmd(settings instance.Settings) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "ilm-policy",
Short: "Export ILM policy",
Run: func(cmd *cobra.Command, args []string) {
b, err := instance.NewBeat(name, idxPrefix, version)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
Expand Down
9 changes: 4 additions & 5 deletions libbeat/cmd/export/index_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import (
)

// GenIndexPatternConfigCmd generates an index pattern for Kibana
func GenIndexPatternConfigCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func GenIndexPatternConfigCmd(settings instance.Settings) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "index-pattern",
Short: "Export kibana index pattern to stdout",
Run: func(cmd *cobra.Command, args []string) {
version, _ := cmd.Flags().GetString("es.version")

b, err := instance.NewBeat(name, idxPrefix, beatVersion)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fatalf("Error initializing beat: %+v", err)
}
Expand All @@ -63,7 +63,7 @@ func GenIndexPatternConfigCmd(settings instance.Settings, name, idxPrefix, beatV
if err != nil {
fatalf("Error creating version: %+v", err)
}
indexPattern, err := kibana.NewGenerator(b.Info.IndexPrefix, b.Info.Beat, b.Fields, beatVersion, *v, withMigration)
indexPattern, err := kibana.NewGenerator(b.Info.IndexPrefix, b.Info.Beat, b.Fields, settings.Version, *v, withMigration)
if err != nil {
log.Fatal(err)
}
Expand All @@ -80,8 +80,7 @@ func GenIndexPatternConfigCmd(settings instance.Settings, name, idxPrefix, beatV
},
}

genTemplateConfigCmd.Flags().String("es.version", beatVersion, "Elasticsearch version")
genTemplateConfigCmd.Flags().String("index", idxPrefix, "Base index name")
genTemplateConfigCmd.Flags().String("es.version", settings.Version, "Elasticsearch version")

return genTemplateConfigCmd
}
8 changes: 4 additions & 4 deletions libbeat/cmd/export/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/elastic/beats/libbeat/template"
)

func GenTemplateConfigCmd(settings instance.Settings, name, idxPrefix, beatVersion string) *cobra.Command {
func GenTemplateConfigCmd(settings instance.Settings) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "template",
Short: "Export index template to stdout",
Expand All @@ -40,7 +40,7 @@ func GenTemplateConfigCmd(settings instance.Settings, name, idxPrefix, beatVersi
index, _ := cmd.Flags().GetString("index")
noILM, _ := cmd.Flags().GetBool("noilm")

b, err := instance.NewBeat(name, idxPrefix, beatVersion)
b, err := instance.NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
fatalf("Error initializing beat: %+v", err)
}
Expand Down Expand Up @@ -106,8 +106,8 @@ func GenTemplateConfigCmd(settings instance.Settings, name, idxPrefix, beatVersi
},
}

genTemplateConfigCmd.Flags().String("es.version", beatVersion, "Elasticsearch version")
genTemplateConfigCmd.Flags().String("index", idxPrefix, "Base index name")
genTemplateConfigCmd.Flags().String("es.version", settings.Version, "Elasticsearch version")
genTemplateConfigCmd.Flags().String("index", settings.IndexPrefix, "Base index name")
genTemplateConfigCmd.Flags().Bool("noilm", false, "Generate template with ILM disabled")

return genTemplateConfigCmd
Expand Down
18 changes: 9 additions & 9 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ func (b *Beat) launch(settings Settings, bt beat.Creator) error {
}

// TestConfig check all settings are ok and the beat can be run
func (b *Beat) TestConfig(bt beat.Creator) error {
func (b *Beat) TestConfig(settings Settings, bt beat.Creator) error {
return handleError(func() error {
err := b.Init()
err := b.InitWithSettings(settings)
if err != nil {
return err
}
Expand All @@ -422,9 +422,9 @@ type SetupSettings struct {
}

// Setup registers ES index template, kibana dashboards, ml jobs and pipelines.
func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
func (b *Beat) Setup(settings Settings, bt beat.Creator, setup SetupSettings) error {
return handleError(func() error {
err := b.Init()
err := b.InitWithSettings(settings)
if err != nil {
return err
}
Expand All @@ -438,7 +438,7 @@ func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
return err
}

if settings.Template || settings.ILMPolicy {
if setup.Template || setup.ILMPolicy {
outCfg := b.Config.Output

if outCfg.Name() != "elasticsearch" {
Expand All @@ -455,15 +455,15 @@ func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
// prepare index by loading templates, lifecycle policies and write aliases

m := b.index.Manager(esClient, idxmgmt.BeatsAssets(b.Fields))
err = m.Setup(settings.Template, settings.ILMPolicy)
err = m.Setup(setup.Template, setup.ILMPolicy)
if err != nil {
return err
}
}
fmt.Println("Index setup complete.")
}

if settings.Dashboard {
if setup.Dashboard {
fmt.Println("Loading dashboards (Kibana must be running and reachable)")
err = b.loadDashboards(context.Background(), true)

Expand All @@ -479,15 +479,15 @@ func (b *Beat) Setup(bt beat.Creator, settings SetupSettings) error {
}
}

if settings.MachineLearning && b.SetupMLCallback != nil {
if setup.MachineLearning && b.SetupMLCallback != nil {
err = b.SetupMLCallback(&b.Beat, b.Config.Kibana)
if err != nil {
return err
}
fmt.Println("Loaded machine learning job configurations")
}

if settings.Pipeline && b.OverwritePipelinesCallback != nil {
if setup.Pipeline && b.OverwritePipelinesCallback != nil {
esConfig := b.Config.Output.Config()
err = b.OverwritePipelinesCallback(esConfig)
if err != nil {
Expand Down
Loading

0 comments on commit d83bb4d

Please sign in to comment.