From b72b1c87354ac91fb754c82a821688e40ac07559 Mon Sep 17 00:00:00 2001 From: Dorin Geman Date: Wed, 21 May 2025 15:16:37 +0300 Subject: [PATCH] client: Add ResetStore Signed-off-by: Dorin Geman --- distribution/client.go | 11 ++++++++++- internal/store/store.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/distribution/client.go b/distribution/client.go index fe5ef97..a571f06 100644 --- a/distribution/client.go +++ b/distribution/client.go @@ -120,7 +120,7 @@ func (c *Client) PullModel(ctx context.Context, reference string, progressWriter return fmt.Errorf("reading model from registry: %w", err) } - //Check for supported type + // Check for supported type if err := checkCompat(remoteModel); err != nil { return err } @@ -308,6 +308,15 @@ func (c *Client) PushModel(ctx context.Context, tag string, progressWriter io.Wr return nil } +func (c *Client) ResetStore() error { + c.log.Infoln("Resetting store") + if err := c.store.Reset(); err != nil { + c.log.Errorln("Failed to reset store:", err) + return fmt.Errorf("resetting store: %w", err) + } + return nil +} + func checkCompat(image types.ModelArtifact) error { manifest, err := image.Manifest() if err != nil { diff --git a/internal/store/store.go b/internal/store/store.go index 4613250..b787e33 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "path/filepath" v1 "github.com/google/go-containerregistry/pkg/v1" ) @@ -42,6 +43,25 @@ func New(opts Options) (*LocalStore, error) { return store, nil } +// Reset clears all contents of the store directory and reinitializes the store. +// It removes all files and subdirectories within the store's root path, but preserves the root directory itself. +// This allows the method to work correctly when the store directory is a mounted volume (e.g., in Docker CE). +func (s *LocalStore) Reset() error { + entries, err := os.ReadDir(s.rootPath) + if err != nil { + return fmt.Errorf("reading store directory: %w", err) + } + + for _, entry := range entries { + entryPath := filepath.Join(s.rootPath, entry.Name()) + if err := os.RemoveAll(entryPath); err != nil { + return fmt.Errorf("removing %s: %w", entryPath, err) + } + } + + return s.initialize() +} + // initialize creates the store directory structure if it doesn't exist func (s *LocalStore) initialize() error { // Check if layout.json exists, create if not