From 70319aca4518b8f8a819200484b85c817229c9c9 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 3 Aug 2023 18:36:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BA=94=E7=94=A8=E5=85=B3=E8=81=94=20?= =?UTF-8?q?redis=20=E6=97=B6=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=85=85=E5=AF=86?= =?UTF-8?q?=E7=A0=81=20(#1826)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/app.go | 4 +++ backend/app/service/app_utils.go | 41 +++++++++++++++-------- backend/init/migration/migrate.go | 1 + backend/init/migration/migrations/init.go | 24 +++++++++++++ 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/backend/app/dto/app.go b/backend/app/dto/app.go index b03425fee..395060ffa 100644 --- a/backend/app/dto/app.go +++ b/backend/app/dto/app.go @@ -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"` diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 664180f2e..46c569d52 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -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 } } diff --git a/backend/init/migration/migrate.go b/backend/init/migration/migrate.go index 06d97d1e5..be5636ef7 100644 --- a/backend/init/migration/migrate.go +++ b/backend/init/migration/migrate.go @@ -36,6 +36,7 @@ func Init() { migrations.UpdateAppDetail, migrations.EncryptHostPassword, migrations.AddRemoteDB, + migrations.UpdateRedisParam, }) if err := m.Migrate(); err != nil { global.LOG.Error(err) diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index 08bcad21a..34d8b4f54 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -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 + + }, +}