/
vfs_executor.go
223 lines (179 loc) · 4.25 KB
/
vfs_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
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
package executor
import (
"bufio"
"io/ioutil"
"os"
"path"
"regexp"
"strconv"
"sync"
log "github.com/sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"
"github.com/akutz/gotil"
"github.com/rexray/rexray/libstorage/api/registry"
"github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/api/utils"
"github.com/rexray/rexray/libstorage/drivers/storage/vfs"
)
var (
devFileLocks = map[string]*sync.RWMutex{}
devFileLocksRWL = &sync.RWMutex{}
)
type driver struct {
config gofig.Config
rootDir string
devFilePath string
}
func init() {
registry.RegisterStorageExecutor(vfs.Name, newDriver)
}
func newDriver() types.StorageExecutor {
return &driver{}
}
func (d *driver) Name() string {
return vfs.Name
}
func (d *driver) Supported(
ctx types.Context,
opts types.Store) (bool, error) {
return true, nil
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
d.config = config
d.rootDir = vfs.RootDir(config)
d.devFilePath = vfs.DeviceFilePath(config)
if !gotil.FileExists(d.devFilePath) {
os.MkdirAll(path.Dir(d.devFilePath), 0755)
err := ioutil.WriteFile(d.devFilePath, initialDeviceFile, 0644)
if err != nil {
return err
}
}
devFileLocksRWL.Lock()
defer devFileLocksRWL.Unlock()
devFileLocks[d.devFilePath] = &sync.RWMutex{}
return nil
}
// Mount mounts a device to a specified path.
func (d *driver) Mount(
ctx types.Context,
deviceName, mountPoint string,
opts *types.DeviceMountOpts) error {
return os.MkdirAll(mountPoint, 0755)
}
// Unmount unmounts the underlying device from the specified path.
func (d *driver) Unmount(
ctx types.Context,
mountPoint string,
opts types.Store) error {
if !gotil.FileExists(mountPoint) {
return os.ErrNotExist
}
return os.RemoveAll(mountPoint)
}
// InstanceID returns the local system's InstanceID.
func (d *driver) InstanceID(
ctx types.Context,
opts types.Store) (*types.InstanceID, error) {
iid := &types.InstanceID{Driver: vfs.Name}
hostName, err := utils.HostName()
if err != nil {
return nil, err
}
if ok, _ := strconv.ParseBool(os.Getenv("VFS_INSTANCEID_USE_FIELDS")); ok {
iid.ID = hostName
iid.Fields = map[string]string{"region": "east"}
return iid, nil
}
if err := iid.MarshalMetadata(hostName); err != nil {
return nil, err
}
return iid, nil
}
var (
avaiDevRX = regexp.MustCompile(`^(/dev/xvd[a-z])$`)
)
// NextDevice returns the next available device.
func (d *driver) NextDevice(
ctx types.Context,
opts types.Store) (string, error) {
var devFileRWL *sync.RWMutex
func() {
devFileLocksRWL.RLock()
defer devFileLocksRWL.RUnlock()
devFileRWL = devFileLocks[d.devFilePath]
}()
devFileRWL.Lock()
defer devFileRWL.Unlock()
if !gotil.FileExists(d.devFilePath) {
return "", goof.New("device file missing")
}
f, err := os.Open(d.devFilePath)
if err != nil {
return "", err
}
defer f.Close()
scn := bufio.NewScanner(f)
for {
if !scn.Scan() {
break
}
m := avaiDevRX.FindSubmatch(scn.Bytes())
if len(m) == 0 {
continue
}
return string(m[1]), nil
}
return "", goof.New("no available devices")
}
var (
devRX = regexp.MustCompile(`^(/dev/xvd[a-z])(?:=(.+))?$`)
)
// LocalDevices returns a map of the system's local devices.
func (d *driver) LocalDevices(
ctx types.Context,
opts *types.LocalDevicesOpts) (*types.LocalDevices, error) {
ctx.WithFields(log.Fields{
"vfs.root": d.rootDir,
"dev.path": d.devFilePath,
}).Debug("config info")
var devFileRWL *sync.RWMutex
func() {
devFileLocksRWL.RLock()
defer devFileLocksRWL.RUnlock()
devFileRWL = devFileLocks[d.devFilePath]
}()
devFileRWL.Lock()
defer devFileRWL.Unlock()
if !gotil.FileExists(d.devFilePath) {
return nil, goof.New("device file missing")
}
f, err := os.Open(d.devFilePath)
if err != nil {
return nil, err
}
defer f.Close()
localDevs := map[string]string{}
scn := bufio.NewScanner(f)
for {
if !scn.Scan() {
break
}
m := devRX.FindSubmatch(scn.Bytes())
if len(m) == 0 {
continue
}
dev := m[1]
var mountPoint []byte
if len(m) > 2 {
mountPoint = m[2]
}
localDevs[string(dev)] = string(mountPoint)
}
return &types.LocalDevices{Driver: vfs.Name, DeviceMap: localDevs}, nil
}
var initialDeviceFile = []byte(`/dev/xvda
/dev/xvdb
/dev/xvdc
/dev/xvdd`)