1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

fix: 解决 PHP 网站切换版本导致 serviceName 变更的问题 (#2737)

Refs https://github.com/1Panel-dev/1Panel/issues/2689
This commit is contained in:
zhengkunwang 2023-10-31 17:53:50 +08:00 committed by GitHub
parent d43bc3e427
commit bc80dfdc68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -1313,6 +1313,7 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error
fpmConfDir = path.Join(confDir, "php-fpm.conf") fpmConfDir = path.Join(confDir, "php-fpm.conf")
phpDir = path.Join(constant.RuntimeDir, runtime.Type, runtime.Name, "php") phpDir = path.Join(constant.RuntimeDir, runtime.Type, runtime.Name, "php")
oldFmContent, _ = fileOp.GetContent(fpmConfDir) oldFmContent, _ = fileOp.GetContent(fpmConfDir)
newComposeByte []byte
) )
envParams := make(map[string]string, len(envs)) envParams := make(map[string]string, len(envs))
handleMap(envs, envParams) handleMap(envs, envParams)
@ -1332,7 +1333,13 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error
if busErr = env.Write(envParams, envPath); busErr != nil { if busErr = env.Write(envParams, envPath); busErr != nil {
return busErr return busErr
} }
if busErr = fileOp.WriteFile(composePath, strings.NewReader(appDetail.DockerCompose), 0775); busErr != nil {
newComposeByte, busErr = changeServiceName(composePath, appInstall.ServiceName)
if busErr != nil {
return err
}
if busErr = fileOp.WriteFile(composePath, bytes.NewReader(newComposeByte), 0775); busErr != nil {
return busErr return busErr
} }
if !req.RetainConfig { if !req.RetainConfig {
@ -1362,7 +1369,7 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error
appInstall.AppDetailId = runtime.AppDetailID appInstall.AppDetailId = runtime.AppDetailID
appInstall.AppId = appDetail.AppId appInstall.AppId = appDetail.AppId
appInstall.Version = appDetail.Version appInstall.Version = appDetail.Version
appInstall.DockerCompose = appDetail.DockerCompose appInstall.DockerCompose = string(newComposeByte)
_ = appInstallRepo.Save(context.Background(), &appInstall) _ = appInstallRepo.Save(context.Background(), &appInstall)
website.RuntimeID = req.RuntimeID website.RuntimeID = req.RuntimeID

View File

@ -5,6 +5,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components" "github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
"gopkg.in/yaml.v3"
"path" "path"
"strconv" "strconv"
"strings" "strings"
@ -641,3 +642,34 @@ func chownRootDir(path string) error {
} }
return nil return nil
} }
func changeServiceName(composePath, newServiceName string) (composeByte []byte, err error) {
composeMap := make(map[string]interface{})
fileOp := files.NewFileOp()
composeContent, _ := fileOp.GetContent(composePath)
if err = yaml.Unmarshal(composeContent, &composeMap); err != nil {
return
}
value, ok := composeMap["services"]
if !ok {
err = buserr.New(constant.ErrFileParse)
return
}
servicesMap := value.(map[string]interface{})
index := 0
serviceName := ""
for k := range servicesMap {
serviceName = k
if index > 0 {
continue
}
index++
}
if newServiceName != serviceName {
servicesMap[newServiceName] = servicesMap[serviceName]
delete(servicesMap, serviceName)
}
return yaml.Marshal(composeMap)
}