mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
fix: 修改应用导入恢复时数据库连接信息 (#1367)
This commit is contained in:
parent
b1d40960c4
commit
47dda4ac9f
@ -172,6 +172,7 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newEnvFile := ""
|
||||||
resource, _ := appInstallResourceRepo.GetFirst(appInstallResourceRepo.WithAppInstallId(install.ID))
|
resource, _ := appInstallResourceRepo.GetFirst(appInstallResourceRepo.WithAppInstallId(install.ID))
|
||||||
if resource.ID != 0 && install.App.Key != "mysql" {
|
if resource.ID != 0 && install.App.Key != "mysql" {
|
||||||
mysqlInfo, err := appInstallRepo.LoadBaseInfo(resource.Key, "")
|
mysqlInfo, err := appInstallRepo.LoadBaseInfo(resource.Key, "")
|
||||||
@ -183,7 +184,15 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
newDB, err := reCreateDB(db.ID, oldInstall.Env)
|
newDB, envMap, err := reCreateDB(db.ID, oldInstall.Env)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
oldHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", envMap["PANEL_DB_HOST"].(string))
|
||||||
|
newHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", mysqlInfo.ServiceName)
|
||||||
|
oldInstall.Env = strings.ReplaceAll(oldInstall.Env, oldHost, newHost)
|
||||||
|
envMap["PANEL_DB_HOST"] = mysqlInfo.ServiceName
|
||||||
|
newEnvFile, err = coverEnvJsonToStr(oldInstall.Env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -200,6 +209,16 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(newEnvFile) != 0 {
|
||||||
|
envPath := fmt.Sprintf("%s/%s/%s/.env", constant.AppInstallDir, install.App.Key, install.Name)
|
||||||
|
file, err := os.OpenFile(envPath, os.O_WRONLY|os.O_TRUNC, 0640)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
_, _ = file.WriteString(newEnvFile)
|
||||||
|
}
|
||||||
|
|
||||||
oldInstall.ID = install.ID
|
oldInstall.ID = install.ID
|
||||||
oldInstall.Status = constant.StatusRunning
|
oldInstall.Status = constant.StatusRunning
|
||||||
oldInstall.AppId = install.AppId
|
oldInstall.AppId = install.AppId
|
||||||
@ -212,14 +231,14 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, error) {
|
func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, map[string]interface{}, error) {
|
||||||
mysqlService := NewIMysqlService()
|
mysqlService := NewIMysqlService()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
_ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: dbID, DeleteBackup: true, ForceDelete: true})
|
_ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: dbID, DeleteBackup: true, ForceDelete: true})
|
||||||
|
|
||||||
envMap := make(map[string]interface{})
|
envMap := make(map[string]interface{})
|
||||||
if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil {
|
if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil {
|
||||||
return nil, err
|
return nil, envMap, err
|
||||||
}
|
}
|
||||||
oldName, _ := envMap["PANEL_DB_NAME"].(string)
|
oldName, _ := envMap["PANEL_DB_NAME"].(string)
|
||||||
oldUser, _ := envMap["PANEL_DB_USER"].(string)
|
oldUser, _ := envMap["PANEL_DB_USER"].(string)
|
||||||
@ -232,7 +251,8 @@ func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, error) {
|
|||||||
Permission: "%",
|
Permission: "%",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, envMap, err
|
||||||
}
|
}
|
||||||
return createDB, nil
|
|
||||||
|
return createDB, envMap, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user