Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit f0ec74a

Browse files
committed
refactor code
Signed-off-by: Victor Vieux <[email protected]>
1 parent 83cf646 commit f0ec74a

File tree

5 files changed

+311
-257
lines changed

5 files changed

+311
-257
lines changed

driver.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"sync"
7+
8+
"github.com/Sirupsen/logrus"
9+
"github.com/docker/go-plugins-helpers/volume"
10+
)
11+
12+
const socketAddress = "/run/docker/plugins/sshfs.sock"
13+
14+
type sshfsDriver struct {
15+
sync.RWMutex
16+
17+
root string
18+
statePath string
19+
volumes map[string]*sshfsVolume
20+
}
21+
22+
func newSshfsDriver(root string) (*sshfsDriver, error) {
23+
logrus.WithField("method", "new driver").Debug(root)
24+
25+
statePath := filepath.Join(root, "state", "sshfs-state.json")
26+
volumes, err := readState(statePath)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
return &sshfsDriver{
32+
root: filepath.Join(root, "volumes"),
33+
statePath: statePath,
34+
volumes: volumes,
35+
}, nil
36+
}
37+
38+
func (d *sshfsDriver) Create(r *volume.CreateRequest) error {
39+
logrus.WithField("method", "create").Debugf("%#v", r)
40+
41+
v, err := newSshfsVolume(d.root, r.Options)
42+
if err != nil {
43+
return err
44+
}
45+
46+
d.Lock()
47+
defer d.Unlock()
48+
49+
d.volumes[r.Name] = v
50+
51+
return saveState(d.statePath, d.volumes)
52+
}
53+
54+
func (d *sshfsDriver) Remove(r *volume.RemoveRequest) error {
55+
logrus.WithField("method", "remove").Debugf("%#v", r)
56+
57+
d.Lock()
58+
defer d.Unlock()
59+
60+
v, ok := d.volumes[r.Name]
61+
if !ok {
62+
return logError("volume %s not found", r.Name)
63+
}
64+
65+
if v.connections != 0 {
66+
return logError("volume %s is currently used by a container", r.Name)
67+
}
68+
69+
if err := os.RemoveAll(v.Mountpoint); err != nil {
70+
return logError(err.Error())
71+
}
72+
73+
delete(d.volumes, r.Name)
74+
75+
return saveState(d.statePath, d.volumes)
76+
}
77+
78+
func (d *sshfsDriver) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
79+
logrus.WithField("method", "path").Debugf("%#v", r)
80+
81+
d.RLock()
82+
defer d.RUnlock()
83+
84+
v, ok := d.volumes[r.Name]
85+
if !ok {
86+
return &volume.PathResponse{}, logError("volume %s not found", r.Name)
87+
}
88+
89+
return &volume.PathResponse{Mountpoint: v.Mountpoint}, nil
90+
}
91+
92+
func (d *sshfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
93+
logrus.WithField("method", "mount").Debugf("%#v", r)
94+
95+
d.Lock()
96+
defer d.Unlock()
97+
98+
v, ok := d.volumes[r.Name]
99+
if !ok {
100+
return &volume.MountResponse{}, logError("volume %s not found", r.Name)
101+
}
102+
103+
if v.connections == 0 {
104+
fi, err := os.Lstat(v.Mountpoint)
105+
if os.IsNotExist(err) {
106+
if err := os.MkdirAll(v.Mountpoint, 0755); err != nil {
107+
return &volume.MountResponse{}, logError(err.Error())
108+
}
109+
} else if err != nil {
110+
return &volume.MountResponse{}, logError(err.Error())
111+
}
112+
113+
if fi != nil && !fi.IsDir() {
114+
return &volume.MountResponse{}, logError("%v already exist and it's not a directory", v.Mountpoint)
115+
}
116+
117+
if err := mountVolume(v); err != nil {
118+
return &volume.MountResponse{}, logError(err.Error())
119+
}
120+
}
121+
122+
v.connections++
123+
124+
return &volume.MountResponse{Mountpoint: v.Mountpoint}, nil
125+
}
126+
127+
func (d *sshfsDriver) Unmount(r *volume.UnmountRequest) error {
128+
logrus.WithField("method", "unmount").Debugf("%#v", r)
129+
130+
d.Lock()
131+
defer d.Unlock()
132+
133+
v, ok := d.volumes[r.Name]
134+
if !ok {
135+
return logError("volume %s not found", r.Name)
136+
}
137+
138+
v.connections--
139+
140+
if v.connections <= 0 {
141+
if err := unmountVolume(v.Mountpoint); err != nil {
142+
return logError(err.Error())
143+
}
144+
v.connections = 0
145+
}
146+
147+
return nil
148+
}
149+
150+
func (d *sshfsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
151+
logrus.WithField("method", "get").Debugf("%#v", r)
152+
153+
d.RLock()
154+
defer d.RUnlock()
155+
156+
v, ok := d.volumes[r.Name]
157+
if !ok {
158+
return &volume.GetResponse{}, logError("volume %s not found", r.Name)
159+
}
160+
161+
return &volume.GetResponse{Volume: &volume.Volume{Name: r.Name, Mountpoint: v.Mountpoint}}, nil
162+
}
163+
164+
func (d *sshfsDriver) List() (*volume.ListResponse, error) {
165+
logrus.WithField("method", "list").Debug("")
166+
167+
d.RLock()
168+
defer d.RUnlock()
169+
170+
var vols []*volume.Volume
171+
for name, v := range d.volumes {
172+
vols = append(vols, &volume.Volume{Name: name, Mountpoint: v.Mountpoint})
173+
}
174+
return &volume.ListResponse{Volumes: vols}, nil
175+
}
176+
177+
func (d *sshfsDriver) Capabilities() *volume.CapabilitiesResponse {
178+
logrus.WithField("method", "capabilities").Debug("")
179+
180+
return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "local"}}
181+
}

0 commit comments

Comments
 (0)