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 ##############################################################################
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
23 @six.add_metaclass(abc.ABCMeta)
24 class BaseInstaller(object):
25 def __init__(self, conf, log):
31 def node_user_name(self):
32 """user name for login to cloud node"""
35 def get_ssh_key_from_installer(self):
39 def get_host_ip_from_hostname(self, hostname):
50 def create_flavor(self):
52 nova_client(self.conf.nova_version,
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)
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':
64 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
65 elif self.conf.test_case == 'all':
67 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
71 for node_ip in self.controllers:
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"
86 server = subprocess.Popen('exec ' + cmd, shell=True)
87 self.servers.append(server)
89 def _get_ssh_key(self, client, key_path):
90 self.log.info('Get SSH keys from %s installer......'
91 % self.conf.installer.type)
93 if self.key_file is not None:
94 self.log.info('Already have SSH keys from %s installer......'
95 % self.conf.installer.type)
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)
107 def _run_cmd_remote(self, client, command):
108 self.log.info('Run command=%s in %s installer......'
109 % (command, self.conf.installer.type))
111 ret, output = client.ssh(command)
113 raise Exception('Exec command in %s installer failed,'
115 % (self.conf.installer.type,
117 self.log.info('Output=%s command=%s in %s installer'
118 % (output, command, self.conf.installer.type))
121 def _run_apply_patches(self, client, restart_cmd, script_names):
122 installer_dir = os.path.dirname(os.path.realpath(__file__))
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)
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)