-
Notifications
You must be signed in to change notification settings - Fork 6
/
quobyte_driver.go
123 lines (96 loc) · 3.17 KB
/
quobyte_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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"github.com/docker/go-plugins-helpers/volume"
quobyte_api "github.com/quobyte/api"
)
type quobyteDriver struct {
client *quobyte_api.QuobyteClient
quobyteMount string
m *sync.Mutex
}
func newQuobyteDriver(apiURL string, username string, password string, quobyteMount string) quobyteDriver {
driver := quobyteDriver{
client: quobyte_api.NewQuobyteClient(apiURL, username, password),
quobyteMount: quobyteMount,
m: &sync.Mutex{},
}
return driver
}
func (driver quobyteDriver) Create(request volume.Request) volume.Response {
log.Printf("Creating volume %s\n", request.Name)
driver.m.Lock()
defer driver.m.Unlock()
user, group := "root", "root"
if usr, ok := request.Options["user"]; ok {
user = usr
}
if grp, ok := request.Options["group"]; ok {
group = grp
}
if _, err := driver.client.CreateVolume(&quobyte_api.CreateVolumeRequest{
Name: request.Name,
RootUserID: user,
RootGroupID: group,
}); err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{Err: ""}
}
func (driver quobyteDriver) Remove(request volume.Request) volume.Response {
log.Printf("Removing volume %s\n", request.Name)
driver.m.Lock()
defer driver.m.Unlock()
if err := driver.client.DeleteVolumeByName(request.Name, ""); err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{Err: ""}
}
func (driver quobyteDriver) Mount(request volume.MountRequest) volume.Response {
driver.m.Lock()
defer driver.m.Unlock()
mPoint := filepath.Join(driver.quobyteMount, request.Name)
log.Printf("Mounting volume %s on %s\n", request.Name, mPoint)
if fi, err := os.Lstat(mPoint); err != nil || !fi.IsDir() {
return volume.Response{Err: fmt.Sprintf("%v not mounted", mPoint)}
}
return volume.Response{Err: "", Mountpoint: mPoint}
}
func (driver quobyteDriver) Path(request volume.Request) volume.Response {
return volume.Response{Mountpoint: filepath.Join(driver.quobyteMount, request.Name)}
}
func (driver quobyteDriver) Unmount(request volume.UnmountRequest) volume.Response {
return volume.Response{}
}
func (driver quobyteDriver) Get(request volume.Request) volume.Response {
driver.m.Lock()
defer driver.m.Unlock()
mPoint := filepath.Join(driver.quobyteMount, request.Name)
if fi, err := os.Lstat(mPoint); err != nil || !fi.IsDir() {
return volume.Response{Err: fmt.Sprintf("%v not mounted", mPoint)}
}
return volume.Response{Volume: &volume.Volume{Name: request.Name, Mountpoint: mPoint}}
}
func (driver quobyteDriver) List(request volume.Request) volume.Response {
driver.m.Lock()
defer driver.m.Unlock()
var vols []*volume.Volume
files, err := ioutil.ReadDir(driver.quobyteMount)
if err != nil {
return volume.Response{Err: err.Error()}
}
for _, entry := range files {
if entry.IsDir() {
vols = append(vols, &volume.Volume{Name: entry.Name(), Mountpoint: filepath.Join(driver.quobyteMount, entry.Name())})
}
}
return volume.Response{Volumes: vols}
}
func (driver quobyteDriver) Capabilities(request volume.Request) volume.Response {
return volume.Response{Capabilities: volume.Capability{Scope: "global"}}
}