Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 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 | # |
| 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 | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame] | 23 | import yaml |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 24 | |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 25 | from structlog_setup import setup_logging |
| 26 | from coordinator import Coordinator |
| 27 | |
| 28 | |
| 29 | defs = dict( |
| 30 | consul=os.environ.get('CONSUL', 'localhost:8500'), |
| 31 | instance_id=os.environ.get('INSTANCE_ID', '1'), |
| 32 | config=os.environ.get('CONFIG', './voltha.yml'), |
| 33 | interface=os.environ.get('INTERFACE', 'eth0'), |
| 34 | internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', 'localhost'), |
| 35 | external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', 'localhost'), |
| 36 | fluentd=os.environ.get('FLUENTD', None) |
| 37 | ) |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def parse_args(): |
| 41 | |
| 42 | parser = argparse.ArgumentParser() |
| 43 | |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 44 | parser.add_argument('-c', '--config', dest='config', action='store', |
| 45 | default=defs['config'], |
| 46 | help='Path to voltha.yml config file (default: %s). ' |
| 47 | 'If relative, it is relative to main.py of voltha.' % defs['config']) |
| 48 | |
| 49 | parser.add_argument('-C', '--consul', dest='consul', action='store', |
| 50 | default=defs['consul'], |
| 51 | help='<hostname>:<port> to consul agent (default: %s)' % defs['consul']) |
| 52 | |
| 53 | parser.add_argument('-E', '--external-host-address', dest='external_host_address', action='store', |
| 54 | default=defs['external_host_address'], |
| 55 | help='<hostname> or <ip> at which Voltha is reachable from outside the cluster' |
| 56 | '(default: %s)' % defs['external_host_address']) |
| 57 | |
| 58 | parser.add_argument('-F', '--fluentd', dest='fluentd', action='store', |
| 59 | default=defs['fluentd'], |
| 60 | help='<hostname>:<port> to fluentd server (default: %s).' |
| 61 | '(If not specified (None), the address from the config file is used' |
| 62 | % defs['fluentd']) |
| 63 | |
| 64 | parser.add_argument('-H', '--internal-host-address', dest='internal_host_address', action='store', |
| 65 | default=defs['internal_host_address'], |
| 66 | help='<hostname> or <ip> at which Voltha is reachable from inside the cluster' |
| 67 | '(default: %s)' % defs['internal_host_address']) |
| 68 | |
| 69 | parser.add_argument('-i', '--instance-id', dest='instance_id', action='store', |
| 70 | default=defs['instance_id'], |
| 71 | help='unique string id of this voltha instance (default: %s)' % defs['interface']) |
| 72 | |
| 73 | # TODO placeholder, not used yet |
| 74 | parser.add_argument('-I', '--interface', dest='interface', action='store', |
| 75 | default=defs['interface'], |
| 76 | help='ETH interface to send (default: %s)' % defs['interface']) |
| 77 | |
| 78 | parser.add_argument('-n', '--no-banner', dest='no_banner', action='store_true', default=False, |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 79 | help='omit startup banner log lines') |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 80 | |
| 81 | parser.add_argument('-N', '--no-heartbeat', dest='no_heartbeat', action='store_true', default=False, |
| 82 | help='do not emit periodic heartbeat log messages') |
| 83 | |
Zsolt Haraszti | 59b7a88 | 2016-09-12 14:42:59 -0700 | [diff] [blame] | 84 | parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False, |
| 85 | help="suppress debug and info logs") |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 86 | |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 87 | parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False, |
| 88 | help='enable verbose logging') |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 89 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 90 | return parser.parse_args() |
| 91 | |
| 92 | |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame] | 93 | def load_config(args): |
| 94 | path = args.config |
| 95 | if path.startswith('.'): |
| 96 | dir = os.path.dirname(os.path.abspath(__file__)) |
| 97 | path = os.path.join(dir, path) |
| 98 | path = os.path.abspath(path) |
| 99 | with open(path) as fd: |
| 100 | config = yaml.load(fd) |
| 101 | return config |
| 102 | |
| 103 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 104 | def print_banner(args, log): |
| 105 | log.info(' _ ______ __ ________ _____ ') |
| 106 | log.info('| | / / __ \/ / /_ __/ / / / |') |
| 107 | log.info('| | / / / / / / / / / /_/ / /| |') |
| 108 | log.info('| |/ / /_/ / /___/ / / __ / ___ |') |
| 109 | log.info('|___/\____/_____/_/ /_/ /_/_/ |_|') |
Zsolt Haraszti | 59b7a88 | 2016-09-12 14:42:59 -0700 | [diff] [blame] | 110 | log.info('(to stop: press Ctrl-C)') |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 111 | |
| 112 | |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 113 | def startup(log, args, config): |
| 114 | log.info('starting-internal-services') |
| 115 | coordinator = Coordinator( |
| 116 | internal_host_address=args.internal_host_address, |
| 117 | external_host_address=args.external_host_address, |
| 118 | instance_id=args.instance_id, |
| 119 | consul=args.consul) |
| 120 | log.info('started-internal-services') |
| 121 | |
| 122 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 123 | def cleanup(log): |
| 124 | """Execute before the reactor is shut down""" |
| 125 | log.info('exiting-on-keyboard-interrupt') |
| 126 | |
| 127 | |
| 128 | def start_reactor(args, log): |
| 129 | from twisted.internet import reactor |
| 130 | reactor.callWhenRunning(lambda: log.info('twisted-reactor-started')) |
| 131 | reactor.addSystemEventTrigger('before', 'shutdown', lambda: cleanup(log)) |
| 132 | reactor.run() |
| 133 | |
| 134 | |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 135 | def start_heartbeat(log): |
| 136 | |
| 137 | t0 = time.time() |
| 138 | t0s = time.ctime(t0) |
| 139 | |
| 140 | def heartbeat(): |
| 141 | log.info(status='up', since=t0s, uptime=time.time() - t0) |
| 142 | |
| 143 | from twisted.internet.task import LoopingCall |
| 144 | lc = LoopingCall(heartbeat) |
| 145 | lc.start(10) |
| 146 | |
| 147 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 148 | def main(): |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 149 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 150 | args = parse_args() |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 151 | |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame] | 152 | config = load_config(args) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 153 | |
| 154 | log = setup_logging(config.get('logging', {}), fluentd=args.fluentd) |
| 155 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 156 | if not args.no_banner: |
| 157 | print_banner(args, log) |
Zsolt Haraszti | f2da1d0 | 2016-09-13 23:21:35 -0700 | [diff] [blame^] | 158 | |
| 159 | if not args.no_heartbeat: |
| 160 | start_heartbeat(log) |
| 161 | |
| 162 | startup(log, args, config) |
| 163 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 164 | start_reactor(args, log) # will not return except Keyboard interrupt |
| 165 | |
| 166 | |
| 167 | if __name__ == '__main__': |
| 168 | main() |