f312130a3c27b743bd25b8ab541c35f6e22ee2a0
[doctor.git] / doctor_tests / installer / base.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import abc
10 import getpass
11 import grp
12 import os
13 import pwd
14 import six
15 import stat
16 import subprocess
17
18 from doctor_tests.common.utils import get_doctor_test_root_dir
19 from doctor_tests.identity_auth import get_session
20 from doctor_tests.os_clients import nova_client
21
22
23 @six.add_metaclass(abc.ABCMeta)
24 class BaseInstaller(object):
25     def __init__(self, conf, log):
26         self.conf = conf
27         self.log = log
28         self.servers = list()
29
30     @abc.abstractproperty
31     def node_user_name(self):
32         """user name for login to cloud node"""
33
34     @abc.abstractmethod
35     def get_ssh_key_from_installer(self):
36         pass
37
38     @abc.abstractmethod
39     def get_host_ip_from_hostname(self, hostname):
40         pass
41
42     @abc.abstractmethod
43     def setup(self):
44         pass
45
46     @abc.abstractmethod
47     def cleanup(self):
48         pass
49
50     def create_flavor(self):
51         self.nova = \
52             nova_client(self.conf.nova_version,
53                         get_session())
54         flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()}
55         if self.conf.flavor not in flavors:
56             self.nova.flavors.create(self.conf.flavor, 512, 1, 1)
57
58     def setup_stunnel(self):
59         self.log.info('Setup ssh stunnel in %s installer......'
60                       % self.conf.installer.type)
61
62         for node_ip in self.controllers:
63             cmd = ("ssh -o UserKnownHostsFile=/dev/null"
64                    " -o StrictHostKeyChecking=no"
65                    " -i %s %s@%s -R %s:localhost:%s"
66                    " sleep 600 > ssh_tunnel.%s"
67                    " 2>&1 < /dev/null &"
68                    % (self.key_file,
69                       self.node_user_name,
70                       node_ip,
71                       self.conf.consumer.port,
72                       self.conf.consumer.port,
73                       node_ip))
74             server = subprocess.Popen(cmd, shell=True)
75             self.servers.append(server)
76             server.communicate()
77
78     def _get_ssh_key(self, client, key_path):
79         self.log.info('Get SSH keys from %s installer......'
80                       % self.conf.installer.type)
81
82         if self.key_file is not None:
83             self.log.info('Already have SSH keys from %s installer......'
84                           % self.conf.installer.type)
85             return self.key_file
86
87         ssh_key = '{0}/{1}'.format(get_doctor_test_root_dir(), 'instack_key')
88         client.scp(key_path, ssh_key, method='get')
89         user = getpass.getuser()
90         uid = pwd.getpwnam(user).pw_uid
91         gid = grp.getgrnam(user).gr_gid
92         os.chown(ssh_key, uid, gid)
93         os.chmod(ssh_key, stat.S_IREAD)
94         return ssh_key
95
96     def _run_cmd_remote(self, client, command):
97         self.log.info('Run command=%s in %s installer......'
98                       % (command, self.conf.installer.type))
99
100         ret, output = client.ssh(command)
101         if ret:
102             raise Exception('Exec command in %s installer failed,'
103                             'ret=%s, output=%s'
104                             % (self.conf.installer.type,
105                                ret, output))
106         self.log.info('Output=%s command=%s in %s installer'
107                       % (output, command, self.conf.installer.type))
108         return output
109
110     def _run_apply_patches(self, client, restart_cmd, script_name):
111         installer_dir = os.path.dirname(os.path.realpath(__file__))
112         script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
113                                                'common', script_name)
114
115         client.scp(script_abs_path, script_name)
116         cmd = 'sudo python %s' % script_name
117         ret, output = client.ssh(cmd)
118         if ret:
119             raise Exception('Do the command in controller'
120                             ' node failed, ret=%s, cmd=%s, output=%s'
121                             % (ret, cmd, output))
122         client.ssh(restart_cmd)