2024-01-09 03:26:03 +08:00
|
|
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
|
2023-01-11 06:08:57 +08:00
|
|
|
|
2019-02-18 13:40:22 +08:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-29 12:08:40 +08:00
|
|
|
"fmt"
|
2019-02-18 13:40:22 +08:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2023-01-14 01:52:54 +08:00
|
|
|
"github.com/docker/docker/client"
|
2019-02-18 13:40:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
2021-03-29 12:08:40 +08: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-04 12:15:42 +08:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-02-18 13:40:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-10-25 00:50:43 +08:00
|
|
|
defer cli.Close()
|
2019-02-18 13:40:22 +08:00
|
|
|
|
2023-01-14 01:52:54 +08:00
|
|
|
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
2019-02-18 13:40:22 +08:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-14 01:52:54 +08:00
|
|
|
if platform == "" || platform == "any" || fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture) == platform {
|
|
|
|
return true, nil
|
2021-03-29 12:08:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:15:13 +08:00
|
|
|
// RemoveImage removes image from local store, the function is used to run different
|
2021-03-29 12:08:40 +08:00
|
|
|
// container image architectures
|
2021-05-02 23:15:13 +08:00
|
|
|
func RemoveImage(ctx context.Context, imageName string, force bool, pruneChildren bool) (bool, error) {
|
2021-03-29 12:08:40 +08:00
|
|
|
cli, err := GetDockerClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-01-14 01:52:54 +08:00
|
|
|
defer cli.Close()
|
2021-03-29 12:08:40 +08:00
|
|
|
|
2023-01-14 01:52:54 +08:00
|
|
|
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
2021-03-29 12:08:40 +08:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-14 01:52:54 +08:00
|
|
|
if _, err = cli.ImageRemove(ctx, inspectImage.ID, types.ImageRemoveOptions{
|
|
|
|
Force: force,
|
|
|
|
PruneChildren: pruneChildren,
|
|
|
|
}); err != nil {
|
|
|
|
return false, err
|
2021-03-29 12:08:40 +08:00
|
|
|
}
|
|
|
|
|
2023-01-14 01:52:54 +08:00
|
|
|
return true, nil
|
2019-02-18 13:40:22 +08:00
|
|
|
}
|