-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.go
275 lines (206 loc) · 6.2 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
// +build linux
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/davecgh/go-spew/spew"
dockerVolume "github.com/docker/go-plugins-helpers/volume"
)
type sharedVolumeDriver struct {
volumes map[string]*sharedVolume
mutex *sync.Mutex
root string
hostname string
}
func newSharedFSDriver(root string) sharedVolumeDriver {
hostname, _ := os.Hostname()
driver := sharedVolumeDriver{
volumes: make(map[string]*sharedVolume),
mutex: &sync.Mutex{},
root: root,
hostname: hostname,
}
// Discover volumes that are already in use by the current node
driver.Discover()
go driver.MaintenanceRoutine()
return driver
}
func (driver sharedVolumeDriver) Capabilities() *dockerVolume.CapabilitiesResponse {
return &dockerVolume.CapabilitiesResponse{
Capabilities: dockerVolume.Capability{
Scope: "global",
},
}
}
func (driver sharedVolumeDriver) Create(request *dockerVolume.CreateRequest) error {
// var volume *sharedVolume
log.Infof("Create: %s, %v", request.Name, request.Options)
// Concurrency lock
driver.mutex.Lock()
defer driver.mutex.Unlock()
// Is this volume already registered?
if _, ok := driver.volumes[request.Name]; ok {
// Path exists and it is already part of the bookkeeping
message := fmt.Sprintf("Volume %s already exists.", request.Name)
log.Warning(message)
return nil
}
// Register a new volume
volume := driver.newVolume(request.Name, request.Options)
var err error
// Does the volume exist already?
if err = volume.loadMetadata(); os.IsNotExist(err) {
// The volume does not yet exist
// Create volume (physical folder structure)
if err = volume.createDirectoryStructure(); err != nil {
return err
}
// Save the volume metadata
if err = volume.saveMetadata(); os.IsExist(err) {
// Failed to save metadata, because the file already exists
// Try to load the metadata again
err = volume.loadMetadata()
}
}
if err != nil {
return err
}
// Try to lock the volume
if err := volume.lock(); err != nil {
// If the volume cannot be locked, we risk that other nodes may delete it
return err
}
// Register in internal bookkeeping
driver.volumes[request.Name] = volume
if *debug {
spew.Dump(driver.volumes)
}
return nil
}
func (driver sharedVolumeDriver) Discover() {
// Look for existing volumes
if files, err := ioutil.ReadDir(*root); err == nil {
for _, file := range files {
filename := file.Name()
if !file.IsDir() {
// Only intersted in directories.
continue
}
// Is this volume registered in bookkeeping already?
if volume, ok := driver.volumes[filename]; !ok {
// Try to load the volume information
volume = &sharedVolume{
Volume: &dockerVolume.Volume{
Name: filename,
Mountpoint: filepath.Join(*root, filename),
},
}
if err := volume.loadMetadata(); err != nil {
// Metadata failed to load, it's likely invalid
log.Errorf("Failed to load metadata for volume %s", volume.Name)
} else {
// If there is a lockfile add it to bookkeeping
if volume.hasLockfile() {
if err := volume.lock(); err == nil {
driver.volumes[volume.Name] = volume
log.Infof("Loaded previously attached volume %s", volume.Name)
} else {
volume.unlock()
log.Warnf("Failed to lock previously attached volume %s", volume.Name)
}
}
// Remove any mounts that belonged to us
mounts := volume.getMounts()
for _, mount := range mounts {
if mount.Hostname == *hostname {
mount.remove()
}
}
}
}
}
}
}
func (driver sharedVolumeDriver) Remove(request *dockerVolume.RemoveRequest) error {
log.Infof("Remove: %s", request.Name)
driver.mutex.Lock()
defer driver.mutex.Unlock()
if volume, ok := driver.volumes[request.Name]; ok {
err := volume.unlock()
if err == nil {
err = volume.delete()
}
if err == nil {
delete(driver.volumes, request.Name)
} else {
return err
}
}
return nil
}
func (driver sharedVolumeDriver) Path(request *dockerVolume.PathRequest) (*dockerVolume.PathResponse, error) {
log.Debugf("Path: %s", request.Name)
if volume, ok := driver.volumes[request.Name]; ok {
return &dockerVolume.PathResponse{
Mountpoint: volume.GetDataDir(),
}, nil
}
return nil, nil
}
func (driver sharedVolumeDriver) Mount(request *dockerVolume.MountRequest) (*dockerVolume.MountResponse, error) {
log.Infof("Mount: %s", request.Name)
if volume, ok := driver.volumes[request.Name]; ok {
if err := volume.mount(request.ID); err != nil {
return nil, fmt.Errorf("Failed to mount volume: %s", err.Error())
}
return &dockerVolume.MountResponse{
Mountpoint: volume.GetDataDir(),
}, nil
}
message := fmt.Sprintf("Cannot mount volume %s as it does not exist in the bookkeeping", request.Name)
log.Error(message)
return nil, errors.New(message)
}
func (driver sharedVolumeDriver) Unmount(request *dockerVolume.UnmountRequest) error {
log.Infof("Unmount: %s", request.Name)
if volume, ok := driver.volumes[request.Name]; ok {
err := volume.unmount(request.ID)
return err
}
return nil
}
func (driver sharedVolumeDriver) Get(request *dockerVolume.GetRequest) (*dockerVolume.GetResponse, error) {
log.Infof("Get: %s", request.Name)
if volume, ok := driver.volumes[request.Name]; ok {
responseVolume := &dockerVolume.Volume{
Name: volume.Name,
Mountpoint: volume.GetDataDir(),
CreatedAt: volume.CreatedAt,
Status: make(map[string]interface{}),
}
responseVolume.Status["protected"] = volume.Protected
responseVolume.Status["exclusive"] = volume.Exclusive
responseVolume.Status["locks"] = volume.getLocks()
responseVolume.Status["mounts"] = volume.getMounts()
return &dockerVolume.GetResponse{
Volume: responseVolume,
}, nil
}
return nil, fmt.Errorf("volume %s unknown", request.Name)
}
func (driver sharedVolumeDriver) List() (*dockerVolume.ListResponse, error) {
log.Infof("List")
volumes := []*dockerVolume.Volume{}
for _, volume := range driver.volumes {
volumes = append(volumes, &dockerVolume.Volume{
Name: volume.Name,
Mountpoint: volume.GetDataDir(),
})
}
return &dockerVolume.ListResponse{Volumes: volumes}, nil
}