99 "os"
1010 "path/filepath"
1111 "strings"
12- "sync"
1312)
1413
1514const (
@@ -20,11 +19,9 @@ const (
2019)
2120
2221type 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
3027type cifsCreds struct {
@@ -36,11 +33,9 @@ type cifsCreds struct {
3633
3734func 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
0 commit comments