Skip to content

Commit

Permalink
setup direct sub-process
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Dec 15, 2017
1 parent 4126198 commit 1ded20e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ test-unit: dev-deps deps format

test-integration: dev-deps deps format
@echo -e "$(OK_COLOR)==> Running integration tests...$(NO_COLOR)"
go test -v -timeout 1h -race -coverprofile=coverage.inte.out -covermode=atomic -coverpkg ./gluster/driver ./gluster/integration
go test -v -timeout 1h -coverprofile=coverage.inte.out -covermode=atomic -coverpkg ./gluster/driver ./gluster/integration
# go test -v -timeout 1h -race -coverprofile=coverage.inte.out -covermode=atomic -coverpkg ./gluster/driver ./gluster/integration

test-coverage: test
@echo -e "$(OK_COLOR)==> Uploading coverage ...$(NO_COLOR)"
Expand Down
6 changes: 4 additions & 2 deletions common/common_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Driver interface {
GetVolumes() map[string]Volume
GetMounts() map[string]Mount
SaveConfig() error
StartCmd(string) (*exec.Cmd, error)
StartCmd(name string, arg ...string) (*exec.Cmd, error)
}

//Volume needed interface for some commons interactions
Expand All @@ -33,6 +33,8 @@ type Volume interface {
type Mount interface {
increasable
GetPath() string
//GetProcess() *exec.Cmd
SetProcess(*exec.Cmd)
}

type increasable interface {
Expand Down Expand Up @@ -137,7 +139,7 @@ func Unmount(d Driver, vName string) error {
}

if m.GetConnections() <= 1 {
c, err := d.StartCmd(fmt.Sprintf("/usr/bin/umount %s", m.GetPath()))
c, err := d.StartCmd("/usr/bin/umount", m.GetPath())
if err != nil {
return err
}
Expand Down
30 changes: 24 additions & 6 deletions gluster/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"sync"
"time"

"github.com/sapk/docker-volume-gluster/common"

Expand All @@ -34,6 +35,9 @@ type GlusterMountpoint struct {
func (d *GlusterMountpoint) GetPath() string {
return d.Path
}
func (d *GlusterMountpoint) SetProcess(c *exec.Cmd) {
d.Process = c
}

func (d *GlusterMountpoint) GetConnections() int {
return d.Connections
Expand Down Expand Up @@ -75,6 +79,7 @@ func (v *GlusterVolume) GetStatus() map[string]interface{} {
type GlusterDriver struct {
lock sync.RWMutex
root string
binary string
mountUniqName bool
persitence *viper.Viper
volumes map[string]*GlusterVolume
Expand Down Expand Up @@ -107,8 +112,14 @@ func (d *GlusterDriver) GetLock() *sync.RWMutex {
func Init(root string, mountUniqName bool) *GlusterDriver {
log.Debugf("Init gluster driver at %s, UniqName: %v", root, mountUniqName)
logger := log.New() //TODO defer close writer
path, err := exec.LookPath("glusterfs")
if err != nil {
log.Fatal("glusterfs binary not found")
}

d := &GlusterDriver{
root: root,
binary: path,
mountUniqName: mountUniqName,
persitence: viper.New(),
volumes: make(map[string]*GlusterVolume),
Expand Down Expand Up @@ -244,17 +255,24 @@ func (d *GlusterDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, er
d.GetLock().Lock()
defer d.GetLock().Unlock()

c, err := d.StartCmd(fmt.Sprintf("glusterfs %s %s", parseVolURI(v.GetRemote()), m.GetPath()))
c, err := d.StartCmd(d.binary, append(parseVolURI(v.GetRemote()), "--no-daemon", m.GetPath())...) // TODO --debug
if err != nil {
return nil, err
}
err = c.Wait() //TODO start glusterfs in foreground wait a minimum of time of no failure
if err != nil {

done := make(chan error, 1)
go func() {
done <- c.Wait()
}()
// Wait if failed for 15 seconds
select {
case err := <-done:
return nil, err
case <-time.After(time.Second * 15):
m.SetProcess(c)
common.AddN(1, v, m)
return &volume.MountResponse{Mountpoint: m.GetPath()}, d.SaveConfig()
}
//time.Sleep(3 * time.Second)
common.AddN(1, v, m)
return &volume.MountResponse{Mountpoint: m.GetPath()}, d.SaveConfig()
}

//Unmount unmount the requested volume
Expand Down
15 changes: 10 additions & 5 deletions gluster/driver/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (d *GlusterDriver) SaveConfig() error {
}

//StartCmd start deamon in context of this gluster driver and keep it in backgroud
func (d *GlusterDriver) StartCmd(cmd string) (*exec.Cmd, error) {
log.Debugf(cmd)
c := exec.Command(cmd)
func (d *GlusterDriver) StartCmd(bin string, arg ...string) (*exec.Cmd, error) {
log.Debugf("%s %s", bin, strings.Join(arg, " "))
c := exec.Command(bin, arg...)
c.Stdout = d.logOut
c.Stderr = d.logErr
return c, c.Start()
Expand All @@ -66,10 +66,15 @@ func isValidURI(volURI string) bool {
return re.MatchString(volURI)
}

func parseVolURI(volURI string) string {
func parseVolURI(volURI string) []string {
volParts := strings.Split(volURI, ":")
volServers := strings.Split(volParts[0], ",")
return fmt.Sprintf("--volfile-id='%s' -s '%s'", volParts[1], strings.Join(volServers, "' -s '"))
ret := make([]string, 1+len(volServers))
ret[0] = fmt.Sprintf("--volfile-id=%s", volParts[1])
for i, server := range volServers {
ret[i+1] = fmt.Sprintf("--volfile-server=%s", server)
}
return ret
}

func getMountName(d *GlusterDriver, r *volume.CreateRequest) string {
Expand Down
13 changes: 7 additions & 6 deletions gluster/driver/tools_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package driver

import (
"strings"
"testing"

"github.com/docker/go-plugins-helpers/volume"
Expand Down Expand Up @@ -34,16 +35,16 @@ func TestParseVolURI(t *testing.T) {
value string
result string
}{
{"test:volume", "--volfile-id='volume' -s 'test'"},
{"test,test2:volume", "--volfile-id='volume' -s 'test' -s 'test2'"},
{"192.168.1.1:volume", "--volfile-id='volume' -s '192.168.1.1'"},
{"192.168.1.1,10.8.0.1:volume", "--volfile-id='volume' -s '192.168.1.1' -s '10.8.0.1'"},
{"192.168.1.1,test2:volume", "--volfile-id='volume' -s '192.168.1.1' -s 'test2'"},
{"test:volume", "--volfile-id=volume --volfile-server=test"},
{"test,test2:volume", "--volfile-id=volume --volfile-server=test --volfile-server=test2"},
{"192.168.1.1:volume", "--volfile-id=volume --volfile-server=192.168.1.1"},
{"192.168.1.1,10.8.0.1:volume", "--volfile-id=volume --volfile-server=192.168.1.1 --volfile-server=10.8.0.1"},
{"192.168.1.1,test2:volume", "--volfile-id=volume --volfile-server=192.168.1.1 --volfile-server=test2"},
}

for _, test := range tt {
r := parseVolURI(test.value)
if test.result != r {
if test.result != strings.Join(r, " ") {
t.Errorf("Expected to be '%v' , got '%v'", test.result, r)
}
}
Expand Down

0 comments on commit 1ded20e

Please sign in to comment.