From 71d291fdf1641f5adcd69db9d2dd2b200f1126ea Mon Sep 17 00:00:00 2001 From: Hardy Date: Sat, 15 Feb 2025 21:11:50 +0800 Subject: [PATCH] fix: init user just one time (#140) * fix: init user just one time * chore: init system cluster metrics default settings --------- Co-authored-by: hardy --- plugin/setup/setup.go | 69 +++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/plugin/setup/setup.go b/plugin/setup/setup.go index a994dcf9..819c6368 100644 --- a/plugin/setup/setup.go +++ b/plugin/setup/setup.go @@ -69,8 +69,11 @@ import ( "infini.sh/framework/plugins/replay" ) +// Easysearch auto create ingest user password const ingestUser = "infini_ingest" +var ingestPassword = util.GenerateRandomString(20) + type Module struct { api.Handler } @@ -503,6 +506,42 @@ func (module *Module) initialize(w http.ResponseWriter, r *http.Request, ps http //保存默认集群 t := time.Now() + toSaveCfg.MetadataConfigs = &elastic.MetadataConfig{ + HealthCheck: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + ClusterSettingsCheck: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + MetadataRefresh: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + NodeAvailabilityCheck: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + } + toSaveCfg.MonitorConfigs = &elastic.MonitorConfig{ + ClusterStats: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + NodeStats: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + ClusterHealth: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + IndexStats: elastic.TaskConfig{ + Enabled: true, + Interval: "10s", + }, + } toSaveCfg.Created = &t err = orm.Save(nil, &toSaveCfg) if err != nil { @@ -683,6 +722,7 @@ func getYamlData(filename string) []byte { escapedContent = bytes.ReplaceAll(escapedContent, []byte("\""), []byte("\\\"")) return escapedContent } + func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if !global.Env().SetupRequired() { module.WriteError(w, "setup not permitted", 500) @@ -730,6 +770,17 @@ func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request, case "view": dslTplFileName = "view.tpl" case "agent": + if ver.Distribution == elastic.Easysearch { + err = keystore.SetValue("SYSTEM_CLUSTER_INGEST_PASSWORD", []byte(ingestPassword)) + if err != nil { + panic(err) + } + client := elastic.GetClient(GlobalSystemElasticsearchID) + err = initIngestUser(client, cfg1.IndexPrefix, ingestUser, ingestPassword) + if err != nil { + panic(err) + } + } dslTplFileName = "agent.tpl" default: panic(fmt.Sprintf("unsupport template name [%s]", request.InitializeTemplate)) @@ -786,20 +837,6 @@ func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request, return } - // Easysearch auto create ingest user - ingestPassword := util.GenerateRandomString(20) - if ver.Distribution == elastic.Easysearch { - err = keystore.SetValue("SYSTEM_CLUSTER_INGEST_PASSWORD", []byte(ingestPassword)) - if err != nil { - panic(err) - } - client := elastic.GetClient(GlobalSystemElasticsearchID) - err = initIngestUser(client, cfg1.IndexPrefix, ingestUser, ingestPassword) - if err != nil { - panic(err) - } - } - output := tpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) { switch tag { case "SETUP_SYSTEM_INGEST_CONFIG": @@ -925,7 +962,7 @@ func initIngestUser(client elastic.API, indexPrefix string, username, password s }] }` roleBody := fmt.Sprintf(roleTpl, indexPrefix, indexPrefix) - err := client.PutRole(ingestUser, []byte(roleBody)) + err := client.PutRole(username, []byte(roleBody)) if err != nil { return fmt.Errorf("failed to create ingest role: %w", err) } @@ -935,7 +972,7 @@ func initIngestUser(client elastic.API, indexPrefix string, username, password s ], "password": "%s"}` - userBody := fmt.Sprintf(userTpl, ingestUser, password) + userBody := fmt.Sprintf(userTpl, username, password) err = client.PutUser(username, []byte(userBody)) if err != nil { return fmt.Errorf("failed to create ingest user: %w", err)