-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
141 lines (120 loc) · 4.78 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
package config
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"strings"
)
const (
LogLevel = "log_level"
LogFormatJSON = "log_format_json"
NiceValue = "nice_value"
NiceRevertDelayMinutes = "nice_revert_delay_minutes"
SubscriberBindAddress = "subscriber_bind_address"
SubscriberPublisherAddress = "subscriber_publisher_address"
PublisherBindAddress = "publisher_bind_address"
PrometheusPort = "prometheus_port"
RunWithSudo = "run_with_sudo"
Chains = "chains"
)
// InitConfig initializes the configuration for the CLI. See documentation.
// Use the dump-config command to generate a config.yaml file.
func InitConfig() {
viper.SetDefault(LogLevel, "info")
viper.SetDefault(LogFormatJSON, false)
viper.SetDefault(NiceValue, -10)
viper.SetDefault(NiceRevertDelayMinutes, 5)
viper.SetDefault(SubscriberBindAddress, "")
viper.SetDefault(SubscriberPublisherAddress, "127.0.0.1:5555")
viper.SetDefault(PublisherBindAddress, "127.0.0.1:5555")
viper.SetDefault(PrometheusPort, 8083)
viper.SetDefault(RunWithSudo, false)
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/pocket-autonice")
viper.AddConfigPath("$HOME/.pocket-autonice")
viper.SetEnvPrefix("AUTONICE")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
panic(fmt.Errorf("fatal error reading config file: %w \n", err))
}
}
var level log.Level
switch strings.ToLower(viper.GetString(LogLevel)) {
case "panic":
level = log.PanicLevel
case "fatal":
level = log.FatalLevel
case "error":
level = log.ErrorLevel
case "warn", "warning":
level = log.WarnLevel
case "debug":
level = log.DebugLevel
case "trace":
level = log.TraceLevel
default:
level = log.InfoLevel
}
log.SetLevel(level)
logJson := viper.GetBool(LogFormatJSON)
var logFormatter log.Formatter
if logJson {
customFormatter := &log.JSONFormatter{}
logFormatter = customFormatter
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
} else {
customFormatter := &log.TextFormatter{}
logFormatter = customFormatter
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
customFormatter.PadLevelText = true
}
log.SetFormatter(logFormatter)
}
const ConfigExample = `# Place the config.yaml file in either the $HOME/.pocket-autonice directory
# or the /etc/pocket-autonice directory.
# All config values can be set with environment variables with precedence
# over this file by prefixing the uppercase key with "AUTONICE_".
# For example AUTONICE_LOG_LEVEL will set the log level.
# Autonice runs the Linux 'renice' command which is privileged. The program must
# either be run as root or with sudo.
# run_with_sudo: false
# For each blockchain running on the server, add a map from the relay network
# ID or chain ID to the Linux user under with that blockchain is running. Do
# not use 'root' as a user here as the renice is done at the user level.
# Boosting the nice of root may have unintended consequences on server
# performance. NOTE: by default NO CHAINS are configured and so no processes
# would be reniced by default. Thus this configuration is mandatory.
# chains:
# "0001": pocket # enables pocket renice during all relay sessions'
# "0005": fuse
# "0009": polygon
# etc...
# Port that pocket-core prometheus is configured on. This value can be found in
# the pocket-core config.json file.
# prometheus_port: 8083
# For all servers, the address to bind ZeroMQ subscriber socket. If pocket-core
# relies on a network relay blockchains on other servers over a LAN, then set
# this to the LAN IP address of the client node. Note that the pocket-core
# node should also be setup as a client to receive messages to it. If all
# blockchains are running locally, then this value can can be left as
# localhost (the default).
# subscriber_publisher_address: 127.0.0.1:5555
# For the pocket-core server, the address to bind the zeromq publisher to.
# publisher_address: 127.0.0.1:5555
# When a blockchain is receiving relays, the Linux user that it is running
# under will be upgraded to this nice value. Zero is normal, negative values
# boost priority. The max boost is at -20, though that is not recommended
# as the blockchain would then compete with essential kernel services.
# nice_value: -10
# Once the blockchain stops receiving relays, the client will wait for this
# many minutes before reverting to a nice value of 0.
# nice_revert_delay_minutes: 5
# Logs will be output to this level of verbosity. Valid values are panic,
# fatal, error, warn, info, debug, and trace.
# log_level: info
# To make the logger output in JSON format, set this to true.
# log_format_json: false
`