blob: f51a8ff83132d8256aa2b6a3faaee6eaded92fe7 [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
Sergio Slobodrianf39aaf82017-02-28 16:10:16 -050047 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
48 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
49 get_my_primary_local_ipv4()),
50)
51
52
53def parse_args():
54 parser = argparse.ArgumentParser("Manage Grafana dashboards")
55
56 _help = ('Path to dashd.yml config file (default: %s). '
57 'If relative, it is relative to main.py of dashd.'
58 % defs['config'])
59 parser.add_argument('-c', '--config',
60 dest='config',
61 action='store',
62 default=defs['config'],
63 help=_help)
64
65 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
66 parser.add_argument(
67 '-C', '--consul', dest='consul', action='store',
68 default=defs['consul'],
69 help=_help)
70
71 _help = '<hostname>:<port> to the kafka bus (default: %s)' % defs['kafka']
72 parser.add_argument(
73 '-k', '--kafka', dest='kafka', action='store',
74 default=defs['kafka'],
75 help=_help)
76
77 _help = 'The kafka topic to listen to (default: %s)' % defs['topic']
78 parser.add_argument(
79 '-t', '--topic', dest='topic', action='store',
80 default=defs['topic'],
81 help=_help)
82
83 _help = 'The URL of the Grafana server (default: %s)' % \
84 defs['grafana_url']
85 parser.add_argument(
86 '-g', '--grafana_url', dest='grafana_url', action='store',
87 default=defs['grafana_url'],
88 help=_help)
89
90 _help = 'The docker host ip (default %s)' % \
91 defs['docker_host']
92 parser.add_argument(
93 '-d', '--docker_host', dest='docker_host', action='store',
94 default=defs['docker_host'],
95 help=_help)
96
Sergio Slobodrianf39aaf82017-02-28 16:10:16 -050097 _help = ('unique string id of this netconf server instance (default: %s)'
98 % defs['instance_id'])
99 parser.add_argument('-i', '--instance-id',
100 dest='instance_id',
101 action='store',
102 default=defs['instance_id'],
103 help=_help)
104
105 _help = 'omit startup banner log lines'
106 parser.add_argument('-n', '--no-banner',
107 dest='no_banner',
108 action='store_true',
109 default=False,
110 help=_help)
111
112 _help = "suppress debug and info logs"
113 parser.add_argument('-q', '--quiet',
114 dest='quiet',
115 action='count',
116 help=_help)
117
118 _help = 'enable verbose logging'
119 parser.add_argument('-v', '--verbose',
120 dest='verbose',
121 action='count',
122 help=_help)
123
124 _help = ('use docker container name as netconf server instance id'
125 ' (overrides -i/--instance-id option)')
126 parser.add_argument('--instance-id-is-container-name',
127 dest='instance_id_is_container_name',
128 action='store_true',
129 default=False,
130 help=_help)
131
132 args = parser.parse_args()
133
134 # post-processing
135
136 if args.instance_id_is_container_name:
137 args.instance_id = get_my_containers_name()
138
139 return args
140
141
142def load_config(args):
143 path = args.config
144 if path.startswith('.'):
145 dir = os.path.dirname(os.path.abspath(__file__))
146 path = os.path.join(dir, path)
147 path = os.path.abspath(path)
148 with open(path) as fd:
149 config = yaml.load(fd)
150 return config
151
152
153banner = r'''
154 __
155| \ _ _ __ _ _
156||\ |/ ' |/ /| |__ __| |
157||/ | o |\ \| _ \ / _ |
158|__/ \_._|/_/|_| |_|\__._|
159'''
160
161
162def print_banner(log):
163 for line in banner.strip('\n').splitlines():
164 log.info(line)
165 log.info('(to stop: press Ctrl-C)')
166
167
168class Main(object):
169 def __init__(self):
170
171 self.args = args = parse_args()
172 self.config = load_config(args)
173
174 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
175 self.log = setup_logging(self.config.get('logging', {}),
176 args.instance_id,
khenaidoo6b766f12018-03-13 14:29:11 -0400177 verbosity_adjust=verbosity_adjust)
Sergio Slobodrianf39aaf82017-02-28 16:10:16 -0500178
179 self.dashd_server = None
180
181 self.dashd_server_started = False
182
183 self.exiting = False
184
185 if not args.no_banner:
186 print_banner(self.log)
187
188 self.startup_components()
189
190 def start(self):
191 #pass
192 self.start_reactor() # will not return except Keyboard interrupt
193
194 @inlineCallbacks
195 def startup_components(self):
196 try:
197 args = self.args
198
199 self.log.info('starting-dash-daemon', consul=args.consul,
200 grafana_url=args.grafana_url,
201 topic=args.topic)
202 self.dashd_server = yield \
203 DashDaemon(args.consul, #'10.0.2.15:8500',
Richard Jankowskid1232062017-07-17 14:10:23 -0400204 args.kafka,
Sergio Slobodrianf39aaf82017-02-28 16:10:16 -0500205 args.grafana_url, #'http://admin:admin@localhost:8882/api',
206 topic=args.topic ) #"voltha.kpis")
207
208 reactor.callWhenRunning(self.dashd_server.start)
209
210 self.log.info('started')
211 except:
212 e = sys.exc_info()
213 print("ERROR: ", e)
214
215
216 @inlineCallbacks
217 def shutdown_components(self):
218 """Execute before the reactor is shut down"""
219 self.log.info('exiting-on-keyboard-interrupt')
220 self.exiting = True
221
222 def start_reactor(self):
223 reactor.callWhenRunning(
224 lambda: self.log.info('twisted-reactor-started'))
225
226 reactor.addSystemEventTrigger('before', 'shutdown',
227 self.shutdown_components)
228 reactor.run()
229
230
231if __name__ == '__main__':
232 Main().start()