Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading