-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsqadmin.go
191 lines (167 loc) · 5.1 KB
/
nsqadmin.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
package nsqadmin
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"sync"
"sync/atomic"
"github.com/nsqio/nsq/internal/http_api"
"github.com/nsqio/nsq/internal/lg"
"github.com/nsqio/nsq/internal/util"
"github.com/nsqio/nsq/internal/version"
)
type NSQAdmin struct {
sync.RWMutex
opts atomic.Value
httpListener net.Listener
waitGroup util.WaitGroupWrapper
notifications chan *AdminAction
graphiteURL *url.URL
httpClientTLSConfig *tls.Config
}
func New(opts *Options) *NSQAdmin {
if opts.Logger == nil {
opts.Logger = log.New(os.Stderr, opts.LogPrefix, log.Ldate|log.Ltime|log.Lmicroseconds)
}
n := &NSQAdmin{
notifications: make(chan *AdminAction),
}
n.swapOpts(opts)
var err error
opts.logLevel, err = lg.ParseLogLevel(opts.LogLevel, opts.Verbose)
if err != nil {
n.logf(LOG_FATAL, "%s", err)
os.Exit(1)
}
if len(opts.NSQDHTTPAddresses) == 0 && len(opts.NSQLookupdHTTPAddresses) == 0 {
n.logf(LOG_FATAL, "--nsqd-http-address or --lookupd-http-address required.")
os.Exit(1)
}
if len(opts.NSQDHTTPAddresses) != 0 && len(opts.NSQLookupdHTTPAddresses) != 0 {
n.logf(LOG_FATAL, "use --nsqd-http-address or --lookupd-http-address not both")
os.Exit(1)
}
// verify that the supplied address is valid
verifyAddress := func(arg string, address string) *net.TCPAddr {
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
n.logf(LOG_FATAL, "failed to resolve %s address (%s) - %s", arg, address, err)
os.Exit(1)
}
return addr
}
if opts.HTTPClientTLSCert != "" && opts.HTTPClientTLSKey == "" {
n.logf(LOG_FATAL, "--http-client-tls-key must be specified with --http-client-tls-cert")
os.Exit(1)
}
if opts.HTTPClientTLSKey != "" && opts.HTTPClientTLSCert == "" {
n.logf(LOG_FATAL, "--http-client-tls-cert must be specified with --http-client-tls-key")
os.Exit(1)
}
n.httpClientTLSConfig = &tls.Config{
InsecureSkipVerify: opts.HTTPClientTLSInsecureSkipVerify,
}
if opts.HTTPClientTLSCert != "" && opts.HTTPClientTLSKey != "" {
cert, err := tls.LoadX509KeyPair(opts.HTTPClientTLSCert, opts.HTTPClientTLSKey)
if err != nil {
n.logf(LOG_FATAL, "failed to LoadX509KeyPair %s, %s - %s",
opts.HTTPClientTLSCert, opts.HTTPClientTLSKey, err)
os.Exit(1)
}
n.httpClientTLSConfig.Certificates = []tls.Certificate{cert}
}
if opts.HTTPClientTLSRootCAFile != "" {
tlsCertPool := x509.NewCertPool()
caCertFile, err := ioutil.ReadFile(opts.HTTPClientTLSRootCAFile)
if err != nil {
n.logf(LOG_FATAL, "failed to read TLS root CA file %s - %s",
opts.HTTPClientTLSRootCAFile, err)
os.Exit(1)
}
if !tlsCertPool.AppendCertsFromPEM(caCertFile) {
n.logf(LOG_FATAL, "failed to AppendCertsFromPEM %s", opts.HTTPClientTLSRootCAFile)
os.Exit(1)
}
n.httpClientTLSConfig.RootCAs = tlsCertPool
}
// require that both the hostname and port be specified
for _, address := range opts.NSQLookupdHTTPAddresses {
verifyAddress("--lookupd-http-address", address)
}
for _, address := range opts.NSQDHTTPAddresses {
verifyAddress("--nsqd-http-address", address)
}
if opts.ProxyGraphite {
url, err := url.Parse(opts.GraphiteURL)
if err != nil {
n.logf(LOG_FATAL, "failed to parse --graphite-url='%s' - %s", opts.GraphiteURL, err)
os.Exit(1)
}
n.graphiteURL = url
}
if opts.AllowConfigFromCIDR != "" {
_, _, err := net.ParseCIDR(opts.AllowConfigFromCIDR)
if err != nil {
n.logf(LOG_FATAL, "failed to parse --allow-config-from-cidr='%s' - %s", opts.AllowConfigFromCIDR, err)
os.Exit(1)
}
}
n.logf(LOG_INFO, version.String("nsqadmin"))
return n
}
func (n *NSQAdmin) getOpts() *Options {
return n.opts.Load().(*Options)
}
func (n *NSQAdmin) swapOpts(opts *Options) {
n.opts.Store(opts)
}
func (n *NSQAdmin) RealHTTPAddr() *net.TCPAddr {
n.RLock()
defer n.RUnlock()
return n.httpListener.Addr().(*net.TCPAddr)
}
func (n *NSQAdmin) handleAdminActions() {
for action := range n.notifications {
content, err := json.Marshal(action)
if err != nil {
n.logf(LOG_ERROR, "failed to serialize admin action - %s", err)
}
httpclient := &http.Client{
Transport: http_api.NewDeadlineTransport(n.getOpts().HTTPClientConnectTimeout, n.getOpts().HTTPClientRequestTimeout),
}
n.logf(LOG_INFO, "POSTing notification to %s", n.getOpts().NotificationHTTPEndpoint)
resp, err := httpclient.Post(n.getOpts().NotificationHTTPEndpoint,
"application/json", bytes.NewBuffer(content))
if err != nil {
n.logf(LOG_ERROR, "failed to POST notification - %s", err)
}
resp.Body.Close()
}
}
func (n *NSQAdmin) Main() {
httpListener, err := net.Listen("tcp", n.getOpts().HTTPAddress)
if err != nil {
n.logf(LOG_FATAL, "listen (%s) failed - %s", n.getOpts().HTTPAddress, err)
os.Exit(1)
}
n.Lock()
n.httpListener = httpListener
n.Unlock()
httpServer := NewHTTPServer(&Context{n})
n.waitGroup.Wrap(func() {
http_api.Serve(n.httpListener, http_api.CompressHandler(httpServer), "HTTP", n.logf)
})
n.waitGroup.Wrap(func() { n.handleAdminActions() })
}
func (n *NSQAdmin) Exit() {
n.httpListener.Close()
close(n.notifications)
n.waitGroup.Wait()
}