This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
image_rm.go
111 lines (93 loc) · 2.68 KB
/
image_rm.go
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
// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"github.com/rkt/rkt/store/imagestore"
"github.com/spf13/cobra"
)
var (
cmdImageRm = &cobra.Command{
Use: "rm IMAGE...",
Short: "Remove one or more images with the given IDs or image names from the local store",
Long: `Unlike image gc, image rm allows users to remove specific images.`,
Run: runWrapper(runRmImage),
}
)
func init() {
cmdImage.AddCommand(cmdImageRm)
}
func rmImages(s *imagestore.Store, images []string) error {
imageMap := make(map[string]string)
for _, img := range images {
key, err := getStoreKeyFromAppOrHash(s, img)
if err != nil {
stderr.Error(err)
continue
}
aciinfo, err := s.GetACIInfoWithBlobKey(key)
if err != nil {
stderr.PrintE(fmt.Sprintf("error retrieving aci infos for image %q", key), err)
continue
}
imageMap[key] = aciinfo.Name
}
done := 0
errors := 0
staleErrors := 0
for key, name := range imageMap {
if err := s.RemoveACI(key); err != nil {
if serr, ok := err.(*imagestore.StoreRemovalError); ok {
staleErrors++
stderr.PrintE(fmt.Sprintf("some files cannot be removed for image %q (%q)", key, name), serr)
} else {
errors++
stderr.PrintE(fmt.Sprintf("error removing aci for image %q (%q)", key, name), err)
continue
}
}
stdout.Printf("successfully removed aci for image: %q", key)
done++
}
if done > 0 {
stderr.Printf("%d image(s) successfully removed", done)
}
// If anything didn't complete, return exit status of 1
if (errors + staleErrors) > 0 {
if staleErrors > 0 {
stderr.Printf("%d image(s) removed but left some stale files", staleErrors)
}
if errors > 0 {
stderr.Printf("%d image(s) cannot be removed", errors)
}
return fmt.Errorf("error(s) found while removing images")
}
return nil
}
func runRmImage(cmd *cobra.Command, args []string) (exit int) {
if len(args) < 1 {
stderr.Print("must provide at least one image ID")
return 254
}
s, err := imagestore.NewStore(storeDir())
if err != nil {
stderr.PrintE("cannot open store", err)
return 254
}
if err := rmImages(s, args); err != nil {
stderr.Error(err)
return 254
}
return 0
}