Skip to content

Commit a3310e9

Browse files
authored
STAC-24013: Settings commands support pvc as a backup storage (#9)
* STAC-24013: settings command support pvc as a backup storage
1 parent c947e26 commit a3310e9

File tree

26 files changed

+378
-317
lines changed

26 files changed

+378
-317
lines changed

cmd/clickhouse/check_and_finalize.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package clickhouse
22

33
import (
44
"fmt"
5-
"os"
65
"time"
76

87
"github.com/spf13/cobra"
8+
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
99
"github.com/stackvista/stackstate-backup-cli/internal/app"
1010
"github.com/stackvista/stackstate-backup-cli/internal/clients/clickhouse"
1111
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
@@ -34,15 +34,7 @@ func checkAndFinalizeCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
3434
This command is useful when a restore was started without --wait flag or was interrupted.
3535
It will check the restore status and if complete, execute post-restore tasks and scale up resources.`,
3636
Run: func(_ *cobra.Command, _ []string) {
37-
appCtx, err := app.NewContext(globalFlags)
38-
if err != nil {
39-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
40-
os.Exit(1)
41-
}
42-
if err := runCheckAndFinalize(appCtx); err != nil {
43-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
44-
os.Exit(1)
45-
}
37+
cmdutils.Run(globalFlags, runCheckAndFinalize, cmdutils.MinioIsRequired)
4638
},
4739
}
4840

cmd/clickhouse/list.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package clickhouse
22

33
import (
44
"fmt"
5-
"os"
65
"sort"
76

87
"github.com/spf13/cobra"
8+
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
99
"github.com/stackvista/stackstate-backup-cli/internal/app"
1010
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
1111
"github.com/stackvista/stackstate-backup-cli/internal/foundation/output"
@@ -18,15 +18,7 @@ func listCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
1818
Short: "List available Clickhouse backups",
1919
Long: `List all Clickhouse backups from the ClickHouse Backup API.`,
2020
Run: func(_ *cobra.Command, _ []string) {
21-
appCtx, err := app.NewContext(globalFlags)
22-
if err != nil {
23-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
24-
os.Exit(1)
25-
}
26-
if err := runList(appCtx); err != nil {
27-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
28-
os.Exit(1)
29-
}
21+
cmdutils.Run(globalFlags, runList, cmdutils.MinioIsRequired)
3022
},
3123
}
3224
}

cmd/clickhouse/restore.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package clickhouse
22

33
import (
44
"fmt"
5-
"os"
65
"sort"
76

87
"github.com/spf13/cobra"
8+
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
99
"github.com/stackvista/stackstate-backup-cli/internal/app"
1010
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
1111
"github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward"
@@ -27,15 +27,7 @@ func restoreCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
2727
Short: "Restore ClickHouse from a backup archive",
2828
Long: `Restore ClickHouse data from a backup archive via ClickHouse Backup API. Waits for completion by default; use --background to run asynchronously.`,
2929
Run: func(_ *cobra.Command, _ []string) {
30-
appCtx, err := app.NewContext(globalFlags)
31-
if err != nil {
32-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
33-
os.Exit(1)
34-
}
35-
if err := runRestore(appCtx); err != nil {
36-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
37-
os.Exit(1)
38-
}
30+
cmdutils.Run(globalFlags, runRestore, cmdutils.MinioIsRequired)
3931
},
4032
}
4133

cmd/cmdutils/common.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmdutils
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
"os"
8+
9+
"github.com/stackvista/stackstate-backup-cli/internal/app"
10+
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
11+
)
12+
13+
const (
14+
MinioIsRequired bool = true
15+
MinioIsNotRequired bool = false
16+
)
17+
18+
func Run(globalFlags *config.CLIGlobalFlags, runFunc func(ctx *app.Context) error, minioRequired bool) {
19+
appCtx, err := app.NewContext(globalFlags)
20+
if err != nil {
21+
exitWithError(err, os.Stderr)
22+
}
23+
if minioRequired && !appCtx.Config.Minio.Enabled {
24+
exitWithError(errors.New("commands that interact with Minio require SUSE Observability to be deployed with .Values.global.backup.enabled=true"), os.Stderr)
25+
}
26+
if err := runFunc(appCtx); err != nil {
27+
exitWithError(err, os.Stderr)
28+
}
29+
}
30+
31+
// ExitWithError prints an error message to the writer and exits with status code 1.
32+
// This is a helper function to avoid repeating error handling code in commands.
33+
func exitWithError(err error, w io.Writer) {
34+
_, _ = fmt.Fprintf(w, "error: %v\n", err)
35+
os.Exit(1)
36+
}

cmd/elasticsearch/check_and_finalize.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package elasticsearch
22

33
import (
44
"fmt"
5-
"os"
65

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

cmd/elasticsearch/configure.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package elasticsearch
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/spf13/cobra"
7+
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
88
"github.com/stackvista/stackstate-backup-cli/internal/app"
99
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
1010
"github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward"
@@ -16,15 +16,7 @@ func configureCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
1616
Short: "Configure Elasticsearch snapshot repository and SLM policy",
1717
Long: `Configure Elasticsearch snapshot repository and Snapshot Lifecycle Management (SLM) policy for automated backups.`,
1818
Run: func(_ *cobra.Command, _ []string) {
19-
appCtx, err := app.NewContext(globalFlags)
20-
if err != nil {
21-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
22-
os.Exit(1)
23-
}
24-
if err := runConfigure(appCtx); err != nil {
25-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
26-
os.Exit(1)
27-
}
19+
cmdutils.Run(globalFlags, runConfigure, cmdutils.MinioIsRequired)
2820
},
2921
}
3022
}

cmd/elasticsearch/list-indices.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package elasticsearch
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/spf13/cobra"
7+
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
88
"github.com/stackvista/stackstate-backup-cli/internal/app"
99
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
1010
"github.com/stackvista/stackstate-backup-cli/internal/foundation/output"
@@ -16,15 +16,7 @@ func listIndicesCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
1616
Use: "list-indices",
1717
Short: "List Elasticsearch indices",
1818
Run: func(_ *cobra.Command, _ []string) {
19-
appCtx, err := app.NewContext(globalFlags)
20-
if err != nil {
21-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
22-
os.Exit(1)
23-
}
24-
if err := runListIndices(appCtx); err != nil {
25-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
26-
os.Exit(1)
27-
}
19+
cmdutils.Run(globalFlags, runListIndices, cmdutils.MinioIsRequired)
2820
},
2921
}
3022
}

cmd/elasticsearch/list.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package elasticsearch
22

33
import (
44
"fmt"
5-
"os"
65
"sort"
76

87
"github.com/spf13/cobra"
8+
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
99
"github.com/stackvista/stackstate-backup-cli/internal/app"
1010
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
1111
"github.com/stackvista/stackstate-backup-cli/internal/foundation/output"
@@ -17,15 +17,7 @@ func listCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
1717
Use: "list",
1818
Short: "List available Elasticsearch snapshots",
1919
Run: func(_ *cobra.Command, _ []string) {
20-
appCtx, err := app.NewContext(globalFlags)
21-
if err != nil {
22-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
23-
os.Exit(1)
24-
}
25-
if err := runListSnapshots(appCtx); err != nil {
26-
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
27-
os.Exit(1)
28-
}
20+
cmdutils.Run(globalFlags, runListSnapshots, cmdutils.MinioIsRequired)
2921
},
3022
}
3123
}

cmd/elasticsearch/list_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
// minimalMinioStackgraphConfig provides the required Minio and Stackgraph configuration for tests
2525
const minimalMinioStackgraphConfig = `
2626
minio:
27+
enabled: true
2728
service:
2829
name: minio
2930
port: 9000
@@ -79,6 +80,7 @@ settings:
7980
receiverBaseUrl: "http://receiver:7077"
8081
platformVersion: "5.2.0"
8182
zookeeperQuorum: "zookeeper:2181"
83+
pvc: "suse-observability-settings-backup-data"
8284
job:
8385
image: settings-backup:latest
8486
waitImage: wait:latest

cmd/elasticsearch/restore.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package elasticsearch
22

33
import (
44
"fmt"
5-
"os"
65
"sort"
76
"strings"
87
"time"
98

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

5244
cmd.Flags().StringVarP(&snapshotName, "snapshot", "s", "", "Snapshot name to restore (mutually exclusive with --latest)")

0 commit comments

Comments
 (0)