-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathetcddb_conf.go
145 lines (119 loc) · 4.21 KB
/
etcddb_conf.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
package etcddb
import (
"strings"
"time"
"github.com/coreos/pkg/capnslog"
"github.com/singnet/snet-daemon/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// EtcdClientConf config
// ConnectionTimeout - timeout for failing to establish a connection
// RequestTimeout - per request timeout
// Endpoints - cluster endpoints
type EtcdClientConf struct {
ConnectionTimeout time.Duration `json:"connection_timeout" mapstructure:"connection_timeout"`
RequestTimeout time.Duration `json:"request_timeout" mapstructure:"request_timeout"`
Endpoints []string
}
// GetEtcdClientConf gets EtcdServerConf from viper
// The DefaultEtcdClientConf is used in case the PAYMENT_CHANNEL_STORAGE_CLIENT field
// is not set in the configuration file
func GetEtcdClientConf(vip *viper.Viper) (conf *EtcdClientConf, err error) {
key := config.PaymentChannelStorageClientKey
conf = &EtcdClientConf{}
subVip := config.SubWithDefault(vip, key)
err = subVip.Unmarshal(conf)
return
}
func normalizeDefaultConf(conf string) string {
return strings.Replace(conf, "_", "", -1)
}
// EtcdServerConf contains embedded etcd server config
// ID - unique name of the etcd server node
// Scheme - URL schema used to create client and peer and urls
// Host - host where the etcd server is executed
// ClientPort - port to listen clients, used together with
// Schema and host to compose listen-client-urls (see link below)
// PeerPort - port to listen etcd peers, used together with
// Schema and host to compose listen-client-urls (see link below)
// Token - unique initial cluster token. Using unique token etcd can generate unique
// cluster IDs and member IDs for the clusters even if they otherwise have
// the exact same configuration. This can protect etcd from
// cross-cluster-interaction, which might corrupt the clusters.
// StartupTimeout - time to wait the etcd server successfully started
// Enabled - enable running embedded etcd server
// For more details see etcd Clustering Guide link:
// https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/clustering.md
type EtcdServerConf struct {
ID string
Scheme string
Host string
ClientPort int `json:"client_port" mapstructure:"CLIENT_PORT"`
PeerPort int `json:"peer_port" mapstructure:"PEER_PORT"`
Token string
Cluster string
StartupTimeout time.Duration `json:"startup_timeout" mapstructure:"startup_timeout"`
Enabled bool
DataDir string `json:"data_dir" mapstructure:"DATA_DIR"`
LogLevel string `json:"log_level" mapstructure:"LOG_LEVEL"`
}
// GetEtcdServerConf gets EtcdServerConf from viper
// The DefaultEtcdServerConf is used in case the PAYMENT_CHANNEL_STORAGE_SERVER field
// is not set in the configuration file
func GetEtcdServerConf(vip *viper.Viper) (conf *EtcdServerConf, err error) {
key := config.PaymentChannelStorageServerKey
conf = &EtcdServerConf{}
subVip := config.SubWithDefault(vip, key)
err = subVip.Unmarshal(conf)
if err != nil {
return
}
if !vip.InConfig(strings.ToLower(key)) {
return
}
conf.Enabled = true
err = vip.UnmarshalKey(key, conf)
if err != nil {
return
}
err = initEtcdLogger(conf)
return
}
// capnslog to logrus formatter implementation
// with methods Format and Flush
type capnslogToLogrusLogFormatter struct {
}
func (formatter *capnslogToLogrusLogFormatter) Format(pkg string, level capnslog.LogLevel,
depth int, entries ...interface{}) {
l := log.WithField("pkg", pkg)
switch level {
case capnslog.CRITICAL, capnslog.ERROR:
l.Error(entries)
case capnslog.WARNING, capnslog.NOTICE:
l.Warning(entries)
case capnslog.INFO:
l.Info(entries)
case capnslog.DEBUG:
fallthrough
case capnslog.TRACE:
l.Debug(entries)
default:
l.Warning("Unknown log level", level)
}
}
func (formatter *capnslogToLogrusLogFormatter) Flush() {
}
func initEtcdLogger(conf *EtcdServerConf) (err error) {
etcdLogger, err := capnslog.GetRepoLogger("github.com/coreos/etcd")
if err != nil {
return
}
logLevel, err := capnslog.ParseLevel(strings.ToUpper(conf.LogLevel))
if err != nil {
return
}
etcdLogger.SetRepoLogLevel(logLevel)
capnslog.SetFormatter(&capnslogToLogrusLogFormatter{})
return
}