-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (135 loc) · 3.55 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"os/user"
"syscall"
"time"
"github.com/coreos/go-systemd/activation"
"github.com/docker/go-plugins-helpers/volume"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
version = "0.2.3"
shutdownTimeout = 10 * time.Second
)
func main() {
fmt.Printf("Starting docker-rbd-plugin version: %v\n", version)
verbose := false
app := cli.NewApp()
app.Name = "docker-rbd-plugin"
app.Usage = "Docker RBD Plugin"
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "pool",
Value: "docker",
Usage: "Name of the pool in which to create our rbd images. Default \"docker\". Pool MUST already exist in ceph cluster.",
},
cli.StringFlag{
Name: "default-size",
Value: "20G",
Usage: "Default size when creating an rbd image.",
},
cli.StringFlag{
Name: "default-filesystem",
Value: "xfs",
Usage: "Default filesystem when creating an rbd image.",
EnvVar: "RBD_DEFAULT_FS",
},
cli.StringFlag{
Name: "mountpoint",
Value: "/var/lib/docker-volumes/rbd",
Usage: "Mountpoint for rbd images.",
EnvVar: "RBD_VOLUME_DIR",
},
cli.DurationFlag{
Name: "reap",
Value: time.Second * 30,
// this will scan every 5m images that are mounted in mountpoint, but nowhere else,
// or images that are not mounted anywhere, and unmap them, if the modification time of
// the block device is older than the duration here (to prevent unmapping something
// an admistrator just mapped manually
Usage: "reap mapped images (0 to disable)",
},
cli.BoolFlag{
Name: "verbose",
Usage: "verbose output",
Destination: &verbose,
},
}
app.Action = Run
app.Before = func(c *cli.Context) error {
if verbose {
log.SetLevel(log.DebugLevel)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.WithError(err).Fatal()
}
}
// Run runs the driver
func Run(ctx *cli.Context) error {
u, err := user.Current()
if err != nil {
return fmt.Errorf("error getting the current user")
}
if u.Uid != "0" {
return fmt.Errorf("user is not root")
}
d, err := NewRbdDriver(ctx.String("pool"), ctx.String("default-size"), ctx.String("default-filesystem"), ctx.String("mountpoint"))
if err != nil {
return err
}
reapDur := ctx.Duration("reap")
if reapDur != 0 {
ticker := time.NewTicker(reapDur)
go func() {
for t := range ticker.C {
d.reap(t.Add(-reapDur))
}
}()
}
h := volume.NewHandler(d)
errCh := make(chan error)
listeners, _ := activation.Listeners() // wtf coreos, this funciton never returns errors
if len(listeners) > 1 {
log.Warn("driver does not support multiple sockets")
}
if len(listeners) == 0 {
log.Debug("launching volume handler on default socket")
go func() { errCh <- h.ServeUnix("rbd", 0) }()
} else {
l := listeners[0]
log.WithField("listener", l.Addr().String()).Debug("launching volume handler")
go func() { errCh <- h.Serve(l) }()
}
c := make(chan os.Signal)
defer close(c)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
select {
case err = <-errCh:
log.WithError(err).Error("error running handler")
close(errCh)
case <-c:
}
toCtx, toCtxCancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer toCtxCancel()
if sErr := h.Shutdown(toCtx); sErr != nil {
err = sErr
log.WithError(err).Error("error shutting down handler")
}
if hErr := <-errCh; hErr != nil && !errors.Is(hErr, http.ErrServerClosed) {
err = hErr
log.WithError(err).Error("error in handler after shutdown")
}
return err
}