2019-02-17 23:40:22 -06:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-28 23:08:40 -05:00
|
|
|
"fmt"
|
2019-02-17 23:40:22 -06:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
2021-03-28 23:08:40 -05:00
|
|
|
// requested name, tag and architecture exists in the local docker image store
|
|
|
|
func ImageExistsLocally(ctx context.Context, imageName string, platform string) (bool, error) {
|
2020-05-03 23:15:42 -05:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-02-17 23:40:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-10-24 11:50:43 -05:00
|
|
|
defer cli.Close()
|
2019-02-17 23:40:22 -06:00
|
|
|
|
|
|
|
filters := filters.NewArgs()
|
|
|
|
filters.Add("reference", imageName)
|
|
|
|
|
|
|
|
imageListOptions := types.ImageListOptions{
|
|
|
|
Filters: filters,
|
|
|
|
}
|
|
|
|
|
|
|
|
images, err := cli.ImageList(ctx, imageListOptions)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-03-28 23:08:40 -05:00
|
|
|
if len(images) > 0 {
|
2021-05-02 10:15:13 -05:00
|
|
|
if platform == "any" || platform == "" {
|
2021-03-28 23:08:40 -05:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
for _, v := range images {
|
|
|
|
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, v.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture) == platform {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-05-02 10:15:13 -05:00
|
|
|
// RemoveImage removes image from local store, the function is used to run different
|
2021-03-28 23:08:40 -05:00
|
|
|
// container image architectures
|
2021-05-02 10:15:13 -05:00
|
|
|
func RemoveImage(ctx context.Context, imageName string, force bool, pruneChildren bool) (bool, error) {
|
2021-03-28 23:08:40 -05:00
|
|
|
if exists, err := ImageExistsLocally(ctx, imageName, "any"); !exists {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := GetDockerClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filters := filters.NewArgs()
|
|
|
|
filters.Add("reference", imageName)
|
|
|
|
|
|
|
|
imageListOptions := types.ImageListOptions{
|
|
|
|
Filters: filters,
|
|
|
|
}
|
|
|
|
|
|
|
|
images, err := cli.ImageList(ctx, imageListOptions)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(images) > 0 {
|
|
|
|
for _, v := range images {
|
|
|
|
if _, err = cli.ImageRemove(ctx, v.ID, types.ImageRemoveOptions{
|
2021-05-02 10:15:13 -05:00
|
|
|
Force: force,
|
|
|
|
PruneChildren: pruneChildren,
|
2021-03-28 23:08:40 -05:00
|
|
|
}); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
2019-02-17 23:40:22 -06:00
|
|
|
}
|