forked from rancher/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (112 loc) · 3 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
package main
import (
"context"
"os"
"github.com/Sirupsen/logrus"
dockerClient "github.com/docker/engine-api/client"
"github.com/docker/go-plugins-helpers/volume"
"github.com/pkg/errors"
"github.com/rancher/go-rancher/v2"
"github.com/rancher/kubernetes-agent/healthcheck"
"github.com/rancher/storage/docker/volumeplugin"
"github.com/urfave/cli"
)
var VERSION = "v0.0.0-dev"
func main() {
app := cli.NewApp()
app.Name = "storage"
app.Version = VERSION
app.Usage = "Magic"
app.Action = func(c *cli.Context) error {
if err := start(c); err != nil {
logrus.Fatal(err)
}
return nil
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "driver-name",
Usage: "The volume driver name",
},
cli.StringFlag{
Name: "cattle-url",
Usage: "The URL endpoint to communicate with cattle server",
EnvVar: "CATTLE_URL",
},
cli.StringFlag{
Name: "cattle-access-key",
Usage: "The access key required to authenticate with cattle server",
EnvVar: "CATTLE_ACCESS_KEY",
},
cli.StringFlag{
Name: "cattle-secret-key",
Usage: "The secret key required to authenticate with cattle server",
EnvVar: "CATTLE_SECRET_KEY",
},
cli.IntFlag{
Name: "healthcheck-interval",
Value: 5000,
Usage: "set the frequency of performing healthchecks",
},
cli.IntFlag{
Name: "healthcheck-port",
Usage: "listen port for healthchecks",
},
cli.StringFlag{
Name: "docker-host",
Value: "unix:///var/run/docker.sock",
Usage: "The DOCKER_HOST to connect to",
EnvVar: "DOCKER_HOST",
},
}
logrus.Info("Running")
app.Run(os.Args)
}
func start(c *cli.Context) error {
logrus.Info("Starting")
cli, err := dockerClient.NewClient(c.String("docker-host"), "v1.22", nil, nil)
if err != nil {
return err
}
if _, err := cli.Info(context.Background()); err != nil {
return err
}
opts := &client.ClientOpts{
Url: c.String("cattle-url"),
AccessKey: c.String("cattle-access-key"),
SecretKey: c.String("cattle-secret-key"),
}
client, err := client.NewRancherClient(opts)
if err != nil {
return err
}
driverName := c.String("driver-name")
if driverName == "" {
return errors.New("--driver-name is required")
}
d, err := volumeplugin.NewRancherStorageDriver(driverName, client, cli)
// DriveName: driver,
// Basedir: DefaultBasedir,
// CreateSupported: true,
// Command: driver,
// client: client,
// state: &RancherState{
// client: client,
// },
// mounter: &mount.SafeFormatAndMount{Interface: mount.New(), Runner: exec.New()},
// FsType: DefaultFsType,
if err != nil {
return err
}
logrus.Infof("Starting plugin for %s", driverName)
h := volume.NewHandler(d)
if c.Int("healthcheck-port") > 0 {
go func() {
err := healthcheck.StartHealthCheck(c.Int("healthcheck-port"))
logrus.Fatalf("Error while running healthcheck [%v]", err)
}()
}
volumeplugin.ExtendHandler(h, d)
volumeplugin.ForceSymlinkInDockerPlugins(driverName)
return h.ServeUnix("root", volumeplugin.RancherSocketFile(driverName))
}