-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
127 lines (105 loc) · 4.08 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
package shoveler
import (
"fmt"
"net/url"
"strings"
"github.com/spf13/viper"
)
type Config struct {
MQ string // Which technology to use for the MQ connection
AmqpURL *url.URL // AMQP URL (password comes from the token)
AmqpExchange string // Exchange to shovel messages
AmqpToken string // File location of the token
ListenPort int
ListenIp string
DestUdp []string
Debug bool
Verify bool
StompUser string
StompPassword string
StompURL *url.URL
StompTopic string
Metrics bool
MetricsPort int
StompCert string
StompCertKey string
QueueDir string
IpMapAll string
IpMap map[string]string
}
func (c *Config) ReadConfig() {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/etc/xrootd-monitoring-shoveler/") // path to look for the config file in
viper.AddConfigPath("$HOME/.xrootd-monitoring-shoveler") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AddConfigPath("config/")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Warningln("Unable to read in config file, will check environment for configuration:", err)
}
viper.SetEnvPrefix("SHOVELER")
// Autmatically look to the ENV for all "Gets"
viper.AutomaticEnv()
// Look for environment variables with underscores
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetDefault("mq", "amqp")
c.MQ = viper.GetString("mq")
if c.MQ == "amqp" {
viper.SetDefault("amqp.exchange", "shoveled-xrd")
viper.SetDefault("amqp.token_location", "/etc/xrootd-monitoring-shoveler/token")
// Get the AMQP URL
c.AmqpURL, err = url.Parse(viper.GetString("amqp.url"))
if err != nil {
panic(fmt.Errorf("Fatal error parsing AMQP URL: %s \n", err))
}
log.Debugln("AMQP URL:", c.AmqpURL.String())
// Get the AMQP Exchange
c.AmqpExchange = viper.GetString("amqp.exchange")
log.Debugln("AMQP Exchange:", c.AmqpExchange)
// Get the Token location
c.AmqpToken = viper.GetString("amqp.token_location")
log.Debugln("AMQP Token location:", c.AmqpToken)
} else if c.MQ == "stomp" {
viper.SetDefault("stomp.topic", "xrootd.shoveler")
c.StompUser = viper.GetString("stomp.user")
log.Debugln("STOMP User:", c.StompUser)
c.StompPassword = viper.GetString("stomp.password")
// Get the STOMP URL
c.StompURL, err = url.Parse(viper.GetString("stomp.url"))
if err != nil {
panic(fmt.Errorf("Fatal error parsing STOMP URL: %s \n", err))
}
log.Debugln("STOMP URL:", c.StompURL.String())
c.StompTopic = viper.GetString("stomp.topic")
log.Debugln("STOMP Topic:", c.StompTopic)
// Get the STOMP cert
c.StompCert = viper.GetString("stomp.cert")
log.Debugln("STOMP CERT:", c.StompCert)
// Get the STOMP certkey
c.StompCertKey = viper.GetString("stomp.certkey")
log.Debugln("STOMP CERTKEY:", c.StompCertKey)
} else {
log.Panic("MQ option is not one of the allowed ones (amqp, stomp)")
}
// Get the UDP listening parameters
viper.SetDefault("listen.port", 9993)
c.ListenPort = viper.GetInt("listen.port")
c.ListenIp = viper.GetString("listen.ip")
c.DestUdp = viper.GetStringSlice("outputs.destinations")
c.Debug = viper.GetBool("debug")
viper.SetDefault("verify", true)
c.Verify = viper.GetBool("verify")
// Metrics defaults
viper.SetDefault("metrics.enable", true)
c.Metrics = viper.GetBool("metrics.enable")
viper.SetDefault("metrics.port", 8000)
c.MetricsPort = viper.GetInt("metrics.port")
viper.SetDefault("queue_directory", "/var/spool/xrootd-monitoring-shoveler/queue")
c.QueueDir = viper.GetString("queue_directory")
// Configure the mapper
// First, check for the map environment variable
c.IpMapAll = viper.GetString("map.all")
// If the map is not set
c.IpMap = viper.GetStringMapString("map")
}