forked from jaxxstorm/ploop-flexvol
/
main.go
182 lines (156 loc) · 4.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
package main
import (
"github.com/jaxxstorm/flexvolume"
//"fmt"
"github.com/dustin/go-humanize"
"github.com/kolyshkin/goploop-cli"
"github.com/urfave/cli"
"os"
"syscall"
)
func main() {
app := cli.NewApp()
app.Name = "ploop flexvolume"
app.Usage = "Mount ploop volumes in kubernetes using the flexvolume driver"
app.Commands = flexvolume.Commands(Ploop{})
app.Authors = []cli.Author{
cli.Author{
Name: "Lee Briggs",
},
}
app.Version = "0.1a"
app.Run(os.Args)
}
type Ploop struct{}
func (p Ploop) Init() flexvolume.Response {
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Ploop is available",
}
}
func (p Ploop) Attach(options map[string]string) flexvolume.Response {
if options["volumePath"] == "" {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Must specify a volume path",
}
}
if options["volumeId"] == "" {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Must specify a volume id",
}
}
if _, err := os.Stat(options["volumePath"] + "/" + options["volumeId"] + "/" + "DiskDescriptor.xml"); err == nil {
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Volume already exists",
Device: options["volumePath"] + "/" + options["volumeId"],
}
}
// get a human readable size from the map
bytes, _ := humanize.ParseBytes(options["size"])
// ploop driver takes kilobytes, so convert it
volume_size := bytes / 1024
// make the base directory where the volume will go
err := os.MkdirAll(options["volumePath"]+"/"+options["volumeId"], 0700)
if err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
}
}
// Create the ploop volume
cp := ploop.CreateParam{Size: volume_size, File: options["volumePath"] + "/" + options["volumeId"] + "/" + options["volumeId"]} // use correct path
// if there's an issue, return a failure
if err := ploop.Create(&cp); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully attached the ploop volume",
Device: options["volumePath"] + "/" + options["volumeId"] + "/" + options["volumeId"],
}
}
func (p Ploop) Detach(device string) flexvolume.Response {
if err := ploop.UmountByDevice(device); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Unable to detach ploop volume",
Device: device,
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully detached the ploop volume",
Device: device,
}
}
func (p Ploop) Mount(target, device string, options map[string]string) flexvolume.Response {
// make the target directory we're going to mount to
err := os.MkdirAll(target, 0755)
if err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
Device: device,
}
}
// open the disk descriptor first
volume, err := ploop.Open(options["volumePath"] + "/" + options["volumeId"] + "/" + "DiskDescriptor.xml")
if err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
Device: device,
}
}
defer volume.Close()
if m, _ := volume.IsMounted(); !m {
// If it's mounted, let's mount it!
mp := ploop.MountParam{Target: target}
dev, err := volume.Mount(&mp)
if err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
Device: dev,
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully mounted the ploop volume",
Device: device,
}
} else {
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Ploop volume already mounted",
Device: device,
}
}
}
func (p Ploop) Unmount(mount string) flexvolume.Response {
if err := syscall.Unmount(mount, 0x2); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Unable to unmount ploop volume from pod",
Device: mount,
}
}
if err := os.Remove(mount); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Unable to remove stale directory from pod",
Device: mount,
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully unmounted the ploop volume",
Device: mount,
}
}