Skip to content

Commit

Permalink
feat: 增加运行环境编辑功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 committed Apr 6, 2023
1 parent 22d9bda commit d4c1caa
Show file tree
Hide file tree
Showing 23 changed files with 581 additions and 107 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ build_linux_on_mac:
cd $(SERVER_PATH) \
&& CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ $(GOBUILD) -trimpath -ldflags '-s -w --extldflags "-static -fpic"' -o $(BUILD_PATH)/$(APP_NAME) $(MAIN)

build_on_archlinux:
cd $(SERVER_PATH) \
&& CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GOBUILD) -trimpath -ldflags '-s -w --extldflags "-fpic"' -tags osusergo -o $(BUILD_PATH)/$(APP_NAME) $(MAIN)


build_all: build_web build_bin
44 changes: 44 additions & 0 deletions backend/app/api/v1/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,47 @@ func (b *BaseApi) DeleteRuntime(c *gin.Context) {
}
helper.SuccessWithOutData(c)
}

// @Tags Runtime
// @Summary Update runtime
// @Description 更新运行环境
// @Accept json
// @Param request body request.RuntimeUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /runtimes/update [post]
// @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"更新运行环境 [name]","formatEN":"Update runtime [name]"}
func (b *BaseApi) UpdateRuntime(c *gin.Context) {
var req request.RuntimeUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := runtimeService.Update(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}

// @Tags Runtime
// @Summary Get runtime
// @Description 获取运行环境
// @Accept json
// @Param id path string true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /runtimes/:id [get]
func (b *BaseApi) GetRuntime(c *gin.Context) {
id, err := helper.GetIntParamByKey(c, "id")
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil)
return
}
res, err := runtimeService.Get(id)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, res)
}
1 change: 1 addition & 0 deletions backend/app/dto/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type AppFormFields struct {
Disabled bool `json:"disabled"`
Edit bool `json:"edit"`
Rule string `json:"rule"`
Multiple bool `json:"multiple"`
Values []AppFormValue `json:"values"`
}

Expand Down
10 changes: 9 additions & 1 deletion backend/app/dto/request/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ type RuntimeCreate struct {
}

type RuntimeDelete struct {
ID uint `json:"Id"`
ID uint `json:"id"`
}

type RuntimeUpdate struct {
Name string `json:"name"`
ID uint `json:"id"`
Params map[string]interface{} `json:"params"`
Image string `json:"image"`
Version string `json:"version"`
}
3 changes: 3 additions & 0 deletions backend/app/dto/response/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type AppDetailDTO struct {
model.AppDetail
Enable bool `json:"enable"`
Params interface{} `json:"params"`
Image string `json:"image"`
}

type AppInstalledDTO struct {
Expand Down Expand Up @@ -70,4 +71,6 @@ type AppParam struct {
Type string `json:"type"`
Values interface{} `json:"values"`
ShowValue string `json:"showValue"`
Required bool `json:"required"`
Multiple bool `json:"multiple"`
}
3 changes: 3 additions & 0 deletions backend/app/dto/response/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import "github.com/1Panel-dev/1Panel/backend/app/model"

type RuntimeRes struct {
model.Runtime
AppParams []AppParam `json:"appParams"`
AppID uint `json:"appId"`
Version string `json:"version"`
}
15 changes: 15 additions & 0 deletions backend/app/repo/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package repo
import (
"context"
"github.com/1Panel-dev/1Panel/backend/app/model"
"gorm.io/gorm"
)

type RuntimeRepo struct {
}

type IRuntimeRepo interface {
WithNameOrImage(name string, image string) DBOption
WithOtherNameOrImage(name string, image string, id uint) DBOption
Page(page, size int, opts ...DBOption) (int64, []model.Runtime, error)
Create(ctx context.Context, runtime *model.Runtime) error
Save(runtime *model.Runtime) error
Expand All @@ -20,6 +23,18 @@ func NewIRunTimeRepo() IRuntimeRepo {
return &RuntimeRepo{}
}

func (r *RuntimeRepo) WithNameOrImage(name string, image string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("name = ? or image = ?", name, image)
}
}

func (r *RuntimeRepo) WithOtherNameOrImage(name string, image string, id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("name = ? or image = ? and id != ?", name, image, id)
}
}

func (r *RuntimeRepo) Page(page, size int, opts ...DBOption) (int64, []model.Runtime, error) {
var runtimes []model.Runtime
db := getDb(opts...).Model(&model.Runtime{})
Expand Down
21 changes: 20 additions & 1 deletion backend/app/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ func (a AppService) GetAppDetail(appId uint, version, appType string) (response.
if err != nil {
return appDetailDTO, err
}
paramsPath := path.Join(constant.AppResourceDir, app.Key, "versions", detail.Version, "build", "config.json")
fileOp := files.NewFileOp()
buildPath := path.Join(constant.AppResourceDir, app.Key, "versions", detail.Version, "build")
paramsPath := path.Join(buildPath, "config.json")
if !fileOp.Stat(paramsPath) {
return appDetailDTO, buserr.New(constant.ErrFileNotExist)
}
Expand All @@ -170,6 +171,24 @@ func (a AppService) GetAppDetail(appId uint, version, appType string) (response.
return appDetailDTO, err
}
appDetailDTO.Params = paramMap
composePath := path.Join(buildPath, "docker-compose.yml")
if !fileOp.Stat(composePath) {
return appDetailDTO, buserr.New(constant.ErrFileNotExist)
}
compose, err := fileOp.GetContent(composePath)
if err != nil {
return appDetailDTO, err
}
composeMap := make(map[string]interface{})
if err := yaml.Unmarshal(compose, &composeMap); err != nil {
return appDetailDTO, err
}
if service, ok := composeMap["services"]; ok {
servicesMap := service.(map[string]interface{})
for k := range servicesMap {
appDetailDTO.Image = k
}
}
} else {
paramMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(detail.Params), &paramMap); err != nil {
Expand Down
Loading

0 comments on commit d4c1caa

Please sign in to comment.