Skip to content

Commit a452c7e

Browse files
committed
client: Add ResetStore (docker#78)
Signed-off-by: Dorin Geman <dorin.geman@docker.com>
1 parent ec2c025 commit a452c7e

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

pkg/distribution/distribution/client.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (c *Client) PullModel(ctx context.Context, reference string, progressWriter
120120
return fmt.Errorf("reading model from registry: %w", err)
121121
}
122122

123-
//Check for supported type
123+
// Check for supported type
124124
if err := checkCompat(remoteModel); err != nil {
125125
return err
126126
}
@@ -308,6 +308,15 @@ func (c *Client) PushModel(ctx context.Context, tag string, progressWriter io.Wr
308308
return nil
309309
}
310310

311+
func (c *Client) ResetStore() error {
312+
c.log.Infoln("Resetting store")
313+
if err := c.store.Reset(); err != nil {
314+
c.log.Errorln("Failed to reset store:", err)
315+
return fmt.Errorf("resetting store: %w", err)
316+
}
317+
return nil
318+
}
319+
311320
func checkCompat(image types.ModelArtifact) error {
312321
manifest, err := image.Manifest()
313322
if err != nil {

pkg/distribution/internal/store/store.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"path/filepath"
78

89
v1 "github.com/google/go-containerregistry/pkg/v1"
910
)
@@ -42,6 +43,25 @@ func New(opts Options) (*LocalStore, error) {
4243
return store, nil
4344
}
4445

46+
// Reset clears all contents of the store directory and reinitializes the store.
47+
// It removes all files and subdirectories within the store's root path, but preserves the root directory itself.
48+
// This allows the method to work correctly when the store directory is a mounted volume (e.g., in Docker CE).
49+
func (s *LocalStore) Reset() error {
50+
entries, err := os.ReadDir(s.rootPath)
51+
if err != nil {
52+
return fmt.Errorf("reading store directory: %w", err)
53+
}
54+
55+
for _, entry := range entries {
56+
entryPath := filepath.Join(s.rootPath, entry.Name())
57+
if err := os.RemoveAll(entryPath); err != nil {
58+
return fmt.Errorf("removing %s: %w", entryPath, err)
59+
}
60+
}
61+
62+
return s.initialize()
63+
}
64+
4565
// initialize creates the store directory structure if it doesn't exist
4666
func (s *LocalStore) initialize() error {
4767
// Check if layout.json exists, create if not

0 commit comments

Comments
 (0)