(req dto.SearchWithPage)
| 33 | const composeCreatedBy = "createdBy" |
| 34 | |
| 35 | func (u *ContainerService) PageCompose(req dto.SearchWithPage) (int64, interface{}, error) { |
| 36 | var ( |
| 37 | records []dto.ComposeInfo |
| 38 | BackDatas []dto.ComposeInfo |
| 39 | ) |
| 40 | client, err := docker.NewDockerClient() |
| 41 | if err != nil { |
| 42 | return 0, nil, err |
| 43 | } |
| 44 | defer client.Close() |
| 45 | |
| 46 | options := container.ListOptions{All: true} |
| 47 | options.Filters = filters.NewArgs() |
| 48 | options.Filters.Add("label", composeProjectLabel) |
| 49 | |
| 50 | list, err := client.ContainerList(context.Background(), options) |
| 51 | if err != nil { |
| 52 | return 0, nil, err |
| 53 | } |
| 54 | |
| 55 | composeCreatedByLocal, _ := composeRepo.ListRecord() |
| 56 | composeLocalMap := make(map[string]dto.ComposeInfo) |
| 57 | for _, localItem := range composeCreatedByLocal { |
| 58 | composeItemLocal := dto.ComposeInfo{ |
| 59 | ContainerCount: 0, |
| 60 | CreatedAt: localItem.CreatedAt.Format(constant.DateTimeLayout), |
| 61 | ConfigFile: localItem.Path, |
| 62 | Workdir: strings.TrimSuffix(localItem.Path, "/docker-compose.yml"), |
| 63 | } |
| 64 | composeItemLocal.CreatedBy = "1Panel" |
| 65 | composeItemLocal.Path = localItem.Path |
| 66 | composeLocalMap[localItem.Name] = composeItemLocal |
| 67 | } |
| 68 | |
| 69 | composeMap := make(map[string]dto.ComposeInfo) |
| 70 | for _, container := range list { |
| 71 | if name, ok := container.Labels[composeProjectLabel]; ok { |
| 72 | containerItem := dto.ComposeContainer{ |
| 73 | ContainerID: container.ID, |
| 74 | Name: container.Names[0][1:], |
| 75 | State: container.State, |
| 76 | CreateTime: time.Unix(container.Created, 0).Format(constant.DateTimeLayout), |
| 77 | Ports: transPortToStr(container.Ports), |
| 78 | } |
| 79 | if compose, has := composeMap[name]; has { |
| 80 | compose.ContainerCount++ |
| 81 | if strings.ToLower(containerItem.State) == "running" { |
| 82 | compose.RunningCount++ |
| 83 | } |
| 84 | compose.Containers = append(compose.Containers, containerItem) |
| 85 | composeMap[name] = compose |
| 86 | } else { |
| 87 | config := container.Labels[composeConfigLabel] |
| 88 | workdir := container.Labels[composeWorkdirLabel] |
| 89 | composeItem := dto.ComposeInfo{ |
| 90 | ContainerCount: 1, |
| 91 | CreatedAt: time.Unix(container.Created, 0).Format(constant.DateTimeLayout), |
| 92 | ConfigFile: config, |
nothing calls this directly
no test coverage detected