@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "os"
6
7
"path"
7
8
"path/filepath"
8
9
"sync"
@@ -16,16 +17,17 @@ import (
16
17
17
18
// A single volume instance
18
19
type moosefsMount struct {
19
- name string
20
- mountpoint string
20
+ name string
21
+ path string
22
+ root string
21
23
}
22
24
23
25
type moosefsDriver struct {
24
26
mounts map [string ]* moosefsMount
25
27
m * sync.Mutex
26
28
}
27
29
28
- func newMooseFSDriver (mountpoint string ) moosefsDriver {
30
+ func newMooseFSDriver (root string ) moosefsDriver {
29
31
d := moosefsDriver {
30
32
mounts : make (map [string ]* moosefsMount ),
31
33
m : & sync.Mutex {},
@@ -34,33 +36,43 @@ func newMooseFSDriver(mountpoint string) moosefsDriver {
34
36
}
35
37
36
38
func (d moosefsDriver ) Create (r * volume.CreateRequest ) error {
37
- var volumeMountpoint string
39
+ var volumeRoot string
38
40
39
41
d .m .Lock ()
40
42
defer d .m .Unlock ()
41
43
42
- if optsMountpoint , ok := r .Options ["mountpoint " ]; ok {
43
- volumeMountpoint = optsMountpoint
44
+ if optsRoot , ok := r .Options ["root " ]; ok {
45
+ volumeRoot = optsRoot
44
46
} else {
45
- // Assume the default mountpoint
46
- volumeMountpoint = filepath . Join ( * mountpoint , r . Name )
47
+ // Assume the default root
48
+ volumeRoot = * root
47
49
}
48
50
49
- if ! ismoosefs (volumeMountpoint ) {
50
- emsg := fmt .Sprintf ("Cannot create volume %s as it's not a valid MooseFS mount" , volumeMountpoint )
51
+ volumePath := filepath .Join (volumeRoot , r .Name )
52
+
53
+ if err := mkdir (volumePath ); err != nil {
54
+ return err
55
+ }
56
+
57
+ if ! ismoosefs (volumePath ) {
58
+ emsg := fmt .Sprintf ("Cannot create volume %s as it's not a valid MooseFS mount" , volumePath )
51
59
log .Error (emsg )
52
60
return errors .New (emsg )
53
61
}
54
62
55
63
if _ , ok := d .mounts [r .Name ]; ok {
56
- emsg := fmt .Sprintf ("Cannot create volume %s, it already exists" , volumeMountpoint )
64
+ emsg := fmt .Sprintf ("Cannot create volume %s, it already exists" , volumePath )
57
65
log .Error (emsg )
58
66
return errors .New (emsg )
59
67
}
60
68
69
+ if err := mkdir (volumePath ); err != nil {
70
+ return err
71
+ }
61
72
d .mounts [r .Name ] = & moosefsMount {
62
- name : r .Name ,
63
- mountpoint : volumeMountpoint ,
73
+ name : r .Name ,
74
+ path : volumePath ,
75
+ root : volumeRoot ,
64
76
}
65
77
66
78
if * verbose {
@@ -81,20 +93,20 @@ func (d moosefsDriver) Remove(r *volume.RemoveRequest) error {
81
93
82
94
func (d moosefsDriver ) Path (r * volume.PathRequest ) (* volume.PathResponse , error ) {
83
95
if _ , ok := d .mounts [r .Name ]; ok {
84
- return & volume.PathResponse {Mountpoint : d .mounts [r .Name ].mountpoint }, nil
96
+ return & volume.PathResponse {Mountpoint : d .mounts [r .Name ].path }, nil
85
97
}
86
98
return & volume.PathResponse {}, nil
87
99
}
88
100
89
101
func (d moosefsDriver ) Mount (r * volume.MountRequest ) (* volume.MountResponse , error ) {
90
- volumeMountpoint := d .mounts [r .Name ].mountpoint
91
- if ! ismoosefs (volumeMountpoint ) {
92
- emsg := fmt .Sprintf ("Cannot mount volume %s as it's not a valid MooseFS mount" , volumeMountpoint )
102
+ volumePath := filepath . Join ( d .mounts [r .Name ].root , r . Name )
103
+ if ! ismoosefs (volumePath ) {
104
+ emsg := fmt .Sprintf ("Cannot mount volume %s as it's not a valid MooseFS mount" , volumePath )
93
105
log .Error (emsg )
94
106
return & volume.MountResponse {}, errors .New (emsg )
95
107
}
96
108
if _ , ok := d .mounts [r .Name ]; ok {
97
- return & volume.MountResponse {Mountpoint : d .mounts [r .Name ].mountpoint }, nil
109
+ return & volume.MountResponse {Mountpoint : d .mounts [r .Name ].path }, nil
98
110
}
99
111
return & volume.MountResponse {}, nil
100
112
}
@@ -106,15 +118,15 @@ func (d moosefsDriver) Unmount(r *volume.UnmountRequest) error {
106
118
func (d moosefsDriver ) Get (r * volume.GetRequest ) (* volume.GetResponse , error ) {
107
119
if v , ok := d .mounts [r .Name ]; ok {
108
120
return & volume.GetResponse {
109
- Volume : & volume.Volume {Name : v .name , Mountpoint : v .mountpoint }}, nil
121
+ Volume : & volume.Volume {Name : v .name , Mountpoint : v .path }}, nil
110
122
}
111
123
return & volume.GetResponse {}, fmt .Errorf ("volume %s unknown" , r .Name )
112
124
}
113
125
114
126
func (d moosefsDriver ) List () (* volume.ListResponse , error ) {
115
127
volumes := []* volume.Volume {}
116
128
for v := range d .mounts {
117
- volumes = append (volumes , & volume.Volume {Name : d .mounts [v ].name , Mountpoint : d .mounts [v ].mountpoint })
129
+ volumes = append (volumes , & volume.Volume {Name : d .mounts [v ].name , Mountpoint : d .mounts [v ].path })
118
130
}
119
131
return & volume.ListResponse {Volumes : volumes }, nil
120
132
}
@@ -135,3 +147,21 @@ func ismoosefs(mountpoint string) bool {
135
147
}
136
148
return true
137
149
}
150
+
151
+ func mkdir (path string ) error {
152
+ fstat , err := os .Lstat (path )
153
+
154
+ if os .IsNotExist (err ) {
155
+ if err := os .MkdirAll (path , 0755 ); err != nil {
156
+ return err
157
+ }
158
+ } else if err != nil {
159
+ return err
160
+ }
161
+
162
+ if fstat != nil && ! fstat .IsDir () {
163
+ return fmt .Errorf ("%v already exist and it's not a directory" , path )
164
+ }
165
+
166
+ return nil
167
+ }
0 commit comments