/
gce_executor.go
107 lines (85 loc) · 2.32 KB
/
gce_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package executor
import (
"io/ioutil"
"path"
"regexp"
gofig "github.com/akutz/gofig/types"
"github.com/codedellemc/rexray/libstorage/api/registry"
"github.com/codedellemc/rexray/libstorage/api/types"
"github.com/codedellemc/rexray/libstorage/drivers/storage/gcepd"
gceUtils "github.com/codedellemc/rexray/libstorage/drivers/storage/gcepd/utils"
)
const (
diskIDPath = "/dev/disk/by-id"
diskPrefix = "google-"
)
// driver is the storage executor for the storage driver.
type driver struct {
config gofig.Config
}
func init() {
registry.RegisterStorageExecutor(gcepd.Name, newDriver)
}
func newDriver() types.StorageExecutor {
return &driver{}
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
d.config = config
return nil
}
func (d *driver) Name() string {
return gcepd.Name
}
// Supported returns a flag indicating whether or not the platform
// implementing the executor is valid for the host on which the executor
// resides.
func (d *driver) Supported(
ctx types.Context,
opts types.Store) (bool, error) {
return gceUtils.IsGCEInstance(ctx)
}
// InstanceID returns the instance ID from the current instance from metadata
func (d *driver) InstanceID(
ctx types.Context,
opts types.Store) (*types.InstanceID, error) {
return gceUtils.InstanceID(ctx)
}
// NextDevice returns the next available device.
func (d *driver) NextDevice(
ctx types.Context,
opts types.Store) (string, error) {
return "", types.ErrNotImplemented
}
// Retrieve device paths currently attached and/or mounted
func (d *driver) LocalDevices(
ctx types.Context,
opts *types.LocalDevicesOpts) (*types.LocalDevices, error) {
files, err := ioutil.ReadDir(diskIDPath)
if err != nil {
return nil, err
}
persistentDiskRX, err := regexp.Compile(
diskPrefix + `(` + gceUtils.DiskNameRX + `)`)
if err != nil {
return nil, err
}
attachedDisks, err := gceUtils.GetDisks(ctx)
if err != nil {
return nil, err
}
ld := &types.LocalDevices{Driver: d.Name()}
devMap := map[string]string{}
for _, f := range files {
if persistentDiskRX.MatchString(f.Name()) {
matches := persistentDiskRX.FindStringSubmatch(f.Name())
volID := matches[1]
if _, ok := attachedDisks[volID]; ok && volID != "" {
devMap[volID] = path.Join(diskIDPath, f.Name())
}
}
}
if len(devMap) > 0 {
ld.DeviceMap = devMap
}
return ld, nil
}