Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions cmd/clickhouse/check_and_finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package clickhouse

import (
"fmt"
"os"
"time"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/clients/clickhouse"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
Expand Down Expand Up @@ -34,15 +34,7 @@ func checkAndFinalizeCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
This command is useful when a restore was started without --wait flag or was interrupted.
It will check the restore status and if complete, execute post-restore tasks and scale up resources.`,
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runCheckAndFinalize(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runCheckAndFinalize, cmdutils.MinioIsRequired)
},
}

Expand Down
12 changes: 2 additions & 10 deletions cmd/clickhouse/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package clickhouse

import (
"fmt"
"os"
"sort"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/output"
Expand All @@ -18,15 +18,7 @@ func listCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
Short: "List available Clickhouse backups",
Long: `List all Clickhouse backups from the ClickHouse Backup API.`,
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runList(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runList, cmdutils.MinioIsRequired)
},
}
}
Expand Down
12 changes: 2 additions & 10 deletions cmd/clickhouse/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package clickhouse

import (
"fmt"
"os"
"sort"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward"
Expand All @@ -27,15 +27,7 @@ func restoreCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
Short: "Restore ClickHouse from a backup archive",
Long: `Restore ClickHouse data from a backup archive via ClickHouse Backup API. Waits for completion by default; use --background to run asynchronously.`,
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runRestore(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runRestore, cmdutils.MinioIsRequired)
},
}

Expand Down
36 changes: 36 additions & 0 deletions cmd/cmdutils/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmdutils

import (
"errors"
"fmt"
"io"
"os"

"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
)

const (
MinioIsRequired bool = true
MinioIsNotRequired bool = false
)

func Run(globalFlags *config.CLIGlobalFlags, runFunc func(ctx *app.Context) error, minioRequired bool) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
exitWithError(err, os.Stderr)
}
if minioRequired && !appCtx.Config.Minio.Enabled {
exitWithError(errors.New("commands that interact with Minio require SUSE Observability to be deployed with .Values.global.backup.enabled=true"), os.Stderr)
}
if err := runFunc(appCtx); err != nil {
exitWithError(err, os.Stderr)
}
}

// ExitWithError prints an error message to the writer and exits with status code 1.
// This is a helper function to avoid repeating error handling code in commands.
func exitWithError(err error, w io.Writer) {
_, _ = fmt.Fprintf(w, "error: %v\n", err)
os.Exit(1)
}
12 changes: 2 additions & 10 deletions cmd/elasticsearch/check_and_finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package elasticsearch

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward"
Expand All @@ -25,15 +25,7 @@ func checkAndFinalizeCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
Long: `Check the status of a restore operation and perform finalization (scale up deployments) if complete.
If the restore is still running and --wait is specified, wait for completion before finalizing.`,
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runCheckAndFinalize(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runCheckAndFinalize, cmdutils.MinioIsRequired)
},
}

Expand Down
12 changes: 2 additions & 10 deletions cmd/elasticsearch/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package elasticsearch

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward"
Expand All @@ -16,15 +16,7 @@ func configureCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
Short: "Configure Elasticsearch snapshot repository and SLM policy",
Long: `Configure Elasticsearch snapshot repository and Snapshot Lifecycle Management (SLM) policy for automated backups.`,
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runConfigure(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runConfigure, cmdutils.MinioIsRequired)
},
}
}
Expand Down
12 changes: 2 additions & 10 deletions cmd/elasticsearch/list-indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package elasticsearch

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/output"
Expand All @@ -16,15 +16,7 @@ func listIndicesCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
Use: "list-indices",
Short: "List Elasticsearch indices",
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runListIndices(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runListIndices, cmdutils.MinioIsRequired)
},
}
}
Expand Down
12 changes: 2 additions & 10 deletions cmd/elasticsearch/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package elasticsearch

import (
"fmt"
"os"
"sort"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/output"
Expand All @@ -17,15 +17,7 @@ func listCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
Use: "list",
Short: "List available Elasticsearch snapshots",
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runListSnapshots(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runListSnapshots, cmdutils.MinioIsRequired)
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/elasticsearch/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
// minimalMinioStackgraphConfig provides the required Minio and Stackgraph configuration for tests
const minimalMinioStackgraphConfig = `
minio:
enabled: true
service:
name: minio
port: 9000
Expand Down Expand Up @@ -79,6 +80,7 @@ settings:
receiverBaseUrl: "http://receiver:7077"
platformVersion: "5.2.0"
zookeeperQuorum: "zookeeper:2181"
pvc: "suse-observability-settings-backup-data"
job:
image: settings-backup:latest
waitImage: wait:latest
Expand Down
12 changes: 2 additions & 10 deletions cmd/elasticsearch/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package elasticsearch

import (
"fmt"
"os"
"sort"
"strings"
"time"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
es "github.com/stackvista/stackstate-backup-cli/internal/clients/elasticsearch"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
Expand Down Expand Up @@ -38,15 +38,7 @@ func restoreCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
Short: "Restore Elasticsearch from a snapshot",
Long: `Restore Elasticsearch indices from a snapshot. Deletes existing STS indices before restore. Waits for completion by default; use --background to run asynchronously.`,
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runRestore(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runRestore, cmdutils.MinioIsRequired)
}}

cmd.Flags().StringVarP(&snapshotName, "snapshot", "s", "", "Snapshot name to restore (mutually exclusive with --latest)")
Expand Down
14 changes: 2 additions & 12 deletions cmd/settings/check_and_finalize.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package settings

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/orchestration/restore"
Expand Down Expand Up @@ -33,15 +31,7 @@ Examples:
# Wait for job completion and cleanup
sts-backup settings check-and-finalize --job settings-restore-20250128t143000 --wait -n my-namespace`,
Run: func(_ *cobra.Command, _ []string) {
appCtx, err := app.NewContext(globalFlags)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := runCheckAndFinalize(appCtx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
cmdutils.Run(globalFlags, runCheckAndFinalize, cmdutils.MinioIsNotRequired)
},
}

Expand Down
Loading