|
|
@ -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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|