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 ##############################################################################
20 from doctor_tests.common import utils
21 from doctor_tests.identity_auth import get_session
22 from doctor_tests.os_clients import nova_client
25 @six.add_metaclass(abc.ABCMeta)
26 class BaseInstaller(object):
27 def __init__(self, conf, log):
31 self.use_containers = False
34 def node_user_name(self):
35 """user name for login to cloud node"""
38 def get_ssh_key_from_installer(self):
42 def get_host_ip_from_hostname(self, hostname):
53 def create_flavor(self):
55 nova_client(self.conf.nova_version,
57 flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()}
58 if self.conf.flavor not in flavors:
59 self.nova.flavors.create(self.conf.flavor, 512, 1, 1)
61 def setup_stunnel(self):
62 self.log.info('Setup ssh stunnel in %s installer......'
63 % self.conf.installer.type)
64 tunnels = [self.conf.consumer.port]
65 if self.conf.test_case == 'maintenance':
67 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
68 elif self.conf.test_case == 'all':
70 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
74 for node_ip in self.controllers:
76 self.log.info('tunnel for port %s' % port)
77 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
78 " -o StrictHostKeyChecking=no"
79 " -i %s %s@%s -R %s:localhost:%s"
80 " sleep %s > ssh_tunnel.%s.%s"
90 server = subprocess.Popen('exec ' + cmd, shell=True)
91 self.servers.append(server)
92 if self.conf.admin_tool.type == 'fenix':
93 port = self.conf.admin_tool.port
94 self.log.info('tunnel for port %s' % port)
95 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
96 " -o StrictHostKeyChecking=no"
97 " -i %s %s@%s -L %s:localhost:%s"
98 " sleep %s > ssh_tunnel.%s.%s"
108 server = subprocess.Popen('exec ' + cmd, shell=True)
109 self.servers.append(server)
111 def _get_ssh_key(self, client, key_path):
112 self.log.info('Get SSH keys from %s installer......'
113 % self.conf.installer.type)
115 if self.key_file is not None:
116 self.log.info('Already have SSH keys from %s installer......'
117 % self.conf.installer.type)
120 ssh_key = '{0}/{1}'.format(utils.get_doctor_test_root_dir(),
122 client.scp(key_path, ssh_key, method='get')
123 user = getpass.getuser()
124 uid = pwd.getpwnam(user).pw_uid
125 gid = grp.getgrnam(user).gr_gid
126 os.chown(ssh_key, uid, gid)
127 os.chmod(ssh_key, stat.S_IREAD)
130 def get_transport_url(self):
131 client = utils.SSHClient(self.controllers[0], self.node_user_name,
132 key_filename=self.key_file)
133 if self.use_containers:
134 ncbase = "/var/lib/config-data/puppet-generated/nova"
138 cmd = 'sudo grep "^transport_url" %s/etc/nova/nova.conf' % ncbase
139 ret, url = client.ssh(cmd)
141 raise Exception('Exec command to get transport from '
142 'controller(%s) failed, '
144 % (self.controllers[0], ret, url))
145 elif self.controllers[0] not in url:
146 # need to use ip instead of hostname
147 ret = (re.sub("@.*:", "@%s:" % self.controllers[0],
148 url[0].split("=", 1)[1]))
150 cmd = 'grep -i "^rabbit" %s/etc/nova/nova.conf' % ncbase
151 ret, lines = client.ssh(cmd)
153 raise Exception('Exec command to get transport from '
154 'controller(%s) in Apex installer failed, '
156 % (self.controllers[0], ret, url))
158 for line in lines.split('\n'):
159 if line.startswith("rabbit_userid"):
160 rabbit_userid = line.split("=")
161 if line.startswith("rabbit_port"):
162 rabbit_port = line.split("=")
163 if line.startswith("rabbit_password"):
164 rabbit_password = line.split("=")
165 ret = "rabbit://%s:%s@%s:%s/?ssl=0" % (rabbit_userid,
169 self.log.debug('get_transport_url %s' % ret)
172 def _run_cmd_remote(self, client, command):
173 self.log.info('Run command=%s in %s installer......'
174 % (command, self.conf.installer.type))
176 ret, output = client.ssh(command)
178 raise Exception('Exec command in %s installer failed,'
180 % (self.conf.installer.type,
182 self.log.info('Output=%s command=%s in %s installer'
183 % (output, command, self.conf.installer.type))
186 def _check_cmd_remote(self, client, command):
187 self.log.info('Check command=%s return in %s installer......'
188 % (command, self.conf.installer.type))
190 ret, output = client.ssh(command, raise_enabled=False)
191 self.log.info('return %s' % ret)
199 def _run_apply_patches(self, client, restart_cmd, script_names,
201 installer_dir = os.path.dirname(os.path.realpath(__file__))
203 if isinstance(script_names, list):
204 for script_name in script_names:
205 script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
206 'common', script_name)
208 client.scp(script_abs_path, script_name)
210 client.scp(script_abs_path, script_name)
212 if ".py" in script_name:
213 cmd = 'sudo %s %s' % (python, script_name)
215 cmd = 'sudo chmod 700 %s;sudo ./%s' % (script_name,
217 ret, output = client.ssh(cmd)
218 self.log.info('Command %s output %s' % (cmd, output))
220 ret, output = client.ssh(cmd)
223 raise Exception('Do the command in remote'
224 ' node failed, ret=%s, cmd=%s, output=%s'
225 % (ret, cmd, output))
226 if 'nova' in restart_cmd:
227 # Make sure scheduler has proper cpu_allocation_ratio
229 client.ssh(restart_cmd)