| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
} |