From c75f4e468c6399c06f8cd68e5497908adb75a513 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 14 Sep 2022 11:45:40 +0200 Subject: [PATCH] remove also the container image on delete --- internal/pkg/api/container/container.go | 8 ++++++-- internal/pkg/container/util.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/internal/pkg/api/container/container.go b/internal/pkg/api/container/container.go index 934262d1..ce546b26 100644 --- a/internal/pkg/api/container/container.go +++ b/internal/pkg/api/container/container.go @@ -119,9 +119,13 @@ ARG_LOOP: err := container.DeleteSource(containerName) if err != nil { wwlog.Error("Could not remove source: %s\n", containerName) - } else { - fmt.Printf("Container has been deleted: %s\n", containerName) } + err = container.DeleteImage(containerName) + if err != nil { + wwlog.Error("Could not remove image files %s\n", containerName) + } + + fmt.Printf("Container has been deleted: %s\n", containerName) } return diff --git a/internal/pkg/container/util.go b/internal/pkg/container/util.go index 49a1cf4e..0d722297 100644 --- a/internal/pkg/container/util.go +++ b/internal/pkg/container/util.go @@ -64,9 +64,33 @@ func ValidSource(name string) bool { return true } +/* +Delete the chroot of a container +*/ func DeleteSource(name string) error { fullPath := SourceDir(name) wwlog.Verbose("Removing path: %s\n", fullPath) return os.RemoveAll(fullPath) } + +/* +Delete the image of a container +*/ +func DeleteImage(name string) error { + imageFile := ImageFile(name) + if util.IsFile(imageFile) { + wwlog.Verbose("removing %s for container %s\n", imageFile, name) + errImg := os.Remove(imageFile) + wwlog.Verbose("removing %s for container %s\n", imageFile+".gz", name) + errGz := os.Remove(imageFile + ".gz") + if errImg != nil { + return errors.Errorf("Problems delete %s for container %s: %s\n", imageFile, name, errImg) + } + if errGz != nil { + return errors.Errorf("Problems delete %s for container %s: %s\n", imageFile+".gz", name, errGz) + } + return nil + } + return errors.Errorf("Image %s of container %s doesn't exist\n", imageFile, name) +}