summaryrefslogtreecommitdiffstats
path: root/odl-pipeline/lib/utils/service.py
diff options
context:
space:
mode:
Diffstat (limited to 'odl-pipeline/lib/utils/service.py')
-rwxr-xr-xodl-pipeline/lib/utils/service.py67
1 files changed, 67 insertions, 0 deletions
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