-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (233 loc) · 7.42 KB
/
main.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
package main
import (
"context"
"fmt"
"github.com/docker/go-plugins-helpers/volume"
reaper "github.com/ramr/go-reaper"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"strings"
"syscall"
"time"
)
const socketAddress = "/run/docker/plugins/lizardfs.sock"
const containerVolumePath = "/mnt/docker-volumes"
const hostVolumePath = "/mnt/docker-volumes"
const volumeRoot = "/mnt/lizardfs/"
var host = os.Getenv("HOST")
var port = os.Getenv("PORT")
var remotePath = os.Getenv("REMOTE_PATH")
var mountOptions = os.Getenv("MOUNT_OPTIONS")
var rootVolumeName = os.Getenv("ROOT_VOLUME_NAME")
var connectTimeoutStr = os.Getenv("CONNECT_TIMEOUT")
var connectTimeout = 3000
var mounted = make(map[string][]string)
type lizardfsVolume struct {
Name string
Goal int
Path string
}
type lizardfsDriver struct {
volumes map[string]*lizardfsVolume
statePath string
}
func (l lizardfsDriver) Create(request *volume.CreateRequest) error {
log.WithField("method", "create").Debugf("%#v", l)
volumeName := request.Name
volumePath := fmt.Sprintf("%s%s", volumeRoot, volumeName)
replicationGoal := request.Options["ReplicationGoal"]
if volumeName == rootVolumeName {
log.Warning("tried to create a volume with same name as root volume. Ignoring request.")
}
err := os.MkdirAll(volumePath, 760)
if err != nil {
return err
}
_, err = strconv.Atoi(replicationGoal)
if err == nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(connectTimeout)*time.Millisecond)
defer cancel()
output, err := exec.CommandContext(ctx, "lizardfs", "setgoal", "-r", replicationGoal, volumePath).CombinedOutput()
if err != nil {
log.Error(string(output))
return err
}
log.Debug(string(output))
}
return nil
}
func (l lizardfsDriver) List() (*volume.ListResponse, error) {
log.WithField("method", "list").Debugf("")
var vols []*volume.Volume
directories, err := ioutil.ReadDir(volumeRoot)
for _, directory := range directories {
if len(mounted[directory.Name()]) == 0 {
vols = append(vols, &volume.Volume{Name: directory.Name()})
} else {
vols = append(vols, &volume.Volume{Name: directory.Name(), Mountpoint: path.Join(hostVolumePath, directory.Name())})
}
}
if err != nil {
return nil, err
}
if rootVolumeName != "" {
if len(mounted[rootVolumeName]) == 0 {
vols = append(vols, &volume.Volume{Name: rootVolumeName})
} else {
vols = append(vols, &volume.Volume{Name: rootVolumeName, Mountpoint: path.Join(hostVolumePath, rootVolumeName)})
}
}
return &volume.ListResponse{Volumes: vols}, nil
}
func (l lizardfsDriver) Get(request *volume.GetRequest) (*volume.GetResponse, error) {
log.WithField("method", "get").Debugf("")
volumeName := request.Name
volumePath := volumeRoot
if volumeName != rootVolumeName {
volumePath = fmt.Sprintf("%s%s", volumeRoot, volumeName)
}
if _, err := os.Stat(volumePath); os.IsNotExist(err) {
return nil, err
}
return &volume.GetResponse{Volume: &volume.Volume{Name: volumeName, Mountpoint: volumePath}}, nil
}
func (l lizardfsDriver) Remove(request *volume.RemoveRequest) error {
log.WithField("method", "remove").Debugf("")
volumeName := request.Name
volumePath := fmt.Sprintf("%s%s", volumeRoot, volumeName)
if volumeName == rootVolumeName {
return fmt.Errorf("can't remove root volume %s", rootVolumeName)
}
err := os.RemoveAll(volumePath)
return err
}
func (l lizardfsDriver) Path(request *volume.PathRequest) (*volume.PathResponse, error) {
log.WithField("method", "path").Debugf("")
var volumeName = request.Name
var hostMountpoint = path.Join(hostVolumePath, volumeName)
if len(mounted[volumeName]) == 0 {
return &volume.PathResponse{Mountpoint: hostMountpoint}, nil
}
return &volume.PathResponse{}, nil
}
func (l lizardfsDriver) Mount(request *volume.MountRequest) (*volume.MountResponse, error) {
log.WithField("method", "mount").Debugf("")
var volumeName = request.Name
var mountID = request.ID
var containerMountpoint = path.Join(containerVolumePath, volumeName)
var hostMountpoint = path.Join(hostVolumePath, volumeName)
if len(mounted[volumeName]) == 0 {
err := os.MkdirAll(containerMountpoint, 760)
if err != nil && err != os.ErrExist {
return nil, err
}
mountRemotePath := remotePath
if volumeName != rootVolumeName {
mountRemotePath = path.Join(remotePath, volumeName)
}
params := []string{containerMountpoint, "-H", host, "-P", port, "-S", mountRemotePath}
if mountOptions != "" {
params = append(params, strings.Split(mountOptions, " ")...)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(connectTimeout)*time.Millisecond)
defer cancel()
cmd := exec.CommandContext(ctx, "lfsmount", params...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 1}
err = cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
log.Error(err)
}
mounted[volumeName] = append(mounted[volumeName], mountID)
return &volume.MountResponse{Mountpoint: hostMountpoint}, nil
} else {
return &volume.MountResponse{Mountpoint: hostMountpoint}, nil
}
}
func indexOf(word string, data []string) int {
for k, v := range data {
if word == v {
return k
}
}
return -1
}
func (l lizardfsDriver) Unmount(request *volume.UnmountRequest) error {
log.WithField("method", "unmount").Debugf("")
var volumeName = request.Name
var mountID = request.ID
var containerMountpoint = path.Join(containerVolumePath, volumeName)
index := indexOf(mountID, mounted[volumeName])
if index > -1 {
mounted[volumeName] = append(mounted[volumeName][:index], mounted[volumeName][index+1:]...)
}
if len(mounted[volumeName]) == 0 {
output, err := exec.Command("umount", containerMountpoint).CombinedOutput()
if err != nil {
log.Error(string(output))
return err
}
log.Debug(string(output))
return nil
}
return nil
}
func (l lizardfsDriver) Capabilities() *volume.CapabilitiesResponse {
log.WithField("method", "capabilities").Debugf("")
return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "global"}}
}
func newLizardfsDriver(root string) (*lizardfsDriver, error) {
log.WithField("method", "new driver").Debug(root)
d := &lizardfsDriver{
volumes: map[string]*lizardfsVolume{},
}
return d, nil
}
func initClient() {
log.WithField("host", host).WithField("port", port).WithField("remote path", remotePath).Info("initializing client")
err := os.MkdirAll(volumeRoot, 760)
if err != nil {
log.Error(err)
}
params := []string{volumeRoot, "-H", host, "-P", port, "-S", remotePath}
if mountOptions != "" {
params = append(params, strings.Split(mountOptions, " ")...)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(connectTimeout)*time.Millisecond)
defer cancel()
output, err := exec.CommandContext(ctx, "lfsmount", params...).CombinedOutput()
if err != nil {
log.Error(string(output))
log.Fatal(err)
}
log.Debug(string(output))
}
func main() {
logLevel, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(logLevel)
}
go reaper.Reap()
log.Debugf("log level set to %s", log.GetLevel())
connectTimeout, err = strconv.Atoi(connectTimeoutStr)
if err != nil {
log.Errorf("failed to parse timeout with error %v. Assuming default %v", err, connectTimeout)
}
initClient()
d, err := newLizardfsDriver("/mnt")
if err != nil {
log.Fatal(err)
}
h := volume.NewHandler(d)
log.Infof("listening on %s", socketAddress)
log.Error(h.ServeUnix(socketAddress, 0))
}