/
gluster.go
99 lines (90 loc) · 2.86 KB
/
gluster.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
package gluster
import (
"fmt"
"os"
"path/filepath"
"github.com/docker/go-plugins-helpers/volume"
"github.com/sapk/docker-volume-gluster/gluster/driver"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
//VerboseFlag flag to set more verbose level
VerboseFlag = "verbose"
//MountUniqNameFlag flag to set mount point based on definition and not name of volume to not have multile mount of same distant volume
MountUniqNameFlag = "mount-uniq"
//BasedirFlag flag to set the basedir of mounted volumes
BasedirFlag = "basedir"
longHelp = `
docker-volume-gluster (GlusterFS Volume Driver Plugin)
Provides docker volume support for GlusterFS.
== Version: %s - Branch: %s - Commit: %s - BuildTime: %s ==
`
)
var (
//Version version of running code
Version string
//Branch branch of running code
Branch string
//Commit commit of running code
Commit string
//BuildTime build time of running code
BuildTime string
//PluginAlias plugin alias name in docker
PluginAlias = "gluster"
//BaseDir of mounted volumes
BaseDir = ""
fuseOpts = ""
mountUniqName = false
rootCmd = &cobra.Command{
Use: "docker-volume-gluster",
Short: "GlusterFS - Docker volume driver plugin",
Long: longHelp,
PersistentPreRun: setupLogger,
}
daemonCmd = &cobra.Command{
Use: "daemon",
Short: "Run listening volume drive deamon to listen for mount request",
Run: DaemonStart,
}
versionCmd = &cobra.Command{
Use: "version",
Short: "Display current version and build date",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("\nVersion: %s - Branch: %s - Commit: %s - BuildTime: %s\n\n", Version, Branch, Commit, BuildTime)
},
}
)
//Init init the program
func Init() {
setupFlags()
rootCmd.Long = fmt.Sprintf(longHelp, Version, Branch, Commit, BuildTime)
rootCmd.AddCommand(versionCmd, daemonCmd)
if err := rootCmd.Execute(); err != nil {
logrus.Fatal(err)
}
}
//DaemonStart Start the deamon
func DaemonStart(cmd *cobra.Command, args []string) {
d := driver.Init(BaseDir, mountUniqName)
logrus.Debug(d)
h := volume.NewHandler(d)
logrus.Debug(h)
err := h.ServeUnix(PluginAlias, 0)
if err != nil {
logrus.Debug(err)
}
}
func setupFlags() {
rootCmd.PersistentFlags().BoolP(VerboseFlag, "v", os.Getenv("DEBUG") == "1", "Turns on verbose logging")
rootCmd.PersistentFlags().StringVarP(&BaseDir, BasedirFlag, "b", filepath.Join(volume.DefaultDockerRootDirectory, PluginAlias), "Mounted volume base directory")
daemonCmd.Flags().BoolVar(&mountUniqName, MountUniqNameFlag, os.Getenv("MOUNT_UNIQ") == "1", "Set mountpoint based on definition and not the name of volume")
}
func setupLogger(cmd *cobra.Command, args []string) {
if verbose, _ := cmd.Flags().GetBool(VerboseFlag); verbose {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("Debug mode on")
} else {
logrus.SetLevel(logrus.InfoLevel)
}
}