remove also the container image on delete

This commit is contained in:
Christian Goll
2022-09-14 11:45:40 +02:00
parent 6cc58fcfff
commit c75f4e468c
2 changed files with 30 additions and 2 deletions

View File

@@ -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

View File

@@ -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)
}