From 416f9cf177801cecfe39f5249d73e3a4a85f0bbd Mon Sep 17 00:00:00 2001 From: Niko Hermanns Date: Wed, 30 Nov 2016 11:59:44 +0100 Subject: adding odl-pipeline Change-Id: I1c08883f0d68a61ce9e10c5596aec1a259eed71f Signed-off-by: Nikolas Hermanns --- odl-pipeline/lib/utils/service.py | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 odl-pipeline/lib/utils/service.py (limited to 'odl-pipeline/lib/utils/service.py') diff --git a/odl-pipeline/lib/utils/service.py b/odl-pipeline/lib/utils/service.py new file mode 100755 index 0000000..39cdce5 --- /dev/null +++ b/odl-pipeline/lib/utils/service.py @@ -0,0 +1,67 @@ +# +# Copyright (c) 2015 All rights reserved +# This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# +import sys +import yaml +import argparse +import traceback +from utils_log import LOG, LOG_PATH +from abc import abstractmethod + + +class Service(object): + + def start(self): + try: + self._run() + except Exception as ex: + LOG.error(ex.message) + LOG.error(traceback.format_exc()) + LOG.error("For more logs check: %(log_path)s" + % {'log_path': LOG_PATH}) + sys.exit(1) + + def _run(self): + parser = self._create_cli_parser() + sys_args = parser.parse_args() + config = self.read_config(sys_args) + self.run(sys_args, config) + + @abstractmethod + def run(self, sys_args, config): + # Do something + return + + @abstractmethod + def create_cli_parser(self, parser): + # Read in own sys args + return parser + + def _create_cli_parser(self): + parser = argparse.ArgumentParser(description='OVS Debugger') + # parser.add_argument('-c', '--config', help="Path to config.yaml", + # required=False) + # parser.add_argument('--boolean', help="", + # required=False, action='store_true') + return self.create_cli_parser(parser) + + def read_config(self, sys_args): + if not hasattr(sys_args, 'config'): + return None + if not sys_args.config: + config_path = './etc/config.yaml' + else: + config_path = sys_args.config + try: + with open(config_path) as f: + return yaml.load(f) + except yaml.scanner.ScannerError as ex: + LOG.error("Yaml file corrupt. Try putting spaces after the " + "colons.") + raise ex -- cgit 1.2.3-korg