change cloner info path
[sdnvpn.git] / odl-pipeline / lib / utils / service.py
1 #
2 # Copyright (c) 2015 All rights reserved
3 # This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #
10 import sys
11 import yaml
12 import argparse
13 import traceback
14 from utils_log import LOG, LOG_PATH
15 from abc import abstractmethod
16
17
18 class Service(object):
19
20     def start(self):
21         try:
22             self._run()
23         except Exception as ex:
24             LOG.error(ex.message)
25             LOG.error(traceback.format_exc())
26             LOG.error("For more logs check: %(log_path)s"
27                       % {'log_path': LOG_PATH})
28             sys.exit(1)
29
30     def _run(self):
31         parser = self._create_cli_parser()
32         sys_args = parser.parse_args()
33         config = self.read_config(sys_args)
34         self.run(sys_args, config)
35
36     @abstractmethod
37     def run(self, sys_args, config):
38         # Do something
39         return
40
41     @abstractmethod
42     def create_cli_parser(self, parser):
43         # Read in own sys args
44         return parser
45
46     def _create_cli_parser(self):
47         parser = argparse.ArgumentParser(description='OVS Debugger')
48         # parser.add_argument('-c', '--config', help="Path to config.yaml",
49         #                     required=False)
50         # parser.add_argument('--boolean', help="",
51         #                     required=False, action='store_true')
52         return self.create_cli_parser(parser)
53
54     def read_config(self, sys_args):
55         if not hasattr(sys_args, 'config'):
56             return None
57         if not sys_args.config:
58             config_path = './etc/config.yaml'
59         else:
60             config_path = sys_args.config
61         try:
62             with open(config_path) as f:
63                 return yaml.load(f)
64         except yaml.scanner.ScannerError as ex:
65             LOG.error("Yaml file corrupt. Try putting spaces after the "
66                       "colons.")
67             raise ex