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

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Vieux <[email protected]>
  • Loading branch information
vieux committed Sep 6, 2017
1 parent 83cf646 commit f0ec74a
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 257 deletions.
181 changes: 181 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package main

import (
"os"
"path/filepath"
"sync"

"github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
)

const socketAddress = "/run/docker/plugins/sshfs.sock"

type sshfsDriver struct {
sync.RWMutex

root string
statePath string
volumes map[string]*sshfsVolume
}

func newSshfsDriver(root string) (*sshfsDriver, error) {
logrus.WithField("method", "new driver").Debug(root)

statePath := filepath.Join(root, "state", "sshfs-state.json")
volumes, err := readState(statePath)
if err != nil {
return nil, err
}

return &sshfsDriver{
root: filepath.Join(root, "volumes"),
statePath: statePath,
volumes: volumes,
}, nil
}

func (d *sshfsDriver) Create(r *volume.CreateRequest) error {
logrus.WithField("method", "create").Debugf("%#v", r)

v, err := newSshfsVolume(d.root, r.Options)
if err != nil {
return err
}

d.Lock()
defer d.Unlock()

d.volumes[r.Name] = v

return saveState(d.statePath, d.volumes)
}

func (d *sshfsDriver) Remove(r *volume.RemoveRequest) error {
logrus.WithField("method", "remove").Debugf("%#v", r)

d.Lock()
defer d.Unlock()

v, ok := d.volumes[r.Name]
if !ok {
return logError("volume %s not found", r.Name)
}

if v.connections != 0 {
return logError("volume %s is currently used by a container", r.Name)
}

if err := os.RemoveAll(v.Mountpoint); err != nil {
return logError(err.Error())
}

delete(d.volumes, r.Name)

return saveState(d.statePath, d.volumes)
}

func (d *sshfsDriver) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
logrus.WithField("method", "path").Debugf("%#v", r)

d.RLock()
defer d.RUnlock()

v, ok := d.volumes[r.Name]
if !ok {
return &volume.PathResponse{}, logError("volume %s not found", r.Name)
}

return &volume.PathResponse{Mountpoint: v.Mountpoint}, nil
}

func (d *sshfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
logrus.WithField("method", "mount").Debugf("%#v", r)

d.Lock()
defer d.Unlock()

v, ok := d.volumes[r.Name]
if !ok {
return &volume.MountResponse{}, logError("volume %s not found", r.Name)
}

if v.connections == 0 {
fi, err := os.Lstat(v.Mountpoint)
if os.IsNotExist(err) {
if err := os.MkdirAll(v.Mountpoint, 0755); err != nil {
return &volume.MountResponse{}, logError(err.Error())
}
} else if err != nil {
return &volume.MountResponse{}, logError(err.Error())
}

if fi != nil && !fi.IsDir() {
return &volume.MountResponse{}, logError("%v already exist and it's not a directory", v.Mountpoint)
}

if err := mountVolume(v); err != nil {
return &volume.MountResponse{}, logError(err.Error())
}
}

v.connections++

return &volume.MountResponse{Mountpoint: v.Mountpoint}, nil
}

func (d *sshfsDriver) Unmount(r *volume.UnmountRequest) error {
logrus.WithField("method", "unmount").Debugf("%#v", r)

d.Lock()
defer d.Unlock()

v, ok := d.volumes[r.Name]
if !ok {
return logError("volume %s not found", r.Name)
}

v.connections--

if v.connections <= 0 {
if err := unmountVolume(v.Mountpoint); err != nil {
return logError(err.Error())
}
v.connections = 0
}

return nil
}

func (d *sshfsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
logrus.WithField("method", "get").Debugf("%#v", r)

d.RLock()
defer d.RUnlock()

v, ok := d.volumes[r.Name]
if !ok {
return &volume.GetResponse{}, logError("volume %s not found", r.Name)
}

return &volume.GetResponse{Volume: &volume.Volume{Name: r.Name, Mountpoint: v.Mountpoint}}, nil
}

func (d *sshfsDriver) List() (*volume.ListResponse, error) {
logrus.WithField("method", "list").Debug("")

d.RLock()
defer d.RUnlock()

var vols []*volume.Volume
for name, v := range d.volumes {
vols = append(vols, &volume.Volume{Name: name, Mountpoint: v.Mountpoint})
}
return &volume.ListResponse{Volumes: vols}, nil
}

func (d *sshfsDriver) Capabilities() *volume.CapabilitiesResponse {
logrus.WithField("method", "capabilities").Debug("")

return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "local"}}
}
Loading

0 comments on commit f0ec74a

Please sign in to comment.