2023-03-29 14:58:28 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-03-30 16:47:47 +08:00
|
|
|
"context"
|
2023-04-02 16:54:00 +08:00
|
|
|
"encoding/json"
|
2023-10-27 10:12:15 +08:00
|
|
|
"fmt"
|
2024-04-25 16:45:12 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-04-02 16:54:00 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2023-03-29 14:58:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2023-03-30 16:47:47 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-03-29 14:58:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2023-03-30 16:47:47 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-04-12 17:22:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-10-10 17:34:28 +08:00
|
|
|
cmd2 "github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-09-25 17:50:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
2023-04-11 17:08:29 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2023-10-10 17:34:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/env"
|
2023-03-30 16:47:47 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-09-25 17:50:14 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-03-30 16:47:47 +08:00
|
|
|
"github.com/subosito/gotenv"
|
2023-03-29 14:58:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RuntimeService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type IRuntimeService interface {
|
2023-09-25 17:50:14 +08:00
|
|
|
Page(req request.RuntimeSearch) (int64, []response.RuntimeDTO, error)
|
2023-12-12 15:18:09 +08:00
|
|
|
Create(create request.RuntimeCreate) (*model.Runtime, error)
|
2023-09-25 17:50:14 +08:00
|
|
|
Delete(delete request.RuntimeDelete) error
|
2023-04-02 16:54:00 +08:00
|
|
|
Update(req request.RuntimeUpdate) error
|
2023-09-25 17:50:14 +08:00
|
|
|
Get(id uint) (res *response.RuntimeDTO, err error)
|
|
|
|
GetNodePackageRunScript(req request.NodePackageReq) ([]response.PackageScripts, error)
|
2023-09-26 22:50:16 +08:00
|
|
|
OperateRuntime(req request.RuntimeOperate) error
|
2023-10-09 19:36:32 +08:00
|
|
|
GetNodeModules(req request.NodeModuleReq) ([]response.NodeModule, error)
|
2023-10-11 11:22:29 +08:00
|
|
|
OperateNodeModules(req request.NodeModuleOperateReq) error
|
2023-11-02 14:35:58 +08:00
|
|
|
SyncForRestart() error
|
2024-03-15 15:36:07 +08:00
|
|
|
SyncRuntimeStatus() error
|
2024-04-30 11:58:49 +08:00
|
|
|
DeleteCheck(installID uint) ([]dto.AppResource, error)
|
2023-03-29 14:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRuntimeService() IRuntimeService {
|
|
|
|
return &RuntimeService{}
|
|
|
|
}
|
|
|
|
|
2023-12-12 15:18:09 +08:00
|
|
|
func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, error) {
|
2023-09-28 15:28:21 +08:00
|
|
|
var (
|
|
|
|
opts []repo.DBOption
|
|
|
|
)
|
|
|
|
if create.Name != "" {
|
|
|
|
opts = append(opts, commonRepo.WithLikeName(create.Name))
|
|
|
|
}
|
|
|
|
if create.Type != "" {
|
|
|
|
opts = append(opts, commonRepo.WithByType(create.Type))
|
|
|
|
}
|
|
|
|
exist, _ := runtimeRepo.GetFirst(opts...)
|
2023-04-02 16:54:00 +08:00
|
|
|
if exist != nil {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, buserr.New(constant.ErrNameIsExist)
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
|
|
|
|
switch create.Type {
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
if create.Resource == constant.ResourceLocal {
|
|
|
|
runtime := &model.Runtime{
|
|
|
|
Name: create.Name,
|
|
|
|
Resource: create.Resource,
|
|
|
|
Type: create.Type,
|
|
|
|
Version: create.Version,
|
|
|
|
Status: constant.RuntimeNormal,
|
|
|
|
}
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, runtimeRepo.Create(context.Background(), runtime)
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
exist, _ = runtimeRepo.GetFirst(runtimeRepo.WithImage(create.Image))
|
|
|
|
if exist != nil {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, buserr.New(constant.ErrImageExist)
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
|
|
|
case constant.RuntimeNode:
|
|
|
|
if !fileOp.Stat(create.CodeDir) {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, buserr.New(constant.ErrPathNotFound)
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
|
|
|
create.Install = true
|
2023-12-12 15:18:09 +08:00
|
|
|
if err := checkPortExist(create.Port); err != nil {
|
|
|
|
return nil, err
|
2023-09-28 15:28:21 +08:00
|
|
|
}
|
2023-11-05 21:41:31 +08:00
|
|
|
for _, export := range create.ExposedPorts {
|
2023-12-12 15:18:09 +08:00
|
|
|
if err := checkPortExist(export.HostPort); err != nil {
|
|
|
|
return nil, err
|
2023-11-05 21:41:31 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 17:06:14 +08:00
|
|
|
if containerName, ok := create.Params["CONTAINER_NAME"]; ok {
|
|
|
|
if err := checkContainerName(containerName.(string)); err != nil {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, err
|
2023-10-08 17:06:14 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-03 17:47:23 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
2023-03-30 16:47:47 +08:00
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(create.AppDetailID))
|
|
|
|
if err != nil {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, err
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
|
|
|
|
if err != nil {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, err
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-12-12 10:36:12 +08:00
|
|
|
|
|
|
|
appVersionDir := filepath.Join(app.GetAppResourcePath(), appDetail.Version)
|
2023-06-29 15:02:11 +08:00
|
|
|
if !fileOp.Stat(appVersionDir) || appDetail.Update {
|
2023-12-12 15:18:09 +08:00
|
|
|
if err = downloadApp(app, appDetail, nil); err != nil {
|
|
|
|
return nil, err
|
2023-05-18 19:08:20 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
|
|
|
runtime := &model.Runtime{
|
|
|
|
Name: create.Name,
|
|
|
|
AppDetailID: create.AppDetailID,
|
|
|
|
Type: create.Type,
|
|
|
|
Image: create.Image,
|
|
|
|
Resource: create.Resource,
|
|
|
|
Version: create.Version,
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
|
|
|
switch create.Type {
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
if err = handlePHP(create, runtime, fileOp, appVersionDir); err != nil {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, err
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
case constant.RuntimeNode:
|
2023-09-28 15:28:21 +08:00
|
|
|
runtime.Port = create.Port
|
2023-09-25 17:50:14 +08:00
|
|
|
if err = handleNode(create, runtime, fileOp, appVersionDir); err != nil {
|
2023-12-12 15:18:09 +08:00
|
|
|
return nil, err
|
2023-03-31 14:02:28 +08:00
|
|
|
}
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-12-12 15:18:09 +08:00
|
|
|
if err := runtimeRepo.Create(context.Background(), runtime); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return runtime, nil
|
2023-03-29 14:58:28 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 17:50:14 +08:00
|
|
|
func (r *RuntimeService) Page(req request.RuntimeSearch) (int64, []response.RuntimeDTO, error) {
|
2023-03-29 14:58:28 +08:00
|
|
|
var (
|
|
|
|
opts []repo.DBOption
|
2023-09-25 17:50:14 +08:00
|
|
|
res []response.RuntimeDTO
|
2023-03-29 14:58:28 +08:00
|
|
|
)
|
|
|
|
if req.Name != "" {
|
|
|
|
opts = append(opts, commonRepo.WithLikeName(req.Name))
|
|
|
|
}
|
2023-04-06 00:09:58 +08:00
|
|
|
if req.Status != "" {
|
|
|
|
opts = append(opts, runtimeRepo.WithStatus(req.Status))
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
if req.Type != "" {
|
|
|
|
opts = append(opts, commonRepo.WithByType(req.Type))
|
|
|
|
}
|
2023-03-29 14:58:28 +08:00
|
|
|
total, runtimes, err := runtimeRepo.Page(req.Page, req.PageSize, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
for _, runtime := range runtimes {
|
2023-09-25 17:50:14 +08:00
|
|
|
runtimeDTO := response.NewRuntimeDTO(runtime)
|
|
|
|
runtimeDTO.Params = make(map[string]interface{})
|
|
|
|
envs, err := gotenv.Unmarshal(runtime.Env)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
for k, v := range envs {
|
|
|
|
runtimeDTO.Params[k] = v
|
|
|
|
}
|
|
|
|
res = append(res, runtimeDTO)
|
2023-03-29 14:58:28 +08:00
|
|
|
}
|
|
|
|
return total, res, nil
|
|
|
|
}
|
2023-03-31 14:02:28 +08:00
|
|
|
|
2024-04-30 11:58:49 +08:00
|
|
|
func (r *RuntimeService) DeleteCheck(runTimeId uint) ([]dto.AppResource, error) {
|
|
|
|
var res []dto.AppResource
|
|
|
|
websites, _ := websiteRepo.GetBy(websiteRepo.WithRuntimeID(runTimeId))
|
|
|
|
for _, website := range websites {
|
|
|
|
res = append(res, dto.AppResource{
|
|
|
|
Type: "website",
|
|
|
|
Name: website.PrimaryDomain,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-09-25 17:50:14 +08:00
|
|
|
func (r *RuntimeService) Delete(runtimeDelete request.RuntimeDelete) error {
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(runtimeDelete.ID))
|
2023-03-31 14:02:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
website, _ := websiteRepo.GetFirst(websiteRepo.WithRuntimeID(runtimeDelete.ID))
|
2023-04-06 00:09:58 +08:00
|
|
|
if website.ID > 0 {
|
|
|
|
return buserr.New(constant.ErrDelWithWebsite)
|
|
|
|
}
|
2023-03-31 14:02:28 +08:00
|
|
|
if runtime.Resource == constant.ResourceAppstore {
|
2023-09-25 17:50:14 +08:00
|
|
|
projectDir := runtime.GetPath()
|
|
|
|
switch runtime.Type {
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
client, err := docker.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-25 16:45:12 +08:00
|
|
|
defer client.Close()
|
2023-09-25 17:50:14 +08:00
|
|
|
imageID, err := client.GetImageIDByName(runtime.Image)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if imageID != "" {
|
|
|
|
if err := client.DeleteImage(imageID); err != nil {
|
|
|
|
global.LOG.Errorf("delete image id [%s] error %v", imageID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case constant.RuntimeNode:
|
|
|
|
if out, err := compose.Down(runtime.GetComposePath()); err != nil && !runtimeDelete.ForceDelete {
|
|
|
|
if out != "" {
|
|
|
|
return errors.New(out)
|
|
|
|
}
|
|
|
|
return err
|
2023-04-12 17:22:30 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
if err := files.NewFileOp().DeleteDir(projectDir); err != nil && !runtimeDelete.ForceDelete {
|
2023-03-31 14:02:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
return runtimeRepo.DeleteBy(commonRepo.WithByID(runtimeDelete.ID))
|
2023-03-31 14:02:28 +08:00
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
|
2023-09-25 17:50:14 +08:00
|
|
|
func (r *RuntimeService) Get(id uint) (*response.RuntimeDTO, error) {
|
2023-04-02 16:54:00 +08:00
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
|
|
|
res := response.NewRuntimeDTO(*runtime)
|
2023-04-02 16:54:00 +08:00
|
|
|
if runtime.Resource == constant.ResourceLocal {
|
2023-09-25 17:50:14 +08:00
|
|
|
return &res, nil
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(runtime.AppDetailID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.AppID = appDetail.AppId
|
2023-09-25 17:50:14 +08:00
|
|
|
switch runtime.Type {
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
var (
|
|
|
|
appForm dto.AppForm
|
|
|
|
appParams []response.AppParam
|
|
|
|
)
|
|
|
|
if err := json.Unmarshal([]byte(runtime.Params), &appForm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
envs, err := gotenv.Unmarshal(runtime.Env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v, ok := envs["CONTAINER_PACKAGE_URL"]; ok {
|
|
|
|
res.Source = v
|
|
|
|
}
|
|
|
|
for _, form := range appForm.FormFields {
|
|
|
|
if v, ok := envs[form.EnvKey]; ok {
|
|
|
|
appParam := response.AppParam{
|
|
|
|
Edit: false,
|
|
|
|
Key: form.EnvKey,
|
|
|
|
Rule: form.Rule,
|
|
|
|
Type: form.Type,
|
|
|
|
Required: form.Required,
|
|
|
|
}
|
|
|
|
if form.Edit {
|
|
|
|
appParam.Edit = true
|
|
|
|
}
|
|
|
|
appParam.LabelZh = form.LabelZh
|
|
|
|
appParam.LabelEn = form.LabelEn
|
|
|
|
appParam.Multiple = form.Multiple
|
|
|
|
appParam.Value = v
|
|
|
|
if form.Type == "select" {
|
|
|
|
if form.Multiple {
|
|
|
|
if v == "" {
|
|
|
|
appParam.Value = []string{}
|
|
|
|
} else {
|
|
|
|
appParam.Value = strings.Split(v, ",")
|
|
|
|
}
|
2023-04-06 00:09:58 +08:00
|
|
|
} else {
|
2023-09-25 17:50:14 +08:00
|
|
|
for _, fv := range form.Values {
|
|
|
|
if fv.Value == v {
|
|
|
|
appParam.ShowValue = fv.Label
|
|
|
|
break
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
appParam.Values = form.Values
|
|
|
|
}
|
|
|
|
appParams = append(appParams, appParam)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.AppParams = appParams
|
|
|
|
case constant.RuntimeNode:
|
|
|
|
res.Params = make(map[string]interface{})
|
|
|
|
envs, err := gotenv.Unmarshal(runtime.Env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range envs {
|
|
|
|
switch k {
|
|
|
|
case "NODE_APP_PORT", "PANEL_APP_PORT_HTTP":
|
|
|
|
port, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
res.Params[k] = port
|
|
|
|
default:
|
2023-10-27 10:12:15 +08:00
|
|
|
if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") {
|
|
|
|
if strings.Contains(k, "CONTAINER_PORT") {
|
|
|
|
r := regexp.MustCompile(`_(\d+)$`)
|
|
|
|
matches := r.FindStringSubmatch(k)
|
|
|
|
containerPort, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hostPort, err := strconv.Atoi(envs[fmt.Sprintf("HOST_PORT_%s", matches[1])])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.ExposedPorts = append(res.ExposedPorts, request.ExposedPort{
|
|
|
|
ContainerPort: containerPort,
|
|
|
|
HostPort: hostPort,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.Params[k] = v
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-07 17:00:47 +08:00
|
|
|
if v, ok := envs["CONTAINER_PACKAGE_URL"]; ok {
|
|
|
|
res.Source = v
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
|
|
|
return &res, nil
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if runtime.Resource == constant.ResourceLocal {
|
|
|
|
runtime.Version = req.Version
|
|
|
|
return runtimeRepo.Save(runtime)
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
oldImage := runtime.Image
|
|
|
|
switch runtime.Type {
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
exist, _ := runtimeRepo.GetFirst(runtimeRepo.WithImage(req.Name), runtimeRepo.WithNotId(req.ID))
|
|
|
|
if exist != nil {
|
|
|
|
return buserr.New(constant.ErrImageExist)
|
|
|
|
}
|
2023-09-28 15:28:21 +08:00
|
|
|
case constant.RuntimeNode:
|
|
|
|
if runtime.Port != req.Port {
|
|
|
|
if err = checkPortExist(req.Port); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
runtime.Port = req.Port
|
|
|
|
}
|
2023-11-05 21:41:31 +08:00
|
|
|
for _, export := range req.ExposedPorts {
|
|
|
|
if err = checkPortExist(export.HostPort); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-10-08 17:06:14 +08:00
|
|
|
if containerName, ok := req.Params["CONTAINER_NAME"]; ok {
|
|
|
|
envs, err := gotenv.Unmarshal(runtime.Env)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldContainerName := envs["CONTAINER_NAME"]
|
|
|
|
if containerName != oldContainerName {
|
|
|
|
if err := checkContainerName(containerName.(string)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-25 16:05:03 +08:00
|
|
|
|
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(runtime.AppDetailID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
appVersionDir := path.Join(constant.AppResourceDir, app.Resource, app.Key, appDetail.Version)
|
|
|
|
if !fileOp.Stat(appVersionDir) || appDetail.Update {
|
|
|
|
if err := downloadApp(app, appDetail, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = fileOp.Rename(path.Join(runtime.GetPath(), "run.sh"), path.Join(runtime.GetPath(), "run.sh.bak"))
|
|
|
|
_ = fileOp.CopyFile(path.Join(appVersionDir, "run.sh"), runtime.GetPath())
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
|
|
|
|
projectDir := path.Join(constant.RuntimeDir, runtime.Type, runtime.Name)
|
|
|
|
create := request.RuntimeCreate{
|
|
|
|
Image: req.Image,
|
|
|
|
Type: runtime.Type,
|
|
|
|
Source: req.Source,
|
|
|
|
Params: req.Params,
|
|
|
|
CodeDir: req.CodeDir,
|
|
|
|
Version: req.Version,
|
2023-09-28 15:28:21 +08:00
|
|
|
NodeConfig: request.NodeConfig{
|
2023-10-27 10:12:15 +08:00
|
|
|
Port: req.Port,
|
|
|
|
Install: true,
|
|
|
|
ExposedPorts: req.ExposedPorts,
|
2023-09-28 15:28:21 +08:00
|
|
|
},
|
2023-09-25 17:50:14 +08:00
|
|
|
}
|
|
|
|
composeContent, envContent, _, err := handleParams(create, projectDir)
|
2023-04-02 16:54:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
runtime.Env = string(envContent)
|
|
|
|
runtime.DockerCompose = string(composeContent)
|
2023-09-25 17:50:14 +08:00
|
|
|
|
|
|
|
switch runtime.Type {
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
runtime.Image = req.Image
|
|
|
|
runtime.Status = constant.RuntimeBuildIng
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
client, err := docker.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-25 16:45:12 +08:00
|
|
|
defer client.Close()
|
2023-09-25 17:50:14 +08:00
|
|
|
imageID, err := client.GetImageIDByName(oldImage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go buildRuntime(runtime, imageID, req.Rebuild)
|
|
|
|
case constant.RuntimeNode:
|
|
|
|
runtime.Version = req.Version
|
|
|
|
runtime.CodeDir = req.CodeDir
|
2023-09-28 15:28:21 +08:00
|
|
|
runtime.Port = req.Port
|
2023-09-25 17:50:14 +08:00
|
|
|
runtime.Status = constant.RuntimeReCreating
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
go reCreateRuntime(runtime)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuntimeService) GetNodePackageRunScript(req request.NodePackageReq) ([]response.PackageScripts, error) {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(req.CodeDir) {
|
|
|
|
return nil, buserr.New(constant.ErrPathNotFound)
|
|
|
|
}
|
|
|
|
if !fileOp.Stat(path.Join(req.CodeDir, "package.json")) {
|
|
|
|
return nil, buserr.New(constant.ErrPackageJsonNotFound)
|
|
|
|
}
|
|
|
|
content, err := fileOp.GetContent(path.Join(req.CodeDir, "package.json"))
|
2023-04-11 17:08:29 +08:00
|
|
|
if err != nil {
|
2023-09-25 17:50:14 +08:00
|
|
|
return nil, err
|
2023-04-11 17:08:29 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
var packageMap map[string]interface{}
|
|
|
|
err = json.Unmarshal(content, &packageMap)
|
2023-04-11 17:08:29 +08:00
|
|
|
if err != nil {
|
2023-09-25 17:50:14 +08:00
|
|
|
return nil, err
|
2023-04-11 17:08:29 +08:00
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
scripts, ok := packageMap["scripts"]
|
|
|
|
if !ok {
|
|
|
|
return nil, buserr.New(constant.ErrScriptsNotFound)
|
|
|
|
}
|
|
|
|
var packageScripts []response.PackageScripts
|
|
|
|
for k, v := range scripts.(map[string]interface{}) {
|
|
|
|
packageScripts = append(packageScripts, response.PackageScripts{
|
|
|
|
Name: k,
|
|
|
|
Script: v.(string),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return packageScripts, nil
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
2023-09-26 22:50:16 +08:00
|
|
|
|
|
|
|
func (r *RuntimeService) OperateRuntime(req request.RuntimeOperate) error {
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
runtime.Status = constant.RuntimeError
|
|
|
|
runtime.Message = err.Error()
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
switch req.Operate {
|
|
|
|
case constant.RuntimeUp:
|
|
|
|
if err = runComposeCmdWithLog(req.Operate, runtime.GetComposePath(), runtime.GetLogPath()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = SyncRuntimeContainerStatus(runtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case constant.RuntimeDown:
|
|
|
|
if err = runComposeCmdWithLog(req.Operate, runtime.GetComposePath(), runtime.GetLogPath()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
runtime.Status = constant.RuntimeStopped
|
|
|
|
case constant.RuntimeRestart:
|
|
|
|
if err = runComposeCmdWithLog(constant.RuntimeDown, runtime.GetComposePath(), runtime.GetLogPath()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = runComposeCmdWithLog(constant.RuntimeUp, runtime.GetComposePath(), runtime.GetLogPath()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = SyncRuntimeContainerStatus(runtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runtimeRepo.Save(runtime)
|
|
|
|
}
|
2023-10-09 19:36:32 +08:00
|
|
|
|
|
|
|
func (r *RuntimeService) GetNodeModules(req request.NodeModuleReq) ([]response.NodeModule, error) {
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var res []response.NodeModule
|
|
|
|
nodeModulesPath := path.Join(runtime.CodeDir, "node_modules")
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(nodeModulesPath) {
|
|
|
|
return nil, buserr.New("ErrNodeModulesNotFound")
|
|
|
|
}
|
|
|
|
moduleDirs, err := os.ReadDir(nodeModulesPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, moduleDir := range moduleDirs {
|
|
|
|
packagePath := path.Join(nodeModulesPath, moduleDir.Name(), "package.json")
|
|
|
|
if !fileOp.Stat(packagePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
content, err := fileOp.GetContent(packagePath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
module := response.NodeModule{}
|
|
|
|
if err := json.Unmarshal(content, &module); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, module)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2023-10-10 17:34:28 +08:00
|
|
|
|
2023-10-11 11:22:29 +08:00
|
|
|
func (r *RuntimeService) OperateNodeModules(req request.NodeModuleOperateReq) error {
|
2023-10-10 17:34:28 +08:00
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
containerName, err := env.GetEnvValueByKey(runtime.GetEnvPath(), "CONTAINER_NAME")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd := req.PkgManager
|
|
|
|
switch req.Operate {
|
|
|
|
case constant.RuntimeInstall:
|
|
|
|
if req.PkgManager == constant.RuntimeNpm {
|
|
|
|
cmd += " install"
|
|
|
|
} else {
|
|
|
|
cmd += " add"
|
|
|
|
}
|
|
|
|
case constant.RuntimeUninstall:
|
|
|
|
if req.PkgManager == constant.RuntimeNpm {
|
|
|
|
cmd += " uninstall"
|
|
|
|
} else {
|
|
|
|
cmd += " remove"
|
|
|
|
}
|
|
|
|
case constant.RuntimeUpdate:
|
2023-10-11 17:50:30 +08:00
|
|
|
if req.PkgManager == constant.RuntimeNpm {
|
|
|
|
cmd += " update"
|
|
|
|
} else {
|
|
|
|
cmd += " upgrade"
|
|
|
|
}
|
2023-10-10 17:34:28 +08:00
|
|
|
}
|
|
|
|
cmd += " " + req.Module
|
|
|
|
return cmd2.ExecContainerScript(containerName, cmd, 5*time.Minute)
|
|
|
|
}
|
2023-11-02 14:35:58 +08:00
|
|
|
|
|
|
|
func (r *RuntimeService) SyncForRestart() error {
|
|
|
|
runtimes, err := runtimeRepo.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, runtime := range runtimes {
|
|
|
|
if runtime.Status == constant.RuntimeBuildIng || runtime.Status == constant.RuntimeReCreating || runtime.Status == constant.RuntimeStarting || runtime.Status == constant.RuntimeCreating {
|
|
|
|
runtime.Status = constant.SystemRestart
|
2023-12-11 16:24:06 +08:00
|
|
|
runtime.Message = "System restart causing interrupt"
|
2023-11-02 14:35:58 +08:00
|
|
|
_ = runtimeRepo.Save(&runtime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-15 15:36:07 +08:00
|
|
|
|
|
|
|
func (r *RuntimeService) SyncRuntimeStatus() error {
|
|
|
|
runtimes, err := runtimeRepo.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, runtime := range runtimes {
|
|
|
|
if runtime.Type == constant.RuntimeNode {
|
|
|
|
_ = SyncRuntimeContainerStatus(&runtime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|