Skip to content

Commit ca55b7f

Browse files
committed
Add SOCI CLI root flag
This change adds --root flag for the soci command and refactors common code with the snapshotter grpc for similar logic. Signed-off-by: Austin Vazquez <[email protected]>
1 parent 3176283 commit ca55b7f

File tree

18 files changed

+174
-57
lines changed

18 files changed

+174
-57
lines changed

cmd/soci-snapshotter-grpc/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
"github.com/awslabs/soci-snapshotter/service"
5656
"github.com/awslabs/soci-snapshotter/service/keychain/cri/v1"
5757
crialpha "github.com/awslabs/soci-snapshotter/service/keychain/cri/v1alpha"
58+
"github.com/awslabs/soci-snapshotter/soci"
5859

5960
"github.com/awslabs/soci-snapshotter/service/keychain/dockerconfig"
6061
"github.com/awslabs/soci-snapshotter/service/keychain/kubeconfig"
@@ -84,7 +85,6 @@ const (
8485
defaultAddress = "/run/soci-snapshotter-grpc/soci-snapshotter-grpc.sock"
8586
defaultConfigPath = "/etc/soci-snapshotter-grpc/config.toml"
8687
defaultLogLevel = logrus.InfoLevel
87-
defaultRootDir = "/var/lib/soci-snapshotter-grpc"
8888
)
8989

9090
// logLevel of Debug or Trace may emit sensitive information
@@ -93,7 +93,7 @@ var (
9393
address = flag.String("address", defaultAddress, "address for the snapshotter's GRPC server")
9494
configPath = flag.String("config", defaultConfigPath, "path to the configuration file")
9595
logLevel = flag.String("log-level", defaultLogLevel.String(), "set the logging level [trace, debug, info, warn, error, fatal, panic]")
96-
rootDir = flag.String("root", defaultRootDir, "path to the root directory for this snapshotter")
96+
rootDir = flag.String("root", config.DefaultSociSnapshotterRootPath, "path to the root directory for this snapshotter")
9797
printVersion = flag.Bool("version", false, "print the version")
9898
)
9999

@@ -361,7 +361,7 @@ func listen(ctx context.Context, address string) (net.Listener, error) {
361361

362362
func listenUnix(addr string) (net.Listener, error) {
363363
// Prepare the directory for the socket
364-
if err := os.MkdirAll(filepath.Dir(addr), 0700); err != nil {
364+
if err := soci.EnsureSnapshotterRootPath(filepath.Dir(addr)); err != nil {
365365
return nil, fmt.Errorf("failed to create directory %q: %w", filepath.Dir(addr), err)
366366
}
367367

cmd/soci/commands/convert.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ package commands
1919
import (
2020
"errors"
2121
"fmt"
22-
"os"
2322
"strings"
2423

2524
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/internal"
26-
"github.com/awslabs/soci-snapshotter/config"
2725
"github.com/awslabs/soci-snapshotter/soci"
2826
"github.com/awslabs/soci-snapshotter/soci/store"
2927
"github.com/containerd/containerd/errdefs"
@@ -118,23 +116,13 @@ var ConvertCommand = cli.Command{
118116
}
119117
spanSize := cliContext.Int64(spanSizeFlag)
120118
minLayerSize := cliContext.Int64(minLayerSizeFlag)
121-
// Creating the snapshotter's root path first if it does not exist, since this ensures, that
122-
// it has the limited permission set as drwx--x--x.
123-
// The subsequent oci.New creates a root path dir with too broad permission set.
124-
if _, err := os.Stat(config.SociSnapshotterRootPath); os.IsNotExist(err) {
125-
if err = os.Mkdir(config.SociSnapshotterRootPath, 0711); err != nil {
126-
return err
127-
}
128-
} else if err != nil {
129-
return err
130-
}
131119

132120
blobStore, err := store.NewContentStore(internal.ContentStoreOptions(cliContext)...)
133121
if err != nil {
134122
return err
135123
}
136124

137-
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath())
125+
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
138126
if err != nil {
139127
return err
140128
}

cmd/soci/commands/create.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ package commands
1919
import (
2020
"errors"
2121
"fmt"
22-
"os"
2322

2423
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/internal"
25-
"github.com/awslabs/soci-snapshotter/config"
2624
"github.com/awslabs/soci-snapshotter/soci"
2725
"github.com/awslabs/soci-snapshotter/soci/store"
2826
"github.com/urfave/cli"
@@ -92,16 +90,6 @@ var CreateCommand = cli.Command{
9290
}
9391
spanSize := cliContext.Int64(spanSizeFlag)
9492
minLayerSize := cliContext.Int64(minLayerSizeFlag)
95-
// Creating the snapshotter's root path first if it does not exist, since this ensures, that
96-
// it has the limited permission set as drwx--x--x.
97-
// The subsequent oci.New creates a root path dir with too broad permission set.
98-
if _, err := os.Stat(config.SociSnapshotterRootPath); os.IsNotExist(err) {
99-
if err = os.Mkdir(config.SociSnapshotterRootPath, 0711); err != nil {
100-
return err
101-
}
102-
} else if err != nil {
103-
return err
104-
}
10593

10694
blobStore, err := store.NewContentStore(internal.ContentStoreOptions(cliContext)...)
10795
if err != nil {
@@ -113,7 +101,7 @@ var CreateCommand = cli.Command{
113101
return err
114102
}
115103

116-
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath())
104+
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
117105
if err != nil {
118106
return err
119107
}
@@ -127,7 +115,6 @@ var CreateCommand = cli.Command{
127115
}
128116

129117
builder, err := soci.NewIndexBuilder(cs, blobStore, builderOpts...)
130-
131118
if err != nil {
132119
return err
133120
}

cmd/soci/commands/index/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var infoCommand = cli.Command{
4444
if err != nil {
4545
return err
4646
}
47-
db, err := soci.NewDB(soci.ArtifactsDbPath())
47+
db, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
4848
if err != nil {
4949
return err
5050
}

cmd/soci/commands/index/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ var listCommand = cli.Command{
132132
f = anyMatch(filters)
133133
}
134134

135-
db, err := soci.NewDB(soci.ArtifactsDbPath())
135+
db, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
136136
if err != nil {
137137
return err
138138
}

cmd/soci/commands/index/rm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var rmCommand = cli.Command{
5151
return fmt.Errorf("cannot create local content store: %w", err)
5252
}
5353

54-
db, err := soci.NewDB(soci.ArtifactsDbPath())
54+
db, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
5555
if err != nil {
5656
return err
5757
}

cmd/soci/commands/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ if they are available in the snapshotter's local content store.
9999
return err
100100
}
101101

102-
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath())
102+
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
103103
if err != nil {
104104
return err
105105
}

cmd/soci/commands/rebuild_db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var RebuildDBCommand = cli.Command{
4141
}
4242
defer cancel()
4343
containerdContentStore := client.ContentStore()
44-
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath())
44+
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
4545
if err != nil {
4646
return err
4747
}

cmd/soci/commands/ztoc/get-file.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ var getFileCommand = cli.Command{
6565
return err
6666
}
6767

68-
layerReader, err := getLayer(ctx, ztocDigest, client.ContentStore())
68+
artifactsDB, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
69+
if err != nil {
70+
return err
71+
}
72+
73+
layerReader, err := getLayer(ctx, artifactsDB, ztocDigest, client.ContentStore())
6974
if err != nil {
7075
return err
7176
}
@@ -101,11 +106,7 @@ func getZtoc(ctx context.Context, cliContext *cli.Context, d digest.Digest) (*zt
101106
return ztoc.Unmarshal(reader)
102107
}
103108

104-
func getLayer(ctx context.Context, ztocDigest digest.Digest, cs content.Store) (content.ReaderAt, error) {
105-
metadata, err := soci.NewDB(soci.ArtifactsDbPath())
106-
if err != nil {
107-
return nil, err
108-
}
109+
func getLayer(ctx context.Context, metadata *soci.ArtifactsDb, ztocDigest digest.Digest, cs content.Store) (content.ReaderAt, error) {
109110
artifact, err := metadata.GetArtifactEntry(ztocDigest.String())
110111
if err != nil {
111112
return nil, err

cmd/soci/commands/ztoc/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var infoCommand = cli.Command{
5959
if err != nil {
6060
return err
6161
}
62-
db, err := soci.NewDB(soci.ArtifactsDbPath())
62+
db, err := soci.NewDB(soci.ArtifactsDbPath(cliContext.GlobalString("root")))
6363
if err != nil {
6464
return err
6565
}

0 commit comments

Comments
 (0)