/
snapshot.go
335 lines (277 loc) · 9.6 KB
/
snapshot.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package cli
import (
"fmt"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)
func init() {
setupStatusSubCommand()
setupRestoreSubCommand()
setupListSubCommand()
setupCreateSubCommand()
setupDeleteSubCommand()
rootCmd.AddCommand(cmdSnapshot)
}
var cmdSnapshot = &cobra.Command{
Use: "snapshot",
Short: "Interact with a specific snapshot.",
Long: `Use the status, list, and restore subcommands.`,
}
func setupStatusSubCommand() {
cmdSnapshotStatus.Flags().StringP("snapshot", "s", "", "Snapshot name to query (required)")
err := cmdSnapshotStatus.MarkFlagRequired("snapshot")
if err != nil {
fmt.Printf("Error binding snapshot configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshotStatus.Flags().StringP("repository", "r", "", "Snapshot repository to query (required)")
err = cmdSnapshotStatus.MarkFlagRequired("repository")
if err != nil {
fmt.Printf("Error binding repository configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshot.AddCommand(cmdSnapshotStatus)
}
func setupCreateSubCommand() {
cmdSnapshotCreate.Flags().StringP("snapshot", "s", "", "Snapshot name to query (required)")
err := cmdSnapshotCreate.MarkFlagRequired("snapshot")
if err != nil {
fmt.Printf("Error binding snapshot configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshotCreate.Flags().StringP("repository", "r", "", "Snapshot repository to query (required)")
err = cmdSnapshotCreate.MarkFlagRequired("repository")
if err != nil {
fmt.Printf("Error binding repository configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshotCreate.Flags().BoolP("all-indices", "a", false, "Snapshot all indices on the cluster. Takes precedence over index arguments.")
cmdSnapshotCreate.Flags().StringSliceP("index", "i", []string{}, "Snapshot specific indices on the cluster. Can be repeated.")
cmdSnapshot.AddCommand(cmdSnapshotCreate)
}
func setupDeleteSubCommand() {
cmdSnapshotDelete.Flags().StringP("snapshot", "s", "", "Snapshot name to query (required)")
err := cmdSnapshotDelete.MarkFlagRequired("snapshot")
if err != nil {
fmt.Printf("Error binding snapshot configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshotDelete.Flags().StringP("repository", "r", "", "Snapshot repository to query (required)")
err = cmdSnapshotCreate.MarkFlagRequired("repository")
if err != nil {
fmt.Printf("Error binding repository configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshot.AddCommand(cmdSnapshotDelete)
}
func setupListSubCommand() {
cmdSnapshotList.Flags().StringP("repository", "r", "", "Snapshot repository to query (required)")
err := cmdSnapshotList.MarkFlagRequired("repository")
if err != nil {
fmt.Printf("Error binding repository configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshot.AddCommand(cmdSnapshotList)
}
func setupRestoreSubCommand() {
cmdSnapshotRestore.Flags().StringP("snapshot", "s", "", "Snapshot name to query (required)")
err := cmdSnapshotRestore.MarkFlagRequired("snapshot")
if err != nil {
fmt.Printf("Error binding snapshot configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshotRestore.Flags().StringP("repository", "r", "", "Snapshot repository to query (required)")
err = cmdSnapshotRestore.MarkFlagRequired("repository")
if err != nil {
fmt.Printf("Error binding repository configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshotRestore.Flags().StringP("prefix", "", "restored_", "What to prefix on the restored index")
err = cmdSnapshotRestore.MarkFlagRequired("prefix")
if err != nil {
fmt.Printf("Error binding prefix configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshotRestore.Flags().StringP("index", "i", "", "Which index to restore from the snapshot")
err = cmdSnapshotRestore.MarkFlagRequired("index")
if err != nil {
fmt.Printf("Error binding index configuration flag: %s \n", err)
os.Exit(1)
}
cmdSnapshot.AddCommand(cmdSnapshotRestore)
}
var cmdSnapshotStatus = &cobra.Command{
Use: "status",
Short: "Display info about a snapshot.",
Long: `This command will display detailed information about the given snapshot.`,
Run: func(cmd *cobra.Command, args []string) {
v := getClient()
snapshotName, err := cmd.Flags().GetString("snapshot")
if err != nil {
fmt.Printf("Could not retrieve required argument: snapshot. Error: %s\n", err)
os.Exit(1)
}
repository, err := cmd.Flags().GetString("repository")
if err != nil {
fmt.Printf("Could not retrieve required argument: repository. Error: %s\n", err)
os.Exit(1)
}
snapshot, err := v.GetSnapshotStatus(repository, snapshotName)
if err != nil {
fmt.Printf("Error getting snapshot. Error: %s\n", err)
os.Exit(1)
}
duration, _ := time.ParseDuration(fmt.Sprintf("%dms", snapshot.GetDuration()))
results := [][]string{
{"State", snapshot.State},
{"Name", snapshot.Name},
{"Indices", strings.Join(snapshot.Indices, ", ")},
{"Started", snapshot.StartTime.Format(time.RFC3339)},
{"Finished", snapshot.GetEndTime()},
{"Duration", fmt.Sprintf("%v", duration)},
{"Shards", fmt.Sprintf("Successful shards: %d, failed shards: %d", snapshot.Shards.Successful, snapshot.Shards.Failed)},
}
fmt.Println(renderTable(results, []string{"Metric", "Value"}))
},
}
var cmdSnapshotRestore = &cobra.Command{
Use: "restore",
Short: "Restore a snapshot.",
Long: `This command will restore a specific index from a snapshot to the cluster.`,
Run: func(cmd *cobra.Command, args []string) {
v := getClient()
snapshotName, err := cmd.Flags().GetString("snapshot")
if err != nil {
fmt.Printf("Could not retrieve required argument: snapshot. Error: %s\n", err)
os.Exit(1)
}
repository, err := cmd.Flags().GetString("repository")
if err != nil {
fmt.Printf("Could not retrieve required argument: repository. Error: %s\n", err)
os.Exit(1)
}
prefix, err := cmd.Flags().GetString("prefix")
if err != nil {
fmt.Printf("Could not retrieve required argument: prefix. Error: %s\n", err)
os.Exit(1)
}
index, err := cmd.Flags().GetString("index")
if err != nil {
fmt.Printf("Could not retrieve required argument: index. Error: %s\n", err)
os.Exit(1)
}
err = v.RestoreSnapshotIndices(repository, snapshotName, []string{index}, prefix, nil)
if err != nil {
fmt.Printf("Error while calling restore snapshot API. Error: %s\n", err)
os.Exit(1)
}
fmt.Println("Restore operation called successfully.")
},
}
var cmdSnapshotList = &cobra.Command{
Use: "list",
Short: "Display the snapshots of the cluster.",
Long: `List the 10 most recent snapshots of the given repository`,
Run: func(cmd *cobra.Command, args []string) {
v := getClient()
repository, err := cmd.Flags().GetString("repository")
if err != nil {
fmt.Printf("Could not retrieve required argument: repository. Error: %s\n", err)
os.Exit(1)
}
snapshots, err := v.GetSnapshots(repository)
if err != nil {
fmt.Printf("Could not query snapshots. Error: %s\n", err)
os.Exit(1)
}
header := []string{"State", "Name", "Finished", "Duration"}
if len(snapshots) > 10 {
snapshots = snapshots[len(snapshots)-10:]
}
rows := [][]string{}
for _, snapshot := range snapshots {
duration, _ := time.ParseDuration(fmt.Sprintf("%dms", snapshot.GetDuration()))
row := []string{
snapshot.State,
snapshot.Name,
snapshot.GetEndTime(),
fmt.Sprintf("%v", duration),
}
rows = append(rows, row)
}
fmt.Println(renderTable(rows, header))
},
}
var cmdSnapshotCreate = &cobra.Command{
Use: "create",
Short: "Create a new snapshot.",
Long: `This command will take a new snapshot of the data of either all or specified indices.`,
Run: func(cmd *cobra.Command, args []string) {
v := getClient()
snapshotName, err := cmd.Flags().GetString("snapshot")
if err != nil {
fmt.Printf("Could not retrieve required argument: snapshot. Error: %s\n", err)
os.Exit(1)
}
repository, err := cmd.Flags().GetString("repository")
if err != nil {
fmt.Printf("Could not retrieve required argument: repository. Error: %s\n", err)
os.Exit(1)
}
allIndices, err := cmd.Flags().GetBool("all-indices")
if err != nil {
fmt.Printf("Could not retrieve argument: all-indices. Error: %s\n", err)
os.Exit(1)
}
indices, err := cmd.Flags().GetStringSlice("index")
if err != nil {
fmt.Printf("Could not retrieve argument: index. Error: %s\n", err)
os.Exit(1)
}
if allIndices {
err = v.SnapshotAllIndices(repository, snapshotName)
if err != nil {
fmt.Printf("Error while taking snapshot. Error: %s\n", err)
os.Exit(1)
}
fmt.Println("Snapshot operation started.")
} else {
if len(indices) == 0 {
fmt.Printf("Got 0 indices to snapshot. Please specify indices with `--index` or all indices with `--all-indices`.\n")
os.Exit(1)
}
err = v.SnapshotIndices(repository, snapshotName, indices)
if err != nil {
fmt.Printf("Error while taking snapshot. Error: %s\n", err)
os.Exit(1)
}
fmt.Println("Snapshot operation started.")
}
},
}
var cmdSnapshotDelete = &cobra.Command{
Use: "delete",
Short: "Delete a snapshot.",
Long: `This command will delete a snapshot of the data.`,
Run: func(cmd *cobra.Command, args []string) {
v := getClient()
snapshotName, err := cmd.Flags().GetString("snapshot")
if err != nil {
fmt.Printf("Could not retrieve required argument: snapshot. Error: %s\n", err)
os.Exit(1)
}
repository, err := cmd.Flags().GetString("repository")
if err != nil {
fmt.Printf("Could not retrieve required argument: repository. Error: %s\n", err)
os.Exit(1)
}
err = v.DeleteSnapshot(repository, snapshotName)
if err != nil {
fmt.Printf("Error while deleting snapshot. Error: %s\n", err)
os.Exit(1)
}
fmt.Println("Delete operation called successfully.")
},
}