Skip to content

Commit

Permalink
v1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
donknap committed Oct 30, 2024
1 parent 3f91607 commit 97af114
Show file tree
Hide file tree
Showing 34 changed files with 285 additions and 229 deletions.
38 changes: 29 additions & 9 deletions app/application/http/controller/compose-container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import (
logic2 "github.com/donknap/dpanel/app/common/logic"
"github.com/donknap/dpanel/common/accessor"
"github.com/donknap/dpanel/common/dao"
"github.com/donknap/dpanel/common/function"
"github.com/donknap/dpanel/common/service/docker"
"github.com/donknap/dpanel/common/service/notice"
"github.com/donknap/dpanel/common/service/storage"
"github.com/gin-gonic/gin"
"log/slog"
"os"
"path/filepath"
)

func (self Compose) ContainerDeploy(http *gin.Context) {
Expand Down Expand Up @@ -70,6 +75,7 @@ func (self Compose) ContainerDeploy(http *gin.Context) {
composeRow.Setting.Status = composeRun.Status
}
_, _ = dao.Compose.Updates(composeRow)
notice.Message{}.Success("composeDeploy", composeRow.Name)
self.JsonSuccessResponse(http)
return
}
Expand All @@ -78,8 +84,8 @@ func (self Compose) ContainerDestroy(http *gin.Context) {
type ParamsValidate struct {
Id int32 `json:"id" binding:"required"`
DeleteImage bool `json:"deleteImage"`
DeleteData bool `json:"deleteData"`
DeleteVolume bool `json:"deleteVolume"`
DeleteData bool `json:"deleteData"`
}

params := ParamsValidate{}
Expand All @@ -101,21 +107,35 @@ func (self Compose) ContainerDestroy(http *gin.Context) {
self.JsonResponseWithError(http, err, 500)
return
}
if params.DeleteData {
_, err := dao.Compose.Where(dao.Compose.ID.In(params.Id)).Delete()
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
}
composeRun, err := logic.Compose{}.LsItem(tasker.Name)
if err != nil {
composeRow.Setting.Status = logic.ComposeStatusWaiting
} else {
composeRow.Setting.Status = composeRun.Status
}
_, _ = dao.Compose.Updates(composeRow)
notice.Message{}.Success("composeDestroy", composeRow.Name)

if function.InArray([]string{
logic.ComposeTypeText, logic.ComposeTypeRemoteUrl,
}, composeRow.Setting.Type) {
err = os.RemoveAll(filepath.Join(storage.Local{}.GetComposePath(), composeRow.Name))
if err != nil {
slog.Debug("compose", "destroy", err)
}
} else {
path := filepath.Join(filepath.Dir(tasker.Composer.Project.ComposeFiles[0]), logic.ComposeProjectDeployFileName)
err = os.Remove(path)
if err != nil {
slog.Debug("compose", "delete deploy file", err, "path", path)
}
}
if params.DeleteData {
_, err = dao.Compose.Where(dao.Compose.ID.Eq(composeRow.ID)).Delete()
if err != nil {
slog.Debug("compose", "destroy", err)
}
}
_ = notice.Message{}.Success("composeDestroy", composeRow.Name)
self.JsonSuccessResponse(http)
return
}
Expand Down
37 changes: 10 additions & 27 deletions app/application/http/controller/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/donknap/dpanel/common/entity"
"github.com/donknap/dpanel/common/function"
"github.com/donknap/dpanel/common/service/compose"
"github.com/donknap/dpanel/common/service/storage"
"github.com/gin-gonic/gin"
"github.com/we7coreteam/w7-rangine-go/v2/src/http/controller"
"io"
Expand All @@ -28,7 +27,7 @@ func (self Compose) Create(http *gin.Context) {
type ParamsValidate struct {
Id int32 `json:"id"`
Title string `json:"title"`
Name string `json:"name" binding:"required"`
Name string `json:"name" binding:"required,lowercase"`
Type string `json:"type" binding:"required"`
Yaml string `json:"yaml"`
RemoteUrl string `json:"remoteUrl"`
Expand Down Expand Up @@ -141,30 +140,22 @@ func (self Compose) GetList(http *gin.Context) {

func (self Compose) GetDetail(http *gin.Context) {
type ParamsValidate struct {
Id int32 `json:"id"`
Name string `json:"name"`
Id int32 `json:"id" binding:"required"`
}
params := ParamsValidate{}
if !self.Validate(http, &params) {
return
}
var yamlRow *entity.Compose

if params.Id > 0 {
yamlRow, _ = dao.Compose.Where(dao.Compose.ID.Eq(params.Id)).First()
if yamlRow == nil {
self.JsonResponseWithError(http, errors.New("任务不存在"), 500)
return
}
params.Name = yamlRow.Name
} else if params.Name != "" {
yamlRow, _ = dao.Compose.Where(dao.Compose.Name.Eq(params.Name)).First()
yamlRow, _ := dao.Compose.Where(dao.Compose.ID.Eq(params.Id)).First()
if yamlRow == nil {
self.JsonResponseWithError(http, errors.New("任务不存在"), 500)
return
}

tasker, err := logic.Compose{}.GetTasker(yamlRow)
if err != nil {
// 如果是外部任务并且获取不到yaml,则直接返回基本状态
if yamlRow != nil && yamlRow.Setting.Type == logic.ComposeTypeOutPath {
if yamlRow.Setting.Type == logic.ComposeTypeOutPath {
data := gin.H{
"detail": yamlRow,
}
Expand All @@ -177,7 +168,7 @@ func (self Compose) GetDetail(http *gin.Context) {
self.JsonResponseWithError(http, err, 500)
return
}
yaml, err := tasker.OriginalYaml()
yaml, err := tasker.Yaml()
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
Expand All @@ -189,7 +180,7 @@ func (self Compose) GetDetail(http *gin.Context) {
"project": tasker.Project(),
}

if yamlRow != nil && yamlRow.Setting.Status != logic.ComposeStatusWaiting {
if yamlRow.Setting.Status != logic.ComposeStatusWaiting {
data["containerList"] = tasker.Ps()
}

Expand All @@ -213,20 +204,12 @@ func (self Compose) Delete(http *gin.Context) {
return
}
for _, runItem := range composeRunList {
if fmt.Sprintf(logic.ComposeProjectName, row.ID) == runItem.Name {
if fmt.Sprintf(logic.ComposeProjectName, row.Name) == runItem.Name {
self.JsonResponseWithError(http, errors.New("请先销毁容器"), 500)
return
}
}
if row.Setting.Type == logic.ComposeTypeText || row.Setting.Type == logic.ComposeTypeRemoteUrl {
err = os.RemoveAll(filepath.Join(storage.Local{}.GetComposePath(), row.Name))
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
}
_, err = dao.Compose.Where(dao.Compose.ID.Eq(id)).Delete()

if err != nil {
self.JsonResponseWithError(http, err, 500)
return
Expand Down
Loading

0 comments on commit 97af114

Please sign in to comment.