Updates ODL Pipeline scripts for CSIT
[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 from ssh_util import SSH_CONFIG
17
18
19 class Service(object):
20
21     def start(self):
22         try:
23             self._run()
24         except Exception as ex:
25             LOG.error(ex.message)
26             LOG.error(traceback.format_exc())
27             LOG.error("For more logs check: %(log_path)s"
28                       % {'log_path': LOG_PATH})
29             sys.exit(1)
30
31     def _run(self):
32         parser = self._create_cli_parser()
33         sys_args = parser.parse_args()
34         config = self.read_config(sys_args)
35         if sys_args.ssh_key_file:
36             SSH_CONFIG['ID_RSA_PATH'] = sys_args.ssh_key_file
37         self.run(sys_args, config)
38
39     @abstractmethod
40     def run(self, sys_args, config):
41         # Do something
42         return
43
44     @abstractmethod
45     def create_cli_parser(self, parser):
46         # Read in own sys args
47         return parser
48
49     def _create_cli_parser(self):
50         parser = argparse.ArgumentParser(description='OVS Debugger')
51         # parser.add_argument('-c', '--config', help="Path to config.yaml",
52         #                     required=False)
53         # parser.add_argument('--boolean', help="",
54         #                     required=False, action='store_true')
55         parser.add_argument('--ssh-key-file',
56                             help="SSH private key file to use",
57                             required=False)
58         return self.create_cli_parser(parser)
59
60     def read_config(self, sys_args):
61         if not hasattr(sys_args, 'config'):
62             return None
63         if not sys_args.config:
64             config_path = './etc/config.yaml'
65         else:
66             config_path = sys_args.config
67         try:
68             with open(config_path) as f:
69                 return yaml.load(f)
70         except yaml.scanner.ScannerError as ex:
71             LOG.error("Yaml file corrupt. Try putting spaces after the "
72                       "colons.")
73             raise ex