summaryrefslogtreecommitdiffstats
path: root/odl-pipeline/lib/utils/service.py
blob: a264cea0dea4fa789fc69c045667e2b6d7cc786f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#
# Copyright (c) 2017 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
from ssh_util import SSH_CONFIG


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)
        if sys_args.ssh_key_file:
            SSH_CONFIG['ID_RSA_PATH'] = sys_args.ssh_key_file
        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')
        parser.add_argument('--ssh-key-file',
                            help="SSH private key file to use",
                            required=False)
        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