953b36d9cc54fc31c6526ec4dce831c2d7fad7fd
[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         tunnels = [self.conf.consumer.port]
62         if self.conf.test_case == 'maintenance':
63             tunnel_uptime = 1200
64             tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
65         elif self.conf.test_case == 'all':
66             tunnel_uptime = 1800
67             tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
68         else:
69             tunnel_uptime = 600
70
71         for node_ip in self.controllers:
72             for port in tunnels:
73                 self.log.info('tunnel for port %s' % port)
74                 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
75                        " -o StrictHostKeyChecking=no"
76                        " -i %s %s@%s -R %s:localhost:%s"
77                        " sleep %s > ssh_tunnel.%s"
78                        " 2>&1 < /dev/null "
79                        % (self.key_file,
80                           self.node_user_name,
81                           node_ip,
82                           port,
83                           port,
84                           tunnel_uptime,
85                           node_ip))
86                 server = subprocess.Popen('exec ' + cmd, shell=True)
87                 self.servers.append(server)
88
89     def _get_ssh_key(self, client, key_path):
90         self.log.info('Get SSH keys from %s installer......'
91                       % self.conf.installer.type)
92
93         if self.key_file is not None:
94             self.log.info('Already have SSH keys from %s installer......'
95                           % self.conf.installer.type)
96             return self.key_file
97
98         ssh_key = '{0}/{1}'.format(get_doctor_test_root_dir(), 'instack_key')
99         client.scp(key_path, ssh_key, method='get')
100         user = getpass.getuser()
101         uid = pwd.getpwnam(user).pw_uid
102         gid = grp.getgrnam(user).gr_gid
103         os.chown(ssh_key, uid, gid)
104         os.chmod(ssh_key, stat.S_IREAD)
105         return ssh_key
106
107     def _run_cmd_remote(self, client, command):
108         self.log.info('Run command=%s in %s installer......'
109                       % (command, self.conf.installer.type))
110
111         ret, output = client.ssh(command)
112         if ret:
113             raise Exception('Exec command in %s installer failed,'
114                             'ret=%s, output=%s'
115                             % (self.conf.installer.type,
116                                ret, output))
117         self.log.info('Output=%s command=%s in %s installer'
118                       % (output, command, self.conf.installer.type))
119         return output
120
121     def _run_apply_patches(self, client, restart_cmd, script_names):
122         installer_dir = os.path.dirname(os.path.realpath(__file__))
123
124         if isinstance(script_names, list):
125             for script_name in script_names:
126                 script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
127                                                        'common', script_name)
128                 client.scp(script_abs_path, script_name)
129                 cmd = 'sudo python3 %s' % script_name
130                 ret, output = client.ssh(cmd)
131                 if ret:
132                     raise Exception('Do the command in controller'
133                                     ' node failed, ret=%s, cmd=%s, output=%s'
134                                     % (ret, cmd, output))
135             client.ssh(restart_cmd)