Skip to content

Commit

Permalink
fix: 解决 PHP 网站切换版本导致 serviceName 变更的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 committed Oct 31, 2023
1 parent d43bc3e commit 47edfe0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions backend/app/service/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error
fpmConfDir = path.Join(confDir, "php-fpm.conf")
phpDir = path.Join(constant.RuntimeDir, runtime.Type, runtime.Name, "php")
oldFmContent, _ = fileOp.GetContent(fpmConfDir)
newComposeByte []byte
)
envParams := make(map[string]string, len(envs))
handleMap(envs, envParams)
Expand All @@ -1332,7 +1333,13 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error
if busErr = env.Write(envParams, envPath); busErr != nil {
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
}
if !req.RetainConfig {
Expand Down Expand Up @@ -1362,7 +1369,7 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error
appInstall.AppDetailId = runtime.AppDetailID
appInstall.AppId = appDetail.AppId
appInstall.Version = appDetail.Version
appInstall.DockerCompose = appDetail.DockerCompose
appInstall.DockerCompose = string(newComposeByte)

_ = appInstallRepo.Save(context.Background(), &appInstall)
website.RuntimeID = req.RuntimeID
Expand Down
32 changes: 32 additions & 0 deletions backend/app/service/website_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
"gopkg.in/yaml.v3"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -641,3 +642,34 @@ func chownRootDir(path string) error {
}
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)
}

0 comments on commit 47edfe0

Please sign in to comment.