Skip to content

Commit 124fec9

Browse files
committed
Merge pull request #28 from gondor/Issue21_25
Issue #21, #25 and #30
2 parents 88ee83d + 56f989d commit 124fec9

File tree

10 files changed

+317
-282
lines changed

10 files changed

+317
-282
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = 0.15
1+
VERSION = 0.16
22
GO_FMT = gofmt -s -w -l .
33
GO_XC = goxc -os="linux" -tasks-="rmbin"
44

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ The method below will install the sysvinit and /etc/default options that can be
3636
1. Install the Package
3737

3838
```
39-
$ wget https://github.com/gondor/docker-volume-netshare/releases/download/v0.15/docker-volume-netshare_0.15_amd64.deb
40-
$ sudo dpkg -i docker-volume-netshare_0.15_amd64.deb
39+
$ wget https://github.com/gondor/docker-volume-netshare/releases/download/v0.16/docker-volume-netshare_0.16_amd64.deb
40+
$ sudo dpkg -i docker-volume-netshare_0.16_amd64.deb
4141
```
4242

4343
2. Modify the startup options in `/etc/default/docker-volume-netshare`

netshare/drivers/cifs.go

Lines changed: 51 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"path/filepath"
1111
"strings"
12-
"sync"
1312
)
1413

1514
const (
@@ -20,11 +19,9 @@ const (
2019
)
2120

2221
type cifsDriver struct {
23-
root string
24-
creds *cifsCreds
25-
netrc *netrc.Netrc
26-
mountm *mountManager
27-
m *sync.Mutex
22+
volumeDriver
23+
creds *cifsCreds
24+
netrc *netrc.Netrc
2825
}
2926

3027
type cifsCreds struct {
@@ -36,11 +33,9 @@ type cifsCreds struct {
3633

3734
func NewCIFSDriver(root, user, pass, domain, security, netrc string) cifsDriver {
3835
d := cifsDriver{
39-
root: root,
40-
creds: &cifsCreds{user: user, pass: pass, domain: domain, security: security},
41-
netrc: parseNetRC(netrc),
42-
mountm: NewVolumeManager(),
43-
m: &sync.Mutex{},
36+
volumeDriver: newVolumeDriver(root),
37+
creds: &cifsCreds{user: user, pass: pass, domain: domain, security: security},
38+
netrc: parseNetRC(netrc),
4439
}
4540
return d
4641
}
@@ -54,104 +49,85 @@ func parseNetRC(path string) *netrc.Netrc {
5449
return nil
5550
}
5651

57-
func (s cifsDriver) Create(r volume.Request) volume.Response {
58-
log.Debugf("Create: %s, %v", r.Name, r.Options)
59-
dest := mountpoint(s.root, r.Name)
60-
if err := createDest(dest); err != nil {
61-
return volume.Response{Err: err.Error()}
62-
}
63-
s.mountm.Create(dest, r.Name, r.Options)
64-
return volume.Response{}
65-
}
66-
67-
func (s cifsDriver) Remove(r volume.Request) volume.Response {
68-
log.Debugf("Removing volume %s", r.Name)
69-
return volume.Response{}
70-
}
71-
72-
func (s cifsDriver) Path(r volume.Request) volume.Response {
73-
log.Debugf("Path for %s is at %s", r.Name, mountpoint(s.root, r.Name))
74-
return volume.Response{Mountpoint: mountpoint(s.root, r.Name)}
75-
}
52+
func (c cifsDriver) Mount(r volume.Request) volume.Response {
53+
c.m.Lock()
54+
defer c.m.Unlock()
55+
hostdir := mountpoint(c.root, r.Name)
56+
source := c.fixSource(r)
57+
host := c.parseHost(r)
7658

77-
func (s cifsDriver) Get(r volume.Request) volume.Response {
78-
log.Debugf("Get for %s is at %s", r.Name, mountpoint(s.root, r.Name))
79-
return volume.Response{Volume: &volume.Volume{Name: r.Name, Mountpoint: mountpoint(s.root, r.Name)}}
80-
}
81-
82-
func (s cifsDriver) List(r volume.Request) volume.Response {
83-
log.Debugf("List Volumes")
84-
return volume.Response{Volumes: s.mountm.GetVolumes(s.root)}
85-
}
86-
87-
func (s cifsDriver) Mount(r volume.Request) volume.Response {
88-
s.m.Lock()
89-
defer s.m.Unlock()
90-
dest := mountpoint(s.root, r.Name)
91-
source := s.fixSource(r.Name)
92-
host := parseHost(r.Name)
9359
log.Infof("Mount: %s, %v", r.Name, r.Options)
9460

95-
if s.mountm.HasMount(dest) && s.mountm.Count(dest) > 0 {
96-
log.Infof("Using existing CIFS volume mount: %s", dest)
97-
s.mountm.Increment(dest)
98-
return volume.Response{Mountpoint: dest}
61+
if c.mountm.HasMount(r.Name) && c.mountm.Count(r.Name) > 0 {
62+
log.Infof("Using existing CIFS volume mount: %s", hostdir)
63+
c.mountm.Increment(r.Name)
64+
return volume.Response{Mountpoint: hostdir}
9965
}
10066

101-
log.Infof("Mounting CIFS volume %s on %s", source, dest)
67+
log.Infof("Mounting CIFS volume %s on %s", source, hostdir)
10268

103-
if err := createDest(dest); err != nil {
69+
if err := createDest(hostdir); err != nil {
10470
return volume.Response{Err: err.Error()}
10571
}
10672

107-
if err := s.mountVolume(source, dest, s.getCreds(host)); err != nil {
73+
if err := c.mountVolume(r.Name, source, hostdir, c.getCreds(host)); err != nil {
10874
return volume.Response{Err: err.Error()}
10975
}
110-
s.mountm.Add(dest, r.Name)
111-
return volume.Response{Mountpoint: dest}
76+
c.mountm.Add(r.Name, hostdir)
77+
return volume.Response{Mountpoint: hostdir}
11278
}
11379

114-
func (s cifsDriver) Unmount(r volume.Request) volume.Response {
115-
s.m.Lock()
116-
defer s.m.Unlock()
117-
dest := mountpoint(s.root, r.Name)
118-
source := s.fixSource(r.Name)
80+
func (c cifsDriver) Unmount(r volume.Request) volume.Response {
81+
c.m.Lock()
82+
defer c.m.Unlock()
83+
hostdir := mountpoint(c.root, r.Name)
84+
source := c.fixSource(r)
11985

120-
if s.mountm.HasMount(dest) {
121-
if s.mountm.Count(dest) > 1 {
122-
log.Infof("Skipping unmount for %s - in use by other containers", dest)
123-
s.mountm.Decrement(dest)
86+
if c.mountm.HasMount(r.Name) {
87+
if c.mountm.Count(r.Name) > 1 {
88+
log.Infof("Skipping unmount for %s - in use by other containers", r.Name)
89+
c.mountm.Decrement(r.Name)
12490
return volume.Response{}
12591
}
126-
s.mountm.Decrement(dest)
92+
c.mountm.Decrement(r.Name)
12793
}
12894

129-
log.Infof("Unmounting volume %s from %s", source, dest)
95+
log.Infof("Unmounting volume %s from %s", source, hostdir)
13096

131-
if err := run(fmt.Sprintf("umount %s", dest)); err != nil {
97+
if err := run(fmt.Sprintf("umount %s", hostdir)); err != nil {
13298
return volume.Response{Err: err.Error()}
13399
}
134100

135-
if err := os.RemoveAll(dest); err != nil {
101+
c.mountm.DeleteIfNotManaged(r.Name)
102+
103+
if err := os.RemoveAll(hostdir); err != nil {
136104
return volume.Response{Err: err.Error()}
137105
}
138106

139107
return volume.Response{}
140108
}
141109

142-
func (s cifsDriver) fixSource(name string) string {
143-
return "//" + name
110+
func (c cifsDriver) fixSource(r volume.Request) string {
111+
if c.mountm.HasOption(r.Name, ShareOpt) {
112+
return "//" + c.mountm.GetOption(r.Name, ShareOpt)
113+
}
114+
return "//" + r.Name
144115
}
145116

146-
func parseHost(name string) string {
117+
func (c cifsDriver) parseHost(r volume.Request) string {
118+
name := r.Name
119+
if c.mountm.HasOption(r.Name, ShareOpt) {
120+
name = c.mountm.GetOption(r.Name, ShareOpt)
121+
}
122+
147123
if strings.ContainsAny(name, "/") {
148124
s := strings.Split(name, "/")
149125
return s[0]
150126
}
151127
return name
152128
}
153129

154-
func (s cifsDriver) mountVolume(source, dest string, creds *cifsCreds) error {
130+
func (s cifsDriver) mountVolume(name, source, dest string, creds *cifsCreds) error {
155131
var opts bytes.Buffer
156132

157133
opts.WriteString("-o ")
@@ -160,8 +136,8 @@ func (s cifsDriver) mountVolume(source, dest string, creds *cifsCreds) error {
160136
var domain = creds.domain
161137
var security = creds.security
162138

163-
if s.mountm.HasOptions(dest) {
164-
mopts := s.mountm.GetOptions(dest)
139+
if s.mountm.HasOptions(name) {
140+
mopts := s.mountm.GetOptions(name)
165141
if v, found := mopts[UsernameOpt]; found {
166142
user = v
167143
}
@@ -197,7 +173,7 @@ func (s cifsDriver) mountVolume(source, dest string, creds *cifsCreds) error {
197173

198174
opts.WriteString(fmt.Sprintf("%s %s", source, dest))
199175
cmd := fmt.Sprintf("mount -t cifs %s", opts.String())
200-
log.Debugf("Executing: %s\n", strings.Replace(cmd, pass, "", 1))
176+
log.Debugf("Executing: %s\n", strings.Replace(cmd, "password="+pass, "password=****", 1))
201177
return run(cmd)
202178
}
203179

netshare/drivers/driver.go

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,71 @@
11
package drivers
22

33
import (
4-
"fmt"
5-
"log"
6-
"os"
7-
"os/exec"
8-
"path/filepath"
4+
log "github.com/Sirupsen/logrus"
5+
"github.com/docker/go-plugins-helpers/volume"
6+
"sync"
97
)
108

11-
type DriverType int
12-
13-
const (
14-
CIFS DriverType = iota
15-
NFS
16-
EFS
17-
)
18-
19-
var driverTypes = []string{
20-
"cifs",
21-
"nfs",
22-
"efs",
9+
type volumeDriver struct {
10+
root string
11+
mountm *mountManager
12+
m *sync.Mutex
2313
}
2414

25-
func (dt DriverType) String() string {
26-
return driverTypes[dt]
15+
func newVolumeDriver(root string) volumeDriver {
16+
return volumeDriver{
17+
root: root,
18+
mountm: NewVolumeManager(),
19+
m: &sync.Mutex{},
20+
}
2721
}
2822

29-
func createDest(dest string) error {
30-
fi, err := os.Lstat(dest)
23+
func (v volumeDriver) Create(r volume.Request) volume.Response {
24+
log.Debugf("Entering Create: name: %s, options %v", r.Name, r.Options)
25+
26+
v.m.Lock()
27+
defer v.m.Unlock()
28+
29+
log.Debugf("Create volume -> name: %s, %v", r.Name, r.Options)
3130

32-
if os.IsNotExist(err) {
33-
if err := os.MkdirAll(dest, 0755); err != nil {
34-
return err
35-
}
36-
} else if err != nil {
37-
return err
31+
dest := mountpoint(v.root, r.Name)
32+
if err := createDest(dest); err != nil {
33+
return volume.Response{Err: err.Error()}
3834
}
35+
v.mountm.Create(r.Name, dest, r.Options)
36+
return volume.Response{}
37+
}
38+
39+
func (v volumeDriver) Remove(r volume.Request) volume.Response {
40+
log.Debugf("Entering Remove: name: %s, options %v", r.Name, r.Options)
41+
v.m.Lock()
42+
defer v.m.Unlock()
3943

40-
if fi != nil && !fi.IsDir() {
41-
return fmt.Errorf("%v already exist and it's not a directory", dest)
44+
if err := v.mountm.Delete(r.Name); err != nil {
45+
return volume.Response{Err: err.Error()}
4246
}
43-
return nil
47+
return volume.Response{}
4448
}
4549

46-
func mountpoint(root, name string) string {
47-
return filepath.Join(root, name)
50+
func (v volumeDriver) Path(r volume.Request) volume.Response {
51+
log.Debugf("Host path for %s is at %s", r.Name, mountpoint(v.root, r.Name))
52+
return volume.Response{Mountpoint: mountpoint(v.root, r.Name)}
4853
}
4954

50-
func run(cmd string) error {
51-
if out, err := exec.Command("sh", "-c", cmd).CombinedOutput(); err != nil {
52-
log.Println(string(out))
53-
return err
55+
func (v volumeDriver) Get(r volume.Request) volume.Response {
56+
log.Debugf("Entering Get: %v", r)
57+
v.m.Lock()
58+
defer v.m.Unlock()
59+
hostdir := mountpoint(v.root, r.Name)
60+
61+
if v.mountm.HasMount(r.Name) {
62+
log.Debugf("Get: mount found for %s, host directory: %s", r.Name, hostdir)
63+
return volume.Response{Volume: &volume.Volume{Name: r.Name, Mountpoint: hostdir}}
5464
}
55-
return nil
65+
return volume.Response{}
66+
}
67+
68+
func (v volumeDriver) List(r volume.Request) volume.Response {
69+
log.Debugf("Entering List: %v", r)
70+
return volume.Response{Volumes: v.mountm.GetVolumes(v.root)}
5671
}

netshare/drivers/driver_types.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package drivers
2+
3+
type DriverType int
4+
5+
const (
6+
CIFS DriverType = iota
7+
NFS
8+
EFS
9+
)
10+
11+
var driverTypes = []string{
12+
"cifs",
13+
"nfs",
14+
"efs",
15+
}
16+
17+
func (dt DriverType) String() string {
18+
return driverTypes[dt]
19+
}

0 commit comments

Comments
 (0)