forked from NetApp/netappdvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (137 loc) · 4.81 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
153
154
155
156
157
158
159
160
// Copyright 2016 NetApp, Inc. All Rights Reserved.
package main
import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"runtime"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
"github.com/netapp/netappdvp/storage_drivers"
"github.com/netapp/netappdvp/utils"
)
var (
debug = flag.Bool("debug", false, "Enable debugging output")
configFile = flag.String("config", "config.json", "Path to configuration file")
driverID = flag.String("volume-driver", "netapp", "Register as a docker volume plugin with this driver name")
port = flag.String("port", "", "Listen on this port instead of using a bsd socket")
printVersion = flag.Bool("version", false, "Print version and exit")
)
func initLogging(logName string) *os.File {
logRoot := "/var/log/netappdvp"
// if logdir doesn't exist, make it
dir, err := os.Lstat(logRoot)
if os.IsNotExist(err) {
if err := os.MkdirAll(logRoot, 0755); err != nil {
fmt.Printf("problem creating log directory: '%v' error: %v", logRoot, err)
os.Exit(1)
}
}
// if logRoot isn't a directory, error
if dir != nil && !dir.IsDir() {
fmt.Printf("log directory '%v' exists and it's not a directory, please remove", logRoot)
os.Exit(1)
}
// open a file for logging
logFileLocation := ""
switch runtime.GOOS {
case utils.Linux:
logFileLocation = logRoot + "/" + logName + ".log"
break
case utils.Darwin:
logFileLocation = logRoot + "/" + logName + ".log"
break
case utils.Windows:
logFileLocation = logName + ".log"
break
}
logFile, err := os.OpenFile(logFileLocation, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("error opening log file: %v error: %v", logFileLocation, err)
os.Exit(1)
}
log.SetOutput(logFile) // os.Stderr OR logFile
if *debug == true {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
fmt.Printf("Logfile Location (Level: %s): %s\n", log.GetLevel().String(), logFileLocation)
return logFile
}
func main() {
// initially log to console, we'll switch to a file once we know where to write it
log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) // default for logrus
log.SetOutput(os.Stderr)
if *debug == true {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
rand.Seed(int64(time.Now().Nanosecond()))
if *printVersion == true {
fmt.Printf("NetApp Docker Volume Plugin version %v\n", storage_drivers.DriverVersion)
os.Exit(0)
}
// open config file and read contents in to configJson
fileContents, fileErr := ioutil.ReadFile(*configFile)
if fileErr != nil {
log.Error("Error reading configuration file: ", fileErr)
os.Exit(1)
}
// validate the common settings and, if successful, we can continue and validate driver specific configuration
configJSON := string(fileContents)
commonConfig, commonConfigErr := storage_drivers.ValidateCommonSettings(configJSON)
if commonConfigErr != nil {
log.Errorf("Problem while validating configuration file: %v error: %v", *configFile, commonConfigErr)
os.Exit(1)
}
log.WithFields(log.Fields{
"Version": commonConfig.Version,
"StorageDriverName": commonConfig.StorageDriverName,
"Debug": commonConfig.Debug,
"DisableDelete": commonConfig.DisableDelete,
"StoragePrefixRaw": string(commonConfig.StoragePrefixRaw),
}).Debugf("Parsed commonConfig")
// lookup the specified storageDriver
storageDriver := storage_drivers.Drivers[commonConfig.StorageDriverName]
if storageDriver == nil {
log.Errorf("Unknown storage driver '%v' in configuration file: %v", commonConfig.StorageDriverName, *configFile)
os.Exit(1)
}
// initialize the specified storageDriver which also triggers a call to Validate
if initializeErr := storageDriver.Initialize(configJSON); initializeErr != nil {
log.Errorf("Problem initializing storage driver: '%v' error: %v", commonConfig.StorageDriverName, initializeErr)
os.Exit(1)
}
logFile := initLogging(*driverID)
defer logFile.Close() // don't forget to close it
log.Infof("Using storage driver: %v", commonConfig.StorageDriverName)
log.Infof("Using config: %v", *commonConfig)
volumeDir := filepath.Join(volume.DefaultDockerRootDirectory, *driverID)
log.WithFields(log.Fields{
"volumeDir": volumeDir,
"volume-driver": *driverID,
"port": *port,
}).Info("Starting docker volume plugin with the following options:")
// plugin connection registered in /var/run/docker/plugins
d, err := newNetAppDockerVolumePlugin(volumeDir, *commonConfig)
if err != nil {
log.Error(err)
os.Exit(1)
}
h := volume.NewHandler(d)
if *port != "" {
log.Info(h.ServeTCP(*driverID, ":"+*port, nil))
} else {
log.Info(h.ServeUnix(*driverID, 0)) // 0 is the unix group to start as (root gid)
}
log.SetOutput(os.Stderr)
log.Errorf("Unexpected exit")
}