Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 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 | # |
| 17 | |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 18 | """Virtual OLT Hardware Abstraction main entry point""" |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 19 | |
| 20 | import argparse |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame] | 21 | import os |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 22 | import time |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 23 | |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame] | 24 | import yaml |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 25 | from simplejson import dumps |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 26 | from twisted.internet.defer import inlineCallbacks |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 27 | from zope.interface import implementer |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 28 | |
Zsolt Haraszti | 3bfff66 | 2016-12-14 23:41:49 -0800 | [diff] [blame] | 29 | from common.event_bus import EventBusClient |
| 30 | from common.manhole import Manhole |
Zsolt Haraszti | d70cd4d | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 31 | from common.structlog_setup import setup_logging |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 32 | from common.utils.dockerhelpers import get_my_containers_name |
| 33 | from common.utils.nethelpers import get_my_primary_interface, \ |
Khen Nursimulu | 441dedd | 2016-10-05 14:44:26 -0700 | [diff] [blame] | 34 | get_my_primary_local_ipv4 |
Zsolt Haraszti | 7eeb2b3 | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 35 | from voltha.adapters.loader import AdapterLoader |
Zsolt Haraszti | d70cd4d | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 36 | from voltha.coordinator import Coordinator |
Zsolt Haraszti | dafefe1 | 2016-11-14 21:29:58 -0800 | [diff] [blame] | 37 | from voltha.core.core import VolthaCore |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 38 | from voltha.northbound.diagnostics import Diagnostics |
Zsolt Haraszti | eb43507 | 2016-09-23 17:10:49 -0700 | [diff] [blame] | 39 | from voltha.northbound.grpc.grpc_server import VolthaGrpcServer |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 40 | from voltha.northbound.kafka.kafka_proxy import KafkaProxy, get_kafka_proxy |
Zsolt Haraszti | d70cd4d | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 41 | from voltha.northbound.rest.health_check import init_rest_service |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 42 | from voltha.protos.common_pb2 import LogLevel |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 43 | from voltha.registry import registry, IComponent |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 44 | from common.frameio.frameio import FrameIOManager |
| 45 | |
Zsolt Haraszti | dafefe1 | 2016-11-14 21:29:58 -0800 | [diff] [blame] | 46 | |
| 47 | VERSION = '0.9.0' |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 48 | |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 49 | defs = dict( |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 50 | config=os.environ.get('CONFIG', './voltha.yml'), |
Zsolt Haraszti | 553826c | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 51 | consul=os.environ.get('CONSUL', 'localhost:8500'), |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 52 | external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', |
| 53 | get_my_primary_local_ipv4()), |
Zsolt Haraszti | de22bbc | 2016-09-14 15:27:33 -0700 | [diff] [blame] | 54 | fluentd=os.environ.get('FLUENTD', None), |
Zsolt Haraszti | 553826c | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 55 | grpc_port=os.environ.get('GRPC_PORT', 50055), |
| 56 | instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')), |
| 57 | internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', |
| 58 | get_my_primary_local_ipv4()), |
| 59 | interface=os.environ.get('INTERFACE', get_my_primary_interface()), |
Zsolt Haraszti | de22bbc | 2016-09-14 15:27:33 -0700 | [diff] [blame] | 60 | rest_port=os.environ.get('REST_PORT', 8880), |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 61 | kafka=os.environ.get('KAFKA', 'localhost:9092'), |
Zsolt Haraszti | 3bfff66 | 2016-12-14 23:41:49 -0800 | [diff] [blame] | 62 | manhole_port=os.environ.get('MANHOLE_PORT', 12222), |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 63 | ) |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def parse_args(): |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 67 | parser = argparse.ArgumentParser() |
| 68 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 69 | _help = ('Path to voltha.yml config file (default: %s). ' |
| 70 | 'If relative, it is relative to main.py of voltha.' |
| 71 | % defs['config']) |
| 72 | parser.add_argument('-c', '--config', |
| 73 | dest='config', |
| 74 | action='store', |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 75 | default=defs['config'], |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 76 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 77 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 78 | _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul'] |
| 79 | parser.add_argument( |
| 80 | '-C', '--consul', dest='consul', action='store', |
| 81 | default=defs['consul'], |
| 82 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 83 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 84 | _help = ('<hostname> or <ip> at which Voltha is reachable from outside ' |
| 85 | 'the cluster (default: %s)' % defs['external_host_address']) |
| 86 | parser.add_argument('-E', '--external-host-address', |
| 87 | dest='external_host_address', |
| 88 | action='store', |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 89 | default=defs['external_host_address'], |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 90 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 91 | |
Zsolt Haraszti | 553826c | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 92 | _help = ('port number of the GRPC service exposed by voltha (default: %s)' |
| 93 | % defs['grpc_port']) |
| 94 | parser.add_argument('-g', '--grpc-port', |
| 95 | dest='grpc_port', |
| 96 | action='store', |
| 97 | default=defs['grpc_port'], |
| 98 | help=_help) |
Khen Nursimulu | 441dedd | 2016-10-05 14:44:26 -0700 | [diff] [blame] | 99 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 100 | _help = ('<hostname>:<port> to fluentd server (default: %s). (If not ' |
| 101 | 'specified (None), the address from the config file is used' |
| 102 | % defs['fluentd']) |
| 103 | parser.add_argument('-F', '--fluentd', |
| 104 | dest='fluentd', |
| 105 | action='store', |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 106 | default=defs['fluentd'], |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 107 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 108 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 109 | _help = ('<hostname> or <ip> at which Voltha is reachable from inside the' |
| 110 | 'cluster (default: %s)' % defs['internal_host_address']) |
| 111 | parser.add_argument('-H', '--internal-host-address', |
| 112 | dest='internal_host_address', |
| 113 | action='store', |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 114 | default=defs['internal_host_address'], |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 115 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 116 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 117 | _help = ('unique string id of this voltha instance (default: %s)' |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 118 | % defs['instance_id']) |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 119 | parser.add_argument('-i', '--instance-id', |
| 120 | dest='instance_id', |
| 121 | action='store', |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 122 | default=defs['instance_id'], |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 123 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 124 | |
| 125 | # TODO placeholder, not used yet |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 126 | _help = 'ETH interface to send (default: %s)' % defs['interface'] |
| 127 | parser.add_argument('-I', '--interface', |
| 128 | dest='interface', |
| 129 | action='store', |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 130 | default=defs['interface'], |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 131 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 132 | |
Zsolt Haraszti | 3bfff66 | 2016-12-14 23:41:49 -0800 | [diff] [blame] | 133 | _help = 'open ssh manhole at given port' |
| 134 | parser.add_argument('-m', '--manhole-port', |
| 135 | dest='manhole_port', |
| 136 | action='store', |
| 137 | type=int, |
| 138 | default=None, |
| 139 | help=_help) |
| 140 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 141 | _help = 'omit startup banner log lines' |
| 142 | parser.add_argument('-n', '--no-banner', |
| 143 | dest='no_banner', |
| 144 | action='store_true', |
| 145 | default=False, |
| 146 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 147 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 148 | _help = 'do not emit periodic heartbeat log messages' |
| 149 | parser.add_argument('-N', '--no-heartbeat', |
| 150 | dest='no_heartbeat', |
| 151 | action='store_true', |
| 152 | default=False, |
| 153 | help=_help) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 154 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 155 | _help = ('port number for the rest service (default: %d)' |
| 156 | % defs['rest_port']) |
| 157 | parser.add_argument('-R', '--rest-port', |
| 158 | dest='rest_port', |
| 159 | action='store', |
| 160 | type=int, |
Zsolt Haraszti | de22bbc | 2016-09-14 15:27:33 -0700 | [diff] [blame] | 161 | default=defs['rest_port'], |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 162 | help=_help) |
Zsolt Haraszti | de22bbc | 2016-09-14 15:27:33 -0700 | [diff] [blame] | 163 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 164 | _help = "suppress debug and info logs" |
| 165 | parser.add_argument('-q', '--quiet', |
| 166 | dest='quiet', |
Zsolt Haraszti | 1420def | 2016-09-18 00:07:31 -0700 | [diff] [blame] | 167 | action='count', |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 168 | help=_help) |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 169 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 170 | _help = 'enable verbose logging' |
| 171 | parser.add_argument('-v', '--verbose', |
| 172 | dest='verbose', |
Zsolt Haraszti | 1420def | 2016-09-18 00:07:31 -0700 | [diff] [blame] | 173 | action='count', |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 174 | help=_help) |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 175 | |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 176 | _help = ('use docker container name as voltha instance id' |
| 177 | ' (overrides -i/--instance-id option)') |
| 178 | parser.add_argument('--instance-id-is-container-name', |
| 179 | dest='instance_id_is_container_name', |
| 180 | action='store_true', |
| 181 | default=False, |
| 182 | help=_help) |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 183 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 184 | _help = ('<hostname>:<port> of the kafka broker (default: %s). (If not ' |
| 185 | 'specified (None), the address from the config file is used' |
| 186 | % defs['kafka']) |
| 187 | parser.add_argument('-K', '--kafka', |
| 188 | dest='kafka', |
| 189 | action='store', |
| 190 | default=defs['kafka'], |
| 191 | help=_help) |
| 192 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 193 | args = parser.parse_args() |
| 194 | |
| 195 | # post-processing |
| 196 | |
| 197 | if args.instance_id_is_container_name: |
| 198 | args.instance_id = get_my_containers_name() |
| 199 | |
| 200 | return args |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 201 | |
| 202 | |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame] | 203 | def load_config(args): |
| 204 | path = args.config |
| 205 | if path.startswith('.'): |
| 206 | dir = os.path.dirname(os.path.abspath(__file__)) |
| 207 | path = os.path.join(dir, path) |
| 208 | path = os.path.abspath(path) |
| 209 | with open(path) as fd: |
| 210 | config = yaml.load(fd) |
| 211 | return config |
| 212 | |
| 213 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 214 | def print_banner(log): |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 215 | log.info(' _ ______ __ ________ _____ ') |
| 216 | log.info('| | / / __ \/ / /_ __/ / / / |') |
| 217 | log.info('| | / / / / / / / / / /_/ / /| |') |
| 218 | log.info('| |/ / /_/ / /___/ / / __ / ___ |') |
| 219 | log.info('|___/\____/_____/_/ /_/ /_/_/ |_|') |
Zsolt Haraszti | 59b7a88 | 2016-09-12 14:42:59 -0700 | [diff] [blame] | 220 | log.info('(to stop: press Ctrl-C)') |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 221 | |
| 222 | |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 223 | @implementer(IComponent) |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 224 | class Main(object): |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 225 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 226 | def __init__(self): |
Zsolt Haraszti | 1420def | 2016-09-18 00:07:31 -0700 | [diff] [blame] | 227 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 228 | self.args = args = parse_args() |
| 229 | self.config = load_config(args) |
Zsolt Haraszti | 1420def | 2016-09-18 00:07:31 -0700 | [diff] [blame] | 230 | |
| 231 | verbosity_adjust = (args.verbose or 0) - (args.quiet or 0) |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 232 | self.log = setup_logging(self.config.get('logging', {}), |
| 233 | args.instance_id, |
Zsolt Haraszti | 1420def | 2016-09-18 00:07:31 -0700 | [diff] [blame] | 234 | verbosity_adjust=verbosity_adjust, |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 235 | fluentd=args.fluentd) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 236 | |
Rouzbahan Rashidi-Tabrizi | 1c3eba8 | 2016-10-27 21:47:18 -0400 | [diff] [blame] | 237 | # configurable variables from voltha.yml file |
| 238 | #self.configurable_vars = self.config.get('Constants', {}) |
| 239 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 240 | if not args.no_banner: |
| 241 | print_banner(self.log) |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 242 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 243 | self.startup_components() |
| 244 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 245 | if not args.no_heartbeat: |
| 246 | self.start_heartbeat() |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 247 | self.start_kafka_heartbeat(args.instance_id) |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 248 | |
Zsolt Haraszti | 3bfff66 | 2016-12-14 23:41:49 -0800 | [diff] [blame] | 249 | self.manhole = None |
| 250 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 251 | def start(self): |
| 252 | self.start_reactor() # will not return except Keyboard interrupt |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 253 | |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 254 | def stop(self): |
| 255 | pass |
| 256 | |
| 257 | def get_args(self): |
| 258 | """Allow access to command line args""" |
| 259 | return self.args |
| 260 | |
| 261 | def get_config(self): |
| 262 | """Allow access to content of config file""" |
| 263 | return self.config |
| 264 | |
Zsolt Haraszti | 7eeb2b3 | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 265 | @inlineCallbacks |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 266 | def startup_components(self): |
Khen Nursimulu | 3c74d3b | 2016-11-08 15:14:01 -0800 | [diff] [blame] | 267 | try: |
| 268 | self.log.info('starting-internal-components') |
Zsolt Haraszti | dafefe1 | 2016-11-14 21:29:58 -0800 | [diff] [blame] | 269 | |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 270 | registry.register('main', self) |
| 271 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 272 | yield registry.register( |
| 273 | 'coordinator', |
| 274 | Coordinator( |
| 275 | internal_host_address=self.args.internal_host_address, |
| 276 | external_host_address=self.args.external_host_address, |
| 277 | rest_port=self.args.rest_port, |
| 278 | instance_id=self.args.instance_id, |
| 279 | config=self.config, |
| 280 | consul=self.args.consul) |
| 281 | ).start() |
Zsolt Haraszti | dafefe1 | 2016-11-14 21:29:58 -0800 | [diff] [blame] | 282 | |
Khen Nursimulu | 3c74d3b | 2016-11-08 15:14:01 -0800 | [diff] [blame] | 283 | init_rest_service(self.args.rest_port) |
Zsolt Haraszti | eb43507 | 2016-09-23 17:10:49 -0700 | [diff] [blame] | 284 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 285 | yield registry.register( |
| 286 | 'grpc_server', |
| 287 | VolthaGrpcServer(self.args.grpc_port) |
| 288 | ).start() |
Zsolt Haraszti | eb43507 | 2016-09-23 17:10:49 -0700 | [diff] [blame] | 289 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 290 | yield registry.register( |
| 291 | 'kafka_proxy', |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 292 | KafkaProxy( |
| 293 | self.args.consul, |
| 294 | self.args.kafka, |
| 295 | config=self.config.get('kafka-proxy', {}) |
| 296 | ) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 297 | ).start() |
| 298 | |
| 299 | yield registry.register( |
| 300 | 'core', |
| 301 | VolthaCore( |
Zsolt Haraszti | dafefe1 | 2016-11-14 21:29:58 -0800 | [diff] [blame] | 302 | instance_id=self.args.instance_id, |
| 303 | version=VERSION, |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 304 | log_level=LogLevel.INFO |
| 305 | ) |
| 306 | ).start() |
Zsolt Haraszti | 7eeb2b3 | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 307 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 308 | yield registry.register( |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 309 | 'frameio', |
| 310 | FrameIOManager() |
| 311 | ).start() |
| 312 | |
| 313 | yield registry.register( |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 314 | 'adapter_loader', |
| 315 | AdapterLoader(config=self.config.get('adapter_loader', {})) |
| 316 | ).start() |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 317 | |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 318 | yield registry.register( |
| 319 | 'diag', |
| 320 | Diagnostics(config=self.config.get('diagnostics', {})) |
| 321 | ).start() |
| 322 | |
Zsolt Haraszti | 3bfff66 | 2016-12-14 23:41:49 -0800 | [diff] [blame] | 323 | if self.args.manhole_port is not None: |
| 324 | self.start_manhole(self.args.manhole_port) |
| 325 | |
Khen Nursimulu | 3c74d3b | 2016-11-08 15:14:01 -0800 | [diff] [blame] | 326 | self.log.info('started-internal-services') |
| 327 | |
| 328 | except Exception as e: |
| 329 | self.log.exception('Failure to start all components {}'.format(e)) |
| 330 | |
Zsolt Haraszti | 3bfff66 | 2016-12-14 23:41:49 -0800 | [diff] [blame] | 331 | def start_manhole(self, port): |
| 332 | self.manhole = Manhole( |
| 333 | port, |
| 334 | pws=dict(admin='adminpw'), |
| 335 | eventbus = EventBusClient(), |
| 336 | **registry.components |
| 337 | ) |
| 338 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 339 | @inlineCallbacks |
| 340 | def shutdown_components(self): |
| 341 | """Execute before the reactor is shut down""" |
| 342 | self.log.info('exiting-on-keyboard-interrupt') |
Zsolt Haraszti | dafefe1 | 2016-11-14 21:29:58 -0800 | [diff] [blame] | 343 | for component in reversed(registry.iterate()): |
| 344 | yield component.stop() |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 345 | |
Zsolt Haraszti | 3300f74 | 2017-01-09 01:14:20 -0800 | [diff] [blame] | 346 | import threading |
| 347 | self.log.info('THREADS:') |
| 348 | main_thread = threading.current_thread() |
| 349 | for t in threading.enumerate(): |
| 350 | if t is main_thread: |
| 351 | continue |
| 352 | if not t.isDaemon(): |
| 353 | continue |
| 354 | self.log.info('joining thread {} {}'.format( |
| 355 | t.getName(), "daemon" if t.isDaemon() else "not-daemon")) |
| 356 | t.join() |
| 357 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 358 | def start_reactor(self): |
| 359 | from twisted.internet import reactor |
Zsolt Haraszti | 109db83 | 2016-09-16 16:32:36 -0700 | [diff] [blame] | 360 | reactor.callWhenRunning( |
| 361 | lambda: self.log.info('twisted-reactor-started')) |
| 362 | reactor.addSystemEventTrigger('before', 'shutdown', |
| 363 | self.shutdown_components) |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 364 | reactor.run() |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 365 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 366 | def start_heartbeat(self): |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 367 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 368 | t0 = time.time() |
| 369 | t0s = time.ctime(t0) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 370 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 371 | def heartbeat(): |
| 372 | self.log.debug(status='up', since=t0s, uptime=time.time() - t0) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame] | 373 | |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 374 | from twisted.internet.task import LoopingCall |
| 375 | lc = LoopingCall(heartbeat) |
| 376 | lc.start(10) |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 377 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 378 | # Temporary function to send a heartbeat message to the external kafka |
| 379 | # broker |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 380 | def start_kafka_heartbeat(self, instance_id): |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 381 | # For heartbeat we will send a message to a specific "voltha-heartbeat" |
| 382 | # topic. The message is a protocol buf |
| 383 | # message |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 384 | message = dumps(dict( |
| 385 | type='heartbeat', |
| 386 | voltha_instance=instance_id, |
| 387 | ip=get_my_primary_local_ipv4() |
| 388 | )) |
| 389 | topic = "heartbeat.voltha" |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 390 | |
| 391 | from twisted.internet.task import LoopingCall |
Khen Nursimulu | 3c74d3b | 2016-11-08 15:14:01 -0800 | [diff] [blame] | 392 | kafka_proxy = get_kafka_proxy() |
| 393 | if kafka_proxy: |
| 394 | lc = LoopingCall(kafka_proxy.send_message, topic, message) |
| 395 | lc.start(10) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 396 | pass |
Khen Nursimulu | 3c74d3b | 2016-11-08 15:14:01 -0800 | [diff] [blame] | 397 | else: |
| 398 | self.log.error('Kafka proxy has not been created!') |
Khen Nursimulu | 441dedd | 2016-10-05 14:44:26 -0700 | [diff] [blame] | 399 | |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 400 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 401 | if __name__ == '__main__': |
Zsolt Haraszti | e060a7d | 2016-09-16 11:08:24 -0700 | [diff] [blame] | 402 | Main().start() |