add skip to initialize api

This commit is contained in:
medcl 2022-10-24 19:52:09 +08:00
parent e607254cc4
commit 9a91ca7b70
1 changed files with 77 additions and 75 deletions

View File

@ -80,6 +80,7 @@ type SetupRequest struct {
Password string `json:"password"` Password string `json:"password"`
} `json:"cluster"` } `json:"cluster"`
Skip bool `json:"skip"`
BootstrapUsername string `json:"bootstrap_username"` BootstrapUsername string `json:"bootstrap_username"`
BootstrapPassword string `json:"bootstrap_password"` BootstrapPassword string `json:"bootstrap_password"`
} }
@ -336,93 +337,102 @@ func (module *Module) initialize(w http.ResponseWriter, r *http.Request, ps http
handler := elastic2.ElasticORM{Client: client, Config:cfg1 } handler := elastic2.ElasticORM{Client: client, Config:cfg1 }
orm.Register("elastic_setup_"+util.GetUUID(), handler) orm.Register("elastic_setup_"+util.GetUUID(), handler)
//处理模版 if !request.Skip{
elastic2.InitTemplate(true) //处理模版
elastic2.InitTemplate(true)
//处理生命周期 //处理生命周期
//TEMPLATE_NAME //TEMPLATE_NAME
//INDEX_PREFIX //INDEX_PREFIX
dslTplFile:=path.Join(global.Env().GetConfigDir(),"initialization.tpl") dslTplFile:=path.Join(global.Env().GetConfigDir(),"initialization.tpl")
dslFile:=path.Join(global.Env().GetConfigDir(),"initialization.dsl") dslFile:=path.Join(global.Env().GetConfigDir(),"initialization.dsl")
var dsl []byte var dsl []byte
dsl,err=util.FileGetContent(dslTplFile) dsl,err=util.FileGetContent(dslTplFile)
if err!=nil{
panic(err)
}
var dslWriteSuccess=false
if len(dsl)>0{
var tpl *fasttemplate.Template
tpl,err=fasttemplate.NewTemplate(string(dsl), "$[[", "]]")
if err!=nil{ if err!=nil{
panic(err) panic(err)
} }
if tpl!=nil{
output:=tpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) { var dslWriteSuccess=false
switch tag { if len(dsl)>0{
case "TEMPLATE_NAME": var tpl *fasttemplate.Template
return w.Write([]byte(cfg1.TemplateName)) tpl,err=fasttemplate.NewTemplate(string(dsl), "$[[", "]]")
case "INDEX_PREFIX":
return w.Write([]byte(cfg1.IndexPrefix))
case "RESOURCE_ID":
return w.Write([]byte(cfg.ID))
case "RESOURCE_NAME":
return w.Write([]byte(cfg.Name))
}
panic(errors.Errorf("unknown tag: %v",tag))
})
_,err=util.FilePutContent(dslFile,output)
if err!=nil{ if err!=nil{
panic(err) panic(err)
} }
dslWriteSuccess=true if tpl!=nil{
output:=tpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
switch tag {
case "TEMPLATE_NAME":
return w.Write([]byte(cfg1.TemplateName))
case "INDEX_PREFIX":
return w.Write([]byte(cfg1.IndexPrefix))
case "RESOURCE_ID":
return w.Write([]byte(cfg.ID))
case "RESOURCE_NAME":
return w.Write([]byte(cfg.Name))
}
panic(errors.Errorf("unknown tag: %v",tag))
})
_,err=util.FilePutContent(dslFile,output)
if err!=nil{
panic(err)
}
dslWriteSuccess=true
}
} }
}
if dslWriteSuccess{ if dslWriteSuccess{
lines := util.FileGetLines(dslFile) lines := util.FileGetLines(dslFile)
_,err,_:=replay.ReplayLines(pipeline.AcquireContext(),lines,cfg.Schema,cfg.Host) _,err,_:=replay.ReplayLines(pipeline.AcquireContext(),lines,cfg.Schema,cfg.Host)
if err!=nil{ if err!=nil{
log.Error(err) log.Error(err)
}
} }
}
//处理索引
elastic2.InitSchema()
//init security
security.InitSecurity()
//处理索引 //保存默认集群
elastic2.InitSchema() err=orm.Save(&cfg)
//init security
security.InitSecurity()
//保存默认集群
err=orm.Save(&cfg)
if err!=nil{
panic(err)
}
if request.BootstrapUsername!=""&&request.BootstrapPassword!=""{
//Save bootstrap user
user:=rbac.User{}
user.ID="default_user_"+request.BootstrapUsername
user.Name=request.BootstrapUsername
user.NickName=request.BootstrapUsername
var hash []byte
hash, err = bcrypt.GenerateFromPassword([]byte(request.BootstrapPassword), bcrypt.DefaultCost)
if err!=nil{ if err!=nil{
panic(err) panic(err)
} }
user.Password=string(hash) if request.BootstrapUsername!=""&&request.BootstrapPassword!=""{
role:=[]rbac.UserRole{} //Save bootstrap user
role=append(role,rbac.UserRole{ user:=rbac.User{}
ID: rbac.RoleAdminName, user.ID="default_user_"+request.BootstrapUsername
Name: rbac.RoleAdminName, user.Name=request.BootstrapUsername
}) user.NickName=request.BootstrapUsername
user.Roles=role var hash []byte
err=orm.Save(&user) hash, err = bcrypt.GenerateFromPassword([]byte(request.BootstrapPassword), bcrypt.DefaultCost)
if err!=nil{
panic(err)
}
user.Password=string(hash)
role:=[]rbac.UserRole{}
role=append(role,rbac.UserRole{
ID: rbac.RoleAdminName,
Name: rbac.RoleAdminName,
})
user.Roles=role
err=orm.Save(&user)
if err!=nil{
panic(err)
}
}
//disable builtin auth
err=api.DisableBuiltinUserAdmin()
if err!=nil{ if err!=nil{
panic(err) panic(err)
} }
} }
@ -436,17 +446,9 @@ func (module *Module) initialize(w http.ResponseWriter, r *http.Request, ps http
panic(err) panic(err)
} }
//处理 ILM
//callback //callback
InvokeSetupCallback() InvokeSetupCallback()
//disable builtin auth
err=api.DisableBuiltinUserAdmin()
if err!=nil{
panic(err)
}
//place setup lock file //place setup lock file
setupLock:=path.Join(global.Env().GetDataDir(),".setup_lock") setupLock:=path.Join(global.Env().GetDataDir(),".setup_lock")
_,err=util.FilePutContent(setupLock,time.Now().String()) _,err=util.FilePutContent(setupLock,time.Now().String())