blob: ba9c2425acc4667b7e84f86afa644d683673fe39 [file] [log] [blame]
Sergio Slobodrianf39aaf82017-02-28 16:10:16 -05001#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17import argparse
18import os
19import sys
20import yaml
21from twisted.internet import reactor
22from twisted.internet.defer import inlineCallbacks
23
24base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
25sys.path.append(base_dir)
26#sys.path.append(os.path.join(base_dir, '/netconf/protos/third_party'))
27
28from common.structlog_setup import setup_logging
29from common.utils.dockerhelpers import get_my_containers_name
30from common.utils.nethelpers import get_my_primary_local_ipv4
31#from netconf.grpc_client.grpc_client import GrpcClient
32#from netconf.nc_server import NCServer
33from dashd.dashd_impl import DashDaemon
34
35
36defs = dict(
37 config=os.environ.get('CONFIG', './dashd.yml'),
38 consul=os.environ.get('CONSUL', 'localhost:8500'),
39 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
40 get_my_primary_local_ipv4()),
41 grafana_url=os.environ.get('GRAFANA_URL',
42 'http://admin:admin@localhost:8882/api'),
43 kafka=os.environ.get('KAFKA', None),
44 topic=os.environ.get('KAFKA_TOPIC', 'voltha.kpis'),
45 docker_host=os.environ.get('DOCKER_HOST', None),
46
47 fluentd=os.environ.get('FLUENTD', None),
48 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
49 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
50 get_my_primary_local_ipv4()),
51)
52
53
54def parse_args():
55 parser = argparse.ArgumentParser("Manage Grafana dashboards")
56
57 _help = ('Path to dashd.yml config file (default: %s). '
58 'If relative, it is relative to main.py of dashd.'
59 % defs['config'])
60 parser.add_argument('-c', '--config',
61 dest='config',
62 action='store',
63 default=defs['config'],
64 help=_help)
65
66 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
67 parser.add_argument(
68 '-C', '--consul', dest='consul', action='store',
69 default=defs['consul'],
70 help=_help)
71
72 _help = '<hostname>:<port> to the kafka bus (default: %s)' % defs['kafka']
73 parser.add_argument(
74 '-k', '--kafka', dest='kafka', action='store',
75 default=defs['kafka'],
76 help=_help)
77
78 _help = 'The kafka topic to listen to (default: %s)' % defs['topic']
79 parser.add_argument(
80 '-t', '--topic', dest='topic', action='store',
81 default=defs['topic'],
82 help=_help)
83
84 _help = 'The URL of the Grafana server (default: %s)' % \
85 defs['grafana_url']
86 parser.add_argument(
87 '-g', '--grafana_url', dest='grafana_url', action='store',
88 default=defs['grafana_url'],
89 help=_help)
90
91 _help = 'The docker host ip (default %s)' % \
92 defs['docker_host']
93 parser.add_argument(
94 '-d', '--docker_host', dest='docker_host', action='store',
95 default=defs['docker_host'],
96 help=_help)
97
98 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
99 'specified (None), the address from the config file is used'
100 % defs['fluentd'])
101 parser.add_argument('-F', '--fluentd',
102 dest='fluentd',
103 action='store',
104 default=defs['fluentd'],
105 help=_help)
106
107 _help = ('unique string id of this netconf server instance (default: %s)'
108 % defs['instance_id'])
109 parser.add_argument('-i', '--instance-id',
110 dest='instance_id',
111 action='store',
112 default=defs['instance_id'],
113 help=_help)
114
115 _help = 'omit startup banner log lines'
116 parser.add_argument('-n', '--no-banner',
117 dest='no_banner',
118 action='store_true',
119 default=False,
120 help=_help)
121
122 _help = "suppress debug and info logs"
123 parser.add_argument('-q', '--quiet',
124 dest='quiet',
125 action='count',
126 help=_help)
127
128 _help = 'enable verbose logging'
129 parser.add_argument('-v', '--verbose',
130 dest='verbose',
131 action='count',
132 help=_help)
133
134 _help = ('use docker container name as netconf server instance id'
135 ' (overrides -i/--instance-id option)')
136 parser.add_argument('--instance-id-is-container-name',
137 dest='instance_id_is_container_name',
138 action='store_true',
139 default=False,
140 help=_help)
141
142 args = parser.parse_args()
143
144 # post-processing
145
146 if args.instance_id_is_container_name:
147 args.instance_id = get_my_containers_name()
148
149 return args
150
151
152def load_config(args):
153 path = args.config
154 if path.startswith('.'):
155 dir = os.path.dirname(os.path.abspath(__file__))
156 path = os.path.join(dir, path)
157 path = os.path.abspath(path)
158 with open(path) as fd:
159 config = yaml.load(fd)
160 return config
161
162
163banner = r'''
164 __
165| \ _ _ __ _ _
166||\ |/ ' |/ /| |__ __| |
167||/ | o |\ \| _ \ / _ |
168|__/ \_._|/_/|_| |_|\__._|
169'''
170
171
172def print_banner(log):
173 for line in banner.strip('\n').splitlines():
174 log.info(line)
175 log.info('(to stop: press Ctrl-C)')
176
177
178class Main(object):
179 def __init__(self):
180
181 self.args = args = parse_args()
182 self.config = load_config(args)
183
184 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
185 self.log = setup_logging(self.config.get('logging', {}),
186 args.instance_id,
187 verbosity_adjust=verbosity_adjust,
188 fluentd=args.fluentd)
189
190 self.dashd_server = None
191
192 self.dashd_server_started = False
193
194 self.exiting = False
195
196 if not args.no_banner:
197 print_banner(self.log)
198
199 self.startup_components()
200
201 def start(self):
202 #pass
203 self.start_reactor() # will not return except Keyboard interrupt
204
205 @inlineCallbacks
206 def startup_components(self):
207 try:
208 args = self.args
209
210 self.log.info('starting-dash-daemon', consul=args.consul,
211 grafana_url=args.grafana_url,
212 topic=args.topic)
213 self.dashd_server = yield \
214 DashDaemon(args.consul, #'10.0.2.15:8500',
Richard Jankowskid1232062017-07-17 14:10:23 -0400215 args.kafka,
Sergio Slobodrianf39aaf82017-02-28 16:10:16 -0500216 args.grafana_url, #'http://admin:admin@localhost:8882/api',
217 topic=args.topic ) #"voltha.kpis")
218
219 reactor.callWhenRunning(self.dashd_server.start)
220
221 self.log.info('started')
222 except:
223 e = sys.exc_info()
224 print("ERROR: ", e)
225
226
227 @inlineCallbacks
228 def shutdown_components(self):
229 """Execute before the reactor is shut down"""
230 self.log.info('exiting-on-keyboard-interrupt')
231 self.exiting = True
232
233 def start_reactor(self):
234 reactor.callWhenRunning(
235 lambda: self.log.info('twisted-reactor-started'))
236
237 reactor.addSystemEventTrigger('before', 'shutdown',
238 self.shutdown_components)
239 reactor.run()
240
241
242if __name__ == '__main__':
243 Main().start()