Implement teardown removeAllImages

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov
2015-04-24 10:37:21 -07:00
parent 310fc37fe7
commit a9688cdca5
20 changed files with 50 additions and 269 deletions
+49
View File
@@ -396,6 +396,55 @@ func deleteAllContainers() error {
return nil
}
var protectedImages = map[string]struct{}{}
func init() {
out, err := exec.Command(dockerBinary, "images").CombinedOutput()
if err != nil {
panic(err)
}
lines := strings.Split(string(out), "\n")[1:]
for _, l := range lines {
if l == "" {
continue
}
fields := strings.Fields(l)
imgTag := fields[0] + ":" + fields[1]
protectedImages[imgTag] = struct{}{}
}
}
func deleteAllImages() error {
out, err := exec.Command(dockerBinary, "images").CombinedOutput()
if err != nil {
return err
}
lines := strings.Split(string(out), "\n")[1:]
var imgs []string
for _, l := range lines {
if l == "" {
continue
}
fields := strings.Fields(l)
imgTag := fields[0] + ":" + fields[1]
if _, ok := protectedImages[imgTag]; !ok {
if fields[0] == "<none>" {
imgs = append(imgs, fields[2])
continue
}
imgs = append(imgs, imgTag)
}
}
if len(imgs) == 0 {
return nil
}
args := append([]string{"rmi", "-f"}, imgs...)
if err := exec.Command(dockerBinary, args...).Run(); err != nil {
return err
}
return nil
}
func getPausedContainers() (string, error) {
getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)