forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
298 lines (260 loc) · 8.29 KB
/
env.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
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package env contains common command line and initialization code for SCION services.
// If something is specific to one app, it should go into that app's code and not here.
//
// During initialization, SIGHUPs are masked. To call a function on each
// SIGHUP, pass the function when calling Init.
package env
import (
"fmt"
"io"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/config"
"github.com/scionproto/scion/go/lib/fatal"
"github.com/scionproto/scion/go/lib/infra/modules/itopo"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/sciond"
_ "github.com/scionproto/scion/go/lib/scrypto" // Make sure math/rand is seeded
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/util"
)
const (
DefaultLoggingLevel = "info"
// Default max size of log files in MiB
DefaultLoggingFileSize = 50
// Default max age of log file in days
DefaultLoggingFileMaxAge = 7
// Default file name for topology file (only the last element of the path)
DefaultTopologyPath = "topology.json"
// SciondInitConnectPeriod is the default total amount of time spent
// attempting to connect to sciond on start.
SciondInitConnectPeriod = 20 * time.Second
// ShutdownGraceInterval is the time applications wait after issuing a
// clean shutdown signal, before forcerfully tearing down the application.
ShutdownGraceInterval = 5 * time.Second
)
var sighupC chan os.Signal
func init() {
os.Setenv("TZ", "UTC")
sighupC = make(chan os.Signal, 1)
signal.Notify(sighupC, syscall.SIGHUP)
}
var _ config.Config = (*General)(nil)
type General struct {
// ID is the SCION element ID. This is used to choose the relevant
// portion of the topology file for some services.
ID string
// ConfigDir for loading extra files (currently, only topology.json)
ConfigDir string
// Topology is the file path for the local topology JSON file.
Topology string
// ReconnectToDispatcher can be set to true to enable transparent dispatcher
// reconnects.
ReconnectToDispatcher bool
}
// InitDefaults sets the default value for Topology if not already set.
func (cfg *General) InitDefaults() {
if cfg.Topology == "" {
cfg.Topology = filepath.Join(cfg.ConfigDir, DefaultTopologyPath)
}
}
func (cfg *General) Validate() error {
if cfg.ID == "" {
return serrors.New("No element ID specified")
}
return cfg.checkDir()
}
// checkDir checks that the config dir is a directory.
func (cfg *General) checkDir() error {
if cfg.ConfigDir != "" {
info, err := os.Stat(cfg.ConfigDir)
if err != nil {
return err
}
if !info.IsDir() {
return common.NewBasicError("Not a directory", nil, "dir", cfg.ConfigDir)
}
}
return nil
}
func (cfg *General) Sample(dst io.Writer, path config.Path, ctx config.CtxMap) {
config.WriteString(dst, fmt.Sprintf(generalSample, ctx[config.ID]))
}
func (cfg *General) ConfigName() string {
return "general"
}
var _ config.Config = (*SciondClient)(nil)
// SciondClient contains information for running snet with sciond.
type SciondClient struct {
// Path is the sciond path. It defaults to sciond.DefaultSCIONDPath.
Path string
// InitialConnectPeriod is the maximum amount of time spent attempting to
// connect to sciond on start.
InitialConnectPeriod util.DurWrap
}
func (cfg *SciondClient) InitDefaults() {
if cfg.Path == "" {
cfg.Path = sciond.DefaultSCIONDPath
}
if cfg.InitialConnectPeriod.Duration == 0 {
cfg.InitialConnectPeriod.Duration = SciondInitConnectPeriod
}
}
func (cfg *SciondClient) Validate() error {
if cfg.InitialConnectPeriod.Duration == 0 {
return serrors.New("InitialConnectPeriod must not be zero")
}
return nil
}
func (cfg *SciondClient) Sample(dst io.Writer, path config.Path, _ config.CtxMap) {
config.WriteString(dst, sciondClientSample)
}
func (cfg *SciondClient) ConfigName() string {
return "sd_client"
}
// SetupEnv initializes a basic environment for applications. If reloadF is not
// nil, the application will call reloadF whenever it receives a SIGHUP signal.
func SetupEnv(reloadF func()) {
setupSignals(reloadF)
}
// setupSignals sets up a goroutine that on received SIGTERM/SIGINT signals
// informs the application that it should shut down. If reloadF is not nil,
// setupSignals also calls reloadF on SIGHUP.
func setupSignals(reloadF func()) {
fatal.Check()
sig := make(chan os.Signal, 2)
signal.Notify(sig, os.Interrupt)
signal.Notify(sig, syscall.SIGTERM)
go func() {
defer log.LogPanicAndExit()
s := <-sig
log.Info("Received signal, exiting...", "signal", s)
fatal.Shutdown(ShutdownGraceInterval)
}()
if reloadF != nil {
go func() {
defer log.LogPanicAndExit()
for range sighupC {
log.Info("Received config reload signal")
reloadF()
}
}()
}
}
func ReloadTopology(topologyPath string) {
topo, err := itopo.LoadFromFile(topologyPath)
if err != nil {
log.Error("Unable to reload topology", "err", err)
return
}
if _, _, err := itopo.SetStatic(topo, true); err != nil {
log.Error("Unable to set topology", "err", err)
return
}
log.Info("Reloaded topology")
}
var _ config.Config = (*Metrics)(nil)
type Metrics struct {
config.NoDefaulter
config.NoValidator
// Prometheus contains the address to export prometheus metrics on. If
// not set, metrics are not exported.
Prometheus string
}
func (cfg *Metrics) Sample(dst io.Writer, path config.Path, _ config.CtxMap) {
config.WriteString(dst, metricsSample)
}
func (cfg *Metrics) ConfigName() string {
return "metrics"
}
func (cfg *Metrics) StartPrometheus() {
fatal.Check()
if cfg.Prometheus != "" {
http.Handle("/metrics", promhttp.Handler())
log.Info("Exporting prometheus metrics", "addr", cfg.Prometheus)
go func() {
defer log.LogPanicAndExit()
if err := http.ListenAndServe(cfg.Prometheus, nil); err != nil {
fatal.Fatal(common.NewBasicError("HTTP ListenAndServe error", err))
}
}()
}
}
// Tracing contains configuration for tracing.
type Tracing struct {
// Enabled enables tracing for this service.
Enabled bool
// Enable debug mode.
Debug bool
// Agent is the address of the local agent that handles the reported
// traces. (default: localhost:6831)
Agent string
}
func (cfg *Tracing) InitDefaults() {
if cfg.Agent == "" {
cfg.Agent = fmt.Sprintf("%s:%d",
jaeger.DefaultUDPSpanServerHost, jaeger.DefaultUDPSpanServerPort)
}
}
func (cfg *Tracing) Sample(dst io.Writer, path config.Path, _ config.CtxMap) {
config.WriteString(dst, tracingSample)
}
func (cfg *Tracing) ConfigName() string {
return "tracing"
}
// NewTracer creates a new Tracer for the given configuration. In case tracing
// is disabled this still returns noop-objects for convenience of the caller.
func (cfg *Tracing) NewTracer(id string) (opentracing.Tracer, io.Closer, error) {
traceConfig := jaegercfg.Configuration{
ServiceName: id,
Disabled: !cfg.Enabled,
Reporter: &jaegercfg.ReporterConfig{
LocalAgentHostPort: cfg.Agent,
},
}
if cfg.Debug {
traceConfig.Sampler = &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
}
}
bp := jaeger.NewBinaryPropagator(nil)
return traceConfig.NewTracer(
jaegercfg.Extractor(opentracing.Binary, bp),
jaegercfg.Injector(opentracing.Binary, bp))
}
// QUIC contains configuration for control-plane speakers.
type QUIC struct {
ResolutionFraction float64
Address string
CertFile string
KeyFile string
}
func (cfg *QUIC) Sample(dst io.Writer, path config.Path, _ config.CtxMap) {
config.WriteString(dst, quicSample)
}
func (cfg *QUIC) ConfigName() string {
return "quic"
}