Skip to content

Commit cb4a456

Browse files
committed
fix: Fix the issue of container compose log cleanup failure
1 parent f9a20ec commit cb4a456

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

agent/app/api/v2/container.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,28 @@ func (b *BaseApi) CleanContainerLog(c *gin.Context) {
393393
helper.Success(c)
394394
}
395395

396+
// @Tags Container
397+
// @Summary Clean compose log
398+
// @Accept json
399+
// @Param request body dto.ComposeLogClean true "request"
400+
// @Success 200
401+
// @Security ApiKeyAuth
402+
// @Security Timestamp
403+
// @Router /containers/compose/clean/log [post]
404+
// @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理容器编排 [name] 日志","formatEN":"clean compose [name] logs"}
405+
func (b *BaseApi) CleanComposeLog(c *gin.Context) {
406+
var req dto.ComposeLogClean
407+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
408+
return
409+
}
410+
411+
if err := containerService.ComposeLogClean(req); err != nil {
412+
helper.InternalServer(c, err)
413+
return
414+
}
415+
helper.Success(c)
416+
}
417+
396418
// @Tags Container
397419
// @Summary Rename Container
398420
// @Accept json

agent/app/dto/container.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ type ComposeUpdate struct {
291291
Content string `json:"content" validate:"required"`
292292
Env []string `json:"env"`
293293
}
294+
type ComposeLogClean struct {
295+
Name string `json:"name" validate:"required"`
296+
Path string `json:"path" validate:"required"`
297+
}
294298

295299
type ContainerLog struct {
296300
Container string `json:"container" validate:"required"`

agent/app/service/container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type IContainerService interface {
8585
CreateVolume(req dto.VolumeCreate) error
8686
TestCompose(req dto.ComposeCreate) (bool, error)
8787
ComposeUpdate(req dto.ComposeUpdate) error
88+
ComposeLogClean(req dto.ComposeLogClean) error
8889
Prune(req dto.ContainerPrune) error
8990

9091
LoadUsers(req dto.OperationWithName) []string

agent/app/service/container_compose.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path"
9+
"path/filepath"
910
"sort"
1011
"strings"
1112
"time"
@@ -284,6 +285,57 @@ func (u *ContainerService) ComposeUpdate(req dto.ComposeUpdate) error {
284285
return nil
285286
}
286287

288+
func (u *ContainerService) ComposeLogClean(req dto.ComposeLogClean) error {
289+
client, err := docker.NewDockerClient()
290+
if err != nil {
291+
return err
292+
}
293+
defer client.Close()
294+
295+
options := container.ListOptions{All: true}
296+
options.Filters = filters.NewArgs()
297+
options.Filters.Add("label", composeProjectLabel)
298+
299+
list, err := client.ContainerList(context.Background(), options)
300+
if err != nil {
301+
return err
302+
}
303+
ctx := context.Background()
304+
for _, item := range list {
305+
if name, ok := item.Labels[composeProjectLabel]; ok {
306+
if name != req.Name {
307+
continue
308+
}
309+
containerItem, err := client.ContainerInspect(ctx, item.ID)
310+
if err != nil {
311+
return err
312+
}
313+
if err := client.ContainerStop(ctx, containerItem.ID, container.StopOptions{}); err != nil {
314+
return err
315+
}
316+
file, err := os.OpenFile(containerItem.LogPath, os.O_RDWR|os.O_CREATE, constant.FilePerm)
317+
if err != nil {
318+
return err
319+
}
320+
defer file.Close()
321+
if err = file.Truncate(0); err != nil {
322+
return err
323+
}
324+
_, _ = file.Seek(0, 0)
325+
326+
files, _ := filepath.Glob(fmt.Sprintf("%s.*", containerItem.LogPath))
327+
for _, file := range files {
328+
_ = os.Remove(file)
329+
}
330+
}
331+
}
332+
return u.ComposeOperation(dto.ComposeOperation{
333+
Name: req.Name,
334+
Path: req.Path,
335+
Operation: "restart",
336+
})
337+
}
338+
287339
func (u *ContainerService) loadPath(req *dto.ComposeCreate) error {
288340
if req.From == "template" || req.From == "edit" {
289341
dir := fmt.Sprintf("%s/docker/compose/%s", global.Dir.DataDir, req.Name)

agent/router/ro_container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (s *ContainerRouter) InitRouter(Router *gin.RouterGroup) {
4848
baRouter.POST("/compose", baseApi.CreateCompose)
4949
baRouter.POST("/compose/test", baseApi.TestCompose)
5050
baRouter.POST("/compose/operate", baseApi.OperatorCompose)
51+
baRouter.POST("/compose/clean/log", baseApi.CleanComposeLog)
5152
baRouter.POST("/compose/update", baseApi.ComposeUpdate)
5253

5354
baRouter.GET("/template", baseApi.ListComposeTemplate)

0 commit comments

Comments
 (0)