Debian Codegraph @

cmd/soci/commands/index/rm.go

raw symbols SCIP
1 /*
2 Copyright The Soci Snapshotter Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package index
18
19 import (
20 "context"
21 "fmt"
22
23 "github.com/awslabs/soci-snapshotter/soci"
24 "github.com/awslabs/soci-snapshotter/soci/store"
25 "github.com/containerd/containerd/cmd/ctr/commands"
26 "github.com/opencontainers/go-digest"
27 "github.com/urfave/cli"
28 )
29
30 var rmCommand = cli.Command{
31 Name: "remove",
32 Aliases: []string{"rm"},
33 Usage: "remove indices",
34 Description: "remove an index from local db, and from content store if supported",
35 Flags: []cli.Flag{
36 cli.StringFlag{
37 Name: "ref",
38 Usage: "only remove indices that are associated with a specific image ref",
39 },
40 },
41 Action: func(cliContext *cli.Context) error {
42 args := cliContext.Args()
43 ref := cliContext.String("ref")
44
45 if len(args) != 0 && ref != "" {
46 return fmt.Errorf("please provide either index digests or image ref, but not both")
47 }
48
49 ctx := context.Background()
50 ctx, contentStore, err := store.NewContentStore(ctx, store.WithType(store.ContentStoreType(cliContext.GlobalString("content-store"))), store.WithNamespace(cliContext.GlobalString("namespace")))
51 if err != nil {
52 return fmt.Errorf("cannot create local content store: %w", err)
53 }
54
55 db, err := soci.NewDB(soci.ArtifactsDbPath())
56 if err != nil {
57 return err
58 }
59 if ref == "" {
60 byteArgs := make([][]byte, len(args))
61 for i, arg := range args {
62 byteArgs[i] = []byte(arg)
63 }
64 err = removeArtifactsAndContent(ctx, db, contentStore, byteArgs)
65 if err != nil {
66 return err
67 }
68 } else {
69 client, ctx, cancel, err := commands.NewClient(cliContext)
70 if err != nil {
71 return err
72 }
73 defer cancel()
74
75 is := client.ImageService()
76 img, err := is.Get(ctx, ref)
77 if err != nil {
78 return err
79 }
80 entries, err := db.GetArtifactEntriesByImageDigest(img.Target.Digest.String())
81 if err != nil {
82 return err
83 }
84 err = removeArtifactsAndContent(ctx, db, contentStore, entries)
85 if err != nil {
86 return err
87 }
88 }
89 return nil
90 },
91 }
92
93 // removeArtifactsAndContent takes a list of content digests and removes them from the artifact db and content store
94 func removeArtifactsAndContent(ctx context.Context, db *soci.ArtifactsDb, contentStore store.Store, digests [][]byte) error {
95 for _, dgst := range digests {
96 err := db.RemoveArtifactEntryByIndexDigest(dgst)
97 if err != nil {
98 return err
99 }
100 dgst, err := digest.Parse(string(dgst))
101 if err != nil {
102 return err
103 }
104 err = contentStore.Delete(ctx, dgst)
105 if err != nil {
106 return err
107 }
108 }
109 return nil
110 }