-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
285 lines (254 loc) · 7.48 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/docker/go-plugins-helpers/volume"
)
type CephDriver struct {
Mutex *sync.Mutex
MountPoint string
FSType string
Username string
Pool string
DefaultSize string
}
func New() CephDriver {
fsType := os.Getenv("CEPH_DRIVER_FSTYPE")
if fsType == "" {
fsType = "ext4"
}
mountPoint := os.Getenv("CEPH_DRIVER_MOUNTPOINT")
if mountPoint == "" {
mountPoint = "/var/lib/ceph-docker-driver"
}
userName := os.Getenv("CEPH_DRIVER_USERNAME")
if userName == "" {
userName = "admin"
}
poolName := os.Getenv("CEPH_DRIVER_POOL")
if poolName == "" {
poolName = "rbd"
}
defaultSize := os.Getenv("CEPH_DRIVER_DEFAULT_SIZE")
if defaultSize == "" {
defaultSize = "8G"
}
_, err := os.Lstat(mountPoint)
if os.IsNotExist(err) {
if err := os.MkdirAll(mountPoint, 0755); err != nil {
fmt.Println("Cannot mkdir")
os.Exit(1)
}
}
d := CephDriver{
Mutex: &sync.Mutex{},
MountPoint: mountPoint,
FSType: fsType,
Username: userName,
Pool: poolName,
DefaultSize: defaultSize,
}
return d
}
func (d CephDriver) Capabilities(r volume.Request) volume.Response {
fmt.Println("Capabilities")
return volume.Response{Capabilities: volume.Capability{Scope: "global"}}
}
func (d CephDriver) Create(r volume.Request) volume.Response {
fmt.Println("Create request recieved")
d.Mutex.Lock()
defer d.Mutex.Unlock()
size := r.Options["size"]
if len(size) == 0 {
size = d.DefaultSize
}
err := exec.Command("rbd", "--id", d.Username, "create", "-p", d.Pool, "-s", size, r.Name).Run()
if err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{}
}
func (d CephDriver) Get(r volume.Request) volume.Response {
fmt.Println("Get")
err := exec.Command("rbd", "--id", d.Username, "info", "-p", d.Pool, r.Name).Run()
if err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{Volume: &volume.Volume{Name: r.Name, Mountpoint: d.MountPoint + "/" + r.Name}}
}
func (d CephDriver) Mount(r volume.MountRequest) volume.Response {
fmt.Println("Staring Mount")
d.Mutex.Lock()
defer d.Mutex.Unlock()
watcherCount, err := d.GetWatcherCount(r.Name)
if watcherCount != 0 {
return volume.Response{Err: "Volume in use"}
}
rbdout, err := exec.Command("rbd", "--id", d.Username, "map", "-p", d.Pool, r.Name).Output()
if err != nil {
return volume.Response{Err: err.Error()}
}
rbd := strings.TrimSuffix(string(rbdout), "\n")
waitForPathToExist(string(rbd), 60)
if GetFSType(string(rbd)) == "" {
fmt.Println("Formatting device")
err = FormatVolume(string(rbd), d.FSType)
if err != nil {
fmt.Println("Failed formatting device")
return volume.Response{Err: err.Error()}
}
}
_, err = os.Lstat(d.MountPoint + "/" + r.Name)
if os.IsNotExist(err) {
if err := os.MkdirAll(d.MountPoint+"/"+r.Name, 0755); err != nil {
fmt.Println("Cannot mkdir")
os.Exit(1)
}
}
_, err = exec.Command("mount", string(rbd), d.MountPoint+"/"+r.Name).CombinedOutput()
if err != nil {
fmt.Println("Failed to mount:", r.Name)
return volume.Response{Err: err.Error()}
}
return volume.Response{Mountpoint: d.MountPoint + "/" + r.Name, Err: ""}
}
func (d CephDriver) Path(r volume.Request) volume.Response {
fmt.Println("Starting Path function call")
_, err := os.Lstat(d.MountPoint + "/" + r.Name)
if err != nil {
fmt.Println("Failed to retrieve volume name:", r.Name)
return volume.Response{Err: err.Error()}
}
return volume.Response{Volume: &volume.Volume{Mountpoint: d.MountPoint + "/" + r.Name}}
}
func (d CephDriver) Unmount(r volume.UnmountRequest) volume.Response {
fmt.Println("Starting Umount")
d.Mutex.Lock()
defer d.Mutex.Unlock()
err := exec.Command("rbd", "--id", d.Username, "info", "-p", d.Pool, r.Name).Run()
if err != nil {
fmt.Println("Failed to retrieve volume name:", r.Name)
return volume.Response{Err: err.Error()}
}
_, err = os.Lstat(d.MountPoint + "/" + r.Name)
if os.IsNotExist(err) {
fmt.Println("No dir to unmount:", r.Name)
} else {
_, err = exec.Command("umount", d.MountPoint+"/"+r.Name).CombinedOutput()
if err != nil {
if err.Error() == "Volume is not mounted" {
fmt.Println("Not mounted:", r.Name)
} else {
fmt.Println("Failed to umount:", r.Name)
return volume.Response{Err: err.Error()}
}
}
fmt.Println("Removing Dir")
_, err = exec.Command("rmdir", d.MountPoint+"/"+r.Name).CombinedOutput()
if err != nil {
fmt.Println("Failed to rmdir:", r.Name)
return volume.Response{Err: err.Error()}
}
}
err = exec.Command("rbd", "--id", d.Username, "unmap", "-p", d.Pool, r.Name).Run()
if err != nil {
fmt.Println("Failed to start volume:", r.Name)
return volume.Response{Err: err.Error()}
}
return volume.Response{}
}
func (d CephDriver) Remove(r volume.Request) volume.Response {
fmt.Println("Remove request recieved")
d.Mutex.Lock()
defer d.Mutex.Unlock()
err := exec.Command("rbd", "--id", d.Username, "info", "-p", d.Pool, r.Name).Run()
if err != nil {
fmt.Println("Failed to retrieve volume name:", r.Name)
return volume.Response{Err: err.Error()}
}
err = exec.Command("rbd", "--id", d.Username, "rm", "-p", d.Pool, r.Name).Run()
if err != nil {
fmt.Println("Failed to retrieve volume name:", r.Name)
return volume.Response{Err: err.Error()}
}
return volume.Response{}
}
func (d CephDriver) List(r volume.Request) volume.Response {
fmt.Println("List request recieved")
var data []string
out, err := exec.Command("rbd", "--id", d.Username, "--format", "json", "ls", "-p", d.Pool).Output()
if err != nil {
fmt.Println("Failed to run rbd ls command")
return volume.Response{Err: err.Error()}
}
_ = json.Unmarshal(out, &data)
var vols []*volume.Volume
for _, d := range data {
vols = append(vols, &volume.Volume{Name: d})
}
return volume.Response{Volumes: vols}
}
func GetFSType(device string) string {
fmt.Printf("Begin utils.GetFSType: %s", device)
fsType := ""
out, err := exec.Command("blkid", device).CombinedOutput()
if err != nil {
return fsType
}
if strings.Contains(string(out), "TYPE=") {
for _, v := range strings.Split(string(out), " ") {
if strings.Contains(v, "TYPE=") {
fsType = strings.Split(v, "=")[1]
fsType = strings.Replace(fsType, "\"", "", -1)
}
}
}
return fsType
}
func FormatVolume(device, fsType string) error {
fmt.Printf("Begin utils.FormatVolume: %s, %s", device, fsType)
cmd := "mkfs.ext4"
if fsType == "xfs" {
cmd = "mkfs.xfs"
}
fmt.Printf("Perform ", cmd, " on device: ", device)
out, err := exec.Command(cmd, "-F", device).CombinedOutput()
fmt.Printf("Result of mkfs cmd: ", string(out))
return err
}
func waitForPathToExist(fileName string, numTries int) bool {
fmt.Println("Waiting for path")
for i := 0; i < numTries; i++ {
_, err := os.Stat(fileName)
if err == nil {
fmt.Println("path found: ", fileName)
return true
}
if err != nil && !os.IsNotExist(err) {
return false
}
time.Sleep(time.Second)
out, err := exec.Command("partprobe").CombinedOutput()
fmt.Println("Result of partprobe cmd: ", string(out))
}
return false
}
type RBDWatcher struct {
Address string `json:"address"`
Client int `json:"client"`
Cookie int `json:"cookie"`
}
type RBDStatus struct {
Watchers []RBDWatcher `json:"watchers"`
}
func (d CephDriver) GetWatcherCount(volName string) (int, error) {
data := RBDStatus{}
out, _ := exec.Command("rbd", "--id", d.Username, "-p", d.Pool, "--format", "json", "status", volName).Output()
_ = json.Unmarshal(out, &data)
return len(data.Watchers), nil
}