Debian Codegraph @

cmd/soci/commands/create.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 commands
18
19 import (
20 "errors"
21 "os"
22
23 "github.com/awslabs/soci-snapshotter/cmd/soci/commands/internal"
24 "github.com/awslabs/soci-snapshotter/config"
25 "github.com/awslabs/soci-snapshotter/soci"
26 "github.com/awslabs/soci-snapshotter/soci/store"
27 "github.com/containerd/containerd/cmd/ctr/commands"
28 "github.com/urfave/cli"
29 )
30
31 const (
32 buildToolIdentifier = "AWS SOCI CLI v0.1"
33 spanSizeFlag = "span-size"
34 minLayerSizeFlag = "min-layer-size"
35 )
36
37 // CreateCommand creates SOCI index for an image
38 // Output of this command is SOCI layers and SOCI index stored in a local directory
39 // SOCI layer is named as <image-layer-digest>.soci.layer
40 // SOCI index is named as <image-manifest-digest>.soci.index
41 var CreateCommand = cli.Command{
42 Name: "create",
43 Usage: "create SOCI index",
44 ArgsUsage: "[flags] <image_ref>",
45 Flags: append(
46 internal.PlatformFlags,
47 cli.Int64Flag{
48 Name: spanSizeFlag,
49 Usage: "Span size that soci index uses to segment layer data. Default is 4 MiB",
50 Value: 1 << 22,
51 },
52 cli.Int64Flag{
53 Name: minLayerSizeFlag,
54 Usage: "Minimum layer size to build zTOC for. Smaller layers won't have zTOC and not lazy pulled. Default is 10 MiB.",
55 Value: 10 << 20,
56 },
57 ),
58 Action: func(cliContext *cli.Context) error {
59 srcRef := cliContext.Args().Get(0)
60 if srcRef == "" {
61 return errors.New("source image needs to be specified")
62 }
63
64 client, ctx, cancel, err := commands.NewClient(cliContext)
65 if err != nil {
66 return err
67 }
68 defer cancel()
69
70 cs := client.ContentStore()
71 is := client.ImageService()
72 srcImg, err := is.Get(ctx, srcRef)
73 if err != nil {
74 return err
75 }
76 spanSize := cliContext.Int64(spanSizeFlag)
77 minLayerSize := cliContext.Int64(minLayerSizeFlag)
78 // Creating the snapshotter's root path first if it does not exist, since this ensures, that
79 // it has the limited permission set as drwx--x--x.
80 // The subsequent oci.New creates a root path dir with too broad permission set.
81 if _, err := os.Stat(config.SociSnapshotterRootPath); os.IsNotExist(err) {
82 if err = os.Mkdir(config.SociSnapshotterRootPath, 0711); err != nil {
83 return err
84 }
85 } else if err != nil {
86 return err
87 }
88
89 ctx, blobStore, err := store.NewContentStore(ctx, store.WithType(store.ContentStoreType(cliContext.GlobalString("content-store"))), store.WithNamespace(cliContext.GlobalString("namespace")))
90 if err != nil {
91 return err
92 }
93
94 ps, err := internal.GetPlatforms(ctx, cliContext, srcImg, cs)
95 if err != nil {
96 return err
97 }
98
99 artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath())
100 if err != nil {
101 return err
102 }
103
104 builderOpts := []soci.BuildOption{
105 soci.WithMinLayerSize(minLayerSize),
106 soci.WithSpanSize(spanSize),
107 soci.WithBuildToolIdentifier(buildToolIdentifier),
108 }
109
110 for _, plat := range ps {
111 builder, err := soci.NewIndexBuilder(cs, blobStore, artifactsDb, append(builderOpts, soci.WithPlatform(plat))...)
112
113 if err != nil {
114 return err
115 }
116
117 sociIndexWithMetadata, err := builder.Build(ctx, srcImg)
118 if err != nil {
119 return err
120 }
121
122 err = soci.WriteSociIndex(ctx, sociIndexWithMetadata, blobStore, builder.ArtifactsDb)
123 if err != nil {
124 return err
125 }
126 }
127
128 return nil
129 },
130 }