forked from longhorn/longhorn-engine
/
snapshot.go
258 lines (219 loc) · 4.78 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
package app
import (
"encoding/json"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/rancher/longhorn-engine/sync"
"github.com/rancher/longhorn-engine/util"
)
func SnapshotCmd() cli.Command {
return cli.Command{
Name: "snapshots",
ShortName: "snapshot",
Subcommands: []cli.Command{
SnapshotCreateCmd(),
SnapshotRevertCmd(),
SnapshotLsCmd(),
SnapshotRmCmd(),
SnapshotPurgeCmd(),
SnapshotInfoCmd(),
},
Action: func(c *cli.Context) {
if err := lsSnapshot(c); err != nil {
logrus.Fatalf("Error running snapshot command: %v", err)
}
},
}
}
func SnapshotCreateCmd() cli.Command {
return cli.Command{
Name: "create",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "label",
Usage: "specify labels, in the format of `--label key1=value1 --label key2=value2`",
},
},
Action: func(c *cli.Context) {
if err := createSnapshot(c); err != nil {
logrus.Fatalf("Error running create snapshot command: %v", err)
}
},
}
}
func SnapshotRevertCmd() cli.Command {
return cli.Command{
Name: "revert",
Action: func(c *cli.Context) {
if err := revertSnapshot(c); err != nil {
logrus.Fatalf("Error running revert snapshot command: %v", err)
}
},
}
}
func SnapshotRmCmd() cli.Command {
return cli.Command{
Name: "rm",
Action: func(c *cli.Context) {
if err := rmSnapshot(c); err != nil {
logrus.Fatalf("Error running rm snapshot command: %v", err)
}
},
}
}
func SnapshotPurgeCmd() cli.Command {
return cli.Command{
Name: "purge",
Action: func(c *cli.Context) {
if err := purgeSnapshot(c); err != nil {
logrus.Fatalf("Error running purge snapshot command: %v", err)
}
},
}
}
func SnapshotLsCmd() cli.Command {
return cli.Command{
Name: "ls",
Action: func(c *cli.Context) {
if err := lsSnapshot(c); err != nil {
logrus.Fatalf("Error running ls snapshot command: %v", err)
}
},
}
}
func SnapshotInfoCmd() cli.Command {
return cli.Command{
Name: "info",
Action: func(c *cli.Context) {
if err := infoSnapshot(c); err != nil {
logrus.Fatalf("Error running snapshot info command: %v", err)
}
},
}
}
func createSnapshot(c *cli.Context) error {
var (
labelMap map[string]string
err error
)
cli := getCli(c)
var name string
if len(c.Args()) > 0 {
name = c.Args()[0]
}
labels := c.StringSlice("label")
if labels != nil {
labelMap, err = util.ParseLabels(labels)
if err != nil {
return errors.Wrap(err, "cannot parse backup labels")
}
}
id, err := cli.Snapshot(name, labelMap)
if err != nil {
return err
}
fmt.Println(id)
return nil
}
func revertSnapshot(c *cli.Context) error {
cli := getCli(c)
name := c.Args()[0]
if name == "" {
return fmt.Errorf("Missing parameter for snapshot")
}
err := cli.RevertSnapshot(name)
if err != nil {
return err
}
return nil
}
func rmSnapshot(c *cli.Context) error {
var lastErr error
url := c.GlobalString("url")
task := sync.NewTask(url)
for _, name := range c.Args() {
if err := task.DeleteSnapshot(name); err != nil {
lastErr = err
fmt.Fprintf(os.Stderr, "Failed to delete %s: %v\n", name, err)
}
}
return lastErr
}
func purgeSnapshot(c *cli.Context) error {
url := c.GlobalString("url")
task := sync.NewTask(url)
if err := task.PurgeSnapshots(); err != nil {
return fmt.Errorf("Failed to purge snapshots: %v", err)
}
return nil
}
func lsSnapshot(c *cli.Context) error {
cli := getCli(c)
replicas, err := cli.ListReplicas()
if err != nil {
return err
}
first := true
snapshots := []string{}
for _, r := range replicas {
if r.Mode != "RW" {
continue
}
if first {
first = false
chain, err := getChain(r.Address)
if err != nil {
return err
}
// Replica can just started and haven't prepare the head
// file yet
if len(chain) == 0 {
break
}
snapshots = chain[1:]
continue
}
chain, err := getChain(r.Address)
if err != nil {
return err
}
snapshots = util.Filter(snapshots, func(i string) bool {
return util.Contains(chain, i)
})
}
format := "%s\n"
tw := tabwriter.NewWriter(os.Stdout, 0, 20, 1, ' ', 0)
fmt.Fprintf(tw, format, "ID")
for _, s := range snapshots {
s = strings.TrimSuffix(strings.TrimPrefix(s, "volume-snap-"), ".img")
fmt.Fprintf(tw, format, s)
}
tw.Flush()
return nil
}
func infoSnapshot(c *cli.Context) error {
var output []byte
cli := getCli(c)
replicas, err := cli.ListReplicas()
if err != nil {
return err
}
outputDisks, err := sync.GetSnapshotsInfo(replicas)
if err != nil {
return err
}
output, err = json.MarshalIndent(outputDisks, "", "\t")
if err != nil {
return err
}
if output == nil {
return fmt.Errorf("Cannot find suitable replica for snapshot info")
}
fmt.Println(string(output))
return nil
}