func()

in libbeat/cmd/instance/beat.go [861:961]


func (b *Beat) Setup(settings Settings, bt beat.Creator, setup SetupSettings) error {
	return handleError(func() error {
		err := b.InitWithSettings(settings)
		if err != nil {
			return err
		}
		// Tell the beat that we're in the setup command
		b.InSetupCmd = true

		if setup.ForceEnableModuleFilesets {
			if err := b.Beat.BeatConfig.SetBool("config.modules.force_enable_module_filesets", -1, true); err != nil {
				return fmt.Errorf("error setting force_enable_module_filesets config option %w", err)
			}
		}
		// Create beater to give it the opportunity to set loading callbacks
		_, err = b.createBeater(bt)
		if err != nil {
			return err
		}
		if setup.IndexManagement || setup.Template || setup.ILMPolicy {
			outCfg := b.Config.Output
			if !isElasticsearchOutput(outCfg.Name()) {
				return fmt.Errorf("index management requested but the Elasticsearch output is not configured/enabled")
			}
			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()
			esClient, err := eslegclient.NewConnectedClient(ctx, outCfg.Config(), b.Info.Beat)
			if err != nil {
				return err
			}

			// other components know to skip ILM setup under serverless, this logic block just helps us print an error message
			// in instances where ILM has been explicitly enabled
			var ilmCfg struct {
				Ilm *config.C `config:"setup.ilm"`
			}
			err = b.RawConfig.Unpack(&ilmCfg)
			if err != nil {
				return fmt.Errorf("error unpacking ILM config: %w", err)
			}
			if ilmCfg.Ilm.Enabled() && esClient.IsServerless() {
				fmt.Println("WARNING: ILM is not supported in Serverless projects")
			}

			loadTemplate, loadILM := idxmgmt.LoadModeUnset, idxmgmt.LoadModeUnset
			if setup.IndexManagement || setup.Template {
				loadTemplate = idxmgmt.LoadModeOverwrite
			}
			if setup.IndexManagement || setup.ILMPolicy {
				loadILM = idxmgmt.LoadModeEnabled
			}

			mgmtHandler, err := idxmgmt.NewESClientHandler(esClient, b.Info, b.Config.LifecycleConfig)
			if err != nil {
				return fmt.Errorf("error creating index management handler: %w", err)
			}

			m := b.IdxSupporter.Manager(mgmtHandler, idxmgmt.BeatsAssets(b.Fields))
			if ok, warn := m.VerifySetup(loadTemplate, loadILM); !ok {
				fmt.Println(warn)
			}
			if err = m.Setup(loadTemplate, loadILM); err != nil {
				return err
			}
			fmt.Println("Index setup finished.")
		}

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

			if err != nil {
				var notFoundErr *dashboards.ErrNotFound
				if errors.As(err, &notFoundErr) {
					fmt.Printf("Skipping loading dashboards, %+v\n", err)
				} else {
					return err
				}
			} else {
				fmt.Println("Loaded dashboards")
			}
		}

		if setup.Pipeline && b.OverwritePipelinesCallback != nil {
			if setup.EnableAllFilesets {
				if err := b.Beat.BeatConfig.SetBool("config.modules.enable_all_filesets", -1, true); err != nil {
					return fmt.Errorf("error setting enable_all_filesets config option %w", err)
				}
			}

			esConfig := b.Config.Output.Config()
			err = b.OverwritePipelinesCallback(esConfig)
			if err != nil {
				return err
			}
			fmt.Println("Loaded Ingest pipelines")
		}

		return nil
	}())
}