-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnapshot.go
274 lines (225 loc) · 7.31 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
package vcon
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)
// Snapshot represents a node in a hierarchical list of VM snapshots
type Snapshot struct {
Name string `json:"name"`
Ref string `json:"ref"`
Children []Snapshot `json:"children,omitempty"`
}
// FindSnapshot will locate the Managed Object Reference for a snapshot, either
// by looking it up by name, or converting the provided ref into a MORef.
func (c *Client) FindSnapshot(vm *VirtualMachine, name string, byRef bool) (*types.ManagedObjectReference, error) {
var moRef *types.ManagedObjectReference
if byRef {
moRef = &types.ManagedObjectReference{
Type: "VirtualMachineSnapshot",
Value: name,
}
} else {
err := func() error {
ctx, cancelFn := context.WithTimeout(context.Background(), c.timeout)
defer cancelFn()
var err error
moRef, err = vm.VM.FindSnapshot(ctx, name)
if err := c.checkErr(ctx, err); err != nil {
return errors.Wrapf(err, "While finding snapshot")
}
return nil
}()
if err != nil {
switch err := errors.Cause(err).(type) {
case *TimeoutExceededError:
// handle specifically
return nil, fmt.Errorf("Timeout while attempting to find snapshot for a VM")
default:
// unknown error
return nil, errors.Wrap(err, "Got error while finding snapshot for a VM")
}
}
}
return moRef, nil
}
// SnapshotCreate will create a snapshot of the current VM. It is assumed that the
// VM is already powered off. Taking a snapshot of a powered-on or suspended VM
// _may_ be successful, but certain configurations will cause problems, and we are
// not snapshotting the current memory state or quiescing the file system.
// See https://docs.vmware.com/en/VMware-vSphere/6.5/com.vmware.vsphere.vm_admin.doc/GUID-53F65726-A23B-4CF0-A7D5-48E584B88613.html
func (c *Client) SnapshotCreate(vm *VirtualMachine, name string) (*types.ManagedObjectReference, error) {
if c.Verbose {
fmt.Printf("Creating a VM snapshot...\n")
}
var res types.ManagedObjectReference
err := func() error {
ctx, cancelFn := context.WithTimeout(context.Background(), c.timeout)
defer cancelFn()
task, err := vm.VM.CreateSnapshot(ctx, name, "", false, false)
any, err := c.finishTask(ctx, task, err)
if err != nil {
return errors.Wrapf(err, "While snapshotting VM")
}
res = any.(types.ManagedObjectReference)
return nil
}()
if err != nil {
switch err := errors.Cause(err).(type) {
case *TimeoutExceededError:
// handle specifically
return nil, fmt.Errorf("Timeout while attempting to snapshot VM")
default:
// unknown error
return nil, errors.Wrap(err, "Got error while snapshotting a VM")
}
}
return &res, nil
}
// SnapshotList retrieves all the snapshots for the provided VM, hierarchically
func (c *Client) SnapshotList(vm *VirtualMachine) (*Snapshot, error) {
if c.Verbose {
fmt.Printf("Getting a list of snapshots for VM...\n")
}
var sn *Snapshot
err := func() error {
_, cancelFn := context.WithTimeout(context.Background(), c.timeout)
defer cancelFn()
if vm.MO.Snapshot == nil {
return nil
}
// TODO: unclear how to proceed here.
return nil
}()
if err != nil {
switch err := errors.Cause(err).(type) {
case *TimeoutExceededError:
// handle specifically
return nil, fmt.Errorf("Timeout while attempting to list snapshots for a VM")
default:
// unknown error
return nil, errors.Wrap(err, "Got error while listing snapshots for a VM")
}
}
return sn, nil
}
// SnapshotRemove removes a single snapshot from the provided VM
func (c *Client) SnapshotRemove(vm *VirtualMachine, moRef *types.ManagedObjectReference) error {
err := func() error {
ctx, cancelFn := context.WithTimeout(context.Background(), c.timeout)
defer cancelFn()
consolidate := true
req := types.RemoveSnapshot_Task{
This: moRef.Reference(),
RemoveChildren: false,
Consolidate: &consolidate,
}
res, err := methods.RemoveSnapshot_Task(ctx, vm.VM.Client(), &req)
if err := c.checkErr(ctx, err); err != nil {
return errors.Wrapf(err, "While removing snapshot")
}
task := object.NewTask(vm.VM.Client(), res.Returnval)
_, err = c.finishTask(ctx, task, nil)
if err != nil {
return errors.Wrapf(err, "While waiting for snapshot removal")
}
return nil
}()
if err != nil {
switch err := errors.Cause(err).(type) {
case *TimeoutExceededError:
// handle specifically
return fmt.Errorf("Timeout while attempting to list snapshots for a VM")
default:
// unknown error
return errors.Wrap(err, "Got error while listing snapshots for a VM")
}
}
return nil
}
// SnapshotRemoveAll removes snapshots from the provided VM
func (c *Client) SnapshotRemoveAll(vm *VirtualMachine) error {
err := func() error {
ctx, cancelFn := context.WithTimeout(context.Background(), c.timeout)
defer cancelFn()
consolidate := true
task, err := vm.VM.RemoveAllSnapshot(ctx, &consolidate)
_, err = c.finishTask(ctx, task, err)
if err != nil {
return errors.Wrapf(err, "While removing all snapshots")
}
return nil
}()
if err != nil {
switch err := errors.Cause(err).(type) {
case *TimeoutExceededError:
// handle specifically
return fmt.Errorf("Timeout while attempting to remove all snapshots for a VM")
default:
// unknown error
return errors.Wrap(err, "Got error while removing all snapshots for a VM")
}
}
return nil
}
// SnapshotRevert will revert the provided VM back the previous snapshot
func (c *Client) SnapshotRevert(vm *VirtualMachine) error {
err := func() error {
ctx, cancelFn := context.WithTimeout(context.Background(), c.timeout)
defer cancelFn()
task, err := vm.VM.RevertToCurrentSnapshot(ctx, true)
_, err = c.finishTask(ctx, task, err)
if err != nil {
return errors.Wrapf(err, "While reverting to the most recent snapshot")
}
return nil
}()
if err != nil {
switch err := errors.Cause(err).(type) {
case *TimeoutExceededError:
// handle specifically
return fmt.Errorf("Timeout while attempting to revert to the most recent snapshot for a VM")
default:
// unknown error
return errors.Wrap(err, "Got error while reverting to the most recent snapshot for a VM")
}
}
return nil
}
// SnapshotRevertTo will revert the provided VM to the specified snapshot
func (c *Client) SnapshotRevertTo(vm *VirtualMachine, moRef *types.ManagedObjectReference) error {
err := func() error {
ctx, cancelFn := context.WithTimeout(context.Background(), c.timeout)
defer cancelFn()
suppress := true
req := types.RevertToSnapshot_Task{
This: *moRef,
SuppressPowerOn: &suppress,
}
res, err := methods.RevertToSnapshot_Task(ctx, c.Client.Client, &req)
err = c.checkErr(ctx, err)
if err != nil {
return err
}
task := object.NewTask(c.Client.Client, res.Returnval)
_, err = c.finishTask(ctx, task, err)
if err != nil {
return errors.Wrapf(err, "While reverting to the most recent snapshot")
}
return nil
}()
if err != nil {
switch err := errors.Cause(err).(type) {
case *TimeoutExceededError:
// handle specifically
return fmt.Errorf("Timeout while attempting to revert to a specific snapshot for a VM")
default:
// unknown error
return errors.Wrap(err, "Got error while reverting to a specific snapshot for a VM")
}
}
return nil
}