fix: 修改 mongodb 和 postgresql 的数据库用户名获取 (#2232)

pull/2252/head
ssongliu 2023-09-10 17:18:16 +08:00 committed by GitHub
parent 645e6bbb79
commit 9d11e56074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 46 deletions

View File

@ -161,6 +161,7 @@ type RootInfo struct {
Name string `json:"name"` Name string `json:"name"`
Port int64 `json:"port"` Port int64 `json:"port"`
HttpsPort int64 `json:"httpsPort"` HttpsPort int64 `json:"httpsPort"`
UserName string `json:"userName"`
Password string `json:"password"` Password string `json:"password"`
UserPassword string `json:"userPassword"` UserPassword string `json:"userPassword"`
ContainerName string `json:"containerName"` ContainerName string `json:"containerName"`
@ -204,6 +205,15 @@ func (a *AppInstallRepo) LoadBaseInfo(key string, name string) (*RootInfo, error
if ok { if ok {
info.Password = password info.Password = password
} }
case "mongodb", "postgresql":
user, ok := envMap["PANEL_DB_ROOT_USER"].(string)
if ok {
info.UserName = user
}
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
if ok {
info.Password = password
}
} }
userPassword, ok := envMap["PANEL_DB_USER_PASSWORD"].(string) userPassword, ok := envMap["PANEL_DB_USER_PASSWORD"].(string)

View File

@ -1,8 +1,6 @@
package migrations package migrations
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -489,38 +487,21 @@ var AddRemoteDB = &gormigrate.Migration{
if err := tx.AutoMigrate(&model.Database{}, &model.DatabaseMysql{}); err != nil { if err := tx.AutoMigrate(&model.Database{}, &model.DatabaseMysql{}); err != nil {
return err return err
} }
var ( installRepo := repo.NewIAppInstallRepo()
app model.App mysqlInfo, err := installRepo.LoadBaseInfo("mysql", "")
appInstall model.AppInstall if err == nil {
)
if err := global.DB.Where("key = ?", "mysql").First(&app).Error; err != nil {
return nil
}
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
envMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(appInstall.Env), &envMap); err != nil {
return err
}
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
if !ok {
return errors.New("error password in app env")
}
if err := tx.Create(&model.Database{ if err := tx.Create(&model.Database{
Name: "local", Name: "local",
Type: "mysql", Type: "mysql",
Version: appInstall.Version, Version: mysqlInfo.Version,
From: "local", From: "local",
Address: "127.0.0.1", Address: "127.0.0.1",
Username: "root", Username: "root",
Password: password, Password: mysqlInfo.Password,
}).Error; err != nil { }).Error; err != nil {
return err return err
} }
}
return nil return nil
}, },
} }
@ -532,10 +513,10 @@ var UpdateRedisParam = &gormigrate.Migration{
app model.App app model.App
appInstall model.AppInstall appInstall model.AppInstall
) )
if err := global.DB.Where("key = ?", "redis").First(&app).Error; err != nil { if err := tx.Where("key = ?", "redis").First(&app).Error; err != nil {
return nil return nil
} }
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil { if err := tx.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
return nil return nil
} }
appInstall.Param = strings.ReplaceAll(appInstall.Param, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD") appInstall.Param = strings.ReplaceAll(appInstall.Param, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
@ -545,7 +526,6 @@ var UpdateRedisParam = &gormigrate.Migration{
return err return err
} }
return nil return nil
}, },
} }
@ -553,13 +533,13 @@ var UpdateCronjobWithDb = &gormigrate.Migration{
ID: "20230809-update-cronjob-with-db", ID: "20230809-update-cronjob-with-db",
Migrate: func(tx *gorm.DB) error { Migrate: func(tx *gorm.DB) error {
var cronjobs []model.Cronjob var cronjobs []model.Cronjob
if err := global.DB.Where("type = ? AND db_name != ?", "database", "all").Find(&cronjobs).Error; err != nil { if err := tx.Where("type = ? AND db_name != ?", "database", "all").Find(&cronjobs).Error; err != nil {
return nil return nil
} }
for _, job := range cronjobs { for _, job := range cronjobs {
var db model.DatabaseMysql var db model.DatabaseMysql
if err := global.DB.Where("name = ?", job.DBName).First(&db).Error; err != nil { if err := tx.Where("name = ?", job.DBName).First(&db).Error; err != nil {
continue continue
} }
if err := tx.Model(&model.Cronjob{}). if err := tx.Model(&model.Cronjob{}).
@ -590,6 +570,24 @@ var AddDatabases = &gormigrate.Migration{
ID: "20230831-add-databases", ID: "20230831-add-databases",
Migrate: func(tx *gorm.DB) error { Migrate: func(tx *gorm.DB) error {
installRepo := repo.NewIAppInstallRepo() installRepo := repo.NewIAppInstallRepo()
mysqlInfo, err := installRepo.LoadBaseInfo("mysql", "")
if err == nil {
var mysqlDb model.Database
_ = tx.Where("name = ?", mysqlInfo.Name).First(&mysqlDb).Error
if mysqlDb.ID == 0 {
if err := tx.Create(&model.Database{
Name: mysqlDb.Name,
Type: "mysql",
Version: mysqlInfo.Version,
From: "local",
Address: mysqlInfo.ServiceName,
Username: "root",
Password: mysqlInfo.Password,
}).Error; err != nil {
return err
}
}
}
mariadbInfo, err := installRepo.LoadBaseInfo("mariadb", "") mariadbInfo, err := installRepo.LoadBaseInfo("mariadb", "")
if err == nil { if err == nil {
if err := tx.Create(&model.Database{ if err := tx.Create(&model.Database{
@ -632,7 +630,7 @@ var AddDatabases = &gormigrate.Migration{
From: "local", From: "local",
Address: pgInfo.ServiceName, Address: pgInfo.ServiceName,
Port: uint(pgInfo.Port), Port: uint(pgInfo.Port),
Username: "root", Username: pgInfo.UserName,
Password: pgInfo.Password, Password: pgInfo.Password,
}).Error; err != nil { }).Error; err != nil {
return err return err
@ -648,7 +646,7 @@ var AddDatabases = &gormigrate.Migration{
From: "local", From: "local",
Address: mongodbInfo.ServiceName, Address: mongodbInfo.ServiceName,
Port: uint(mongodbInfo.Port), Port: uint(mongodbInfo.Port),
Username: "root", Username: mongodbInfo.UserName,
Password: mongodbInfo.Password, Password: mongodbInfo.Password,
}).Error; err != nil { }).Error; err != nil {
return err return err
@ -678,14 +676,14 @@ var AddDatabases = &gormigrate.Migration{
var UpdateDatabase = &gormigrate.Migration{ var UpdateDatabase = &gormigrate.Migration{
ID: "20230831-update-database", ID: "20230831-update-database",
Migrate: func(tx *gorm.DB) error { Migrate: func(tx *gorm.DB) error {
if err := global.DB.Model(&model.DatabaseMysql{}).Where("`from` != ?", "local").Updates(map[string]interface{}{ if err := tx.Model(&model.DatabaseMysql{}).Where("`from` != ?", "local").Updates(map[string]interface{}{
"from": "remote", "from": "remote",
}).Error; err != nil { }).Error; err != nil {
return err return err
} }
var datas []model.Database var datas []model.Database
if err := global.DB.Find(&datas).Error; err != nil { if err := tx.Find(&datas).Error; err != nil {
return nil return nil
} }
for _, data := range datas { for _, data := range datas {
@ -700,7 +698,7 @@ var UpdateDatabase = &gormigrate.Migration{
global.LOG.Errorf("encrypt database %s password failed, err: %v", data.Name, err) global.LOG.Errorf("encrypt database %s password failed, err: %v", data.Name, err)
continue continue
} }
if err := global.DB.Model(&model.Database{}).Where("id = ?", data.ID).Updates(map[string]interface{}{ if err := tx.Model(&model.Database{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
"app_install_id": mysqlInfo.ID, "app_install_id": mysqlInfo.ID,
"name": mysqlInfo.Name, "name": mysqlInfo.Name,
"password": pass, "password": pass,
@ -714,7 +712,7 @@ var UpdateDatabase = &gormigrate.Migration{
global.LOG.Errorf("encrypt database %s password failed, err: %v", data.Name, err) global.LOG.Errorf("encrypt database %s password failed, err: %v", data.Name, err)
continue continue
} }
if err := global.DB.Model(&model.Database{}).Where("id = ?", data.ID).Updates(map[string]interface{}{ if err := tx.Model(&model.Database{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
"password": pass, "password": pass,
}).Error; err != nil { }).Error; err != nil {
global.LOG.Errorf("updata database %s info failed, err: %v", data.Name, err) global.LOG.Errorf("updata database %s info failed, err: %v", data.Name, err)
@ -723,7 +721,7 @@ var UpdateDatabase = &gormigrate.Migration{
} }
var mysqls []model.DatabaseMysql var mysqls []model.DatabaseMysql
if err := global.DB.Find(&mysqls).Error; err != nil { if err := tx.Find(&mysqls).Error; err != nil {
return nil return nil
} }
for _, data := range mysqls { for _, data := range mysqls {
@ -732,7 +730,7 @@ var UpdateDatabase = &gormigrate.Migration{
global.LOG.Errorf("encrypt database db %s password failed, err: %v", data.Name, err) global.LOG.Errorf("encrypt database db %s password failed, err: %v", data.Name, err)
continue continue
} }
if err := global.DB.Model(&model.DatabaseMysql{}).Where("id = ?", data.ID).Updates(map[string]interface{}{ if err := tx.Model(&model.DatabaseMysql{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
"password": pass, "password": pass,
}).Error; err != nil { }).Error; err != nil {
global.LOG.Errorf("updata database db %s info failed, err: %v", data.Name, err) global.LOG.Errorf("updata database db %s info failed, err: %v", data.Name, err)
@ -748,7 +746,7 @@ var UpdateAppInstallResource = &gormigrate.Migration{
if err := tx.AutoMigrate(&model.AppInstallResource{}); err != nil { if err := tx.AutoMigrate(&model.AppInstallResource{}); err != nil {
return err return err
} }
if err := global.DB.Model(&model.AppInstallResource{}).Where("1 = 1").Updates(map[string]interface{}{ if err := tx.Model(&model.AppInstallResource{}).Where("1 = 1").Updates(map[string]interface{}{
"from": "local", "from": "local",
}).Error; err != nil { }).Error; err != nil {
return err return err