1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
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 ##############################################################################
19 from doctor_tests.common import utils
20 from doctor_tests.identity_auth import get_session
21 from doctor_tests.os_clients import nova_client
24 @six.add_metaclass(abc.ABCMeta)
25 class BaseInstaller(object):
26 def __init__(self, conf, log):
30 self.use_containers = False
33 def node_user_name(self):
34 """user name for login to cloud node"""
37 def get_ssh_key_from_installer(self):
41 def get_host_ip_from_hostname(self, hostname):
52 def create_flavor(self):
54 nova_client(self.conf.nova_version,
56 flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()}
57 if self.conf.flavor not in flavors:
58 self.nova.flavors.create(self.conf.flavor, 512, 1, 1)
60 def setup_stunnel(self):
61 self.log.info('Setup ssh stunnel in %s installer......'
62 % self.conf.installer.type)
63 tunnels = [self.conf.consumer.port]
64 if self.conf.test_case == 'maintenance':
66 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
67 elif self.conf.test_case == 'all':
69 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
73 for node_ip in self.controllers:
75 self.log.info('tunnel for port %s' % port)
76 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
77 " -o StrictHostKeyChecking=no"
78 " -i %s %s@%s -R %s:localhost:%s"
79 " sleep %s > ssh_tunnel.%s.%s"
89 server = subprocess.Popen('exec ' + cmd, shell=True)
90 self.servers.append(server)
91 if self.conf.admin_tool.type == 'fenix':
92 port = self.conf.admin_tool.port
93 self.log.info('tunnel for port %s' % port)
94 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
95 " -o StrictHostKeyChecking=no"
96 " -i %s %s@%s -L %s:localhost:%s"
97 " sleep %s > ssh_tunnel.%s.%s"
107 server = subprocess.Popen('exec ' + cmd, shell=True)
108 self.servers.append(server)
110 def _get_ssh_key(self, client, key_path):
111 self.log.info('Get SSH keys from %s installer......'
112 % self.conf.installer.type)
114 if self.key_file is not None:
115 self.log.info('Already have SSH keys from %s installer......'
116 % self.conf.installer.type)
119 ssh_key = '{0}/{1}'.format(utils.get_doctor_test_root_dir(),
121 client.scp(key_path, ssh_key, method='get')
122 user = getpass.getuser()
123 uid = pwd.getpwnam(user).pw_uid
124 gid = grp.getgrnam(user).gr_gid
125 os.chown(ssh_key, uid, gid)
126 os.chmod(ssh_key, stat.S_IREAD)
129 def _run_cmd_remote(self, client, command):
130 self.log.info('Run command=%s in %s installer......'
131 % (command, self.conf.installer.type))
133 ret, output = client.ssh(command)
135 raise Exception('Exec command in %s installer failed,'
137 % (self.conf.installer.type,
139 self.log.info('Output=%s command=%s in %s installer'
140 % (output, command, self.conf.installer.type))
143 def _check_cmd_remote(self, client, command):
144 self.log.info('Check command=%s return in %s installer......'
145 % (command, self.conf.installer.type))
147 ret, output = client.ssh(command, raise_enabled=False)
148 self.log.info('return %s' % ret)
156 def _run_apply_patches(self, client, restart_cmd, script_names,
158 installer_dir = os.path.dirname(os.path.realpath(__file__))
160 if isinstance(script_names, list):
161 for script_name in script_names:
162 script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
163 'common', script_name)
164 client.scp(script_abs_path, script_name)
165 cmd = 'sudo %s %s' % (python, script_name)
166 ret, output = client.ssh(cmd)
168 raise Exception('Do the command in remote'
169 ' node failed, ret=%s, cmd=%s, output=%s'
170 % (ret, cmd, output))
171 if 'nova-scheduler' in restart_cmd:
172 # Make sure scheduler has proper cpu_allocation_ratio
174 client.ssh(restart_cmd)