forked from hpe-storage/common-host-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.go
578 lines (463 loc) · 20.5 KB
/
driver.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// (c) Copyright 2019 Hewlett Packard Enterprise Development LP
package driver
import (
"fmt"
"github.com/hpe-storage/common-host-libs/chapi2/cerrors"
"github.com/hpe-storage/common-host-libs/chapi2/fc"
"github.com/hpe-storage/common-host-libs/chapi2/host"
"github.com/hpe-storage/common-host-libs/chapi2/iscsi"
"github.com/hpe-storage/common-host-libs/chapi2/model"
"github.com/hpe-storage/common-host-libs/chapi2/mount"
"github.com/hpe-storage/common-host-libs/chapi2/multipath"
"github.com/hpe-storage/common-host-libs/chapi2/virtualdevice"
log "github.com/hpe-storage/common-host-libs/logger"
)
const (
// Shared error messages
errorMessageEmptyIqnFound = "empty iqn found"
errorMessageMultipleDevices = "multiple (%v) devices enumerated"
errorMessageMultipleDeviceObjects = "multiple device access objects provided"
errorMessageNoDeviceObject = "device access object not provided"
errorMessageNoDevicesOnHost = "no devices found on host"
errorMessageNoInitiatorsFound = "neither of iscsi or fc initiators are found on host"
errorMessageNoMountPointsFound = "no mount points found"
errorMessageNoNetworkInterfaces = "no network interfaces found on host"
errorMessageNoPartitionsOnVolume = "no partitions found on volume"
errorMessageNotYetImplemented = "not yet implemented"
errorMessageVolumeMounted = "volume mounted"
)
// Driver provides a common interface for host related operations
type Driver interface {
///////////////////////////////////////////////////////////////////////////////////////////
// Host Methods
///////////////////////////////////////////////////////////////////////////////////////////
GetHostInfo() (*model.Host, error) // GET /api/v1/hosts
GetHostInitiators() ([]*model.Initiator, error) // GET /api/v1/initiators
GetHostNetworks() ([]*model.Network, error) // GET /api/v1/networks
///////////////////////////////////////////////////////////////////////////////////////////
// Device Methods
///////////////////////////////////////////////////////////////////////////////////////////
// GET /api/v1/devices or
// GET /api/v1/devices?serial=serial
GetDevices(serialNumber string) ([]*model.Device, error)
// GET /api/v1/devices/details or
// GET /api/v1/devices/details?serial=serial
GetAllDeviceDetails(serialNumber string) ([]*model.Device, error)
// GET /api/v1/devices/{serialnumber}/partitions
GetPartitionInfo(serialNumber string) ([]*model.DevicePartition, error)
// POST /api/v1/devices
CreateDevice(publishInfo model.PublishInfo) (*model.Device, error)
// DELETE /api/v1/devices/{serialnumber}
DeleteDevice(serialNumber string) error
// PUT /api/v1/devices/{serialnumber}/actions/offline
OfflineDevice(serialNumber string) error
// PUT /api/v1/devices/{serialnumber}/filesystem/{filesystem}
CreateFileSystem(serialNumber string, filesystem string) error
///////////////////////////////////////////////////////////////////////////////////////////
// Mount Methods
///////////////////////////////////////////////////////////////////////////////////////////
// GET /api/v1/mounts or
// GET /api/v1/mounts?serial=serial
GetMounts(serialNumber string) ([]*model.Mount, error)
// GET /api/v1/mounts/details or filter by serial using
// GET /api/v1/mounts/details?serial=serial or filter by serial and specific mount using
// GET /api/v1/mounts/details?serial=serial,mountId=mount
GetAllMountDetails(serialNumber, mountPointID string) ([]*model.Mount, error)
// POST /api/v1/mounts
CreateMount(serialNumber string, mountPoint string, fsOptions *model.FileSystemOptions) (*model.Mount, error)
// DELETE /api/v1/mounts/{mountId}
DeleteMount(serialNumber, mountPointID string) error
// TODO: check with George/Suneeth on this
// POST /api/v1/mounts/bind
CreateBindMount(sourceMount string, targetMount string, bindType string) (*model.Mount, error)
}
// ChapiServer ... Implements the "Driver" interfaces
type ChapiServer struct {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Host methods
///////////////////////////////////////////////////////////////////////////////////////////////////
// GetHostInfo returns host name, domain, and network interfaces
func (driver *ChapiServer) GetHostInfo() (*model.Host, error) {
log.Trace(">>>>> GetHostInfo called")
defer log.Trace("<<<<< GetHostInfo")
hostPlugin := host.NewHostPlugin()
log.Info("Get Host Information")
id, err := hostPlugin.GetUuid()
if err != nil {
return nil, cerrors.NewChapiError(err)
}
log.Infof("Host UUID - %v", id)
hostName, err := hostPlugin.GetHostName()
if err != nil {
return nil, cerrors.NewChapiError(err)
}
log.Infof("Host Name - %v", hostName)
domainName, err := hostPlugin.GetDomainName()
if err != nil {
return nil, cerrors.NewChapiError(err)
}
log.Infof("Domain Name - %v", domainName)
return &model.Host{UUID: id, Name: hostName, Domain: domainName}, nil
}
// GetHostNetworks reports the networks on this host
func (driver *ChapiServer) GetHostNetworks() ([]*model.Network, error) {
log.Trace(">>>>> GetHostNetworks called")
defer log.Trace("<<<<< GetHostNetworks")
hostPlugin := host.NewHostPlugin()
log.Info("Get Host Networks")
networks, err := hostPlugin.GetNetworks()
if err != nil {
return nil, cerrors.NewChapiError(err)
}
if len(networks) == 0 {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoNetworkInterfaces)
}
driver.logNetworks(networks)
return networks, nil
}
// GetHostInitiators reports the initiators on this host
func (driver *ChapiServer) GetHostInitiators() ([]*model.Initiator, error) {
log.Trace(">>>>> GetHostInitiators called")
defer log.Trace("<<<<< GetHostInitiators")
//var inits Initiators
var inits []*model.Initiator
log.Info("Get Host Initiators")
// fetch iscsi initiator details
iscsiPlugin := iscsi.NewIscsiPlugin()
iscsiInits, err := iscsiPlugin.GetIscsiInitiators()
if err != nil {
log.Trace("Error getting iscsiInitiator: ", err)
}
// fetch fc initiator details
fcPlugin := fc.NewFcPlugin()
fcInits, err := fcPlugin.GetFcInitiators()
if err != nil {
log.Trace("Error getting FcInitiator: ", err)
}
if fcInits != nil {
inits = append(inits, fcInits)
}
if iscsiInits != nil {
inits = append(inits, iscsiInits)
}
if fcInits == nil && iscsiInits == nil {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoInitiatorsFound)
}
// Log enumerated iSCSI and FC initiators
for _, initiator := range inits {
for _, init := range initiator.Init {
log.Infof("AccessProtocol=%v, Initiator=%v", initiator.AccessProtocol, init)
}
}
return inits, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Device methods
///////////////////////////////////////////////////////////////////////////////////////////////////
// GetDevices enumerates all the Nimble volumes with basic details.
// If serialNumber is non-empty then only specified device is returned
func (driver *ChapiServer) GetDevices(serialNumber string) ([]*model.Device, error) {
log.Tracef(">>>>> GetDevices called, serialNumber=%v", serialNumber)
defer log.Trace("<<<<< GetDevices")
multipathPlugin := multipath.NewMultipathPlugin()
log.Infof("Get Devices, serialNumber=%v", serialNumber)
// Enumerate all the Nimble volumes on this host (basic details only)
devices, err := multipathPlugin.GetDevices(serialNumber)
if err != nil {
return nil, cerrors.NewChapiError(err)
}
// Fail request if no Nimble devices found on this host
if len(devices) == 0 {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoDevicesOnHost)
}
// Log enumerated device serial numbers
for _, device := range devices {
log.Infof("Device SerialNumber=%v", device.SerialNumber)
}
return devices, nil
}
// GetAllDeviceDetails enumerates all the Nimble volumes with detailed information.
// If serialNumber is non-empty then only specified device is returned
func (driver *ChapiServer) GetAllDeviceDetails(serialNumber string) ([]*model.Device, error) {
log.Tracef(">>>>> GetAllDeviceDetails called, serialNumber=%v", serialNumber)
defer log.Trace("<<<<< GetAllDeviceDetails")
multipathPlugin := multipath.NewMultipathPlugin()
log.Infof("Get All Device Details, serialNumber=%v", serialNumber)
// Enumerate all the Nimble volumes on this host (full details)
devices, err := multipathPlugin.GetAllDeviceDetails(serialNumber)
if err != nil {
return nil, cerrors.NewChapiError(err)
}
// Fail request if no Nimble devices found on this host
if len(devices) == 0 {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoDevicesOnHost)
}
// Log enumerated device details
driver.logDeviceArrayDetails(devices)
return devices, nil
}
// GetPartitionInfo reports the partitions on the provided device
func (driver *ChapiServer) GetPartitionInfo(serialNumber string) ([]*model.DevicePartition, error) {
log.Tracef(">>>>> GetPartitionInfo called, serialNumber=%v", serialNumber)
defer log.Trace("<<<<< GetPartitionInfo")
multipathPlugin := multipath.NewMultipathPlugin()
log.Infof("Get Partition Information, serialNumber=%v", serialNumber)
// Enumerate all the Nimble volume's partition
partitions, err := multipathPlugin.GetPartitionInfo(serialNumber)
if err != nil {
return nil, cerrors.NewChapiError(err)
}
// Fail request if no partitions found on this host
if len(partitions) == 0 {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoPartitionsOnVolume)
}
// Log enumerated partition details
for _, partition := range partitions {
log.Infof("Partition Name=%v, PartitionType=%v, Size=%v", partition.Name, partition.PartitionType, partition.Size)
}
return partitions, nil
}
// CreateDevice will attach device on this host based on the details provided
func (driver *ChapiServer) CreateDevice(publishInfo model.PublishInfo) (*model.Device, error) {
log.Tracef(">>>>> CreateDevice called, publishInfo=%v", publishInfo)
defer log.Trace("<<<<< CreateDevice")
log.Info("Create Device")
// Invalid request if no device access object provided
if (publishInfo.BlockDev == nil) && (publishInfo.VirtualDev == nil) {
err := cerrors.NewChapiError(cerrors.InvalidArgument, errorMessageNoDeviceObject)
log.Error(err)
return nil, err
}
// Invalid request if multiple device access objects provided
if (publishInfo.BlockDev != nil) && (publishInfo.VirtualDev != nil) {
err := cerrors.NewChapiError(cerrors.InvalidArgument, errorMessageMultipleDeviceObjects)
log.Error(err)
return nil, err
}
// Attach the virtual device
if publishInfo.VirtualDev != nil {
virtualDevPlugin := virtualdevice.NewVirtualDevPlugin()
_ = virtualDevPlugin // Avoid "declared and not used" errors until feature is implemented
return nil, cerrors.NewChapiError(cerrors.Unimplemented, errorMessageNotYetImplemented)
}
// Attach the block device
multipathPlugin := multipath.NewMultipathPlugin()
device, err := multipathPlugin.AttachDevice(publishInfo.SerialNumber, *publishInfo.BlockDev)
if err != nil {
return nil, err
}
driver.logDeviceDetails(device)
return device, nil
}
// DeleteDevice will delete the given device from the host
func (driver *ChapiServer) DeleteDevice(serialNumber string) error {
log.Tracef(">>>>> DeleteDevice called, serialNumber=%v", serialNumber)
defer log.Trace("<<<<< DeleteDevice")
multipathPlugin := multipath.NewMultipathPlugin()
log.Infof("Delete Device, serialNumber=%v", serialNumber)
// TODO - handle VirtualDev vs BlockDev
// Find the device serial number details. If the device is not present on this host (i.e.
// cerrors.NotFound), there is no device to detach so we return no error.
devices, err := multipathPlugin.GetAllDeviceDetails(serialNumber)
if len(devices) == 0 {
log.Infof("Serial number %v not present, returning success", serialNumber)
return nil
} else if err != nil {
return err
}
// Fail request if device is mounted. We only allow deleting the device if it isn't already
// mounted. Caller should dismount the device before attempting to delete the device.
if mounts, _ := driver.GetMounts(serialNumber); len(mounts) > 0 {
err = cerrors.NewChapiError(cerrors.PermissionDenied, errorMessageVolumeMounted)
log.Error(err)
return err
}
// Detach the block device
driver.logDeviceDetails(devices[0])
if err := multipathPlugin.DetachDevice(*devices[0]); err != nil {
return err
}
// Success!!!
log.Infof("Device Deleted, SerialNumber=%v", serialNumber)
return nil
}
// OfflineDevice will offline the given device from the host
func (driver *ChapiServer) OfflineDevice(serialNumber string) error {
log.Tracef(">>>>> OfflineDevice called, serialNumber=%v", serialNumber)
defer log.Trace("<<<<< OfflineDevice")
multipathPlugin := multipath.NewMultipathPlugin()
log.Infof("Offline Device, serialNumber=%v", serialNumber)
// Enumerate basic details for the serial number
device, err := driver.getSingleDeviceSummary(serialNumber)
if err != nil {
return err
}
// Offline the device
if err := multipathPlugin.OfflineDevice(*device); err != nil {
return err
}
// Success!!!
log.Infof("Device Offlined, SerialNumber=%v", serialNumber)
return nil
}
// CreateFileSystem writes the given file system to the device with the given serial number
func (driver *ChapiServer) CreateFileSystem(serialNumber string, filesystem string) error {
log.Tracef(">>>>> CreateFileSystem called, serialNumber=%v, filesystem=%v", serialNumber, filesystem)
defer log.Trace("<<<<< CreateFileSystem")
multipathPlugin := multipath.NewMultipathPlugin()
log.Infof("Create File System, serialNumber=%v, filesystem=%v", serialNumber, filesystem)
// Enumerate basic details for the serial number
device, err := driver.getSingleDeviceSummary(serialNumber)
if err != nil {
return err
}
// Format the device
driver.logDeviceDetails(device)
return multipathPlugin.CreateFileSystem(*device, filesystem)
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Mount point methods
///////////////////////////////////////////////////////////////////////////////////////////////////
// GetMounts reports all mounts on this host for the specified Nimble volume
func (driver *ChapiServer) GetMounts(serialNumber string) ([]*model.Mount, error) {
log.Tracef(">>>>> GetMounts called, serialNumber=%v", serialNumber)
defer log.Trace("<<<<< GetMounts")
log.Infof("Get Mounts, serialNumber=%v", serialNumber)
// Route request to the mount package to get the mounts
mountPlugin := mount.NewMounter()
mounts, err := mountPlugin.GetMounts(serialNumber)
if err != nil {
return nil, err
}
// Fail request if no mount points detected
if len(mounts) == 0 {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoMountPointsFound)
}
driver.logMountArray(mounts)
return mounts, nil
}
// GetAllMountDetails enumerates the specified mount point ID
func (driver *ChapiServer) GetAllMountDetails(serialNumber string, mountPointID string) ([]*model.Mount, error) {
log.Tracef(">>>>> GetAllMountDetails called, serialNumber=%v, mountPointID=%v", serialNumber, mountPointID)
defer log.Trace("<<<<< GetAllMountDetails")
log.Infof("Get All Mount Details, serialNumber=%v, mountPointID=%v", serialNumber, mountPointID)
// Route request to the mount package to get the mounts
mountPlugin := mount.NewMounter()
mounts, err := mountPlugin.GetAllMountDetails(serialNumber, mountPointID)
if err != nil {
return nil, err
}
// Fail request if no mount points detected
if len(mounts) == 0 {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoMountPointsFound)
}
driver.logMountArray(mounts)
return mounts, nil
}
// CreateMount mounts the given device to the given mount point
func (driver *ChapiServer) CreateMount(serialNumber string, mountPoint string, fsOptions *model.FileSystemOptions) (*model.Mount, error) {
log.Tracef(">>>>> CreateMount called, serialNumber=%v, mountPoint=%v, fsOptions=%v", serialNumber, mountPoint, fsOptions)
defer log.Trace("<<<<< CreateMount")
log.Infof("Create Mount, serialNumber=%v, mountPoint=%v", serialNumber, mountPoint)
// Route request to the mount package to create the mount point
mountPlugin := mount.NewMounter()
mount, err := mountPlugin.CreateMount(serialNumber, mountPoint, fsOptions)
if err != nil {
return nil, err
}
driver.logMount(mount)
return mount, nil
}
// DeleteMount unmounts the given mount point, serialNumber can be optional in the body
func (driver *ChapiServer) DeleteMount(serialNumber string, mountPointId string) error {
log.Tracef(">>>>> DeleteMount called, serialNumber=%v, mountPointID=%v", serialNumber, mountPointId)
defer log.Trace("<<<<< DeleteMount")
log.Infof("Delete Mount, serialNumber=%v, mountPointId=%v", serialNumber, mountPointId)
// Route request to the mount package to delete the mount point
mountPlugin := mount.NewMounter()
if err := mountPlugin.DeleteMount(serialNumber, mountPointId); err != nil {
return err
}
// Success!!!
log.Infof("Mount Point ID %v successfully deleted", mountPointId)
return nil
}
// CreateBindMount creates the given bind mount
func (driver *ChapiServer) CreateBindMount(sourceMount string, targetMount string, bindType string) (*model.Mount, error) {
log.Tracef(">>>>> CreateBindMount called, sourceMount=%s, targetMount=%s bindType=%s", sourceMount, targetMount, bindType)
defer log.Trace("<<<<< CreateBindMount")
log.Infof("Create Bind Mount, sourceMount=%v, targetMount=%v, bindType=%v", sourceMount, targetMount, bindType)
return nil, cerrors.NewChapiError(cerrors.Unimplemented, errorMessageNotYetImplemented)
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Internal helper methods
///////////////////////////////////////////////////////////////////////////////////////////////////
// getSingleDeviceSummary uses the driver.GetDevices() endpoint to query basic summary details
// about the given serial number. If multiple volumes share that serial number (e.g. multipath
// not configured properly), this routine will fail the request.
func (driver *ChapiServer) getSingleDeviceSummary(serialNumber string) (*model.Device, error) {
log.Tracef(">>>>> getSingleDeviceSummary called, serialNumber=%v", serialNumber)
defer log.Trace("<<<<< getSingleDeviceSummary")
multipathPlugin := multipath.NewMultipathPlugin()
// Enumerate the device details for the provided serial number
devices, err := multipathPlugin.GetDevices(serialNumber)
if err != nil {
return nil, cerrors.NewChapiError(err)
}
// Fail request if no Nimble devices found on this host
if len(devices) == 0 {
return nil, cerrors.NewChapiError(cerrors.NotFound, errorMessageNoDevicesOnHost)
}
// If we did not enumerate a single volume, with the provided serial number, the host is likely
// misconfigured (e.g. multipath misconfigured)
if len(devices) != 1 {
err = cerrors.NewChapiErrorf(cerrors.Internal, errorMessageMultipleDevices, len(devices))
log.Errorf(err.Error())
return nil, cerrors.NewChapiError(err)
}
// Return the single enumerated volume
return devices[0], nil
}
// logNetworks records the host NIC details, one line for NIC, to the information log
func (driver *ChapiServer) logNetworks(networks []*model.Network) {
for _, network := range networks {
log.Infof("Network=%v, AddressV4=%v, MaskV4=%v, Up=%v", network.Name, network.AddressV4, network.MaskV4, network.Up)
}
}
// logDeviceArrayDetails records the device array details to the information log
func (driver *ChapiServer) logDeviceArrayDetails(devices []*model.Device) {
for _, device := range devices {
driver.logDeviceDetails(device)
}
}
// logDeviceDetails records the device details to the information log
func (driver *ChapiServer) logDeviceDetails(device *model.Device) {
if device == nil {
log.Error("logDeviceDetails called with nil device")
return
}
msg := fmt.Sprintf("Device SerialNumber=%v, Pathname=%v, Size=%v, State=%v", device.SerialNumber, device.Pathname, device.Size, device.State)
if device.IscsiTarget != nil {
msg += fmt.Sprintf(", IscsiTargetName=%v, TargetScope=%v", device.IscsiTarget.Name, device.IscsiTarget.TargetScope)
}
log.Infoln(msg)
}
// logMountArray records the mount details to the information log
func (driver *ChapiServer) logMountArray(mounts []*model.Mount) {
for _, mount := range mounts {
driver.logMount(mount)
}
}
// logMount records the single mount details to the information log
func (driver *ChapiServer) logMount(mount *model.Mount) {
if mount == nil {
log.Error("logMount called with nil mount")
return
}
msg := fmt.Sprintf("Mount ID=%v", mount.ID)
if (mount.MountPoint != "") || (mount.SerialNumber != "") {
msg += fmt.Sprintf(", MountPoint=%v, SerialNumber=%v", mount.MountPoint, mount.SerialNumber)
}
log.Infoln(msg)
}