-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdriver.go
212 lines (164 loc) · 5.35 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"sync"
"github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
)
const stateFile = "local-mapping.json"
type localPersistDriver struct {
mutex *sync.Mutex
name string
volumes map[string]string
rootMountPoint string
statePath string
}
type saveData struct {
State map[string]string `json:"state"`
}
func newLocalPersistDriver(rootMountPoint string, stateDir string) (*localPersistDriver, error) {
debug := os.Getenv("DEBUG")
if ok, _ := strconv.ParseBool(debug); ok {
logrus.SetLevel(logrus.DebugLevel)
}
logrus.WithField("stateDir", stateDir).Debug("Driver init ...")
driver := &localPersistDriver{
volumes: map[string]string{},
mutex: &sync.Mutex{},
name: "local-mapping",
rootMountPoint: rootMountPoint,
statePath: path.Join(stateDir, stateFile),
}
os.Mkdir(stateDir, 0700)
os.MkdirAll(rootMountPoint, 0755)
driver.volumes, _ = driver.findExistingVolumesFromStateFile()
logrus.WithField("volumes", driver.volumes).Debugf("Init success, found %s volumes", strconv.Itoa(len(driver.volumes)))
return driver, nil
}
func (driver *localPersistDriver) Create(req *volume.CreateRequest) error {
logrus.WithField("args", req).Debug("Create called")
mountpoint := req.Options["mountpoint"]
if mountpoint == "" {
return logError("The `mountpoint` option is required")
}
// transform to container path
mountpoint = path.Join(driver.rootMountPoint, mountpoint)
driver.mutex.Lock()
defer driver.mutex.Unlock()
if driver.exists(req.Name) {
return logError("The volume %s already exists", req.Name)
}
err := os.MkdirAll(mountpoint, 0755)
if err != nil {
return logError("Could not create directory %s", " ", mountpoint)
}
driver.volumes[req.Name] = mountpoint
e := driver.saveState(driver.volumes)
if e != nil {
return logError(e.Error())
}
logrus.WithField("volume", req.Name).WithField("mountpoint", mountpoint).Debug("Create success")
return nil
}
func (driver *localPersistDriver) List() (*volume.ListResponse, error) {
logrus.Debug("List called")
var volumes []*volume.Volume
for name := range driver.volumes {
volumes = append(volumes, driver.volume(name))
}
logrus.WithField("volumes", driver.volumes).Debugf("List success, found %s volumes", strconv.Itoa(len(driver.volumes)))
return &volume.ListResponse{
Volumes: volumes,
}, nil
}
func (driver *localPersistDriver) Get(req *volume.GetRequest) (*volume.GetResponse, error) {
logrus.WithField("args", req).Debug("Get Called")
if driver.exists(req.Name) {
logrus.WithField("volume", req.Name).Debug("Get success")
return &volume.GetResponse{
Volume: driver.volume(req.Name),
}, nil
}
return &volume.GetResponse{}, logError("Volume %s not found", req.Name)
}
func (driver *localPersistDriver) Remove(req *volume.RemoveRequest) error {
logrus.WithField("args", req).Debug("Remove Called")
driver.mutex.Lock()
defer driver.mutex.Unlock()
delete(driver.volumes, req.Name)
err := driver.saveState(driver.volumes)
if err != nil {
return logError(err.Error())
}
logrus.WithField("volume", req.Name).Debug("Remove success")
return nil
}
func (driver *localPersistDriver) Path(req *volume.PathRequest) (*volume.PathResponse, error) {
logrus.WithField("args", req).Debug("Path Called")
mountpoint, err := driver.volumes[req.Name]
if !err {
return &volume.PathResponse{}, logError("Volume %s not found", req.Name)
}
logrus.WithField("mountpoint", mountpoint).Debug("Path success")
return &volume.PathResponse{Mountpoint: mountpoint}, nil
}
func (driver *localPersistDriver) Mount(req *volume.MountRequest) (*volume.MountResponse, error) {
logrus.WithField("args", req).Debug("Mount Called")
mountpoint, err := driver.volumes[req.Name]
if !err {
return &volume.MountResponse{}, logError("Volume %s not found", req.Name)
}
logrus.WithField("mountpoint", mountpoint).Debug("Mount success")
return &volume.MountResponse{Mountpoint: mountpoint}, nil
}
func (driver *localPersistDriver) Unmount(req *volume.UnmountRequest) error {
logrus.WithField("args", req).Debug("Unmount Called")
logrus.WithField("volume", req.Name).Debug("Unmount success")
return nil
}
func (driver *localPersistDriver) Capabilities() *volume.CapabilitiesResponse {
logrus.Debug("Capabilities Called")
return &volume.CapabilitiesResponse{
Capabilities: volume.Capability{Scope: "local"},
}
}
func logError(format string, args ...interface{}) error {
logrus.Errorf(format, args)
return fmt.Errorf(format, args...)
}
func (driver *localPersistDriver) exists(name string) bool {
return driver.volumes[name] != ""
}
func (driver *localPersistDriver) volume(name string) *volume.Volume {
return &volume.Volume{
Name: name,
Mountpoint: driver.volumes[name],
}
}
func (driver *localPersistDriver) findExistingVolumesFromStateFile() (map[string]string, error) {
fileData, err := ioutil.ReadFile(driver.statePath)
if err != nil {
return map[string]string{}, err
}
var data saveData
e := json.Unmarshal(fileData, &data)
if e != nil {
return map[string]string{}, e
}
return data.State, nil
}
func (driver *localPersistDriver) saveState(volumes map[string]string) error {
data := saveData{
State: volumes,
}
fileData, err := json.Marshal(data)
if err != nil {
return err
}
return ioutil.WriteFile(driver.statePath, fileData, 0600)
}