-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquobyte_driver.go
153 lines (122 loc) · 3.8 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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"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 {
log.Println(err)
if !strings.Contains(err.Error(), "ENTITY_EXISTS_ALREADY/POSIX_ERROR_NONE") {
return volume.Response{Err: err.Error()}
}
}
mPoint := filepath.Join(driver.quobyteMount, request.Name)
log.Printf("Validate mounting volume %s on %s\n", request.Name, mPoint)
if err := check_mount_point(mPoint); 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 {
log.Println(err)
return volume.Response{Err: err.Error()}
}
return volume.Response{Err: ""}
}
func check_mount_point(mPoint string) error {
// We try it 5 times ~5 seconds -> move this into config?
max_tries := 5
tries := 0
var mount_error error
for tries <= max_tries {
mount_error = nil
if fi, err := os.Lstat(mPoint); err != nil || !fi.IsDir() {
mount_error = err
} else {
return nil
}
time.Sleep(1 * time.Second)
}
return mount_error
}
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)
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() {
log.Println(err)
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 {
log.Println(err)
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"}}
}