This repository has been archived by the owner on Jan 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
driver.go
276 lines (238 loc) · 6.79 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
azure "github.com/Azure/azure-sdk-for-go/storage"
log "github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
)
type volumeDriver struct {
m sync.Mutex
cl azure.FileServiceClient
meta *metadataDriver
accountName string
accountKey string
mountpoint string
removeShares bool
}
func newVolumeDriver(accountName, accountKey, mountpoint, metadataRoot string, removeShares bool) (*volumeDriver, error) {
storageClient, err := azure.NewBasicClient(accountName, accountKey)
if err != nil {
return nil, fmt.Errorf("error creating azure client: %v", err)
}
metaDriver, err := newMetadataDriver(metadataRoot)
if err != nil {
return nil, fmt.Errorf("cannot initialize metadata driver: %v", err)
}
return &volumeDriver{
cl: storageClient.GetFileService(),
meta: metaDriver,
accountName: accountName,
accountKey: accountKey,
mountpoint: mountpoint,
removeShares: removeShares,
}, nil
}
func (v *volumeDriver) Create(req volume.Request) (resp volume.Response) {
v.m.Lock()
defer v.m.Unlock()
logctx := log.WithFields(log.Fields{
"operation": "create",
"name": req.Name,
"options": req.Options})
volMeta, err := v.meta.Validate(req.Options)
if err != nil {
resp.Err = fmt.Sprintf("error validating metadata: %v", err)
logctx.Error(resp.Err)
return
}
// Additional volume metadata
volMeta.Account = v.accountName
volMeta.CreatedAt = time.Now().UTC()
share := req.Options["share"]
if share == "" {
resp.Err = "missing volume option: 'share'"
logctx.Error(resp.Err)
return
}
logctx.Debug("request accepted")
// Create azure file share
if ok, err := v.cl.CreateShareIfNotExists(share); err != nil {
resp.Err = fmt.Sprintf("error creating azure file share: %v", err)
logctx.Error(resp.Err)
return
} else if ok {
logctx.Infof("created azure file share %q", share)
}
// Save volume metadata
if err := v.meta.Set(req.Name, volMeta); err != nil {
resp.Err = fmt.Sprintf("error saving metadata: %v", err)
logctx.Error(resp.Err)
return
}
return
}
func (v *volumeDriver) Path(req volume.Request) (resp volume.Response) {
v.m.Lock()
defer v.m.Unlock()
log.WithFields(log.Fields{
"operation": "path", "name": req.Name,
}).Debug("request accepted")
resp.Mountpoint = v.pathForVolume(req.Name)
return
}
func (v *volumeDriver) Mount(req volume.Request) (resp volume.Response) {
v.m.Lock()
defer v.m.Unlock()
logctx := log.WithFields(log.Fields{
"operation": "mount",
"name": req.Name,
})
logctx.Debug("request accepted")
path := v.pathForVolume(req.Name)
if err := os.MkdirAll(path, 0700); err != nil {
resp.Err = fmt.Sprintf("could not create mount point: %v", err)
logctx.Error(resp.Err)
return
}
meta, err := v.meta.Get(req.Name)
if err != nil {
resp.Err = fmt.Sprintf("could not fetch metadata: %v", err)
logctx.Error(resp.Err)
return
}
if meta.Account != v.accountName {
resp.Err = fmt.Sprintf("volume hosted on a different account ('%s') cannot mount", meta.Account)
logctx.Error(resp.Err)
return
}
if err := mount(v.accountName, v.accountKey, meta.Options.Share, path); err != nil {
resp.Err = err.Error()
logctx.Error(resp.Err)
return
}
resp.Mountpoint = path
return
}
func (v *volumeDriver) Unmount(req volume.Request) (resp volume.Response) {
v.m.Lock()
defer v.m.Unlock()
logctx := log.WithFields(log.Fields{
"operation": "unmount",
"name": req.Name,
})
logctx.Debug("request accepted")
path := v.pathForVolume(req.Name)
if err := unmount(path); err != nil {
resp.Err = err.Error()
logctx.Error(resp.Err)
return
}
if err := os.RemoveAll(path); err != nil {
resp.Err = fmt.Sprintf("error removing mountpoint: %v", err)
logctx.Error(resp.Err)
return
}
return
}
func (v *volumeDriver) Remove(req volume.Request) (resp volume.Response) {
v.m.Lock()
defer v.m.Unlock()
logctx := log.WithFields(log.Fields{
"operation": "remove",
"name": req.Name,
})
logctx.Debug("request accepted")
meta, err := v.meta.Get(req.Name)
if err != nil {
resp.Err = fmt.Sprintf("could not fetch metadata: %v", err)
logctx.Error(resp.Err)
return
}
share := meta.Options.Share
if v.removeShares {
if ok, err := v.cl.DeleteShareIfExists(share); err != nil {
resp.Err = fmt.Sprintf("error removing azure file share %q: %v", share, err)
logctx.Error(resp.Err)
return
} else if ok {
logctx.Infof("removed azure file share %q", share)
}
} else {
logctx.Debugf("not removing share %q upon volume removal", share)
}
logctx.Debug("removing volume metadata")
if err != v.meta.Delete(req.Name) {
resp.Err = err.Error()
logctx.Error(resp.Err)
return
}
return
}
func (v *volumeDriver) Get(req volume.Request) (resp volume.Response) {
v.m.Lock()
defer v.m.Unlock()
logctx := log.WithFields(log.Fields{
"operation": "get",
"name": req.Name,
})
logctx.Debug("request accepted")
_, err := v.meta.Get(req.Name)
if err != nil {
resp.Err = fmt.Sprintf("could not fetch metadata: %v", err)
logctx.Error(resp.Err)
return
}
resp.Volume = v.volumeEntry(req.Name)
return
}
func (v *volumeDriver) List(req volume.Request) (resp volume.Response) {
v.m.Lock()
defer v.m.Unlock()
logctx := log.WithFields(log.Fields{
"operation": "list",
})
logctx.Debug("request accepted")
vols, err := v.meta.List()
if err != nil {
resp.Err = fmt.Sprintf("failed to list managed volumes: %v", err)
logctx.Error(resp.Err)
return
}
for _, vn := range vols {
resp.Volumes = append(resp.Volumes, v.volumeEntry(vn))
}
logctx.Debugf("response has %d items", len(resp.Volumes))
return
}
func (v *volumeDriver) volumeEntry(name string) *volume.Volume {
return &volume.Volume{Name: name,
Mountpoint: v.pathForVolume(name)}
}
func (v *volumeDriver) pathForVolume(name string) string {
return filepath.Join(v.mountpoint, name)
}
func mount(accountName, accountKey, shareName, mountpoint string) error {
// TODO: replace with mount() syscall using docker/docker/pkg/mount
// (currently gives hard-to-debug 'invalid argument' error with the
// following arguments, my guess is, mount program does IP resolution
// and essentially passes a different set of options to system call).
cmd := exec.Command("mount", "-t", "cifs", fmt.Sprintf("//%s.file.core.windows.net/%s", accountName, shareName), mountpoint, "-o", fmt.Sprintf("vers=3.0,username=%s,password=%s,dir_mode=0777,file_mode=0777", accountName, accountKey), "--verbose")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("mount failed: %v\noutput=%q", err, out)
}
return nil
}
func unmount(mountpoint string) error {
cmd := exec.Command("umount", mountpoint)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("unmount failed: %v\noutput=%q", err, out)
}
return nil
}