diff --git a/Makefile b/Makefile index a58f3ce..aa5713b 100644 --- a/Makefile +++ b/Makefile @@ -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)" diff --git a/common/common_driver.go b/common/common_driver.go index 5a821b7..0deb3ce 100644 --- a/common/common_driver.go +++ b/common/common_driver.go @@ -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 @@ -33,6 +33,8 @@ type Volume interface { type Mount interface { increasable GetPath() string + //GetProcess() *exec.Cmd + SetProcess(*exec.Cmd) } type increasable interface { @@ -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 } diff --git a/gluster/driver/driver.go b/gluster/driver/driver.go index 0838a17..54959b3 100644 --- a/gluster/driver/driver.go +++ b/gluster/driver/driver.go @@ -8,6 +8,7 @@ import ( "path/filepath" "strings" "sync" + "time" "github.com/sapk/docker-volume-gluster/common" @@ -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 @@ -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 @@ -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), @@ -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 diff --git a/gluster/driver/tools.go b/gluster/driver/tools.go index a6d3c04..8e985f3 100644 --- a/gluster/driver/tools.go +++ b/gluster/driver/tools.go @@ -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() @@ -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 { diff --git a/gluster/driver/tools_test.go b/gluster/driver/tools_test.go index 3f8cdc9..be7cfe2 100644 --- a/gluster/driver/tools_test.go +++ b/gluster/driver/tools_test.go @@ -1,6 +1,7 @@ package driver import ( + "strings" "testing" "github.com/docker/go-plugins-helpers/volume" @@ -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) } }