forked from vieux/docker-volume-sshfs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdriver.go
406 lines (347 loc) · 9.9 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/docker/go-plugins-helpers/volume"
log "github.com/sirupsen/logrus"
)
const (
// VolumeDirMode sets the permissions for the volume directory
VolumeDirMode = 0700
// VolumeFileMode sets permissions for the volume files
VolumeFileMode = 0600
)
type sshfsVolume struct {
// Name of the volume
Name string
// Path to where on the host system the mount is created
MountPoint string
// When it was created
CreatedAt string
// Number of containers that are using the volume
RefCount int
// sshfs options
Options []string
SSHCmd string
// File that contains the private key
IdentityFile string
// Should the private key be ephemeral
// Shall it be removed after the first mount
Ephemeral bool
// Password used to authenticate
Password string
// Port on which the volume will try to connect with SSH
Port string
}
type sshfsDriver struct {
mutex *sync.Mutex
volumes map[string]*sshfsVolume
volumePath string
statePath string
}
func (v *sshfsVolume) setupOptions(options map[string]string) error {
for key, val := range options {
switch key {
case "sshcmd":
v.SSHCmd = val
case "password":
v.Password = val
case "port":
v.Port = val
case "identity_file":
v.IdentityFile = val
case "id_rsa":
if val != "" {
// Private keys should end in '\n' such
// that the created v.MountPoint + "_id_rsa"
// file is a valid IdentityFile.
lastChar := string(val[len(val)-1])
if lastChar != "\n" {
val += "\n"
}
// Copy the value of the id_rsa argument
// and save as a prefix to the v.MountPoint
v.IdentityFile = v.MountPoint + "_id_rsa"
if err := v.saveKey(val); err != nil {
return err
}
}
case "ephemeral":
parsedBool, err := strconv.ParseBool(val)
if err != nil {
return err
}
v.Ephemeral = parsedBool
default:
if val != "" {
v.Options = append(v.Options, key+"="+val)
} else {
v.Options = append(v.Options, key)
}
}
}
if v.SSHCmd == "" {
return fmt.Errorf("'sshcmd' option required")
}
if v.Password == "" && v.IdentityFile == "" {
return fmt.Errorf("either 'password', 'identity_file', or 'id_rsa' option must be set")
}
if v.Password != "" && v.IdentityFile != "" {
return fmt.Errorf("'password' and 'identity_file'/'id_rsa' options are mutually exclusive")
}
return nil
}
func (v *sshfsVolume) saveKey(key string) error {
if key == "" {
return fmt.Errorf("can't save an empty key")
}
f, err := os.Create(v.IdentityFile)
if err != nil {
msg := fmt.Sprintf("Failed to create the identity_file file at %s (%s)", v.IdentityFile, err)
log.Error(msg)
return fmt.Errorf(msg)
}
f.WriteString(key)
f.Chmod(VolumeFileMode)
f.Close()
return nil
}
func newSshfsDriver(basePath string) (*sshfsDriver, error) {
log.Infof("Creating a new driver instance %s", basePath)
volumePath := filepath.Join(basePath, "volumes")
statePath := filepath.Join(basePath, "state", "sshfs-state.json")
if verr := os.MkdirAll(volumePath, VolumeDirMode); verr != nil {
return nil, verr
}
log.Infof("Initialized driver, volumes='%s' state='%s", volumePath, statePath)
driver := &sshfsDriver{
volumes: make(map[string]*sshfsVolume),
volumePath: volumePath,
statePath: statePath,
mutex: &sync.Mutex{},
}
data, err := ioutil.ReadFile(driver.statePath)
if err != nil {
if os.IsNotExist(err) {
log.Debugf("No state found at %s", driver.statePath)
} else {
return nil, err
}
} else {
if err := json.Unmarshal(data, &driver.volumes); err != nil {
return nil, err
}
}
return driver, nil
}
func (d *sshfsDriver) saveState() {
data, err := json.Marshal(d.volumes)
if err != nil {
log.Errorf("saveState failed %s", err)
return
}
if err := ioutil.WriteFile(d.statePath, data, VolumeFileMode); err != nil {
log.Errorf("Failed to write state %s to %s (%s)", data, d.statePath, err)
}
}
// Driver API
func (d *sshfsDriver) Create(r *volume.CreateRequest) error {
log.Debugf("Create Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
vol, err := d.newVolume(r.Name)
if err != nil {
return err
}
if err := vol.setupOptions(r.Options); err != nil {
return err
}
d.volumes[r.Name] = vol
d.saveState()
return nil
}
func (d *sshfsDriver) List() (*volume.ListResponse, error) {
log.Debugf("List Request")
var vols = []*volume.Volume{}
for _, vol := range d.volumes {
vols = append(vols,
&volume.Volume{Name: vol.Name, Mountpoint: vol.MountPoint})
}
return &volume.ListResponse{Volumes: vols}, nil
}
func (d *sshfsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
log.Debugf("Get Request %s", r)
vol, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to get volume %s because it doesn't exists", r.Name)
log.Error(msg)
return &volume.GetResponse{}, fmt.Errorf(msg)
}
return &volume.GetResponse{Volume: &volume.Volume{Name: vol.Name, Mountpoint: vol.MountPoint}}, nil
}
func (d *sshfsDriver) Remove(r *volume.RemoveRequest) error {
log.Debugf("Remove Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
vol, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to remove volume %s because it doesn't exists", r.Name)
log.Error(msg)
return fmt.Errorf(msg)
}
if vol.RefCount > 0 {
msg := fmt.Sprintf("Can't remove volume %s because it is mounted by %d containers", vol.Name, vol.RefCount)
log.Error(msg)
return fmt.Errorf(msg)
}
if err := d.removeVolume(vol); err != nil {
return err
}
delete(d.volumes, vol.Name)
d.saveState()
return nil
}
func (d *sshfsDriver) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
log.Debugf("Path Request %s", r)
vol, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to find path for volume %s because it doesn't exists", r.Name)
log.Error(msg)
return &volume.PathResponse{}, fmt.Errorf(msg)
}
return &volume.PathResponse{Mountpoint: vol.MountPoint}, nil
}
func (d *sshfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
log.Debugf("Mount Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
vol, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to mount volume %s because it doesn't exists", r.Name)
log.Error(msg)
return &volume.MountResponse{}, fmt.Errorf(msg)
}
if vol.RefCount == 0 {
log.Debugf("First volume mount %s establish connection to %s", vol.Name, vol.SSHCmd)
if err := d.mountVolume(vol); err != nil {
msg := fmt.Sprintf("Failed to mount %s, %s", vol.Name, err)
log.Error(msg)
return &volume.MountResponse{}, fmt.Errorf(msg)
}
}
vol.RefCount++
d.saveState()
return &volume.MountResponse{Mountpoint: vol.MountPoint}, nil
}
func (d *sshfsDriver) Unmount(r *volume.UnmountRequest) error {
log.Debugf("Umount Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
vol, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to unmount volume %s because it doesn't exists", r.Name)
log.Error(msg)
return fmt.Errorf(msg)
}
vol.RefCount--
if vol.RefCount <= 0 {
if err := d.unmountVolume(vol); err != nil {
return err
}
vol.RefCount = 0
}
d.saveState()
return nil
}
func (d *sshfsDriver) Capabilities() *volume.CapabilitiesResponse {
log.Debugf("Capabilities Request")
return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "global"}}
}
// Helper methods
func (d *sshfsDriver) newVolume(name string) (*sshfsVolume, error) {
path := filepath.Join(d.volumePath, name)
err := os.MkdirAll(path, VolumeDirMode)
if err != nil {
msg := fmt.Sprintf("Failed to create the volume mount path %s (%s)", path, err)
log.Error(msg)
return nil, fmt.Errorf(msg)
}
vol := &sshfsVolume{
Name: name,
MountPoint: path,
CreatedAt: time.Now().Format(time.RFC3339Nano),
Ephemeral: false,
RefCount: 0,
}
return vol, nil
}
func (d *sshfsDriver) removeVolume(vol *sshfsVolume) error {
// Remove the IdentityFile path if it exists
if _, err := os.Stat(vol.MountPoint); !os.IsNotExist(err) {
if vol.IdentityFile != "" && vol.Ephemeral {
if err := os.Remove(vol.IdentityFile); err != nil {
msg := fmt.Sprintf("Ephemeral - Failed to remove the volume %s's identity file: %s (%s)", vol.Name, vol.IdentityFile, err)
log.Error(msg)
}
}
}
// Remove MountPoint
// If the Mountpoint directory exist, remove it
if _, err := os.Stat(vol.MountPoint); !os.IsNotExist(err) {
// Else remove everything in that mountpoint
if err := os.Remove(vol.MountPoint); err != nil {
// If the mount is not mounted, remove legacy
msg := fmt.Sprintf("Failed to remove the volume %s mountpoint %s (%s)", vol.Name, vol.MountPoint, err)
log.Error(msg)
return fmt.Errorf(msg)
}
}
return nil
}
func (d *sshfsDriver) mountVolume(vol *sshfsVolume) error {
cmd := exec.Command("sshfs", "-oStrictHostKeyChecking=no", vol.SSHCmd, vol.MountPoint)
if vol.Port != "" {
cmd.Args = append(cmd.Args, "-p", vol.Port)
}
if vol.Password != "" {
cmd.Args = append(cmd.Args, "-o", "workaround=rename", "-o", "password_stdin")
cmd.Stdin = strings.NewReader(vol.Password)
}
if vol.IdentityFile != "" {
cmd.Args = append(cmd.Args, "-o", "IdentityFile="+vol.IdentityFile)
}
// Append the rest
for _, option := range vol.Options {
cmd.Args = append(cmd.Args, "-o", option)
}
// Ensure that children have the same process pgid
log.Debugf("Executing mount command %v", cmd)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("sshfs command failed %v %v (%s)", cmd, err, output)
}
return nil
}
func (d *sshfsDriver) unmountVolume(vol *sshfsVolume) error {
cmd := fmt.Sprintf("umount %s", vol.MountPoint)
if err := exec.Command("sh", "-c", cmd).Run(); err != nil {
return err
}
// Check that the mountpoint is empty
files, err := ioutil.ReadDir(vol.MountPoint)
if err != nil {
return err
}
if len(files) > 0 {
return fmt.Errorf("after unmount %d files still exists in %s", len(files), vol.MountPoint)
}
return nil
}