Skip to content

Commit

Permalink
Added states for docker-volume-netshare.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patel, Jakir | Ikod | ECTD committed May 16, 2018
1 parent c90abe3 commit 3ab52ba
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 38 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ The method below will install the sysvinit and /etc/default options that can be
```
$ sudo docker-volume-netshare nfs
```
If you are not using the latest stable version of docker engine please specify the version with flag.
For example:
```
$ sudo docker-volume-netshare nfs -a 1.35
```


**2. Launch a container**

Expand Down
4 changes: 2 additions & 2 deletions netshare/drivers/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type cephDriver struct {
cephopts map[string]string
}

func NewCephDriver(root string, username string, password string, context string, cephmount string, cephport string, localmount string, cephopts string) cephDriver {
func NewCephDriver(root string, username string, password string, context string, cephmount string, cephport string, localmount string, cephopts string, mounts *MountManager) cephDriver {
d := cephDriver{
volumeDriver: newVolumeDriver(root),
volumeDriver: newVolumeDriver(root, mounts),
username: username,
password: password,
context: context,
Expand Down
4 changes: 2 additions & 2 deletions netshare/drivers/cifs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func NewCifsCredentials(user, pass, domain, security, fileMode, dirMode string)
}

// NewCIFSDriver creating the cifs driver
func NewCIFSDriver(root string, creds *CifsCreds, netrc, cifsopts string) CifsDriver {
func NewCIFSDriver(root string, creds *CifsCreds, netrc, cifsopts string, mounts *MountManager) CifsDriver {
d := CifsDriver{
volumeDriver: newVolumeDriver(root),
volumeDriver: newVolumeDriver(root, mounts),
creds: creds,
netrc: parseNetRC(netrc),
cifsopts: map[string]string{},
Expand Down
6 changes: 3 additions & 3 deletions netshare/drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

type volumeDriver struct {
root string
mountm *mountManager
mountm *MountManager
m *sync.Mutex
}

func newVolumeDriver(root string) volumeDriver {
func newVolumeDriver(root string, mounts *MountManager) volumeDriver {
return volumeDriver{
root: root,
mountm: NewVolumeManager(),
mountm: mounts,
m: &sync.Mutex{},
}
}
Expand Down
4 changes: 2 additions & 2 deletions netshare/drivers/efs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type efsDriver struct {
dnscache map[string]string
}

func NewEFSDriver(root, nameserver string, resolve bool) efsDriver {
func NewEFSDriver(root, nameserver string, resolve bool, mounts *MountManager) efsDriver {

d := efsDriver{
volumeDriver: newVolumeDriver(root),
volumeDriver: newVolumeDriver(root, mounts),
resolve: resolve,
dnscache: map[string]string{},
}
Expand Down
87 changes: 66 additions & 21 deletions netshare/drivers/mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package drivers

import (
"errors"
"context"
"strings"

log "github.com/sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
"strings"
"github.com/docker/docker/client"
"github.com/docker/docker/api/types"
)

const (
Expand All @@ -20,30 +24,30 @@ type mount struct {
managed bool
}

type mountManager struct {
type MountManager struct {
mounts map[string]*mount
}

func NewVolumeManager() *mountManager {
return &mountManager{
func NewVolumeManager() *MountManager {
return &MountManager{
mounts: map[string]*mount{},
}
}

func (m *mountManager) HasMount(name string) bool {
func (m *MountManager) HasMount(name string) bool {
_, found := m.mounts[name]
return found
}

func (m *mountManager) HasOptions(name string) bool {
func (m *MountManager) HasOptions(name string) bool {
c, found := m.mounts[name]
if found {
return c.opts != nil && len(c.opts) > 0
}
return false
}

func (m *mountManager) HasOption(name, key string) bool {
func (m *MountManager) HasOption(name, key string) bool {
if m.HasOptions(name) {
if _, ok := m.mounts[name].opts[key]; ok {
return ok
Expand All @@ -52,44 +56,44 @@ func (m *mountManager) HasOption(name, key string) bool {
return false
}

func (m *mountManager) GetOptions(name string) map[string]string {
func (m *MountManager) GetOptions(name string) map[string]string {
if m.HasOptions(name) {
c, _ := m.mounts[name]
return c.opts
}
return map[string]string{}
}

func (m *mountManager) GetOption(name, key string) string {
func (m *MountManager) GetOption(name, key string) string {
if m.HasOption(name, key) {
v, _ := m.mounts[name].opts[key]
return v
}
return ""
}

func (m *mountManager) GetOptionAsBool(name, key string) bool {
func (m *MountManager) GetOptionAsBool(name, key string) bool {
rv := strings.ToLower(m.GetOption(name, key))
if rv == "yes" || rv == "true" {
return true
}
return false
}

func (m *mountManager) IsActiveMount(name string) bool {
func (m *MountManager) IsActiveMount(name string) bool {
c, found := m.mounts[name]
return found && c.connections > 0
}

func (m *mountManager) Count(name string) int {
func (m *MountManager) Count(name string) int {
c, found := m.mounts[name]
if found {
return c.connections
}
return 0
}

func (m *mountManager) Add(name, hostdir string) {
func (m *MountManager) Add(name, hostdir string) {
_, found := m.mounts[name]
if found {
m.Increment(name)
Expand All @@ -98,7 +102,7 @@ func (m *mountManager) Add(name, hostdir string) {
}
}

func (m *mountManager) Create(name, hostdir string, opts map[string]string) *mount {
func (m *MountManager) Create(name, hostdir string, opts map[string]string) *mount {
c, found := m.mounts[name]
if found && c.connections > 0 {
c.opts = opts
Expand All @@ -110,10 +114,13 @@ func (m *mountManager) Create(name, hostdir string, opts map[string]string) *mou
}
}

func (m *mountManager) Delete(name string) error {
log.Debugf("Delete volume: %s, connections: %d", name, m.Count(name))
func (m *MountManager) Delete(name string) error {
// Check if any stopped containers are having references with volume.
refCount := checkReferences(name)
log.Debugf("Reference count %d", refCount)
if m.HasMount(name) {
if m.Count(name) < 1 {
if m.Count(name) < 1 && refCount < 1 {
log.Debugf("Delete volume: %s, connections: %d", name, m.Count(name))
delete(m.mounts, name)
return nil
}
Expand All @@ -122,32 +129,38 @@ func (m *mountManager) Delete(name string) error {
return nil
}

func (m *mountManager) DeleteIfNotManaged(name string) error {
func (m *MountManager) DeleteIfNotManaged(name string) error {
if m.HasMount(name) && !m.IsActiveMount(name) && !m.mounts[name].managed {
log.Infof("Removing un-managed volume")
return m.Delete(name)
}
return nil
}

func (m *mountManager) Increment(name string) int {
func (m *MountManager) Increment(name string) int {
log.Infof("Incrementing for %s", name)
c, found := m.mounts[name]
log.Infof("Previous connections state : %d", c.connections)
if found {
c.connections++
log.Infof("Current connections state : %d", c.connections)
return c.connections
}
return 0
}

func (m *mountManager) Decrement(name string) int {
func (m *MountManager) Decrement(name string) int {
log.Infof("Decrementing for %s", name)
c, found := m.mounts[name]
log.Infof("Previous connections state : %d", c.connections)
if found && c.connections > 0 {
c.connections--
log.Infof("Current connections state : %d", c.connections)
}
return 0
}

func (m *mountManager) GetVolumes(rootPath string) []*volume.Volume {
func (m *MountManager) GetVolumes(rootPath string) []*volume.Volume {

volumes := []*volume.Volume{}

Expand All @@ -156,3 +169,35 @@ func (m *mountManager) GetVolumes(rootPath string) []*volume.Volume {
}
return volumes
}

func (m *MountManager) AddMount(name string, hostdir string, connections int) {
m.mounts[name] = &mount{name: name, hostdir: hostdir, managed: true, connections: connections}
}

//Checking volume references with started and stopped containers as well.
func checkReferences(volumeName string) int {

cli, err := client.NewEnvClient()
if err != nil {
log.Error(err)
}

var counter = 0
ContainerListResponse, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true}) // All : true will return the stopped containers as well.
if err != nil {
log.Fatal(err,". Use -a flag to setup the DOCKER_API_VERSION. Run 'docker-volume-netshare --help' for usage.")
}

for _, container := range ContainerListResponse {
if len(container.Mounts) == 0 {
continue
}
for _, mounts := range container.Mounts {
if !(mounts.Name == volumeName) {
continue
}
counter++
}
}
return counter
}
8 changes: 5 additions & 3 deletions netshare/drivers/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ var (
EmptyMap = map[string]string{}
)

func NewNFSDriver(root string, version int, nfsopts string) nfsDriver {
func NewNFSDriver(root string, version int, nfsopts string, mounts *MountManager) nfsDriver {
d := nfsDriver{
volumeDriver: newVolumeDriver(root),
volumeDriver: newVolumeDriver(root, mounts),
version: version,
nfsopts: map[string]string{},
}
Expand Down Expand Up @@ -60,8 +60,10 @@ func (n nfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error)
n.mountm.Increment(resolvedName)
if err := run(fmt.Sprintf("grep -c %s /proc/mounts", hostdir)); err != nil {
log.Infof("Existing NFS volume not mounted, force remount.")
// maintain count
n.mountm.Decrement(resolvedName)
} else {
n.mountm.Increment(resolvedName)
//n.mountm.Increment(resolvedName)
return &volume.MountResponse{Mountpoint: hostdir}, nil
}
}
Expand Down
Loading

0 comments on commit 3ab52ba

Please sign in to comment.