2023-03-30 16:47:47 +08:00
|
|
|
package service
|
2023-03-31 14:02:28 +08:00
|
|
|
|
|
|
|
import (
|
2023-08-08 22:32:11 +08:00
|
|
|
"bytes"
|
2023-04-02 16:54:00 +08:00
|
|
|
"fmt"
|
2023-08-28 22:18:15 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
2023-03-31 14:02:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-04-11 17:08:29 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-03-31 14:02:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2023-04-02 16:54:00 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"github.com/subosito/gotenv"
|
2023-08-08 22:32:11 +08:00
|
|
|
"io"
|
2023-04-17 14:04:22 +08:00
|
|
|
"os"
|
2023-08-08 22:32:11 +08:00
|
|
|
"os/exec"
|
2023-04-02 16:54:00 +08:00
|
|
|
"path"
|
|
|
|
"strings"
|
2023-03-31 14:02:28 +08:00
|
|
|
)
|
|
|
|
|
2023-08-28 22:18:15 +08:00
|
|
|
func buildRuntime(runtime *model.Runtime, oldImageID string, rebuild bool) {
|
2023-08-08 22:32:11 +08:00
|
|
|
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()
|
2023-03-31 14:02:28 +08:00
|
|
|
if err != nil {
|
|
|
|
runtime.Status = constant.RuntimeError
|
2023-08-08 22:32:11 +08:00
|
|
|
runtime.Message = buserr.New(constant.ErrImageBuildErr).Error() + ":" + stderrBuf.String()
|
2023-03-31 14:02:28 +08:00
|
|
|
} else {
|
|
|
|
runtime.Status = constant.RuntimeNormal
|
2023-08-08 22:32:11 +08:00
|
|
|
runtime.Message = ""
|
2023-04-13 14:42:34 +08:00
|
|
|
if oldImageID != "" {
|
2023-04-11 17:08:29 +08:00
|
|
|
client, err := docker.NewClient()
|
|
|
|
if err == nil {
|
2023-04-13 14:42:34 +08:00
|
|
|
newImageID, err := client.GetImageIDByName(runtime.Image)
|
|
|
|
if err == nil && newImageID != oldImageID {
|
|
|
|
global.LOG.Infof("delete imageID [%s] ", oldImageID)
|
|
|
|
if err := client.DeleteImage(oldImageID); err != nil {
|
|
|
|
global.LOG.Errorf("delete imageID [%s] error %v", oldImageID, err)
|
|
|
|
} else {
|
|
|
|
global.LOG.Infof("delete old image success")
|
|
|
|
}
|
2023-04-11 17:08:29 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-04-13 14:42:34 +08:00
|
|
|
global.LOG.Errorf("delete imageID [%s] error %v", oldImageID, err)
|
2023-04-11 17:08:29 +08:00
|
|
|
}
|
|
|
|
}
|
2023-08-28 22:18:15 +08:00
|
|
|
if rebuild && runtime.ID > 0 {
|
|
|
|
websites, _ := websiteRepo.GetBy(websiteRepo.WithRuntimeID(runtime.ID))
|
|
|
|
if len(websites) > 0 {
|
|
|
|
installService := NewIAppInstalledService()
|
|
|
|
installMap := make(map[uint]string)
|
|
|
|
for _, website := range websites {
|
|
|
|
if website.AppInstallID > 0 {
|
|
|
|
installMap[website.AppInstallID] = website.PrimaryDomain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for installID, domain := range installMap {
|
|
|
|
go func(installID uint, domain string) {
|
|
|
|
global.LOG.Infof("rebuild php runtime [%s] domain [%s]", runtime.Name, domain)
|
|
|
|
if err := installService.Operate(request.AppInstalledOperate{
|
|
|
|
InstallId: installID,
|
|
|
|
Operate: constant.Rebuild,
|
|
|
|
}); err != nil {
|
|
|
|
global.LOG.Errorf("rebuild php runtime [%s] domain [%s] error %v", runtime.Name, domain, err)
|
|
|
|
}
|
|
|
|
}(installID, domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 14:02:28 +08:00
|
|
|
}
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
|
|
|
|
func handleParams(image, runtimeType, runtimeDir string, params map[string]interface{}) (composeContent []byte, envContent []byte, forms []byte, err error) {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
composeContent, err = fileOp.GetContent(path.Join(runtimeDir, "docker-compose.yml"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
env, err := gotenv.Read(path.Join(runtimeDir, ".env"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
forms, err = fileOp.GetContent(path.Join(runtimeDir, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
params["IMAGE_NAME"] = image
|
|
|
|
if runtimeType == constant.RuntimePHP {
|
|
|
|
if extends, ok := params["PHP_EXTENSIONS"]; ok {
|
|
|
|
if extendsArray, ok := extends.([]interface{}); ok {
|
|
|
|
strArray := make([]string, len(extendsArray))
|
|
|
|
for i, v := range extendsArray {
|
2023-06-30 23:26:12 +08:00
|
|
|
strArray[i] = strings.ToLower(fmt.Sprintf("%v", v))
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
|
|
|
params["PHP_EXTENSIONS"] = strings.Join(strArray, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newMap := make(map[string]string)
|
|
|
|
handleMap(params, newMap)
|
|
|
|
for k, v := range newMap {
|
|
|
|
env[k] = v
|
|
|
|
}
|
|
|
|
envStr, err := gotenv.Marshal(env)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = gotenv.Write(env, path.Join(runtimeDir, ".env")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
envContent = []byte(envStr)
|
|
|
|
return
|
|
|
|
}
|