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-03-31 14:02:28 +08:00
|
|
|
"fmt"
|
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"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"github.com/subosito/gotenv"
|
|
|
|
"path"
|
2023-03-31 14:02:28 +08:00
|
|
|
"path/filepath"
|
2023-04-02 16:54:00 +08:00
|
|
|
"strings"
|
2023-03-31 14:02:28 +08:00
|
|
|
"time"
|
2023-03-29 14:58:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RuntimeService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type IRuntimeService interface {
|
|
|
|
Page(req request.RuntimeSearch) (int64, []response.RuntimeRes, error)
|
2023-03-30 16:47:47 +08:00
|
|
|
Create(create request.RuntimeCreate) error
|
2023-03-31 14:02:28 +08:00
|
|
|
Delete(id uint) error
|
2023-04-02 16:54:00 +08:00
|
|
|
Update(req request.RuntimeUpdate) error
|
|
|
|
Get(id uint) (res *response.RuntimeRes, err error)
|
2023-03-29 14:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRuntimeService() IRuntimeService {
|
|
|
|
return &RuntimeService{}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:54:00 +08:00
|
|
|
func (r *RuntimeService) Create(create request.RuntimeCreate) (err error) {
|
2023-04-03 17:47:23 +08:00
|
|
|
exist, _ := runtimeRepo.GetFirst(runtimeRepo.WithName(create.Name))
|
2023-04-02 16:54:00 +08:00
|
|
|
if exist != nil {
|
2023-04-03 17:47:23 +08:00
|
|
|
return buserr.New(constant.ErrNameIsExist)
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
2023-03-30 16:47:47 +08:00
|
|
|
if create.Resource == constant.ResourceLocal {
|
|
|
|
runtime := &model.Runtime{
|
|
|
|
Name: create.Name,
|
|
|
|
Resource: create.Resource,
|
|
|
|
Type: create.Type,
|
2023-04-03 17:47:23 +08:00
|
|
|
Version: create.Version,
|
2023-03-30 16:47:47 +08:00
|
|
|
Status: constant.RuntimeNormal,
|
|
|
|
}
|
|
|
|
return runtimeRepo.Create(context.Background(), runtime)
|
|
|
|
}
|
2023-04-03 17:47:23 +08:00
|
|
|
exist, _ = runtimeRepo.GetFirst(runtimeRepo.WithImage(create.Image))
|
|
|
|
if exist != nil {
|
|
|
|
return buserr.New(constant.ErrImageExist)
|
|
|
|
}
|
2023-03-30 16:47:47 +08:00
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(create.AppDetailID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
buildDir := path.Join(constant.AppResourceDir, app.Key, "versions", appDetail.Version, "build")
|
|
|
|
if !fileOp.Stat(buildDir) {
|
|
|
|
return buserr.New(constant.ErrDirNotFound)
|
|
|
|
}
|
2023-03-31 14:02:28 +08:00
|
|
|
runtimeDir := path.Join(constant.RuntimeDir, create.Type)
|
|
|
|
tempDir := filepath.Join(runtimeDir, fmt.Sprintf("%d", time.Now().UnixNano()))
|
2023-04-02 16:54:00 +08:00
|
|
|
if err = fileOp.CopyDir(buildDir, tempDir); err != nil {
|
|
|
|
return
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
|
|
|
oldDir := path.Join(tempDir, "build")
|
2023-03-31 14:02:28 +08:00
|
|
|
newNameDir := path.Join(runtimeDir, create.Name)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2023-03-30 16:47:47 +08:00
|
|
|
_ = fileOp.DeleteDir(newNameDir)
|
|
|
|
}
|
2023-03-31 14:02:28 +08:00
|
|
|
}()
|
2023-03-30 16:47:47 +08:00
|
|
|
if oldDir != newNameDir {
|
2023-04-02 16:54:00 +08:00
|
|
|
if err = fileOp.Rename(oldDir, newNameDir); err != nil {
|
|
|
|
return
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
if err = fileOp.DeleteDir(tempDir); err != nil {
|
|
|
|
return
|
2023-03-31 14:02:28 +08:00
|
|
|
}
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
composeContent, envContent, forms, err := handleParams(create.Image, create.Type, newNameDir, create.Params)
|
2023-03-30 16:47:47 +08:00
|
|
|
if err != nil {
|
2023-04-02 16:54:00 +08:00
|
|
|
return
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
composeService, err := getComposeService(create.Name, newNameDir, composeContent, envContent)
|
2023-03-30 16:47:47 +08:00
|
|
|
if err != nil {
|
2023-04-02 16:54:00 +08:00
|
|
|
return
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
|
|
|
runtime := &model.Runtime{
|
|
|
|
Name: create.Name,
|
2023-04-02 16:54:00 +08:00
|
|
|
DockerCompose: string(composeContent),
|
|
|
|
Env: string(envContent),
|
2023-03-30 16:47:47 +08:00
|
|
|
AppDetailID: create.AppDetailID,
|
|
|
|
Type: create.Type,
|
|
|
|
Image: create.Image,
|
|
|
|
Resource: create.Resource,
|
2023-03-31 14:02:28 +08:00
|
|
|
Status: constant.RuntimeBuildIng,
|
|
|
|
Version: create.Version,
|
2023-04-02 16:54:00 +08:00
|
|
|
Params: string(forms),
|
2023-03-30 16:47:47 +08:00
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
if err = runtimeRepo.Create(context.Background(), runtime); err != nil {
|
|
|
|
return
|
2023-03-31 14:02:28 +08:00
|
|
|
}
|
|
|
|
go buildRuntime(runtime, composeService)
|
2023-04-02 16:54:00 +08:00
|
|
|
return
|
2023-03-29 14:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuntimeService) Page(req request.RuntimeSearch) (int64, []response.RuntimeRes, error) {
|
|
|
|
var (
|
|
|
|
opts []repo.DBOption
|
|
|
|
res []response.RuntimeRes
|
|
|
|
)
|
|
|
|
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-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 {
|
|
|
|
res = append(res, response.RuntimeRes{
|
|
|
|
Runtime: runtime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return total, res, nil
|
|
|
|
}
|
2023-03-31 14:02:28 +08:00
|
|
|
|
|
|
|
func (r *RuntimeService) Delete(id uint) error {
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-06 00:09:58 +08:00
|
|
|
website, _ := websiteRepo.GetFirst(websiteRepo.WithRuntimeID(id))
|
|
|
|
if website.ID > 0 {
|
|
|
|
return buserr.New(constant.ErrDelWithWebsite)
|
|
|
|
}
|
2023-04-03 17:47:23 +08:00
|
|
|
//TODO 删除镜像
|
2023-03-31 14:02:28 +08:00
|
|
|
if runtime.Resource == constant.ResourceAppstore {
|
|
|
|
runtimeDir := path.Join(constant.RuntimeDir, runtime.Type, runtime.Name)
|
|
|
|
if err := files.NewFileOp().DeleteDir(runtimeDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runtimeRepo.DeleteBy(commonRepo.WithByID(id))
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
|
|
|
|
func (r *RuntimeService) Get(id uint) (*response.RuntimeRes, error) {
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := &response.RuntimeRes{}
|
|
|
|
res.Runtime = *runtime
|
|
|
|
if runtime.Resource == constant.ResourceLocal {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(runtime.AppDetailID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.AppID = appDetail.AppId
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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 {
|
2023-04-06 00:09:58 +08:00
|
|
|
if v == "" {
|
|
|
|
appParam.Value = []string{}
|
|
|
|
} else {
|
|
|
|
appParam.Value = strings.Split(v, ",")
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
} else {
|
|
|
|
for _, fv := range form.Values {
|
|
|
|
if fv.Value == v {
|
|
|
|
appParam.ShowValue = fv.Label
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appParam.Values = form.Values
|
|
|
|
}
|
|
|
|
appParams = append(appParams, appParam)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.AppParams = appParams
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-03 17:47:23 +08:00
|
|
|
exist, _ := runtimeRepo.GetFirst(runtimeRepo.WithImage(req.Name), runtimeRepo.WithNotId(req.ID))
|
|
|
|
if exist != nil {
|
|
|
|
return buserr.New(constant.ErrImageExist)
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
runtimeDir := path.Join(constant.RuntimeDir, runtime.Type, runtime.Name)
|
|
|
|
composeContent, envContent, _, err := handleParams(req.Image, runtime.Type, runtimeDir, req.Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
composeService, err := getComposeService(runtime.Name, runtimeDir, composeContent, envContent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
runtime.Image = req.Image
|
|
|
|
runtime.Env = string(envContent)
|
|
|
|
runtime.DockerCompose = string(composeContent)
|
|
|
|
runtime.Status = constant.RuntimeBuildIng
|
|
|
|
_ = runtimeRepo.Save(runtime)
|
|
|
|
go buildRuntime(runtime, composeService)
|
|
|
|
return nil
|
|
|
|
}
|