forked from xmidt-org/caduceus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
270 lines (232 loc) · 8.36 KB
/
main.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
/**
* Copyright 2017 Comcast Cable Communications Management, LLC
*
* 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 main
import (
"crypto/tls"
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"os/signal"
"runtime"
"time"
"github.com/go-kit/kit/log/level"
"github.com/xmidt-org/webpa-common/service/servicecfg"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/xmidt-org/webpa-common/concurrent"
"github.com/xmidt-org/webpa-common/logging"
"github.com/xmidt-org/webpa-common/server"
"github.com/xmidt-org/webpa-common/webhook"
"github.com/xmidt-org/webpa-common/webhook/aws"
)
const (
applicationName = "caduceus"
DEFAULT_KEY_ID = "current"
)
var (
GitCommit = "undefined"
Version = "undefined"
BuildTime = "undefined"
)
// caduceus is the driver function for Caduceus. It performs everything main() would do,
// except for obtaining the command-line arguments (which are passed to it).
func caduceus(arguments []string) int {
beginCaduceus := time.Now()
var (
f = pflag.NewFlagSet(applicationName, pflag.ContinueOnError)
v = viper.New()
logger, metricsRegistry, webPA, err = server.Initialize(applicationName, arguments, f, v, Metrics, webhook.Metrics, aws.Metrics)
)
if parseErr, done := printVersion(f, arguments); done {
// if we're done, we're exiting no matter what
if parseErr != nil {
friendlyError := fmt.Sprintf("failed to parse arguments. detailed error: %s", parseErr)
logging.Error(logger).Log(
logging.ErrorKey(),
friendlyError)
os.Exit(1)
}
os.Exit(0)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to initialize Viper environment: %s\n", err)
return 1
}
var (
infoLog = logging.Info(logger)
errorLog = logging.Error(logger)
debugLog = logging.Debug(logger)
)
infoLog.Log("configurationFile", v.ConfigFileUsed())
caduceusConfig := new(CaduceusConfig)
err = v.Unmarshal(caduceusConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to unmarshal configuration data into struct: %s\n", err)
return 1
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
MaxIdleConnsPerHost: caduceusConfig.Sender.NumWorkersPerSender,
ResponseHeaderTimeout: caduceusConfig.Sender.ResponseHeaderTimeout,
IdleConnTimeout: caduceusConfig.Sender.IdleConnTimeout,
}
caduceusSenderWrapper, err := SenderWrapperFactory{
NumWorkersPerSender: caduceusConfig.Sender.NumWorkersPerSender,
QueueSizePerSender: caduceusConfig.Sender.QueueSizePerSender,
CutOffPeriod: caduceusConfig.Sender.CutOffPeriod,
Linger: caduceusConfig.Sender.Linger,
DeliveryRetries: caduceusConfig.Sender.DeliveryRetries,
DeliveryInterval: caduceusConfig.Sender.DeliveryInterval,
MetricsRegistry: metricsRegistry,
Logger: logger,
Sender: (&http.Client{
Transport: tr,
Timeout: caduceusConfig.Sender.ClientTimeout,
}).Do,
}.New()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to initialize new caduceus sender wrapper: %s\n", err)
return 1
}
serverWrapper := &ServerHandler{
Logger: logger,
caduceusHandler: &CaduceusHandler{
senderWrapper: caduceusSenderWrapper,
Logger: logger,
},
errorRequests: metricsRegistry.NewCounter(ErrorRequestBodyCounter),
emptyRequests: metricsRegistry.NewCounter(EmptyRequestBodyCounter),
invalidCount: metricsRegistry.NewCounter(DropsDueToInvalidPayload),
incomingQueueDepthMetric: metricsRegistry.NewGauge(IncomingQueueDepth),
maxOutstanding: 0,
}
webhookFactory, err := webhook.NewFactory(v)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating new webhook factory: %s\n", err)
return 1
}
webhookRegistry, webhookHandler := webhookFactory.NewRegistryAndHandler(metricsRegistry)
webhookFactory.SetExternalUpdate(caduceusSenderWrapper.Update)
primaryHandler, err := NewPrimaryHandler(logger, v, serverWrapper, &webhookRegistry)
if err != nil {
fmt.Fprintf(os.Stderr, "Validator error: %v\n", err)
return 1
}
scheme := v.GetString("scheme")
if len(scheme) < 1 {
scheme = "https"
}
selfURL := &url.URL{
Scheme: scheme,
Host: v.GetString("fqdn") + v.GetString("primary.address"),
}
webhookFactory.Initialize(primaryHandler, selfURL, v.GetString("soa.provider"), webhookHandler, logger, metricsRegistry, nil)
_, runnable, done := webPA.Prepare(logger, nil, metricsRegistry, primaryHandler)
waitGroup, shutdown, err := concurrent.Execute(runnable)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to start device manager: %s\n", err)
return 1
}
var messageKey = logging.MessageKey()
if webhookFactory != nil {
// wait for DNS to propagate before subscribing to SNS
if err = webhookFactory.DnsReady(); err == nil {
debugLog.Log(messageKey, "Calling webhookFactory.PrepareAndStart. Server is ready to take on subscription confirmations")
webhookFactory.PrepareAndStart()
} else {
errorLog.Log(messageKey, "Server was not ready within a time constraint. SNS confirmation could not happen",
logging.ErrorKey(), err)
}
}
//
// Now, initialize the service discovery infrastructure
//
if false == v.IsSet("service") {
logger.Log(level.Key(), level.InfoValue(), logging.MessageKey(), "no service discovery configured")
} else {
e, err := servicecfg.NewEnvironment(logger, v.Sub("service"))
if err != nil {
logger.Log(level.Key(), level.ErrorValue(), logging.MessageKey(), "Unable to initialize service discovery environment", logging.ErrorKey(), err)
return 4
}
defer e.Close()
logger.Log(level.Key(), level.InfoValue(), "configurationFile", v.ConfigFileUsed())
e.Register()
}
// Attempt to obtain the current listener list from current system without having to wait for listener reregistration.
debugLog.Log(messageKey, "Attempting to obtain current listener list from source", "source",
v.GetString("start.apiPath"))
beginObtainList := time.Now()
startChan := make(chan webhook.Result, 1)
webhookFactory.Start.GetCurrentSystemsHooks(startChan)
var webhookStartResults webhook.Result = <-startChan
if webhookStartResults.Error != nil {
errorLog.Log(logging.ErrorKey(), webhookStartResults.Error)
} else {
// todo: add message
webhookFactory.SetList(webhook.NewList(webhookStartResults.Hooks))
caduceusSenderWrapper.Update(webhookStartResults.Hooks)
}
debugLog.Log(messageKey, "Current listener retrieval.", "elapsedTime", time.Since(beginObtainList))
infoLog.Log(messageKey, "Caduceus is up and running!", "elapsedTime", time.Since(beginCaduceus))
signals := make(chan os.Signal, 10)
signal.Notify(signals)
for exit := false; !exit; {
select {
case s := <-signals:
if s != os.Kill && s != os.Interrupt {
logger.Log(level.Key(), level.InfoValue(), logging.MessageKey(), "ignoring signal", "signal", s)
} else {
logger.Log(level.Key(), level.ErrorValue(), logging.MessageKey(), "exiting due to signal", "signal", s)
exit = true
}
case <-done:
logger.Log(level.Key(), level.ErrorValue(), logging.MessageKey(), "one or more servers exited")
exit = true
}
}
close(shutdown)
waitGroup.Wait()
// shutdown the sender wrapper gently so that all queued messages get serviced
caduceusSenderWrapper.Shutdown(true)
return 0
}
func printVersion(f *pflag.FlagSet, arguments []string) (error, bool) {
printVer := f.BoolP("version", "v", false, "displays the version number")
if err := f.Parse(arguments); err != nil {
return err, true
}
if *printVer {
printVersionInfo(os.Stdout)
return nil, true
}
return nil, false
}
func printVersionInfo(writer io.Writer) {
fmt.Fprintf(writer, "%s:\n", applicationName)
fmt.Fprintf(writer, " version: \t%s\n", Version)
fmt.Fprintf(writer, " go version: \t%s\n", runtime.Version())
fmt.Fprintf(writer, " built time: \t%s\n", BuildTime)
fmt.Fprintf(writer, " git commit: \t%s\n", GitCommit)
fmt.Fprintf(writer, " os/arch: \t%s/%s\n", runtime.GOOS, runtime.GOARCH)
}
func main() {
os.Exit(caduceus(os.Args))
}