Skip to content

Commit f11214a

Browse files
committed
add command options
1 parent d9f14fa commit f11214a

File tree

2 files changed

+84
-24
lines changed

2 files changed

+84
-24
lines changed

cmd/cain.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ func NewBackupCmd(out io.Writer) *cobra.Command {
6868
return nil
6969
},
7070
Run: func(cmd *cobra.Command, args []string) {
71-
if _, err := cain.Backup(b.namespace, b.selector, b.container, b.keyspace, b.dst, b.parallel, b.bufferSize); err != nil {
71+
options := cain.BackupOptions{
72+
Namespace: b.namespace,
73+
Selector: b.selector,
74+
Container: b.container,
75+
Keyspace: b.keyspace,
76+
Dst: b.dst,
77+
Parallel: b.parallel,
78+
BufferSize: b.bufferSize,
79+
}
80+
if _, err := cain.Backup(options); err != nil {
7281
log.Fatal(err)
7382
}
7483
},
@@ -90,6 +99,7 @@ type restoreCmd struct {
9099
src string
91100
keyspace string
92101
tag string
102+
schema string
93103
namespace string
94104
selector string
95105
container string
@@ -120,7 +130,18 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
120130
return nil
121131
},
122132
Run: func(cmd *cobra.Command, args []string) {
123-
if err := cain.Restore(r.src, r.keyspace, r.tag, r.namespace, r.selector, r.container, r.parallel, r.bufferSize); err != nil {
133+
options := cain.RestoreOptions{
134+
Src: r.src,
135+
Keyspace: r.keyspace,
136+
Tag: r.tag,
137+
Schema: r.schema,
138+
Namespace: r.namespace,
139+
Selector: r.selector,
140+
Container: r.container,
141+
Parallel: r.parallel,
142+
BufferSize: r.bufferSize,
143+
}
144+
if err := cain.Restore(options); err != nil {
124145
log.Fatal(err)
125146
}
126147
},
@@ -130,6 +151,7 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
130151
f.StringVar(&r.src, "src", "", "source to restore from. Example: s3://bucket/cassandra/namespace/cluster-name")
131152
f.StringVarP(&r.keyspace, "keyspace", "k", "", "keyspace to act on")
132153
f.StringVarP(&r.tag, "tag", "t", "", "tag to restore")
154+
f.StringVarP(&r.schema, "schema", "s", "", "schema to restore")
133155
f.StringVarP(&r.namespace, "namespace", "n", "", "namespace to find cassandra cluster")
134156
f.StringVarP(&r.selector, "selector", "l", "", "selector to filter on")
135157
f.StringVarP(&r.container, "container", "c", "cassandra", "container name to act on")
@@ -164,7 +186,13 @@ func NewSchemaCmd(out io.Writer) *cobra.Command {
164186
return nil
165187
},
166188
Run: func(cmd *cobra.Command, args []string) {
167-
schema, sum, err := cain.Schema(s.namespace, s.selector, s.container, s.keyspace)
189+
options := cain.SchemaOptions{
190+
Namespace: s.namespace,
191+
Selector: s.selector,
192+
Container: s.container,
193+
Keyspace: s.keyspace,
194+
}
195+
schema, sum, err := cain.Schema(options)
168196
if err != nil {
169197
log.Fatal(err)
170198
}

pkg/cain/cain.go

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ import (
88
"github.com/nuvo/skbn/pkg/skbn"
99
)
1010

11+
// BackupOptions are the options to pass to Backup
12+
type BackupOptions struct {
13+
Namespace string
14+
Selector string
15+
Container string
16+
Keyspace string
17+
Dst string
18+
Parallel int
19+
BufferSize float64
20+
}
21+
1122
// Backup performs backup
12-
func Backup(namespace, selector, container, keyspace, dst string, parallel int, bufferSize float64) (string, error) {
23+
func Backup(o BackupOptions) (string, error) {
1324
log.Println("Backup started!")
14-
dstPrefix, dstPath := utils.SplitInTwo(dst, "://")
25+
dstPrefix, dstPath := utils.SplitInTwo(o.Dst, "://")
1526

1627
if err := skbn.TestImplementationsExist("k8s", dstPrefix); err != nil {
1728
return "", err
@@ -24,42 +35,55 @@ func Backup(namespace, selector, container, keyspace, dst string, parallel int,
2435
}
2536

2637
log.Println("Getting pods")
27-
pods, err := utils.GetPods(k8sClient, namespace, selector)
38+
pods, err := utils.GetPods(k8sClient, o.Namespace, o.Selector)
2839
if err != nil {
2940
return "", err
3041
}
3142

3243
log.Println("Backing up schema")
33-
dstBasePath, err := BackupKeyspaceSchema(k8sClient, dstClient, namespace, pods[0], container, keyspace, dstPrefix, dstPath)
44+
dstBasePath, err := BackupKeyspaceSchema(k8sClient, dstClient, o.Namespace, pods[0], o.Container, o.Keyspace, dstPrefix, dstPath)
3445
if err != nil {
3546
return "", err
3647
}
3748

3849
log.Println("Taking snapshots")
39-
tag := TakeSnapshots(k8sClient, pods, namespace, container, keyspace)
50+
tag := TakeSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace)
4051

4152
log.Println("Calculating paths. This may take a while...")
42-
fromToPathsAllPods, err := utils.GetFromAndToPathsFromK8s(k8sClient, pods, namespace, container, keyspace, tag, dstBasePath)
53+
fromToPathsAllPods, err := utils.GetFromAndToPathsFromK8s(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, tag, dstBasePath)
4354
if err != nil {
4455
return "", err
4556
}
4657

4758
log.Println("Starting files copy")
48-
if err := skbn.PerformCopy(k8sClient, dstClient, "k8s", dstPrefix, fromToPathsAllPods, parallel, bufferSize); err != nil {
59+
if err := skbn.PerformCopy(k8sClient, dstClient, "k8s", dstPrefix, fromToPathsAllPods, o.Parallel, o.BufferSize); err != nil {
4960
return "", err
5061
}
5162

5263
log.Println("Clearing snapshots")
53-
ClearSnapshots(k8sClient, pods, namespace, container, keyspace, tag)
64+
ClearSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, tag)
5465

5566
log.Println("All done!")
5667
return tag, nil
5768
}
5869

70+
// RestoreOptions are the options to pass to Restore
71+
type RestoreOptions struct {
72+
Src string
73+
Keyspace string
74+
Tag string
75+
Schema string
76+
Namespace string
77+
Selector string
78+
Container string
79+
Parallel int
80+
BufferSize float64
81+
}
82+
5983
// Restore performs restore
60-
func Restore(src, keyspace, tag, namespace, selector, container string, parallel int, bufferSize float64) error {
84+
func Restore(o RestoreOptions) error {
6185
log.Println("Restore started!")
62-
srcPrefix, srcBasePath := utils.SplitInTwo(src, "://")
86+
srcPrefix, srcBasePath := utils.SplitInTwo(o.Src, "://")
6387

6488
log.Println("Getting clients")
6589
srcClient, k8sClient, err := skbn.GetClients(srcPrefix, "k8s", srcBasePath, "")
@@ -68,21 +92,21 @@ func Restore(src, keyspace, tag, namespace, selector, container string, parallel
6892
}
6993

7094
log.Println("Getting pods")
71-
existingPods, err := utils.GetPods(k8sClient, namespace, selector)
95+
existingPods, err := utils.GetPods(k8sClient, o.Namespace, o.Selector)
7296
if err != nil {
7397
return err
7498
}
7599

76100
log.Println("Getting current schema")
77-
_, sum, err := DescribeKeyspaceSchema(k8sClient, namespace, existingPods[0], container, keyspace)
101+
_, sum, err := DescribeKeyspaceSchema(k8sClient, o.Namespace, existingPods[0], o.Container, o.Keyspace)
78102
if err != nil {
79103
return err
80104
}
81105
log.Println("Found schema:", sum)
82106

83107
log.Println("Calculating paths. This may take a while...")
84-
srcPath := filepath.Join(srcBasePath, keyspace, sum, tag)
85-
fromToPaths, podsToBeRestored, tablesToRefresh, err := utils.GetFromAndToPathsSrcToK8s(srcClient, k8sClient, srcPrefix, srcPath, srcBasePath, namespace, container)
108+
srcPath := filepath.Join(srcBasePath, o.Keyspace, sum, o.Tag)
109+
fromToPaths, podsToBeRestored, tablesToRefresh, err := utils.GetFromAndToPathsSrcToK8s(srcClient, k8sClient, srcPrefix, srcPath, srcBasePath, o.Namespace, o.Container)
86110
if err != nil {
87111
return err
88112
}
@@ -93,37 +117,45 @@ func Restore(src, keyspace, tag, namespace, selector, container string, parallel
93117
}
94118

95119
log.Println("Getting materialized views to exclude")
96-
materializedViews, err := GetMaterializedViews(k8sClient, namespace, container, existingPods[0], keyspace)
120+
materializedViews, err := GetMaterializedViews(k8sClient, o.Namespace, o.Container, existingPods[0], o.Keyspace)
97121
if err != nil {
98122
return err
99123
}
100124

101125
log.Println("Truncating tables")
102-
TruncateTables(k8sClient, namespace, container, keyspace, existingPods, tablesToRefresh, materializedViews)
126+
TruncateTables(k8sClient, o.Namespace, o.Container, o.Keyspace, existingPods, tablesToRefresh, materializedViews)
103127

104128
log.Println("Starting files copy")
105-
if err := skbn.PerformCopy(srcClient, k8sClient, srcPrefix, "k8s", fromToPaths, parallel, bufferSize); err != nil {
129+
if err := skbn.PerformCopy(srcClient, k8sClient, srcPrefix, "k8s", fromToPaths, o.Parallel, o.BufferSize); err != nil {
106130
return err
107131
}
108132

109133
log.Println("Refreshing tables")
110-
RefreshTables(k8sClient, namespace, container, keyspace, podsToBeRestored, tablesToRefresh)
134+
RefreshTables(k8sClient, o.Namespace, o.Container, o.Keyspace, podsToBeRestored, tablesToRefresh)
111135

112136
log.Println("All done!")
113137
return nil
114138
}
115139

140+
// SchemaOptions are the options to pass to Schema
141+
type SchemaOptions struct {
142+
Namespace string
143+
Selector string
144+
Container string
145+
Keyspace string
146+
}
147+
116148
// Schema gets the schema of the cassandra cluster
117-
func Schema(namespace, selector, container, keyspace string) ([]byte, string, error) {
149+
func Schema(o SchemaOptions) ([]byte, string, error) {
118150
k8sClient, err := skbn.GetClientToK8s()
119151
if err != nil {
120152
return nil, "", err
121153
}
122-
pods, err := utils.GetPods(k8sClient, namespace, selector)
154+
pods, err := utils.GetPods(k8sClient, o.Namespace, o.Selector)
123155
if err != nil {
124156
return nil, "", err
125157
}
126-
schema, sum, err := DescribeKeyspaceSchema(k8sClient, namespace, pods[0], container, keyspace)
158+
schema, sum, err := DescribeKeyspaceSchema(k8sClient, o.Namespace, pods[0], o.Container, o.Keyspace)
127159
if err != nil {
128160
return nil, "", err
129161
}

0 commit comments

Comments
 (0)