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):
29 self.use_containers = False
32 def node_user_name(self):
33 """user name for login to cloud node"""
36 def get_ssh_key_from_installer(self):
40 def get_host_ip_from_hostname(self, hostname):
51 def create_flavor(self):
53 nova_client(self.conf.nova_version,
55 flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()}
56 if self.conf.flavor not in flavors:
57 self.nova.flavors.create(self.conf.flavor, 512, 1, 1)
59 def setup_stunnel(self):
60 self.log.info('Setup ssh stunnel in %s installer......'
61 % self.conf.installer.type)
62 tunnels = [self.conf.consumer.port]
63 if self.conf.test_case == 'maintenance':
65 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
66 elif self.conf.test_case == 'all':
68 tunnels += [self.conf.app_manager.port, self.conf.inspector.port]
72 for node_ip in self.controllers:
74 self.log.info('tunnel for port %s' % port)
75 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
76 " -o StrictHostKeyChecking=no"
77 " -i %s %s@%s -R %s:localhost:%s"
78 " sleep %s > ssh_tunnel.%s"
87 server = subprocess.Popen('exec ' + cmd, shell=True)
88 self.servers.append(server)
90 def _get_ssh_key(self, client, key_path):
91 self.log.info('Get SSH keys from %s installer......'
92 % self.conf.installer.type)
94 if self.key_file is not None:
95 self.log.info('Already have SSH keys from %s installer......'
96 % self.conf.installer.type)
99 ssh_key = '{0}/{1}'.format(get_doctor_test_root_dir(), 'instack_key')
100 client.scp(key_path, ssh_key, method='get')
101 user = getpass.getuser()
102 uid = pwd.getpwnam(user).pw_uid
103 gid = grp.getgrnam(user).gr_gid
104 os.chown(ssh_key, uid, gid)
105 os.chmod(ssh_key, stat.S_IREAD)
108 def _run_cmd_remote(self, client, command):
109 self.log.info('Run command=%s in %s installer......'
110 % (command, self.conf.installer.type))
112 ret, output = client.ssh(command)
114 raise Exception('Exec command in %s installer failed,'
116 % (self.conf.installer.type,
118 self.log.info('Output=%s command=%s in %s installer'
119 % (output, command, self.conf.installer.type))
122 def _check_cmd_remote(self, client, command):
123 self.log.info('Check command=%s return in %s installer......'
124 % (command, self.conf.installer.type))
126 ret, output = client.ssh(command, raise_enabled=False)
127 self.log.info('return %s' % ret)
134 def _run_apply_patches(self, client, restart_cmd, script_names,
136 installer_dir = os.path.dirname(os.path.realpath(__file__))
138 if isinstance(script_names, list):
139 for script_name in script_names:
140 script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
141 'common', script_name)
142 client.scp(script_abs_path, script_name)
143 cmd = 'sudo %s %s' % (python, script_name)
144 ret, output = client.ssh(cmd)
146 raise Exception('Do the command in remote'
147 ' node failed, ret=%s, cmd=%s, output=%s'
148 % (ret, cmd, output))
149 client.ssh(restart_cmd)