Skip to content

Commit

Permalink
Merge pull request #163 from jakirpatel/master
Browse files Browse the repository at this point in the history
Added states for docker-volume-netshare.
  • Loading branch information
gondor authored Dec 18, 2018
2 parents c90abe3 + 5c456b2 commit fa1eb8c
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.7.0
- 1.10.1
- tip
script:
- go test ./...
Expand Down
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ The method below will install the sysvinit and /etc/default options that can be
```
$ sudo docker-volume-netshare nfs
```
**2. Run the plugin - adding the correct DOCKER_API_VERSION**
If you are not using the latest stable version of docker engine please specify the version with flag.
For example:
To check docker API version:
```
docker version
Client:
Version: 17.12.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:11:19 2017
OS/Arch: linux/amd64
Server:
Engine:
Version: 17.12.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:09:53 2017
OS/Arch: linux/amd64
Experimental: false
```
Here the Docker API Version is 1.35. So you should start the plugin with the right version of Docker API.

Minimum supported version for the plugin is 1.12.

```
$ sudo docker-volume-netshare nfs -a 1.35
```


**2. Launch a container**

Expand Down Expand Up @@ -90,7 +122,7 @@ The method below will install the sysvinit and /etc/default options that can be
**1. Run the plugin - can be added to systemd or run in the background**

```
$ sudo docker-volume-netshare cifs --username user --password pass --domain domain --security security
$ sudo docker-volume-netshare cifs --username user --password pass --domain domain --security security -a docker_api_version
```

**2. Launch a container**
Expand Down Expand Up @@ -122,7 +154,7 @@ See example:
**2. Run the plugin**

```
$ sudo docker-volume-netshare cifs
$ sudo docker-volume-netshare cifs -a docker_api_version
```

**3. Launch a container**
Expand All @@ -140,7 +172,7 @@ options and other info can be eliminated when running a container.
**1. Run the plugin - can be added to systemd or run in the background**

```
$ sudo docker-volume-netshare cifs
$ sudo docker-volume-netshare cifs -a docker_api_version
```

**2. Create a Volume**
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
}
Loading

0 comments on commit fa1eb8c

Please sign in to comment.