mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
feat: 应用关联 redis 时自动填充密码 (#1826)
This commit is contained in:
parent
7c6f8ff7c4
commit
70319aca45
@ -15,6 +15,10 @@ type AuthParam struct {
|
||||
RootPassword string `json:"PANEL_DB_ROOT_PASSWORD"`
|
||||
}
|
||||
|
||||
type RedisAuthParam struct {
|
||||
RootPassword string `json:"PANEL_REDIS_ROOT_PASSWORD"`
|
||||
}
|
||||
|
||||
type ContainerExec struct {
|
||||
ContainerName string `json:"containerName"`
|
||||
DbParam AppDatabase `json:"dbParam"`
|
||||
|
@ -83,20 +83,33 @@ func checkPort(key string, params map[string]interface{}) (int, error) {
|
||||
func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall, params map[string]interface{}) error {
|
||||
var dbConfig dto.AppDatabase
|
||||
if app.Type == "runtime" {
|
||||
var authParam dto.AuthParam
|
||||
paramByte, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(paramByte, &authParam); err != nil {
|
||||
return err
|
||||
}
|
||||
if authParam.RootPassword != "" {
|
||||
authByte, err := json.Marshal(authParam)
|
||||
if err != nil {
|
||||
return err
|
||||
switch app.Key {
|
||||
case "mysql", "mariadb", "postgresql":
|
||||
if password, ok := params["PANEL_DB_ROOT_PASSWORD"]; ok {
|
||||
if password != "" {
|
||||
authParam := dto.AuthParam{
|
||||
RootPassword: password.(string),
|
||||
}
|
||||
authByte, err := json.Marshal(authParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
appInstall.Param = string(authByte)
|
||||
}
|
||||
}
|
||||
case "redis":
|
||||
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
|
||||
if password != "" {
|
||||
authParam := dto.RedisAuthParam{
|
||||
RootPassword: password.(string),
|
||||
}
|
||||
authByte, err := json.Marshal(authParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
appInstall.Param = string(authByte)
|
||||
}
|
||||
}
|
||||
appInstall.Param = string(authByte)
|
||||
}
|
||||
}
|
||||
if app.Type == "website" || app.Type == "tool" {
|
||||
@ -104,7 +117,7 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(paramByte, &dbConfig); err != nil {
|
||||
if err = json.Unmarshal(paramByte, &dbConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ func Init() {
|
||||
migrations.UpdateAppDetail,
|
||||
migrations.EncryptHostPassword,
|
||||
migrations.AddRemoteDB,
|
||||
migrations.UpdateRedisParam,
|
||||
})
|
||||
if err := m.Migrate(); err != nil {
|
||||
global.LOG.Error(err)
|
||||
|
@ -523,3 +523,27 @@ var AddRemoteDB = &gormigrate.Migration{
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var UpdateRedisParam = &gormigrate.Migration{
|
||||
ID: "20230804-update-redis-param",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
var (
|
||||
app model.App
|
||||
appInstall model.AppInstall
|
||||
)
|
||||
if err := global.DB.Where("key = ?", "redis").First(&app).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
appInstall.Param = strings.ReplaceAll(appInstall.Param, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
|
||||
appInstall.DockerCompose = strings.ReplaceAll(appInstall.DockerCompose, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
|
||||
appInstall.Env = strings.ReplaceAll(appInstall.Env, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
|
||||
if err := tx.Model(&model.AppInstall{}).Where("id = ?", appInstall.ID).Updates(appInstall).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user