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"
|
2023-09-25 17:50:14 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-04-02 16:54:00 +08:00
|
|
|
"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"
|
2023-09-25 17:50:14 +08:00
|
|
|
"path/filepath"
|
2023-04-02 16:54:00 +08:00
|
|
|
"strings"
|
2023-09-25 17:50:14 +08:00
|
|
|
"time"
|
2023-03-31 14:02:28 +08:00
|
|
|
)
|
|
|
|
|
2023-09-25 17:50:14 +08:00
|
|
|
func handleNode(create request.RuntimeCreate, runtime *model.Runtime, fileOp files.FileOp, appVersionDir string) (err error) {
|
|
|
|
runtimeDir := path.Join(constant.RuntimeDir, create.Type)
|
|
|
|
if err = fileOp.CopyDir(appVersionDir, runtimeDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
versionDir := path.Join(runtimeDir, filepath.Base(appVersionDir))
|
|
|
|
projectDir := path.Join(runtimeDir, create.Name)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
_ = fileOp.DeleteDir(projectDir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err = fileOp.Rename(versionDir, projectDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
composeContent, envContent, _, err := handleParams(create, projectDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
runtime.DockerCompose = string(composeContent)
|
|
|
|
runtime.Env = string(envContent)
|
2023-09-26 22:50:16 +08:00
|
|
|
runtime.Status = constant.RuntimeCreating
|
2023-09-25 17:50:14 +08:00
|
|
|
runtime.CodeDir = create.CodeDir
|
|
|
|
|
|
|
|
go startRuntime(runtime)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePHP(create request.RuntimeCreate, runtime *model.Runtime, fileOp files.FileOp, appVersionDir string) (err error) {
|
|
|
|
buildDir := path.Join(appVersionDir, "build")
|
|
|
|
if !fileOp.Stat(buildDir) {
|
|
|
|
return buserr.New(constant.ErrDirNotFound)
|
|
|
|
}
|
|
|
|
runtimeDir := path.Join(constant.RuntimeDir, create.Type)
|
|
|
|
tempDir := filepath.Join(runtimeDir, fmt.Sprintf("%d", time.Now().UnixNano()))
|
|
|
|
if err = fileOp.CopyDir(buildDir, tempDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
oldDir := path.Join(tempDir, "build")
|
|
|
|
projectDir := path.Join(runtimeDir, create.Name)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
_ = fileOp.DeleteDir(projectDir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if oldDir != projectDir {
|
|
|
|
if err = fileOp.Rename(oldDir, projectDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = fileOp.DeleteDir(tempDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
composeContent, envContent, forms, err := handleParams(create, projectDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
runtime.DockerCompose = string(composeContent)
|
|
|
|
runtime.Env = string(envContent)
|
|
|
|
runtime.Params = string(forms)
|
|
|
|
runtime.Status = constant.RuntimeBuildIng
|
|
|
|
|
|
|
|
go buildRuntime(runtime, "", false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func startRuntime(runtime *model.Runtime) {
|
2023-09-26 22:50:16 +08:00
|
|
|
if err := runComposeCmdWithLog("up", runtime.GetComposePath(), runtime.GetLogPath()); err != nil {
|
|
|
|
runtime.Status = constant.RuntimeError
|
|
|
|
runtime.Message = err.Error()
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
2023-09-25 17:50:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:50:16 +08:00
|
|
|
if err := SyncRuntimeContainerStatus(runtime); err != nil {
|
2023-09-25 17:50:14 +08:00
|
|
|
runtime.Status = constant.RuntimeError
|
2023-09-26 22:50:16 +08:00
|
|
|
runtime.Message = err.Error()
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
return
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
2023-09-26 22:50:16 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
2023-09-26 22:50:16 +08:00
|
|
|
func reCreateRuntime(runtime *model.Runtime) {
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
runtime.Status = constant.RuntimeError
|
|
|
|
runtime.Message = err.Error()
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err = runComposeCmdWithLog("down", runtime.GetComposePath(), runtime.GetLogPath()); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = runComposeCmdWithLog("up", runtime.GetComposePath(), runtime.GetLogPath()); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := SyncRuntimeContainerStatus(runtime); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func runComposeCmdWithLog(operate string, composePath string, logPath string) error {
|
|
|
|
cmd := exec.Command("docker-compose", "-f", composePath, operate)
|
|
|
|
if operate == "up" {
|
|
|
|
cmd = exec.Command("docker-compose", "-f", composePath, operate, "-d")
|
|
|
|
}
|
|
|
|
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("Failed to open log file: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return errors.New(buserr.New(constant.ErrRuntimeStart).Error() + ":" + stderrBuf.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:50:16 +08:00
|
|
|
func SyncRuntimeContainerStatus(runtime *model.Runtime) error {
|
|
|
|
env, err := gotenv.Unmarshal(runtime.Env)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var containerNames []string
|
|
|
|
if containerName, ok := env["CONTAINER_NAME"]; !ok {
|
|
|
|
return buserr.New("ErrContainerNameNotFound")
|
|
|
|
} else {
|
|
|
|
containerNames = append(containerNames, containerName)
|
|
|
|
}
|
|
|
|
cli, err := docker.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
containers, err := cli.ListContainersByName(containerNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(containers) == 0 {
|
|
|
|
return buserr.WithNameAndErr("ErrContainerNotFound", containerNames[0], nil)
|
|
|
|
}
|
|
|
|
container := containers[0]
|
|
|
|
|
|
|
|
interval := 10 * time.Second
|
|
|
|
retries := 60
|
|
|
|
for i := 0; i < retries; i++ {
|
|
|
|
resp, err := cli.InspectContainer(container.ID)
|
2023-09-25 17:50:14 +08:00
|
|
|
if err != nil {
|
2023-09-26 22:50:16 +08:00
|
|
|
time.Sleep(interval)
|
|
|
|
continue
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
2023-09-26 22:50:16 +08:00
|
|
|
if resp.State.Health != nil {
|
|
|
|
status := strings.ToLower(resp.State.Health.Status)
|
|
|
|
switch status {
|
|
|
|
case "starting":
|
|
|
|
runtime.Status = constant.RuntimeStarting
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
case "healthy":
|
|
|
|
runtime.Status = constant.RuntimeRunning
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
return nil
|
|
|
|
case "unhealthy":
|
|
|
|
runtime.Status = constant.RuntimeUnhealthy
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(interval)
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
2023-09-26 22:50:16 +08:00
|
|
|
return nil
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
|
|
|
|
2023-08-28 22:18:15 +08:00
|
|
|
func buildRuntime(runtime *model.Runtime, oldImageID string, rebuild bool) {
|
2023-09-25 17:50:14 +08:00
|
|
|
runtimePath := runtime.GetPath()
|
|
|
|
composePath := runtime.GetComposePath()
|
2023-08-08 22:32:11 +08:00
|
|
|
logPath := path.Join(runtimePath, "build.log")
|
|
|
|
|
|
|
|
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
2023-09-25 17:50:14 +08:00
|
|
|
global.LOG.Errorf("failed to open log file: %v", err)
|
2023-08-08 22:32:11 +08:00
|
|
|
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
|
|
|
|
2023-09-25 17:50:14 +08:00
|
|
|
func handleParams(create request.RuntimeCreate, projectDir string) (composeContent []byte, envContent []byte, forms []byte, err error) {
|
2023-04-02 16:54:00 +08:00
|
|
|
fileOp := files.NewFileOp()
|
2023-09-25 17:50:14 +08:00
|
|
|
composeContent, err = fileOp.GetContent(path.Join(projectDir, "docker-compose.yml"))
|
2023-04-02 16:54:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-28 10:56:16 +08:00
|
|
|
envPath := path.Join(projectDir, ".env")
|
|
|
|
if !fileOp.Stat(envPath) {
|
|
|
|
_ = fileOp.CreateFile(envPath)
|
|
|
|
}
|
|
|
|
env, err := gotenv.Read(envPath)
|
2023-04-02 16:54:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
switch create.Type {
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
create.Params["IMAGE_NAME"] = create.Image
|
|
|
|
forms, err = fileOp.GetContent(path.Join(projectDir, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if extends, ok := create.Params["PHP_EXTENSIONS"]; ok {
|
2023-04-02 16:54:00 +08:00
|
|
|
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
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
create.Params["PHP_EXTENSIONS"] = strings.Join(strArray, ",")
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
create.Params["CONTAINER_PACKAGE_URL"] = create.Source
|
|
|
|
case constant.RuntimeNode:
|
|
|
|
create.Params["CODE_DIR"] = create.CodeDir
|
|
|
|
create.Params["NODE_VERSION"] = create.Version
|
|
|
|
if create.NodeConfig.Install {
|
|
|
|
create.Params["RUN_INSTALL"] = "1"
|
|
|
|
} else {
|
|
|
|
create.Params["RUN_INSTALL"] = "0"
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
2023-04-02 16:54:00 +08:00
|
|
|
newMap := make(map[string]string)
|
2023-09-25 17:50:14 +08:00
|
|
|
handleMap(create.Params, newMap)
|
2023-04-02 16:54:00 +08:00
|
|
|
for k, v := range newMap {
|
|
|
|
env[k] = v
|
|
|
|
}
|
|
|
|
envStr, err := gotenv.Marshal(env)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-28 10:56:16 +08:00
|
|
|
if err = gotenv.Write(env, envPath); err != nil {
|
2023-04-02 16:54:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
envContent = []byte(envStr)
|
|
|
|
return
|
|
|
|
}
|