forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivers_storage.go
413 lines (331 loc) · 10.2 KB
/
drivers_storage.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package core
import (
"bytes"
"sync"
"github.com/emccode/rexray/core/errors"
)
// BlockDevice provides information about a block-storage device.
type BlockDevice struct {
// The name of the provider that owns the block device.
ProviderName string
// The ID of the instance to which the device is connected.
InstanceID string
// The ID of the volume for which the device is mounted.
VolumeID string
// The name of the device.
DeviceName string
// The region from which the device originates.
Region string
// The device status.
Status string
// The name of the network on which the device resides.
NetworkName string
}
// Instance provides information about a storage object.
type Instance struct {
// The name of the provider that owns the object.
ProviderName string
// The ID of the instance to which the object is connected.
InstanceID string
// The region from which the object originates.
Region string
// The name of the instance.
Name string
}
// Snapshot provides information about a storage-layer snapshot.
type Snapshot struct {
// The name of the snapshot.
Name string
// The ID of the volume to which the snapshot belongs.
VolumeID string
// The snapshot's ID.
SnapshotID string
// The size of the volume to which the snapshot belongs/
VolumeSize string
// The time at which the request to create the snapshot was submitted.
StartTime string
// A description of the snapshot.
Description string
// The status of the snapshot.
Status string
}
// Volume provides information about a storage volume.
type Volume struct {
// The name of the volume.
Name string
// The volume ID.
VolumeID string
// The availability zone for which the volume is available.
AvailabilityZone string
// The volume status.
Status string
// The volume type.
VolumeType string
// The volume IOPs.
IOPS int64
// The size of the volume.
Size string
// The name of the network on which the volume resides.
NetworkName string
// The volume's attachments.
Attachments []*VolumeAttachment
}
// VolumeAttachment provides information about an object attached to a
// storage volume.
type VolumeAttachment struct {
// The ID of the volume to which the attachment belongs.
VolumeID string
// The ID of the instance on which the volume to which the attachment
// belongs is mounted.
InstanceID string
// The name of the device on which the volume to which the object is
// attached is mounted.
DeviceName string
// The status of the attachment.
Status string
}
// StorageDriver is the interface implemented by types that provide storage
// introspection and management.
type StorageDriver interface {
Driver
// GetVolumeMapping lists the block devices that are attached to the
GetVolumeMapping() ([]*BlockDevice, error)
// GetInstance retrieves the local instance.
GetInstance() (*Instance, error)
// GetVolume returns all volumes for the instance based on either volumeID
// or volumeName that are available to the instance.
GetVolume(volumeID, volumeName string) ([]*Volume, error)
// GetVolumeAttach returns the attachment details based on volumeID or
// volumeName where the volume is currently attached.
GetVolumeAttach(volumeID, instanceID string) ([]*VolumeAttachment, error)
// CreateSnapshot is a synch/async operation that returns snapshots that
// have been performed based on supplying a snapshotName, source volumeID,
// and optional description.
CreateSnapshot(
runAsync bool,
snapshotName, volumeID, description string) ([]*Snapshot, error)
// GetSnapshot returns a list of snapshots for a volume based on volumeID,
// snapshotID, or snapshotName.
GetSnapshot(volumeID, snapshotID, snapshotName string) ([]*Snapshot, error)
// RemoveSnapshot will remove a snapshot based on the snapshotID.
RemoveSnapshot(snapshotID string) error
// CreateVolume is sync/async and will create an return a new/existing
// Volume based on volumeID/snapshotID with a name of volumeName and a size
// in GB. Optionally based on the storage driver, a volumeType, IOPS, and
// availabilityZone could be defined.
CreateVolume(
runAsync bool,
volumeName, volumeID, snapshotID, volumeType string,
IOPS, size int64,
availabilityZone string) (*Volume, error)
// RemoveVolume will remove a volume based on volumeID.
RemoveVolume(volumeID string) error
// GetDeviceNextAvailable return a device path that will retrieve the next
// available disk device that can be used.
GetDeviceNextAvailable() (string, error)
// AttachVolume returns a list of VolumeAttachments is sync/async that will
// attach a volume to an instance based on volumeID and instanceID.
AttachVolume(
runAsync bool, volumeID, instanceID string, force bool) ([]*VolumeAttachment, error)
// DetachVolume is sync/async that will detach the volumeID from the local
// instance or the instanceID.
DetachVolume(runAsync bool, volumeID string, instanceID string, force bool) error
// CopySnapshot is a sync/async and returns a snapshot that will copy a
// snapshot based on volumeID/snapshotID/snapshotName and create a new
// snapshot of desinationSnapshotName in the destinationRegion location.
CopySnapshot(
runAsync bool, volumeID, snapshotID, snapshotName,
destinationSnapshotName, destinationRegion string) (*Snapshot, error)
}
// StorageDriverManager acts as both a StorageDriverManager and as an aggregate
// of storage drivers, providing batch methods.
type StorageDriverManager interface {
StorageDriver
// Drivers gets a channel which receives a list of all of the configured
// storage drivers.
Drivers() <-chan StorageDriver
// GetInstances gets the instance for each of the configured drivers.
GetInstances() ([]*Instance, error)
}
type sdm struct {
rexray *RexRay
drivers map[string]StorageDriver
}
func (r *sdm) Init(rexray *RexRay) error {
if len(r.drivers) == 0 {
return errors.ErrNoStorageDrivers
}
return nil
}
func (r *sdm) Name() string {
var b bytes.Buffer
for d := range r.Drivers() {
if b.Len() > 0 {
b.WriteString(" ")
}
b.WriteString(d.Name())
}
return b.String()
}
func (r *sdm) Drivers() <-chan StorageDriver {
c := make(chan StorageDriver)
go func() {
if len(r.drivers) == 0 {
close(c)
return
}
for _, v := range r.drivers {
c <- v
}
close(c)
}()
return c
}
// GetVolumeMapping performs storage introspection and
// returns a listing of block devices from the guest
func (r *sdm) GetVolumeMapping() ([]*BlockDevice, error) {
var allBlockDevices []*BlockDevice
for _, driver := range r.drivers {
blockDevices, err := driver.GetVolumeMapping()
if err != nil {
return []*BlockDevice{}, err
}
if len(blockDevices) > 0 {
for _, blockDevice := range blockDevices {
allBlockDevices = append(allBlockDevices, blockDevice)
}
}
}
if len(allBlockDevices) == 0 {
return nil, errors.ErrNoStorageDetected
}
return allBlockDevices, nil
}
func (r *sdm) GetInstances() ([]*Instance, error) {
cI := make(chan *Instance)
cE := make(chan error)
defer close(cI)
defer close(cE)
done := make(chan int)
var wg sync.WaitGroup
wg.Add(len(r.drivers))
go func() {
for _, d := range r.drivers {
go func(d StorageDriver) {
defer wg.Done()
var e error
var i *Instance
i, e = d.GetInstance()
if e != nil {
cE <- e
} else {
cI <- i
}
}(d)
}
}()
go func() {
wg.Wait()
done <- 1
}()
var allInstances []*Instance
for {
select {
case i := <-cI:
allInstances = append(allInstances, i)
case e := <-cE:
return nil, e
case <-done:
if len(allInstances) == 0 {
return nil, errors.ErrNoStorageDetected
}
return allInstances, nil
}
}
}
func (r *sdm) GetInstance() (*Instance, error) {
for _, d := range r.drivers {
return d.GetInstance()
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) GetVolume(volumeID, volumeName string) ([]*Volume, error) {
for _, d := range r.drivers {
return d.GetVolume(volumeID, volumeName)
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) GetSnapshot(volumeID, snapshotID, snapshotName string) ([]*Snapshot, error) {
for _, d := range r.drivers {
return d.GetSnapshot(volumeID, snapshotID, snapshotName)
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) CreateSnapshot(runAsync bool,
snapshotName, volumeID, description string) ([]*Snapshot, error) {
for _, d := range r.drivers {
return d.CreateSnapshot(runAsync, snapshotName, volumeID, description)
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) RemoveSnapshot(snapshotID string) error {
for _, d := range r.drivers {
return d.RemoveSnapshot(snapshotID)
}
return errors.ErrNoStorageDetected
}
func (r *sdm) CreateVolume(runAsync bool,
volumeName, volumeID, snapshotID, volumeType string,
IOPS, size int64, availabilityZone string) (*Volume, error) {
for _, d := range r.drivers {
return d.CreateVolume(
runAsync, volumeName, volumeID, snapshotID, volumeType,
IOPS, size, availabilityZone)
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) RemoveVolume(volumeID string) error {
for _, d := range r.drivers {
return d.RemoveVolume(volumeID)
}
return errors.ErrNoStorageDetected
}
func (r *sdm) AttachVolume(
runAsync bool,
volumeID, instanceID string, force bool) ([]*VolumeAttachment, error) {
for _, d := range r.drivers {
return d.AttachVolume(runAsync, volumeID, instanceID, force)
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) DetachVolume(
runAsync bool,
volumeID, instanceID string, force bool) error {
for _, d := range r.drivers {
return d.DetachVolume(runAsync, volumeID, instanceID, force)
}
return errors.ErrNoStorageDetected
}
func (r *sdm) GetVolumeAttach(
volumeID, instanceID string) ([]*VolumeAttachment, error) {
for _, d := range r.drivers {
return d.GetVolumeAttach(volumeID, instanceID)
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) CopySnapshot(
runAsync bool,
volumeID, snapshotID, snapshotName,
targetSnapshotName, targetRegion string) (*Snapshot, error) {
for _, d := range r.drivers {
return d.CopySnapshot(runAsync, volumeID, snapshotID, snapshotName,
targetSnapshotName, targetRegion)
}
return nil, errors.ErrNoStorageDetected
}
func (r *sdm) GetDeviceNextAvailable() (string, error) {
for _, d := range r.drivers {
return d.GetDeviceNextAvailable()
}
return "", errors.ErrNoStorageDetected
}