This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Container functions to a dedicated file into internal directory
Signed-off-by: Guillaume Lours <[email protected]>
- Loading branch information
Showing
2 changed files
with
56 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/filters" | ||
"github.com/docker/docker/client" | ||
) | ||
|
||
func CollectContainers(cli *client.Client, project string) (map[string][]types.Container, error) { | ||
containerList, err := cli.ContainerList(context.Background(), types.ContainerListOptions{ | ||
All: true, | ||
Filters: filters.NewArgs(filters.Arg("label", LabelProject+"="+project)), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
containers := map[string][]types.Container{} | ||
for _, c := range containerList { | ||
service := c.Labels[LabelService] | ||
l, ok := containers[service] | ||
if !ok { | ||
l = []types.Container{c} | ||
} else { | ||
l = append(l, c) | ||
} | ||
containers[service] = l | ||
} | ||
return containers, nil | ||
} | ||
|
||
func RemoveContainers(cli *client.Client, containers []types.Container) error { | ||
ctx := context.Background() | ||
for _, c := range containers { | ||
if serviceName, ok := c.Labels[LabelService]; ok { | ||
fmt.Printf("Stopping containers for service %s ... ", serviceName) | ||
} | ||
err := cli.ContainerStop(ctx, c.ID, nil) | ||
if err != nil { | ||
return err | ||
} | ||
err = cli.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(c.ID) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters