summaryrefslogtreecommitdiff
path: root/pkg/domain/infra/abi/images.go
blob: e3b3e310b11820404f4671a6d3774c9f82b4e535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// +build ABISupport

package abi

import (
	"context"
	"fmt"

	libpodImage "github.com/containers/libpod/libpod/image"
	"github.com/containers/libpod/pkg/domain/entities"
	"github.com/containers/storage"
	"github.com/pkg/errors"
)

func (ir *ImageEngine) Exists(_ context.Context, nameOrId string) (*entities.BoolReport, error) {
	if _, err := ir.Libpod.ImageRuntime().NewFromLocal(nameOrId); err != nil {
		return &entities.BoolReport{}, nil
	}
	return &entities.BoolReport{Value: true}, nil
}

func (ir *ImageEngine) Delete(ctx context.Context, nameOrId []string, opts entities.ImageDeleteOptions) (*entities.ImageDeleteReport, error) {
	report := entities.ImageDeleteReport{}

	if opts.All {
		var previousTargets []*libpodImage.Image
	repeatRun:
		targets, err := ir.Libpod.ImageRuntime().GetRWImages()
		if err != nil {
			return &report, errors.Wrapf(err, "unable to query local images")
		}

		if len(targets) > 0 && len(targets) == len(previousTargets) {
			return &report, errors.New("unable to delete all images; re-run the rmi command again.")
		}
		previousTargets = targets

		for _, img := range targets {
			isParent, err := img.IsParent(ctx)
			if err != nil {
				return &report, err
			}
			if isParent {
				continue
			}
			err = ir.deleteImage(ctx, img, opts, report)
			report.Errors = append(report.Errors, err)
		}
		if len(previousTargets) != 1 {
			goto repeatRun
		}
		return &report, nil
	}

	for _, id := range nameOrId {
		image, err := ir.Libpod.ImageRuntime().NewFromLocal(id)
		if err != nil {
			return nil, err
		}

		err = ir.deleteImage(ctx, image, opts, report)
		if err != nil {
			return &report, err
		}
	}
	return &report, nil
}

func (ir *ImageEngine) deleteImage(ctx context.Context, img *libpodImage.Image, opts entities.ImageDeleteOptions, report entities.ImageDeleteReport) error {
	results, err := ir.Libpod.RemoveImage(ctx, img, opts.Force)
	switch errors.Cause(err) {
	case nil:
		break
	case storage.ErrImageUsedByContainer:
		report.ImageInUse = errors.New(
			fmt.Sprintf("A container associated with containers/storage, i.e. via Buildah, CRI-O, etc., may be associated with this image: %-12.12s\n", img.ID()))
		return nil
	case libpodImage.ErrNoSuchImage:
		report.ImageNotFound = err
		return nil
	default:
		return err
	}

	report.Deleted = append(report.Deleted, results.Deleted)
	report.Untagged = append(report.Untagged, results.Untagged...)
	return nil
}

func (ir *ImageEngine) Prune(ctx context.Context, opts entities.ImagePruneOptions) (*entities.ImagePruneReport, error) {
	results, err := ir.Libpod.ImageRuntime().PruneImages(ctx, opts.All, []string{})
	if err != nil {
		return nil, err
	}

	report := entities.ImagePruneReport{}
	copy(report.Report.Id, results)
	return &report, nil
}

func (ir *ImageEngine) History(ctx context.Context, nameOrId string, opts entities.ImageHistoryOptions) (*entities.ImageHistoryReport, error) {
	image, err := ir.Libpod.ImageRuntime().NewFromLocal(nameOrId)
	if err != nil {
		return nil, err
	}
	results, err := image.History(ctx)
	if err != nil {
		return nil, err
	}

	history := entities.ImageHistoryReport{
		Layers: make([]entities.ImageHistoryLayer, len(results)),
	}

	for i, layer := range results {
		history.Layers[i] = ToDomainHistoryLayer(layer)
	}
	return &history, nil
}

func ToDomainHistoryLayer(layer *libpodImage.History) entities.ImageHistoryLayer {
	l := entities.ImageHistoryLayer{}
	l.ID = layer.ID
	l.Created = layer.Created.Unix()
	l.CreatedBy = layer.CreatedBy
	copy(l.Tags, layer.Tags)
	l.Size = layer.Size
	l.Comment = layer.Comment
	return l
}

// func (r *imageRuntime) Delete(ctx context.Context, nameOrId string, opts entities.ImageDeleteOptions) (*entities.ImageDeleteReport, error) {
// 	image, err := r.libpod.ImageEngine().NewFromLocal(nameOrId)
// 	if err != nil {
// 		return nil, err
// 	}
//
// 	results, err := r.libpod.RemoveImage(ctx, image, opts.Force)
// 	if err != nil {
// 		return nil, err
// 	}
//
// 	report := entities.ImageDeleteReport{}
// 	if err := utils.DeepCopy(&report, results); err != nil {
// 		return nil, err
// 	}
// 	return &report, nil
// }
//
// func (r *imageRuntime) Prune(ctx context.Context, opts entities.ImagePruneOptions) (*entities.ImagePruneReport, error) {
// 	// TODO: map FilterOptions
// 	id, err := r.libpod.ImageEngine().PruneImages(ctx, opts.All, []string{})
// 	if err != nil {
// 		return nil, err
// 	}
//
// 	// TODO: Determine Size
// 	report := entities.ImagePruneReport{}
// 	copy(report.Report.Id, id)
// 	return &report, nil
// }