-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
107 lines (94 loc) · 2.32 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
zfsdriver "github.com/TrilliumIT/docker-zfs-plugin/zfs"
"github.com/coreos/go-systemd/activation"
"github.com/docker/go-plugins-helpers/volume"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
version = "1.0.5"
shutdownTimeout = 10 * time.Second
)
func main() {
verbose := false
app := cli.NewApp()
app.Name = "docker-zfs-plugin"
app.Usage = "Docker ZFS Plugin"
app.Version = version
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "dataset-name",
Usage: "Name of the ZFS dataset to be used. It will be created if it doesn't exist.",
},
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 {
panic(err)
}
}
// Run runs the driver
func Run(ctx *cli.Context) error {
if ctx.String("dataset-name") == "" {
return fmt.Errorf("zfs dataset name is a required field")
}
d, err := zfsdriver.NewZfsDriver(ctx.StringSlice("dataset-name")...)
if err != nil {
return err
}
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.")
go func() { errCh <- h.ServeUnix("zfs", 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
}