/
dobs_executor.go
86 lines (71 loc) · 1.92 KB
/
dobs_executor.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
package executor
import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
gofig "github.com/akutz/gofig/types"
"github.com/rexray/rexray/libstorage/api/registry"
"github.com/rexray/rexray/libstorage/api/types"
do "github.com/rexray/rexray/libstorage/drivers/storage/dobs"
doUtils "github.com/rexray/rexray/libstorage/drivers/storage/dobs/utils"
)
var (
diskPrefix = regexp.MustCompile(`^` + do.VolumePrefix + `(.*)`)
diskSuffix = regexp.MustCompile("part-.*$")
)
type driver struct {
config gofig.Config
}
func init() {
registry.RegisterStorageExecutor(do.Name, newDriver)
}
func newDriver() types.StorageExecutor {
return &driver{}
}
func (d *driver) Name() string {
return do.Name
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
d.config = config
return nil
}
func (d *driver) InstanceID(
ctx types.Context, opts types.Store) (*types.InstanceID, error) {
return doUtils.InstanceID(ctx)
}
func (d *driver) NextDevice(
ctx types.Context, opts types.Store) (string, error) {
return "", types.ErrNotImplemented
}
func (d *driver) LocalDevices(
ctx types.Context,
opts *types.LocalDevicesOpts) (*types.LocalDevices, error) {
deviceMap := map[string]string{}
diskIDPath := "/dev/disk/by-id"
dir, _ := ioutil.ReadDir(diskIDPath)
for _, device := range dir {
switch {
case !diskPrefix.MatchString(device.Name()):
continue
case diskSuffix.MatchString(device.Name()):
continue
case diskPrefix.MatchString(device.Name()):
volumeName := diskPrefix.FindStringSubmatch(device.Name())[1]
devPath, err := filepath.EvalSymlinks(
fmt.Sprintf("%s/%s", diskIDPath, device.Name()))
if err != nil {
return nil, err
}
deviceMap[volumeName] = devPath
}
}
ld := &types.LocalDevices{Driver: d.Name()}
if len(deviceMap) > 0 {
ld.DeviceMap = deviceMap
}
return ld, nil
}
func (d *driver) Supported(ctx types.Context, opts types.Store) (bool, error) {
return doUtils.IsDroplet(ctx)
}