fix: 解决创建PHP运行环境报错的问题 (#1882)

pull/1883/head
zhengkunwang 1 year ago committed by GitHub
parent f4d5b5437e
commit e262f4fa40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -98,10 +98,6 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (err error) {
if err != nil { if err != nil {
return return
} }
composeService, err := getComposeService(create.Name, newNameDir, composeContent, envContent, false)
if err != nil {
return
}
runtime := &model.Runtime{ runtime := &model.Runtime{
Name: create.Name, Name: create.Name,
DockerCompose: string(composeContent), DockerCompose: string(composeContent),
@ -117,7 +113,7 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (err error) {
if err = runtimeRepo.Create(context.Background(), runtime); err != nil { if err = runtimeRepo.Create(context.Background(), runtime); err != nil {
return return
} }
go buildRuntime(runtime, composeService, "") go buildRuntime(runtime, "")
return return
} }
@ -260,7 +256,6 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
if err != nil { if err != nil {
return err return err
} }
composeService, err := getComposeService(runtime.Name, runtimeDir, composeContent, envContent, false)
if err != nil { if err != nil {
return err return err
} }
@ -277,6 +272,6 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
if err != nil { if err != nil {
return err return err
} }
go buildRuntime(runtime, composeService, imageID) go buildRuntime(runtime, imageID)
return nil return nil
} }

@ -1,6 +1,7 @@
package service package service
import ( import (
"bytes"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/buserr"
@ -8,20 +9,42 @@ import (
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/docker/cli/cli/command"
"github.com/subosito/gotenv" "github.com/subosito/gotenv"
"io"
"os" "os"
"os/exec"
"path" "path"
"strings" "strings"
) )
func buildRuntime(runtime *model.Runtime, service *docker.ComposeService, oldImageID string) { func buildRuntime(runtime *model.Runtime, oldImageID string) {
err := service.ComposeBuild() runtimePath := path.Join(constant.RuntimeDir, runtime.Type, runtime.Name)
composePath := path.Join(runtimePath, "docker-compose.yml")
logPath := path.Join(runtimePath, "build.log")
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("Failed to open log file:", err)
return
}
defer func() {
_ = logFile.Close()
}()
cmd := exec.Command("docker-compose", "-f", composePath, "build")
multiWriterStdout := io.MultiWriter(os.Stdout, logFile)
cmd.Stdout = multiWriterStdout
var stderrBuf bytes.Buffer
multiWriterStderr := io.MultiWriter(&stderrBuf, logFile, os.Stderr)
cmd.Stderr = multiWriterStderr
err = cmd.Run()
if err != nil { if err != nil {
runtime.Status = constant.RuntimeError runtime.Status = constant.RuntimeError
runtime.Message = buserr.New(constant.ErrImageBuildErr).Error() + ":" + err.Error() runtime.Message = buserr.New(constant.ErrImageBuildErr).Error() + ":" + stderrBuf.String()
} else { } else {
runtime.Status = constant.RuntimeNormal runtime.Status = constant.RuntimeNormal
runtime.Message = ""
if oldImageID != "" { if oldImageID != "" {
client, err := docker.NewClient() client, err := docker.NewClient()
if err == nil { if err == nil {
@ -83,25 +106,3 @@ func handleParams(image, runtimeType, runtimeDir string, params map[string]inter
envContent = []byte(envStr) envContent = []byte(envStr)
return return
} }
func getComposeService(name, runtimeDir string, composeFile, env []byte, skipNormalization bool) (*docker.ComposeService, error) {
project, err := docker.GetComposeProject(name, runtimeDir, composeFile, env, skipNormalization)
if err != nil {
return nil, err
}
logPath := path.Join(runtimeDir, "build.log")
fileOp := files.NewFileOp()
if fileOp.Stat(logPath) {
_ = fileOp.DeleteFile(logPath)
}
file, err := os.Create(logPath)
if err != nil {
return nil, err
}
composeService, err := docker.NewComposeService(command.WithOutputStream(file))
if err != nil {
return nil, err
}
composeService.SetProject(project)
return composeService, nil
}

Loading…
Cancel
Save