-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (110 loc) · 3.03 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 (
"fmt"
"flag"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
dkvolume "github.com/docker/go-plugins-helpers/volume"
)
var (
pluginName = flag.String("name","cephfs","Docker plugin name for use on --volume-driver option")
cephcluter = flag.String("cluster","ceph","Ceph cluster")
rootMountDir = flag.String("mount",dkvolume.DefaultDockerRootDirectory, "Mount directory for volumes on host")
pluginDir = flag.String("plugins", "/run/docker/plugins", "Docker plugin directory for socket")
logDir = flag.String("logdir", "/var/log", "Logfile directory")
cephConfigFile = flag.String("config", "", "Ceph cluster config")
)
func init(){
flag.Parse()
}
func socketPath() string{
socket_name := *pluginName+".sock"
return filepath.Join(*pluginDir,socket_name)
}
func logfilePath() string{
return filepath.Join(*logDir,*pluginName+"-docker-cephfs.log")
}
func isDebugEnable() bool{
return os.Getenv("CEPHFS_DOCKER_DEBUG") == "1"
}
func setupLogging() (*os.File, error){
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetPrefix(*pluginName + "-volume-plugin: ")
logfileName := logfilePath()
if !isDebugEnable() && logfileName != ""{
logFile,err := os.OpenFile(logfileName,os.O_RDWR|os.O_CREATE|os.O_APPEND,0666)
if err != nil {
if os.IsPermission(err){
log.Printf("WARN:logging fallback to STDERR: %v",err)
}else{
return nil,err
}
}else{
log.Printf("INFO: setting log file: %s",logfileName)
log.SetOutput(logFile)
return logFile,nil
}
}
return nil,nil
}
func shutdownLogging(logFile *os.File){
if logFile != nil {
log.Println("INFO: closing log file")
logFile.Sync()
logFile.Close()
}
}
func reloadLogging(logFile *os.File) (*os.File,error) {
log.Println("INFO: reloading log")
if logFile != nil {
shutdownLogging(logFile)
}
return setupLogging()
}
func main(){
logFile,err := setupLogging()
if err != nil {
log.Panicf("Unable to setup logging: %s",err)
}
defer shutdownLogging(logFile)
d := newCephfsDriver(
*pluginName,
*cephcluter,
*rootMountDir,
*cephConfigFile,
)
log.Println("INFO: Creating Docker VolumeDriver Handler")
h := dkvolume.NewHandler(d)
dkvolume.NewHandler(d)
socket := socketPath()
log.Printf("INFO: Opening Socket for Docker to Connect: %s",socket)
err = os.MkdirAll(filepath.Dir(socket),os.ModeDir)
if err != nil {
log.Panicf("Error Creating socket directory: %s",err)
}
signalChannel := make(chan os.Signal,2)
signal.Notify(signalChannel,syscall.SIGTERM,syscall.SIGKILL,syscall.SIGHUP)
go func() {
for sig:= range signalChannel {
switch sig {
case syscall.SIGTERM,syscall.SIGKILL:
log.Printf("INFO: received TERM or KILL signal: %s",sig)
shutdownLogging(logFile)
os.Exit(0)
case syscall.SIGHUP:
log.Printf("INFO: received HUG signal: %s",sig)
logFile,err = reloadLogging(logFile)
if err != nil {
log.Printf("Unable to reload log:%s",err)
}
d.connect()
}
}
}()
log.Printf("Start ServeUnix..")
fmt.Println(h.ServeUnix("root", 8080))
fmt.Println("Atlantis")
}