-
Notifications
You must be signed in to change notification settings - Fork 327
/
rexray.go
344 lines (296 loc) · 8.3 KB
/
rexray.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//go:generate go generate ./core
//go:generate go run core/semver/semver.go -f mk -o semver.mk
//go:generate go run core/semver/semver.go -f env -o semver.env -x
package main
import (
"fmt"
golog "log"
"net/http"
"os"
"path/filepath"
"runtime/pprof"
"runtime/trace"
"strconv"
"strings"
"sync"
gofigCore "github.com/akutz/gofig"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/gotil"
log "github.com/sirupsen/logrus"
"github.com/rexray/rexray/cli"
"github.com/rexray/rexray/core"
"github.com/rexray/rexray/libstorage/api/context"
"github.com/rexray/rexray/libstorage/api/registry"
apitypes "github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/api/utils"
rrutils "github.com/rexray/rexray/util"
// import the libstorage config package
_ "github.com/rexray/rexray/libstorage/imports/config"
// load the profiler
_ "net/http/pprof"
)
func main() {
// If X_CSI_NATIVE is set to a truthy value then disable libStorage.
if v := os.Getenv("X_CSI_NATIVE"); v != "" {
if ok, _ := strconv.ParseBool(v); ok {
os.Setenv("LIBSTORAGE", "false")
}
}
// Brand libStorage's path structure with "rexray"
if v := os.Getenv("LIBSTORAGE_APPTOKEN"); v == "" {
os.Setenv("LIBSTORAGE_APPTOKEN", "rexray")
}
// Update REXRAY_HOME and LIBSTORAGE_HOME from the other if
// one is set and the other is not.
rrHome := os.Getenv("REXRAY_HOME")
lsHome := os.Getenv("LIBSTORAGE_HOME")
if rrHome != "" && lsHome == "" {
os.Setenv("LIBSTORAGE_HOME", rrHome)
} else if rrHome == "" && lsHome != "" {
os.Setenv("REXRAY_HOME", lsHome)
}
// Since flags are not parsed yet, manually check to see if a
// -l or --logLevel were provided via the command line's arguments.
if v, _ := rrutils.FindFlagVal(
"-l", os.Args...); v != "" {
os.Setenv("REXRAY_LOGLEVEL", v)
os.Setenv("LIBSTORAGE_LOGGING_LEVEL", v)
} else if v, _ := rrutils.FindFlagVal(
"--loglevel", os.Args...); v != "" {
os.Setenv("REXRAY_LOGLEVEL", v)
os.Setenv("LIBSTORAGE_LOGGING_LEVEL", v)
}
// Since flags are not parsed yet, manually check to see if a
// -c or --config were provided via the command line's arguments.
var configFile string
if v, _ := rrutils.FindFlagVal(
"-c", os.Args...); v != "" {
configFile = v
} else if v, _ := rrutils.FindFlagVal(
"--config", os.Args...); v != "" {
configFile = v
}
// Register REX-Ray's global config options.
registerConfig()
// Create a new context and process registration configs.
ctx := context.Background()
pathConfig := utils.NewPathConfig()
ctx = ctx.WithValue(
context.PathConfigKey, pathConfig)
registry.ProcessRegisteredConfigs(ctx)
// If the configFile value is empty then configure Gofig's global
// search locations.
var config gofig.Config
if configFile == "" {
gofigCore.SetGlobalConfigPath(pathConfig.Etc)
gofigCore.SetUserConfigPath(pathConfig.Home)
} else if !gotil.FileExists(configFile) {
fmt.Fprintf(os.Stderr,
"error: invalid config file: %s\n", configFile)
os.Exit(1)
} else {
rrutils.ValidateConfig(configFile)
config = gofigCore.New()
if err := config.ReadConfigFile(configFile); err != nil {
fmt.Fprintf(os.Stderr,
"error: invalid config file: %s: %v\n", configFile, err)
os.Exit(1)
}
config = config.Scope("rexray")
}
// Update the log level after it's been parsed from every possible
// location.
context.SetLogLevel(ctx, updateLogLevel(config))
// Get the context logger and update Go's standard log facility so that
// it emits logs using the context logger.
golog.SetFlags(0)
golog.SetOutput(rrutils.NewWriterFor(ctx.Infof))
// Print the status of DockerLegacyMode.
ctx.WithField("enabled", core.DockerLegacyMode).Info("DockerLegacyMode")
if config != nil {
ctx.WithField("path", configFile).Info("loaded custom config")
}
var (
err error
traceProfile *os.File
cpuProfile *os.File
exit sync.Once
)
createUserKnownHostsFile(ctx, pathConfig)
onExit := func() {
if traceProfile != nil {
ctx.Info("stopping trace profile")
trace.Stop()
traceProfile.Close()
ctx.Debug("stopped trace profile")
}
if cpuProfile != nil {
ctx.Info("stopping cpu profile")
pprof.StopCPUProfile()
cpuProfile.Close()
ctx.Debug("stopped cpu profile")
}
ctx.Info("exiting process")
}
var waitForExit chan int
core.RegisterSignalHandler(func(ctx apitypes.Context, s os.Signal) {
if ok, _ := core.IsExitSignal(s); ok {
waitForExit = make(chan int)
ctx.Info("received exit signal")
exit.Do(onExit)
}
})
if p := os.Getenv("REXRAY_TRACE_PROFILE"); p != "" {
if traceProfile, err = os.Create(p); err != nil {
panic(err)
}
if err = trace.Start(traceProfile); err != nil {
panic(err)
}
ctx.WithField("path", traceProfile.Name()).Info("trace profile enabled")
}
if p := os.Getenv("REXRAY_CPU_PROFILE"); p != "" {
if cpuProfile, err = os.Create(p); err != nil {
panic(err)
}
if err = pprof.StartCPUProfile(cpuProfile); err != nil {
panic(err)
}
ctx.WithField("path", cpuProfile.Name()).Info("cpu profile enabled")
}
if p := os.Getenv("REXRAY_PROFILE_ADDR"); p != "" {
go http.ListenAndServe(p, http.DefaultServeMux)
ctx.WithField("address", p).Info("http pprof enabled")
}
core.TrapSignals(ctx)
ctx.Debug("trapped signals")
cli.Execute(ctx, config)
ctx.Debug("completed cli execution")
exit.Do(onExit)
ctx.Debug("completed onExit at end of program")
// If an exit signal was received then just block until the
// handler exits the process.
if waitForExit != nil {
<-waitForExit
}
}
const (
configRR = "rexray"
configRRLL = configRR + ".loglevel"
)
func setConfigLogLevel(config gofig.Config, k1, k2 string, level string) {
v, ok := config.Get(k1).(map[string]interface{})
if ok {
v[strings.Replace(k2, k1+".", "", 1)] = level
} else {
config.Set(k2, level)
}
}
func updateLogLevel(config gofig.Config) (level log.Level) {
defer func() {
if config == nil {
return
}
szl := level.String()
setConfigLogLevel(
config, configRR, configRRLL, szl)
setConfigLogLevel(
config, apitypes.ConfigLogging, apitypes.ConfigLogLevel, szl)
}()
if ok, _ := strconv.ParseBool(os.Getenv("REXRAY_DEBUG")); ok {
enableDebugMode()
return log.DebugLevel
}
if ok, _ := strconv.ParseBool(os.Getenv("LIBSTORAGE_DEBUG")); ok {
enableDebugMode()
return log.DebugLevel
}
if ll := os.Getenv("REXRAY_LOGLEVEL"); ll != "" {
if lvl, err := log.ParseLevel(ll); err == nil {
if lvl == log.DebugLevel {
enableDebugMode()
} else {
setLogLevels(lvl)
}
return lvl
}
}
if ll := os.Getenv("LIBSTORAGE_LOGGING_LEVEL"); ll != "" {
if lvl, err := log.ParseLevel(ll); err == nil {
if lvl == log.DebugLevel {
enableDebugMode()
} else {
setLogLevels(lvl)
}
return lvl
}
}
if config != nil {
if ll := config.GetString(configRRLL); ll != "" {
if lvl, err := log.ParseLevel(ll); err == nil {
if lvl == log.DebugLevel {
enableDebugMode()
} else {
setLogLevels(lvl)
}
return lvl
}
}
if ll := config.GetString(apitypes.ConfigLogLevel); ll != "" {
if lvl, err := log.ParseLevel(ll); err == nil {
if lvl == log.DebugLevel {
enableDebugMode()
} else {
setLogLevels(lvl)
}
return lvl
}
}
}
return log.WarnLevel
}
func enableDebugMode() {
core.Debug = true
apitypes.Debug = true
os.Setenv("REXRAY_DEBUG", "true")
os.Setenv("LIBSTORAGE_DEBUG", "true")
setLogLevels(log.DebugLevel)
log.SetLevel(log.DebugLevel)
}
func setLogLevels(lvl log.Level) {
os.Setenv("REXRAY_LOGLEVEL", lvl.String())
os.Setenv("LIBSTORAGE_LOGGING_LEVEL", lvl.String())
}
func createUserKnownHostsFile(
ctx apitypes.Context,
pathConfig *apitypes.PathConfig) {
khPath := pathConfig.UserDefaultTLSKnownHosts
if gotil.FileExists(khPath) {
return
}
khDirPath := filepath.Dir(khPath)
os.MkdirAll(khDirPath, 0755)
khFile, err := os.Create(khPath)
if err != nil {
ctx.WithField("path", khPath).Fatal(
"failed to create known_hosts")
}
defer khFile.Close()
}
func registerConfig() {
r := gofigCore.NewRegistration("Global")
r.SetYAML(`
rexray:
logLevel: warn
`)
r.Key(gofig.String, "h", "",
"The libStorage host.", "rexray.host",
"host")
r.Key(gofig.String, "s", "",
"The libStorage service.", "rexray.service",
"service")
r.Key(gofig.String, "l", "warn",
"The log level (error, warn, info, debug)", "rexray.logLevel",
"logLevel")
gofigCore.Register(r)
}