This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathconfig.go
150 lines (123 loc) · 4.01 KB
/
config.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
package main
import (
"errors"
"net"
"os"
"path"
"github.com/gluster/glusterd2/gdctx"
"github.com/gluster/glusterd2/store"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
config "github.com/spf13/viper"
)
const (
defaultLogLevel = "debug"
defaultClientAddress = ":24007"
defaultPeerAddress = ":24008"
defaultConfName = "glusterd"
)
// Slices,Arrays cannot be constants :(
var (
defaultConfPaths = []string{
"/etc/glusterd",
".",
}
)
// parseFlags sets up the flags and parses them, this needs to be called before any other operation
func parseFlags() {
flag.String("workdir", "", "Working directory for GlusterD. (default: current directory)")
flag.String("localstatedir", "", "Directory to store local state information. (default: workdir)")
flag.String("rundir", "", "Directory to store runtime data. (default: workdir/run)")
flag.String("logdir", "", "Directory to store logs. (default: workdir/log)")
flag.String("logfile", "-", "Log file name. (default: STDERR)")
flag.String("config", "", "Configuration file for GlusterD. By default looks for glusterd.(yaml|toml|json) in /etc/glusterd and current working directory.")
flag.String("loglevel", defaultLogLevel, "Severity of messages to be logged.")
flag.String("clientaddress", defaultClientAddress, "Address to bind the REST service.")
flag.String("peeraddress", defaultPeerAddress, "Address to bind the inter glusterd2 RPC service.")
// TODO: SSL/TLS is currently only implemented for REST interface
flag.String("cert-file", "", "Certificate used for SSL/TLS connections from clients to glusterd2.")
flag.String("key-file", "", "Private key for the SSL/TLS certificate.")
store.InitFlags()
flag.Parse()
}
// setDefaults sets defaults values for config options not available as a flag,
// and flags which don't have default values
func setDefaults() error {
cwd, err := os.Getwd()
if err != nil {
return err
}
wd := config.GetString("workdir")
if wd == "" {
config.SetDefault("workdir", cwd)
wd = cwd
}
if config.GetString("localstatedir") == "" {
config.SetDefault("localstatedir", wd)
}
if config.GetString("rundir") == "" {
config.SetDefault("rundir", path.Join(wd, "run"))
}
if config.GetString("logdir") == "" {
config.SetDefault("logdir", path.Join(wd, "log"))
}
// Set default peer port. This shouldn't be configurable.
config.SetDefault("defaultpeerport", defaultPeerAddress[1:])
// Set peer address.
host, port, err := net.SplitHostPort(config.GetString("peeraddress"))
if err != nil {
return errors.New("invalid peer address specified")
}
if host == "" {
host = gdctx.HostIP
}
if port == "" {
port = config.GetString("defaultpeerport")
}
config.SetDefault("peeraddress", host+":"+port)
return nil
}
func dumpConfigToLog() {
l := log.NewEntry(log.StandardLogger())
for k, v := range config.AllSettings() {
l = l.WithField(k, v)
}
l.Debug("running with configuration")
}
func initConfig(confFile string) error {
// Read in configuration from file
// If a config file is not given try to read from default paths
// If a config file was given, read in configration from that file.
// If the file is not present panic.
if confFile == "" {
config.SetConfigName(defaultConfName)
for _, p := range defaultConfPaths {
config.AddConfigPath(p)
}
} else {
config.SetConfigFile(confFile)
}
if err := config.ReadInConfig(); err != nil {
if confFile == "" {
log.WithFields(log.Fields{
"paths": defaultConfPaths,
"config": defaultConfName + ".(toml|yaml|json)",
"error": err,
}).Debug("failed to read any config files, continuing with defaults")
} else {
log.WithError(err).WithField("file", confFile).Error(
"failed to read given config file")
return err
}
} else {
log.WithField("file", config.ConfigFileUsed()).Info("loaded configuration from file")
}
// Use config given by flags
config.BindPFlags(flag.CommandLine)
// Finally initialize missing config with defaults
if err := setDefaults(); err != nil {
return err
}
dumpConfigToLog()
return nil
}