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)
62 for node_ip in self.controllers:
63 cmd = ("ssh -o UserKnownHostsFile=/dev/null"
64 " -o StrictHostKeyChecking=no"
65 " -i %s %s@%s -R %s:localhost:%s"
66 " sleep 600 > ssh_tunnel.%s.log"
71 self.conf.consumer.port,
72 self.conf.consumer.port,
74 server = subprocess.Popen(cmd, shell=True)
75 self.servers.append(server)
78 def _get_ssh_key(self, client, key_path):
79 self.log.info('Get SSH keys from %s installer......'
80 % self.conf.installer.type)
82 if self.key_file is not None:
83 self.log.info('Already have SSH keys from %s installer......'
84 % self.conf.installer.type)
87 ssh_key = '{0}/{1}'.format(get_doctor_test_root_dir(), 'instack_key')
88 client.scp(key_path, ssh_key, method='get')
89 user = getpass.getuser()
90 uid = pwd.getpwnam(user).pw_uid
91 gid = grp.getgrnam(user).gr_gid
92 os.chown(ssh_key, uid, gid)
93 os.chmod(ssh_key, stat.S_IREAD)
96 def _run_cmd_remote(self, client, command):
97 self.log.info('Run command=%s in %s installer......'
98 % (command, self.conf.installer.type))
100 ret, output = client.ssh(command)
102 raise Exception('Exec command in %s installer failed,'
104 % (self.conf.installer.type,
106 self.log.info('Output=%s command=%s in %s installer'
107 % (output, command, self.conf.installer.type))
110 def _run_apply_patches(self, client, restart_cmd, script_name):
111 installer_dir = os.path.dirname(os.path.realpath(__file__))
112 script_abs_path = '{0}/{1}/{2}'.format(installer_dir,
113 'common', script_name)
115 client.scp(script_abs_path, script_name)
116 cmd = 'sudo python %s' % script_name
117 ret, output = client.ssh(cmd)
119 raise Exception('Do the command in controller'
120 ' node failed, ret=%s, cmd=%s, output=%s'
121 % (ret, cmd, output))
122 client.ssh(restart_cmd)