forked from rexray/rexray
/
storage.go
313 lines (264 loc) · 8.07 KB
/
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
package storage
import (
"fmt"
"os"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/emccode/rexray/config"
"github.com/emccode/rexray/errors"
"github.com/emccode/rexray/util"
)
var driverInitFuncs map[string]InitFunc
func init() {
driverInitFuncs = make(map[string]InitFunc)
}
type StorageDriverManager struct {
Drivers map[string]Driver
Config *config.Config
}
type InitFunc func(conf *config.Config) (Driver, error)
func Register(name string, initFunc InitFunc) {
driverInitFuncs[name] = initFunc
}
func (sdm *StorageDriverManager) GetDriverNames() []string {
return GetDriverNames()
}
func GetDriverNames() []string {
names := make([]string, 0, len(driverInitFuncs))
for n := range driverInitFuncs {
names = append(names, n)
}
return names
}
func getDrivers(conf *config.Config) (map[string]Driver, error) {
driverNames := conf.StorageDrivers
log.WithFields(log.Fields{
"driverInitFuncs": driverInitFuncs,
"driverNames": driverNames}).Debug("getting driver instances")
drivers := map[string]Driver{}
for name, initFunc := range driverInitFuncs {
if len(driverNames) > 0 && !util.StringInSlice(name, driverNames) {
continue
}
var initErr error
drivers[name], initErr = initFunc(conf)
if initErr != nil {
log.WithFields(log.Fields{
"driverName": name,
"error": initErr}).Debug("error initializing driver")
delete(drivers, name)
continue
}
log.WithField("driverName", name).Debug("initialized driver")
}
return drivers, nil
}
var (
ErrDriverBlockDeviceDiscovery = errors.New("Driver Block Device discovery failed")
ErrDriverInstanceDiscovery = errors.New("Driver Instance discovery failed")
ErrDriverVolumeDiscovery = errors.New("Driver Volume discovery failed")
ErrDriverSnapshotDiscovery = errors.New("Driver Snapshot discovery failed")
ErrMultipleDriversDetected = errors.New("Multiple drivers detected, must declare with driver with env of REXRAY_STORAGEDRIVER=")
)
func NewStorageDriverManager(conf *config.Config) (*StorageDriverManager, error) {
sd, sdErr := getDrivers(conf)
if sdErr != nil {
return nil, sdErr
}
if len(sd) == 0 {
return nil, errors.New("no storage drivers initialized")
}
return &StorageDriverManager{
Drivers: sd,
Config: conf,
}, nil
}
func (sdm *StorageDriverManager) IsDrivers() bool {
return len(sdm.Drivers) > 0
}
// GetVolumeMapping performs storage introspection and
// returns a listing of block devices from the guest
func (sdm *StorageDriverManager) GetVolumeMapping() ([]*BlockDevice, error) {
var allBlockDevices []*BlockDevice
for _, driver := range sdm.Drivers {
blockDevices, err := driver.GetVolumeMapping()
if err != nil {
return []*BlockDevice{}, fmt.Errorf("Error: %s: %s", ErrDriverBlockDeviceDiscovery, err)
}
if len(blockDevices) > 0 {
for _, blockDevice := range blockDevices {
allBlockDevices = append(allBlockDevices, blockDevice)
}
}
}
return allBlockDevices, nil
}
func (sdm *StorageDriverManager) GetInstance() ([]*Instance, error) {
var allInstances []*Instance
for _, driver := range sdm.Drivers {
instance, err := driver.GetInstance()
if err != nil {
return nil, fmt.Errorf("Error: %s: %s", ErrDriverInstanceDiscovery, err)
}
allInstances = append(allInstances, instance)
}
return allInstances, nil
}
func (sdm *StorageDriverManager) GetVolume(volumeID, volumeName string) ([]*Volume, error) {
var allVolumes []*Volume
for _, driver := range sdm.Drivers {
volumes, err := driver.GetVolume(volumeID, volumeName)
if err != nil {
return []*Volume{}, fmt.Errorf("Error: %s: %s", ErrDriverVolumeDiscovery, err)
}
if len(volumes) > 0 {
for _, volume := range volumes {
allVolumes = append(allVolumes, volume)
}
}
}
return allVolumes, nil
}
func (sdm *StorageDriverManager) GetSnapshot(volumeID, snapshotID, snapshotName string) ([]*Snapshot, error) {
var allSnapshots []*Snapshot
for _, driver := range sdm.Drivers {
snapshots, err := driver.GetSnapshot(volumeID, snapshotID, snapshotName)
if err != nil {
return nil, fmt.Errorf("Error: %s: %s", ErrDriverSnapshotDiscovery, err)
}
if len(snapshots) > 0 {
for _, snapshot := range snapshots {
allSnapshots = append(allSnapshots, snapshot)
}
}
}
return allSnapshots, nil
}
func (sdm *StorageDriverManager) CreateSnapshot(runAsync bool, snapshotName, volumeID, description string) ([]*Snapshot, error) {
if len(sdm.Drivers) > 1 {
return nil, ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
snapshot, err := driver.CreateSnapshot(runAsync, snapshotName, volumeID, description)
if err != nil {
return nil, err
}
return snapshot, nil
}
return nil, nil
}
func (sdm *StorageDriverManager) RemoveSnapshot(snapshotID string) error {
if len(sdm.Drivers) > 1 {
return ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
err := driver.RemoveSnapshot(snapshotID)
if err != nil {
return err
}
}
return nil
}
func (sdm *StorageDriverManager) CreateVolume(runAsync bool, volumeName string, volumeID, snapshotID string, volumeType string, IOPS int64, size int64, availabilityZone string) (*Volume, error) {
if len(sdm.Drivers) > 1 {
return &Volume{}, ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
var minSize int
var err error
minVolSize := os.Getenv("REXRAY_MINVOLSIZE")
if size != 0 && minVolSize != "" {
minSize, err = strconv.Atoi(os.Getenv("REXRAY_MINVOLSIZE"))
if err != nil {
return &Volume{}, err
}
}
if minSize > 0 && int64(minSize) > size {
size = int64(minSize)
}
volume, err := driver.CreateVolume(runAsync, volumeName, volumeID, snapshotID, volumeType, IOPS, size, availabilityZone)
if err != nil {
return &Volume{}, err
}
return volume, nil
}
return &Volume{}, nil
}
func (sdm *StorageDriverManager) RemoveVolume(volumeID string) error {
if len(sdm.Drivers) > 1 {
return ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
err := driver.RemoveVolume(volumeID)
if err != nil {
return err
}
}
return nil
}
func (sdm *StorageDriverManager) AttachVolume(runAsync bool, volumeID string, instanceID string) ([]*VolumeAttachment, error) {
if len(sdm.Drivers) > 1 {
return []*VolumeAttachment{}, ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
if instanceID == "" {
instance, err := driver.GetInstance()
if err != nil {
return []*VolumeAttachment{}, fmt.Errorf("Error: %s: %s", ErrDriverInstanceDiscovery, err)
}
instanceID = instance.InstanceID
}
volumeAttachment, err := driver.AttachVolume(runAsync, volumeID, instanceID)
if err != nil {
return []*VolumeAttachment{}, err
}
return volumeAttachment, nil
}
return []*VolumeAttachment{}, nil
}
func (sdm *StorageDriverManager) DetachVolume(runAsync bool, volumeID string, instanceID string) error {
if len(sdm.Drivers) > 1 {
return ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
if instanceID == "" {
instance, err := driver.GetInstance()
if err != nil {
fmt.Errorf("Error: %s: %s", ErrDriverInstanceDiscovery, err)
}
instanceID = instance.InstanceID
}
err := driver.DetachVolume(runAsync, volumeID, instanceID)
if err != nil {
return err
}
return nil
}
return nil
}
func (sdm *StorageDriverManager) GetVolumeAttach(volumeID string, instanceID string) ([]*VolumeAttachment, error) {
if len(sdm.Drivers) > 1 {
return []*VolumeAttachment{}, ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
volumeAttachments, err := driver.GetVolumeAttach(volumeID, instanceID)
if err != nil {
return []*VolumeAttachment{}, err
}
return volumeAttachments, nil
}
return []*VolumeAttachment{}, nil
}
func (sdm *StorageDriverManager) CopySnapshot(runAsync bool, volumeID, snapshotID, snapshotName, targetSnapshotName, targetRegion string) (*Snapshot, error) {
if len(sdm.Drivers) > 1 {
return nil, ErrMultipleDriversDetected
}
for _, driver := range sdm.Drivers {
snapshot, err := driver.CopySnapshot(runAsync, volumeID, snapshotID, snapshotName, targetSnapshotName, targetRegion)
if err != nil {
return nil, err
}
return snapshot, nil
}
return nil, nil
}