From f8f565c2b6ade8d813e8ca937e4816515fc242f1 Mon Sep 17 00:00:00 2001 From: r-sc Date: Sat, 24 Jun 2023 09:38:52 +0100 Subject: [PATCH] fix: allow cluster bucket export to export single bucket (#4614) --- cmd/admin-cluster-bucket-export.go | 15 ++++++++------- cmd/cat-main.go | 1 + cmd/retention-clear.go | 2 +- cmd/retention-common.go | 13 ++++++------- cmd/retention-info.go | 2 +- cmd/retention-set.go | 2 +- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/cmd/admin-cluster-bucket-export.go b/cmd/admin-cluster-bucket-export.go index d46b0cde..0888e730 100644 --- a/cmd/admin-cluster-bucket-export.go +++ b/cmd/admin-cluster-bucket-export.go @@ -23,6 +23,7 @@ import ( "io" "os" "path/filepath" + "strings" "time" "github.com/fatih/color" @@ -82,29 +83,31 @@ func mainClusterBucketExport(ctx *cli.Context) error { aliasedURL = filepath.ToSlash(aliasedURL) aliasedURL = filepath.Clean(aliasedURL) _, bucket := url2Alias(aliasedURL) - r, e := client.ExportBucketMetadata(context.Background(), bucket) fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to export bucket metadata.") if bucket == "" { bucket = "bucket" } - // Create bucket metadata zip file - tmpFile, e := os.CreateTemp("", fmt.Sprintf("%s-%s-metadata-", aliasedURL, bucket)) + tmpFile, e := os.CreateTemp("", fmt.Sprintf("%s-%s-metadata-", strings.ReplaceAll(aliasedURL, "/", "-"), bucket)) fatalIf(probe.NewError(e), "Unable to download file data.") ext := "zip" // Copy zip content to target download file _, e = io.Copy(tmpFile, r) fatalIf(probe.NewError(e), "Unable to download bucket metadata.") - // Close everything r.Close() tmpFile.Close() - // We use 4 bytes of the 32 bytes to identify the file. downloadPath := fmt.Sprintf("%s-%s-metadata.%s", aliasedURL, bucket, ext) + // Create necessary directories. + dir := filepath.Dir(downloadPath) + if e := os.MkdirAll(dir, 0o755); e != nil { + fatalIf(probe.NewError(e).Trace(dir), "Unable to create download directory") + } + fi, e := os.Stat(downloadPath) if e == nil && !fi.IsDir() { e = moveFile(downloadPath, downloadPath+"."+time.Now().Format(dateTimeFormatFilename)) @@ -114,14 +117,12 @@ func mainClusterBucketExport(ctx *cli.Context) error { fatal(probe.NewError(e), "Unable to download file data") } } - fatalIf(probe.NewError(moveFile(tmpFile.Name(), downloadPath)), "Unable to rename downloaded data, file exists at %s", tmpFile.Name()) if !globalJSON { console.Infof("Bucket metadata successfully downloaded as %s\n", downloadPath) return nil } - v := struct { File string `json:"file"` Key string `json:"key,omitempty"` diff --git a/cmd/cat-main.go b/cmd/cat-main.go index 244b7370..8ef8bbbd 100644 --- a/cmd/cat-main.go +++ b/cmd/cat-main.go @@ -172,6 +172,7 @@ type catOpts struct { func parseCatSyntax(ctx *cli.Context) catOpts { // Validate command-line arguments. checkCatSyntax(ctx) + var o catOpts o.args = ctx.Args() diff --git a/cmd/retention-clear.go b/cmd/retention-clear.go index 096b6c15..16e68000 100644 --- a/cmd/retention-clear.go +++ b/cmd/retention-clear.go @@ -132,7 +132,7 @@ func mainRetentionClear(cliCtx *cli.Context) error { target, versionID, rewind, withVersions, recursive, bucketMode := parseClearRetentionArgs(cliCtx) - fatalIfBucketLockNotEnabled(ctx, target) + fatalIfBucketLockNotSupported(ctx, target) if bucketMode { return clearBucketLock(target) diff --git a/cmd/retention-common.go b/cmd/retention-common.go index 1aa620b4..faadad87 100644 --- a/cmd/retention-common.go +++ b/cmd/retention-common.go @@ -95,8 +95,8 @@ func (m retentionBucketMessage) String() string { return console.Colorize("RetentionSuccess", "Object lock configuration cleared successfully.") } // info/set command - if m.Mode == "" { - return console.Colorize("RetentionNotFound", "Object locking is not configured.") + if !m.Mode.IsValid() { + return console.Colorize("RetentionNotFound", "Object locking is not enabled.") } return console.Colorize("RetentionSuccess", fmt.Sprintf("Object locking '%s' is configured for %s.", console.Colorize("Mode", m.Mode), console.Colorize("Validity", m.Validity))) @@ -170,11 +170,10 @@ func parseRetentionValidity(validityStr string) (uint64, minio.ValidityUnit, *pr return validity, unit, nil } -func fatalIfBucketLockNotEnabled(ctx context.Context, aliasedURL string) { - enabled, err := getBucketLockStatus(ctx, aliasedURL) - fatalIf(err.Trace(), "Unable to get bucket lock configuration from `%s`", aliasedURL) - if enabled != "Enabled" { - fatalIf(errDummy().Trace(), "Remote bucket does not support locking `%s`", aliasedURL) +func fatalIfBucketLockNotSupported(ctx context.Context, aliasedURL string) { + _, err := getBucketLockStatus(ctx, aliasedURL) + if err != nil { + fatalIf(errDummy().Trace(), "Remote bucket `%s` does not support locking", aliasedURL) } } diff --git a/cmd/retention-info.go b/cmd/retention-info.go index f6c999d4..5757a0c8 100644 --- a/cmd/retention-info.go +++ b/cmd/retention-info.go @@ -381,7 +381,7 @@ func mainRetentionInfo(cliCtx *cli.Context) error { target, versionID, recursive, rewind, withVersions, bucketMode := parseInfoRetentionArgs(cliCtx) - fatalIfBucketLockNotEnabled(ctx, target) + fatalIfBucketLockNotSupported(ctx, target) if bucketMode { return showBucketLock(target) diff --git a/cmd/retention-set.go b/cmd/retention-set.go index 3398da41..d73b8310 100644 --- a/cmd/retention-set.go +++ b/cmd/retention-set.go @@ -148,7 +148,7 @@ func mainRetentionSet(cliCtx *cli.Context) error { target, versionID, recursive, rewind, withVersions, mode, validity, unit, bypass, bucketMode := parseSetRetentionArgs(cliCtx) - fatalIfBucketLockNotEnabled(ctx, target) + fatalIfBucketLockNotSupported(ctx, target) if bucketMode { return setBucketLock(target, mode, validity, unit)