Skip to content

Commit cbf5cc9

Browse files
committed
Skip snapshotter root path creation for standalone convert mode
Signed-off-by: Praful Gupta <prafulgupta6@gmail.com>
1 parent dc2ed58 commit cbf5cc9

10 files changed

Lines changed: 56 additions & 6 deletions

File tree

cmd/soci/commands/convert.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ var ConvertCommand = &cli.Command{
9696
},
9797
},
9898
}),
99+
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
100+
// Standalone mode uses temp directories and doesn't need the snapshotter root path.
101+
// Skipping avoids failures in minimal environments (e.g., scratch images) where the
102+
// default root path cannot be created.
103+
if cmd.Bool(standaloneFlag) {
104+
return ctx, nil
105+
}
106+
return ctx, soci.EnsureSnapshotterRootPath(cmd.Root().String(global.RootFlag))
107+
},
99108
Action: func(ctx context.Context, cmd *cli.Command) error {
100109
src := cmd.Args().Get(0)
101110
if src == "" {

cmd/soci/commands/create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ var CreateCommand = &cli.Command{
7474
Usage: "create SOCI index",
7575
ArgsUsage: "[flags] <image_ref>",
7676
Flags: slices.Concat(internal.PlatformFlags, createZtocFlags, internal.PrefetchFlags),
77+
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
78+
return ctx, soci.EnsureSnapshotterRootPath(cmd.Root().String(global.RootFlag))
79+
},
7780
Action: func(ctx context.Context, cmd *cli.Command) error {
7881
srcRef := cmd.Args().Get(0)
7982
if srcRef == "" {

cmd/soci/commands/index/index.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@
1717
package index
1818

1919
import (
20+
"context"
21+
22+
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/global"
23+
"github.com/awslabs/soci-snapshotter/soci"
2024
"github.com/urfave/cli/v3"
2125
)
2226

2327
var Command = &cli.Command{
2428
Name: "index",
2529
Usage: "manage indices",
30+
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
31+
return ctx, soci.EnsureSnapshotterRootPath(cmd.Root().String(global.RootFlag))
32+
},
2633
Commands: []*cli.Command{
2734
listCommand,
2835
infoCommand,

cmd/soci/commands/prefetch/prefetch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
package prefetch
1818

1919
import (
20+
"context"
21+
22+
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/global"
23+
"github.com/awslabs/soci-snapshotter/soci"
2024
"github.com/urfave/cli/v3"
2125
)
2226

@@ -25,6 +29,9 @@ const JSONFlag = "json"
2529
var Command = &cli.Command{
2630
Name: "prefetch",
2731
Usage: "manage prefetch artifacts",
32+
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
33+
return ctx, soci.EnsureSnapshotterRootPath(cmd.Root().String(global.RootFlag))
34+
},
2835
Commands: []*cli.Command{
2936
listCommand,
3037
infoCommand,

cmd/soci/commands/push.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ if they are available in the snapshotter's local content store.
8383
},
8484
},
8585
),
86+
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
87+
return ctx, soci.EnsureSnapshotterRootPath(cmd.Root().String(global.RootFlag))
88+
},
8689
Action: func(ctx context.Context, cmd *cli.Command) error {
8790
ref := cmd.Args().First()
8891
if ref == "" {

cmd/soci/commands/rebuild_db.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ var RebuildDBCommand = &cli.Command{
3636
Use after pulling an image to discover SOCI indices/ztocs or after "index rm"
3737
when using the containerd content store to clear the database of removed zTOCs.
3838
`,
39+
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
40+
return ctx, soci.EnsureSnapshotterRootPath(cmd.Root().String(global.RootFlag))
41+
},
3942
Action: func(ctx context.Context, cmd *cli.Command) error {
4043
client, ctx, cancel, err := internal.NewClient(ctx, cmd)
4144
if err != nil {

cmd/soci/commands/ztoc/ztoc.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616

1717
package ztoc
1818

19-
import "github.com/urfave/cli/v3"
19+
import (
20+
"context"
21+
22+
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/global"
23+
"github.com/awslabs/soci-snapshotter/soci"
24+
"github.com/urfave/cli/v3"
25+
)
2026

2127
var Command = &cli.Command{
2228
Name: "ztoc",
2329
Usage: "manage ztocs",
30+
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
31+
return ctx, soci.EnsureSnapshotterRootPath(cmd.Root().String(global.RootFlag))
32+
},
2433
Commands: []*cli.Command{
2534
infoCommand,
2635
getFileCommand,

cmd/soci/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/index"
4343
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/prefetch"
4444
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/ztoc"
45-
"github.com/awslabs/soci-snapshotter/soci"
4645
"github.com/awslabs/soci-snapshotter/version"
4746
"github.com/urfave/cli/v3"
4847
)
@@ -61,9 +60,6 @@ func main() {
6160
commands.PushCommand,
6261
commands.RebuildDBCommand,
6362
},
64-
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
65-
return ctx, soci.EnsureSnapshotterRootPath(cmd.String(global.RootFlag))
66-
},
6763
}
6864

6965
ctx, cancel := context.WithCancel(context.Background())

soci/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func EnsureSnapshotterRootPath(root string) error {
3333
// it has the limited permission set as drwx--x--x.
3434
// The subsequent oci.New creates a root path dir with too broad permission set.
3535
if _, err := os.Stat(root); os.IsNotExist(err) {
36-
if err = os.Mkdir(root, 0700); err != nil {
36+
if err = os.MkdirAll(root, 0700); err != nil {
3737
return err
3838
}
3939
} else if err != nil {

soci/fs_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ func TestEnsureSnapshotterRootPath(t *testing.T) {
3838
}
3939
})
4040

41+
t.Run("root path and parent directories do not exist", func(t *testing.T) {
42+
testRoot := t.TempDir()
43+
44+
root := testRoot + "/var/lib/soci-snapshotter-grpc"
45+
if err := EnsureSnapshotterRootPath(root); err != nil {
46+
t.Fatalf("expected no error, got %q", err)
47+
}
48+
49+
if _, err := os.Stat(root); err != nil {
50+
t.Fatalf("expected %q to exist, got %q", root, err)
51+
}
52+
})
53+
4154
t.Run("root path already exists", func(t *testing.T) {
4255
testRoot := t.TempDir()
4356
root := testRoot + "/var/lib/soci-snapshotter-grpc"

0 commit comments

Comments
 (0)