From 60378cacbfc83caa409b464753012bc11d5ec8af Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Thu, 10 Aug 2023 22:50:00 -0600 Subject: [PATCH] Do a better job of removing MMR built images --- Dockerfile | 1 + test/test_internals/deps.go | 1 + test/test_internals/deps_mmr.go | 38 +++++++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0b7aab9d..b40f5a0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ # ---- Stage 0 ---- # Builds media repo binaries FROM golang:1.20-alpine AS builder +LABEL io.t2bot.mmr.cleanup="true" # Install build dependencies RUN apk add --no-cache git musl-dev dos2unix build-base diff --git a/test/test_internals/deps.go b/test/test_internals/deps.go index 24e2543c..0971af82 100644 --- a/test/test_internals/deps.go +++ b/test/test_internals/deps.go @@ -177,6 +177,7 @@ func (c *ContainerDeps) Teardown() { if err := os.Remove(c.mmrExtConfigPath); err != nil && !os.IsNotExist(err) { log.Fatalf("Error cleaning up MMR-External config file '%s': %s", c.mmrExtConfigPath, err.Error()) } + TeardownMmrCaches() } func (c *ContainerDeps) Debug() { diff --git a/test/test_internals/deps_mmr.go b/test/test_internals/deps_mmr.go index 585a28b4..17d49c60 100644 --- a/test/test_internals/deps_mmr.go +++ b/test/test_internals/deps_mmr.go @@ -12,6 +12,7 @@ import ( "strings" "text/template" + "github.com/docker/docker/api/types" "github.com/docker/go-connections/nat" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" @@ -181,14 +182,43 @@ func (c *mmrContainer) Teardown() { if err := os.Remove(c.tmpConfigPath); err != nil && !os.IsNotExist(err) { log.Fatalf("Error cleaning up MMR config file '%s': %s", c.tmpConfigPath, err.Error()) } +} + +func (c *mmrContainer) Logs() (io.ReadCloser, error) { + return c.container.Logs(c.ctx) +} + +func TeardownMmrCaches() { if mmrCachedContext != nil { _ = mmrCachedContext.Close() // ignore errors because testcontainers might have already closed it if err := os.Remove(mmrCachedContext.Name()); err != nil && !os.IsNotExist(err) { log.Fatalf("Error cleaning up MMR cached context file '%s': %s", mmrCachedContext.Name(), err.Error()) } } -} - -func (c *mmrContainer) Logs() (io.ReadCloser, error) { - return c.container.Logs(c.ctx) + if mmrCachedImage != "" { + if p, err := (testcontainers.GenericContainerRequest{}.ProviderType.GetProvider()); err != nil { + log.Fatalf("Error cleaning up MMR cached build image '%s': %s", mmrCachedImage, err.Error()) + } else if dockerProvider, ok := p.(*testcontainers.DockerProvider); !ok { + log.Fatalf("Error cleaning up MMR cached build image '%s': unable to cast provider to DockerProvider", mmrCachedImage) + } else { + rmImage := func(imageName string) { + if _, err = dockerProvider.Client().ImageRemove(context.Background(), imageName, types.ImageRemoveOptions{ + PruneChildren: true, + }); err != nil { + log.Printf("Error removing MMR cached build image '%s': %s", imageName, err.Error()) + log.Println() + } + } + if images, err := dockerProvider.Client().ImageList(context.Background(), types.ImageListOptions{All: true}); err != nil { + log.Fatalf("Error listing Docker images to clean up MMR image '%s': %s", mmrCachedImage, err.Error()) + } else { + rmImage(mmrCachedImage) + for _, i := range images { + if i.Labels != nil && i.Labels["io.t2bot.mmr.cleanup"] == "true" { + rmImage(i.ID) + } + } + } + } + } } -- GitLab