Skip to content

Commit 47fd69f

Browse files
committed
Issue #21 and #25 - Refactored to simplify and support 1.10 docker daemon better
1 parent 88ee83d commit 47fd69f

File tree

9 files changed

+314
-276
lines changed

9 files changed

+314
-276
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

netshare/drivers/cifs.go

Lines changed: 47 additions & 71 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,96 +49,77 @@ 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(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]

netshare/drivers/driver.go

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,74 @@
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+
// TODO - check for share option
32+
// TODO - refactor to use name instead of key
33+
34+
dest := mountpoint(v.root, r.Name)
35+
if err := createDest(dest); err != nil {
36+
return volume.Response{Err: err.Error()}
3837
}
38+
v.mountm.Create(r.Name, dest, r.Options)
39+
return volume.Response{}
40+
}
41+
42+
func (v volumeDriver) Remove(r volume.Request) volume.Response {
43+
log.Debugf("Entering Remove: name: %s, options %v", r.Name, r.Options)
44+
v.m.Lock()
45+
defer v.m.Unlock()
3946

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

46-
func mountpoint(root, name string) string {
47-
return filepath.Join(root, name)
53+
func (v volumeDriver) Path(r volume.Request) volume.Response {
54+
log.Debugf("Host path for %s is at %s", r.Name, mountpoint(v.root, r.Name))
55+
return volume.Response{Mountpoint: mountpoint(v.root, r.Name)}
4856
}
4957

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
58+
func (v volumeDriver) Get(r volume.Request) volume.Response {
59+
log.Debugf("Entering Get: %v", r)
60+
v.m.Lock()
61+
defer v.m.Unlock()
62+
hostdir := mountpoint(v.root, r.Name)
63+
64+
if v.mountm.HasMount(r.Name) {
65+
log.Debugf("Get: mount found for %s, host directory: %s", r.Name, hostdir)
66+
return volume.Response{Volume: &volume.Volume{Name: r.Name, Mountpoint: hostdir}}
5467
}
55-
return nil
68+
return volume.Response{}
69+
}
70+
71+
func (v volumeDriver) List(r volume.Request) volume.Response {
72+
log.Debugf("Entering List: %v", r)
73+
return volume.Response{Volumes: v.mountm.GetVolumes(v.root)}
5674
}

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)