-
Notifications
You must be signed in to change notification settings - Fork 21
/
cain.go
236 lines (203 loc) · 7.24 KB
/
cain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package cain
import (
"fmt"
"log"
"path/filepath"
"github.com/nuvo/cain/pkg/utils"
"github.com/nuvo/skbn/pkg/skbn"
)
// Authentication options if cassandra cluster uses authentication
type Credentials struct {
enabled bool
username string
nodetoolCredentialsFile string
}
// BackupOptions are the options to pass to Backup
type BackupOptions struct {
Namespace string
Selector string
Container string
Keyspace string
Dst string
Parallel int
BufferSize float64
S3MaxUploadParts int
S3PartSize int64
CassandraDataDir string
Authentication bool
CassandraUsername string
NodetoolCredentialsFile string
Verbose bool
}
// Backup performs backup
func Backup(o BackupOptions) (string, error) {
log.Println("Backup started!")
dstPrefix, dstPath := utils.SplitInTwo(o.Dst, "://")
if err := skbn.TestImplementationsExist("k8s", dstPrefix); err != nil {
return "", err
}
log.Println("Getting clients")
k8sClient, dstClient, err := skbn.GetClients("k8s", dstPrefix, "", dstPath)
if err != nil {
return "", err
}
log.Println("Getting pods")
pods, err := utils.GetPods(k8sClient, o.Namespace, o.Selector)
if err != nil {
return "", err
}
log.Println("Testing existence of data dir")
if err := utils.TestK8sDirectory(k8sClient, pods, o.Namespace, o.Container, o.CassandraDataDir); err != nil {
return "", err
}
creds := Credentials{
enabled: o.Authentication,
username: o.CassandraUsername,
nodetoolCredentialsFile: o.NodetoolCredentialsFile,
}
if o.Authentication {
log.Println("Testing existence of nodetool credentials file")
if err := utils.TestK8sDirectory(k8sClient, pods, o.Namespace, o.Container, o.NodetoolCredentialsFile); err != nil {
return "", err
}
}
log.Println("Backing up schema")
dstBasePath, err := BackupKeyspaceSchema(k8sClient, dstClient, o.Namespace, pods[0], o.Container, o.Keyspace, dstPrefix, dstPath, creds, o.S3MaxUploadParts, o.S3PartSize, o.Verbose)
if err != nil {
return "", err
}
log.Println("Taking snapshots")
tag := TakeSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, creds)
log.Println("Calculating paths. This may take a while...")
fromToPathsAllPods, err := utils.GetFromAndToPathsFromK8s(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, tag, dstBasePath, o.CassandraDataDir)
if err != nil {
return "", err
}
log.Println("Starting files copy")
if err := skbn.PerformCopy(k8sClient, dstClient, "k8s", dstPrefix, fromToPathsAllPods, o.Parallel, o.BufferSize, o.S3PartSize, o.S3MaxUploadParts, o.Verbose); err != nil {
return "", err
}
log.Println("Clearing snapshots")
ClearSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, tag, creds)
log.Println("All done!")
return tag, nil
}
// RestoreOptions are the options to pass to Restore
type RestoreOptions struct {
Src string
Keyspace string
Tag string
Schema string
Namespace string
Selector string
Container string
Parallel int
BufferSize float64
S3MaxDownloadParts int
S3PartSize int64
UserGroup string
CassandraDataDir string
Authentication bool
CassandraUsername string
NodetoolCredentialsFile string
Verbose bool
}
// Restore performs restore
func Restore(o RestoreOptions) error {
log.Println("Restore started!")
srcPrefix, srcBasePath := utils.SplitInTwo(o.Src, "://")
log.Println("Getting clients")
srcClient, k8sClient, err := skbn.GetClients(srcPrefix, "k8s", srcBasePath, "")
if err != nil {
return err
}
log.Println("Getting pods")
existingPods, err := utils.GetPods(k8sClient, o.Namespace, o.Selector)
if err != nil {
return err
}
log.Println("Testing existence of data dir")
if err := utils.TestK8sDirectory(k8sClient, existingPods, o.Namespace, o.Container, o.CassandraDataDir); err != nil {
return err
}
creds := Credentials{
enabled: o.Authentication,
username: o.CassandraUsername,
nodetoolCredentialsFile: o.NodetoolCredentialsFile,
}
if o.Authentication {
log.Println("Testing existence of nodetool credentials file")
if err := utils.TestK8sDirectory(k8sClient, existingPods, o.Namespace, o.Container, o.NodetoolCredentialsFile); err != nil {
return err
}
}
log.Println("Getting current schema")
_, sum, err := DescribeKeyspaceSchema(k8sClient, o.Namespace, existingPods[0], o.Container, o.Keyspace)
if err != nil {
if o.Schema == "" {
return err
}
log.Println("Schema not found, restoring schema", o.Schema)
sum, err = RestoreKeyspaceSchema(srcClient, k8sClient, srcPrefix, srcBasePath, o.Namespace, existingPods[0], o.Container, o.Keyspace, o.Schema, o.Parallel, o.BufferSize, o.S3MaxDownloadParts, o.S3PartSize, o.Verbose)
if err != nil {
return err
}
log.Println("Restored schema:", sum)
}
if o.Schema != "" && sum != o.Schema {
return fmt.Errorf("specified schema %s is not the same as found schema %s", o.Schema, sum)
}
log.Println("Found schema:", sum)
log.Println("Calculating paths. This may take a while...")
srcPath := filepath.Join(srcBasePath, o.Keyspace, sum, o.Tag)
fromToPaths, podsToBeRestored, tablesToRefresh, err := utils.GetFromAndToPathsSrcToK8s(srcClient, k8sClient, srcPrefix, srcPath, srcBasePath, o.Namespace, o.Container, o.CassandraDataDir)
if err != nil {
return err
}
log.Println("Validating pods match restore")
if err := utils.SliceContainsSlice(podsToBeRestored, existingPods); err != nil {
return err
}
log.Println("Getting materialized views to exclude")
materializedViews, err := GetMaterializedViews(k8sClient, o.Namespace, o.Container, existingPods[0], o.Keyspace)
if err != nil {
return err
}
log.Println("Truncating tables")
TruncateTables(k8sClient, o.Namespace, o.Container, o.Keyspace, existingPods, tablesToRefresh, materializedViews)
log.Println("Starting files copy")
if err := skbn.PerformCopy(srcClient, k8sClient, srcPrefix, "k8s", fromToPaths, o.Parallel, o.BufferSize, o.S3PartSize, o.S3MaxDownloadParts, o.Verbose); err != nil {
return err
}
log.Println("Changing files ownership")
if err := utils.ChangeFilesOwnership(k8sClient, existingPods, o.Namespace, o.Container, o.UserGroup, o.CassandraDataDir); err != nil {
return err
}
log.Println("Refreshing tables")
RefreshTables(k8sClient, o.Namespace, o.Container, o.Keyspace, podsToBeRestored, tablesToRefresh, creds)
log.Println("All done!")
return nil
}
// SchemaOptions are the options to pass to Schema
type SchemaOptions struct {
Namespace string
Selector string
Container string
Keyspace string
}
// Schema gets the schema of the cassandra cluster
func Schema(o SchemaOptions) ([]byte, string, error) {
k8sClient, err := skbn.GetClientToK8s()
if err != nil {
return nil, "", err
}
pods, err := utils.GetPods(k8sClient, o.Namespace, o.Selector)
if err != nil {
return nil, "", err
}
schema, sum, err := DescribeKeyspaceSchema(k8sClient, o.Namespace, pods[0], o.Container, o.Keyspace)
if err != nil {
return nil, "", err
}
return schema, sum, nil
}