fix: init user just one time (#140)
* fix: init user just one time * chore: init system cluster metrics default settings --------- Co-authored-by: hardy <luohf@infinilabs.com>
This commit is contained in:
parent
fd2746a210
commit
71d291fdf1
|
@ -69,8 +69,11 @@ import (
|
||||||
"infini.sh/framework/plugins/replay"
|
"infini.sh/framework/plugins/replay"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Easysearch auto create ingest user password
|
||||||
const ingestUser = "infini_ingest"
|
const ingestUser = "infini_ingest"
|
||||||
|
|
||||||
|
var ingestPassword = util.GenerateRandomString(20)
|
||||||
|
|
||||||
type Module struct {
|
type Module struct {
|
||||||
api.Handler
|
api.Handler
|
||||||
}
|
}
|
||||||
|
@ -503,6 +506,42 @@ func (module *Module) initialize(w http.ResponseWriter, r *http.Request, ps http
|
||||||
|
|
||||||
//保存默认集群
|
//保存默认集群
|
||||||
t := time.Now()
|
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
|
toSaveCfg.Created = &t
|
||||||
err = orm.Save(nil, &toSaveCfg)
|
err = orm.Save(nil, &toSaveCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -683,6 +722,7 @@ func getYamlData(filename string) []byte {
|
||||||
escapedContent = bytes.ReplaceAll(escapedContent, []byte("\""), []byte("\\\""))
|
escapedContent = bytes.ReplaceAll(escapedContent, []byte("\""), []byte("\\\""))
|
||||||
return escapedContent
|
return escapedContent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
if !global.Env().SetupRequired() {
|
if !global.Env().SetupRequired() {
|
||||||
module.WriteError(w, "setup not permitted", 500)
|
module.WriteError(w, "setup not permitted", 500)
|
||||||
|
@ -730,6 +770,17 @@ func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request,
|
||||||
case "view":
|
case "view":
|
||||||
dslTplFileName = "view.tpl"
|
dslTplFileName = "view.tpl"
|
||||||
case "agent":
|
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"
|
dslTplFileName = "agent.tpl"
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unsupport template name [%s]", request.InitializeTemplate))
|
panic(fmt.Sprintf("unsupport template name [%s]", request.InitializeTemplate))
|
||||||
|
@ -786,20 +837,6 @@ func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request,
|
||||||
return
|
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) {
|
output := tpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
|
||||||
switch tag {
|
switch tag {
|
||||||
case "SETUP_SYSTEM_INGEST_CONFIG":
|
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)
|
roleBody := fmt.Sprintf(roleTpl, indexPrefix, indexPrefix)
|
||||||
err := client.PutRole(ingestUser, []byte(roleBody))
|
err := client.PutRole(username, []byte(roleBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create ingest role: %w", err)
|
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"}`
|
"password": "%s"}`
|
||||||
|
|
||||||
userBody := fmt.Sprintf(userTpl, ingestUser, password)
|
userBody := fmt.Sprintf(userTpl, username, password)
|
||||||
err = client.PutUser(username, []byte(userBody))
|
err = client.PutUser(username, []byte(userBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create ingest user: %w", err)
|
return fmt.Errorf("failed to create ingest user: %w", err)
|
||||||
|
|
Loading…
Reference in New Issue