|
6 | 6 | "fmt" |
7 | 7 | "os" |
8 | 8 | "path" |
| 9 | + "path/filepath" |
9 | 10 | "sort" |
10 | 11 | "strings" |
11 | 12 | "time" |
@@ -284,6 +285,57 @@ func (u *ContainerService) ComposeUpdate(req dto.ComposeUpdate) error { |
284 | 285 | return nil |
285 | 286 | } |
286 | 287 |
|
| 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 | + |
287 | 339 | func (u *ContainerService) loadPath(req *dto.ComposeCreate) error { |
288 | 340 | if req.From == "template" || req.From == "edit" { |
289 | 341 | dir := fmt.Sprintf("%s/docker/compose/%s", global.Dir.DataDir, req.Name) |
|
0 commit comments