-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.go
331 lines (288 loc) · 8.81 KB
/
basic.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package basic
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"github.com/docker/go-plugins-helpers/volume"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/sapk/docker-volume-helpers/driver"
"github.com/sapk/docker-volume-helpers/tools"
)
//Mountpoint represent a docker volume mountpoint
type Mountpoint struct {
Path string `json:"path"`
Connections int `json:"connections"`
}
//GetPath get path of mount
func (d *Mountpoint) GetPath() string {
return d.Path
}
//GetConnections get number of connection on mount
func (d *Mountpoint) GetConnections() int {
return d.Connections
}
//SetConnections set number of connection on mount
func (d *Mountpoint) SetConnections(n int) {
d.Connections = n
}
//Volume represent a docker volume
type Volume struct {
Options map[string]string `json:"options"`
Mount string `json:"mount"`
Connections int `json:"connections"`
}
//GetMount get mount of volume
func (v *Volume) GetMount() string {
return v.Mount
}
//GetOptions get options definition of volume
func (v *Volume) GetOptions() map[string]string {
return v.Options
}
//GetConnections get number of connection on volume
func (v *Volume) GetConnections() int {
return v.Connections
}
//SetConnections set number of connection on volume
func (v *Volume) SetConnections(n int) {
v.Connections = n
}
func (v *Volume) GetStatus() map[string]interface{} {
return map[string]interface{}{
"TODO": "List",
}
}
//Driver the global driver responding to call
type Driver struct {
Lock sync.RWMutex
Persistence *viper.Viper
Volumes map[string]*Volume
Mounts map[string]*Mountpoint
Config *DriverConfig
EventHandler *DriverEventHandler
}
//DriverConfig contains configration of driver
type DriverConfig struct {
Version int
Root string
Folder string
CustomOptions map[string]interface{}
}
//DriverEventHandler contains function to execute on event
type DriverEventHandler struct {
OnInit func(*Driver) error
OnMountVolume func(*Driver, driver.Volume, driver.Mount, *volume.MountRequest) (*volume.MountResponse, error)
GetMountName func(d *Driver, r *volume.CreateRequest) (string, error)
}
//GetVolumes list volumes of driver
func (d *Driver) GetVolumes() map[string]driver.Volume {
vi := make(map[string]driver.Volume, len(d.Volumes))
for k, i := range d.Volumes {
vi[k] = i
}
return vi
}
//GetMounts list mounts of driver
func (d *Driver) GetMounts() map[string]driver.Mount {
mi := make(map[string]driver.Mount, len(d.Mounts))
for k, i := range d.Mounts {
mi[k] = i
}
return mi
}
//RemoveVolume remove a volume of driver
func (d *Driver) RemoveVolume(id string) error {
delete(d.Volumes, id)
return nil
}
//RemoveMount remove a mount of driver
func (d *Driver) RemoveMount(id string) error {
delete(d.Mounts, id)
return nil
}
//GetLock list lock of driver
func (d *Driver) GetLock() *sync.RWMutex {
return &d.Lock
}
//Create create and init the requested volume
func (d *Driver) Create(r *volume.CreateRequest) error {
log.Debug().Str("name", r.Name).Msgf("Entering Create: %v", r.Options)
d.GetLock().Lock()
defer d.GetLock().Unlock()
mountName, err := d.EventHandler.GetMountName(d, r)
if err != nil {
return err
}
v := &Volume{
Options: r.Options,
Mount: mountName,
Connections: 0,
}
if _, ok := d.Mounts[v.Mount]; !ok { //This mountpoint doesn't allready exist -> create it
m := &Mountpoint{
Path: filepath.Join(d.Config.Root, v.Mount),
Connections: 0,
}
_, err := os.Lstat(m.Path) //Create folder if not exist. This will also failed if already exist
if os.IsNotExist(err) {
if err = os.MkdirAll(m.Path, 0700); err != nil {
return err
}
} else if err != nil {
return err
}
isempty, err := tools.FolderIsEmpty(m.Path)
if err != nil {
return err
}
if !isempty {
return fmt.Errorf("%v already exist and is not empty", m.Path)
}
d.Mounts[v.Mount] = m
}
d.Volumes[r.Name] = v
log.Debug().Msgf("Volume Created: %v", v)
return d.SaveConfig()
}
//List Volumes handled by these driver
func (d *Driver) List() (*volume.ListResponse, error) {
return driver.List(d)
}
//Get get info on the requested volume
func (d *Driver) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
v, m, err := driver.Get(d, r.Name)
if err != nil {
return nil, err
}
return &volume.GetResponse{Volume: &volume.Volume{Name: r.Name, Status: v.GetStatus(), Mountpoint: m.GetPath()}}, nil
}
//Remove remove the requested volume
func (d *Driver) Remove(r *volume.RemoveRequest) error {
return driver.Remove(d, r.Name)
}
//Unmount unmount the requested volume
func (d *Driver) Unmount(r *volume.UnmountRequest) error {
return driver.Unmount(d, r.Name)
}
//Capabilities Send capabilities of the local driver
func (d *Driver) Capabilities() *volume.CapabilitiesResponse {
return driver.Capabilities()
}
//RunCmd run deamon in context of this gvfs drive with custome env
func (d *Driver) RunCmd(cmd string) error {
log.Debug().Str("command", cmd).Msg("RunCmd")
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
if err != nil {
log.Warn().Err(err)
}
log.Debug().Msgf("Output: %s", out)
return err
}
//Persistence represent struct of Persistence file
type Persistence struct {
Version int `json:"version"`
Volumes map[string]*Volume `json:"volumes"`
Mounts map[string]*Mountpoint `json:"mounts"`
}
//SaveConfig stroe config/state in file
func (d *Driver) SaveConfig() error {
fi, err := os.Lstat(d.Config.Folder)
if os.IsNotExist(err) {
if err = os.MkdirAll(d.Config.Folder, 0700); err != nil {
return fmt.Errorf("SaveConfig: %s", err)
}
} else if err != nil {
return fmt.Errorf("SaveConfig: %s", err)
}
if fi != nil && !fi.IsDir() {
return fmt.Errorf("SaveConfig: %v already exist and it's not a directory", d.Config.Folder)
}
b, err := json.Marshal(Persistence{Version: d.Config.Version, Volumes: d.Volumes, Mounts: d.Mounts})
if err != nil {
log.Warn().Err(err).Msg("Unable to encode Persistence struct")
}
err = ioutil.WriteFile(d.Config.Folder+"/persistence.json", b, 0600)
if err != nil {
log.Warn().Err(err).Msg("Unable to write Persistence struct")
return fmt.Errorf("SaveConfig: %s", err)
}
return nil
}
//Path get path of the requested volume
func (d *Driver) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
_, m, err := driver.Get(d, r.Name)
if err != nil {
return nil, err
}
return &volume.PathResponse{Mountpoint: m.GetPath()}, nil
}
//Mount mount the requested volume
func (d *Driver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
log.Debug().Msgf("Entering Mount: %v", r)
v, m, err := driver.MountExist(d, r.Name)
if err != nil {
return nil, err
}
if m != nil && m.GetConnections() > 0 {
return &volume.MountResponse{Mountpoint: m.GetPath()}, nil
}
d.GetLock().Lock()
defer d.GetLock().Unlock()
resp, err := d.EventHandler.OnMountVolume(d, v, m, r)
if err != nil {
return nil, err
}
//time.Sleep(3 * time.Second)
driver.AddN(1, v, m)
return resp, d.SaveConfig()
}
//Init load configuration and serve response to API call
func Init(config *DriverConfig, eventHandler *DriverEventHandler) *Driver {
log.Debug().Msgf("Init basic driver at %s", config.Root)
d := &Driver{
Config: config,
Persistence: viper.New(),
Volumes: make(map[string]*Volume),
Mounts: make(map[string]*Mountpoint),
EventHandler: eventHandler,
}
d.Persistence.SetDefault("volumes", map[string]*Volume{})
d.Persistence.SetDefault("mounts", map[string]*Mountpoint{})
d.Persistence.SetConfigName("persistence")
d.Persistence.SetConfigType("json")
d.Persistence.AddConfigPath(d.Config.Folder)
if err := d.Persistence.ReadInConfig(); err != nil { // Handle errors reading the config file
log.Warn().Msgf("No persistence file found, I will start with a empty list of volume : %v", err)
} else {
log.Debug().Msg("Retrieving volume list from persistence file.")
var version int
err := d.Persistence.UnmarshalKey("version", &version)
if err != nil || version != d.Config.Version {
log.Warn().Msgf("Unable to decode version of persistence, %v", err)
d.Volumes = make(map[string]*Volume)
d.Mounts = make(map[string]*Mountpoint)
} else { //We have the same version
err := d.Persistence.UnmarshalKey("volumes", &d.Volumes)
if err != nil {
log.Warn().Msgf("Unable to decode into struct -> start with empty list, %v", err)
d.Volumes = make(map[string]*Volume)
}
err = d.Persistence.UnmarshalKey("mounts", &d.Mounts)
if err != nil {
log.Warn().Msgf("Unable to decode into struct -> start with empty list, %v", err)
d.Mounts = make(map[string]*Mountpoint)
}
}
}
if d.EventHandler.OnInit != nil {
if err := d.EventHandler.OnInit(d); err != nil {
log.Fatal().Msgf("Failed to execute OnInit event handler: %v", err)
}
}
return d
}