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 |
| 22 | |
| 23 | import yaml |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 24 | |
| 25 | from structlog_setup import setup_logging, DEFAULT_FLUENT_SERVER |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def parse_args(): |
| 29 | |
| 30 | parser = argparse.ArgumentParser() |
| 31 | |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame^] | 32 | # generic parameters |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 33 | parser.add_argument('--no-banner', dest='no_banner', action='store_true', default=False, |
| 34 | help='omit startup banner log lines') |
| 35 | parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False, |
Zsolt Haraszti | 59b7a88 | 2016-09-12 14:42:59 -0700 | [diff] [blame] | 36 | help='enable verbose logging') |
| 37 | parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False, |
| 38 | help="suppress debug and info logs") |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame^] | 39 | parser.add_argument('-c', '--config', dest='config', action='store', default='./voltha.yml', |
| 40 | help='Path to voltha.yml config file. If relative, it is relative to main.py of voltha') |
Zsolt Haraszti | adbb88d | 2016-09-12 21:24:57 -0700 | [diff] [blame] | 41 | |
| 42 | # placeholder |
| 43 | parser.add_argument('-i', '--interface', dest='interface', action='store', default='eth0', |
| 44 | help='ETH interface to send (default: eth0)') |
| 45 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 46 | return parser.parse_args() |
| 47 | |
| 48 | |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame^] | 49 | def load_config(args): |
| 50 | path = args.config |
| 51 | if path.startswith('.'): |
| 52 | dir = os.path.dirname(os.path.abspath(__file__)) |
| 53 | path = os.path.join(dir, path) |
| 54 | path = os.path.abspath(path) |
| 55 | with open(path) as fd: |
| 56 | config = yaml.load(fd) |
| 57 | return config |
| 58 | |
| 59 | |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 60 | def print_banner(args, log): |
| 61 | log.info(' _ ______ __ ________ _____ ') |
| 62 | log.info('| | / / __ \/ / /_ __/ / / / |') |
| 63 | log.info('| | / / / / / / / / / /_/ / /| |') |
| 64 | log.info('| |/ / /_/ / /___/ / / __ / ___ |') |
| 65 | log.info('|___/\____/_____/_/ /_/ /_/_/ |_|') |
Zsolt Haraszti | 59b7a88 | 2016-09-12 14:42:59 -0700 | [diff] [blame] | 66 | log.info('(to stop: press Ctrl-C)') |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def cleanup(log): |
| 70 | """Execute before the reactor is shut down""" |
| 71 | log.info('exiting-on-keyboard-interrupt') |
| 72 | |
| 73 | |
| 74 | def start_reactor(args, log): |
| 75 | from twisted.internet import reactor |
| 76 | reactor.callWhenRunning(lambda: log.info('twisted-reactor-started')) |
| 77 | reactor.addSystemEventTrigger('before', 'shutdown', lambda: cleanup(log)) |
| 78 | reactor.run() |
| 79 | |
| 80 | |
| 81 | def main(): |
| 82 | args = parse_args() |
Zsolt Haraszti | d7c7c48 | 2016-09-13 00:45:38 -0700 | [diff] [blame^] | 83 | config = load_config(args) |
| 84 | log = setup_logging(config.get('logging', {})) |
Zsolt Haraszti | b71c2a0 | 2016-09-12 13:12:07 -0700 | [diff] [blame] | 85 | if not args.no_banner: |
| 86 | print_banner(args, log) |
| 87 | start_reactor(args, log) # will not return except Keyboard interrupt |
| 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | main() |