X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=doctor_tests%2Finstaller%2Fbase.py;h=30435931c925d89abc714fbc758b1ab4d6237b20;hb=a6575910a137f8932e294f66c9da3194ad937691;hp=fa39816aa49573d6212b1ee18d7f1be6ad7bde23;hpb=079ac9a481fcc1baa53cb5ab2896bbe037585f3b;p=doctor.git diff --git a/doctor_tests/installer/base.py b/doctor_tests/installer/base.py index fa39816a..30435931 100644 --- a/doctor_tests/installer/base.py +++ b/doctor_tests/installer/base.py @@ -7,13 +7,26 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import abc +import getpass +import grp +import os +import pwd import six +import stat +import subprocess + +from doctor_tests.common.utils import get_doctor_test_root_dir +from doctor_tests.identity_auth import get_session +from doctor_tests.os_clients import nova_client + @six.add_metaclass(abc.ABCMeta) class BaseInstaller(object): def __init__(self, conf, log): self.conf = conf self.log = log + self.servers = list() + self.use_containers = False @abc.abstractproperty def node_user_name(self): @@ -34,3 +47,103 @@ class BaseInstaller(object): @abc.abstractmethod def cleanup(self): pass + + def create_flavor(self): + self.nova = \ + nova_client(self.conf.nova_version, + get_session()) + flavors = {flavor.name: flavor for flavor in self.nova.flavors.list()} + if self.conf.flavor not in flavors: + self.nova.flavors.create(self.conf.flavor, 512, 1, 1) + + def setup_stunnel(self): + self.log.info('Setup ssh stunnel in %s installer......' + % self.conf.installer.type) + tunnels = [self.conf.consumer.port] + if self.conf.test_case == 'maintenance': + tunnel_uptime = 1200 + tunnels += [self.conf.app_manager.port, self.conf.inspector.port] + elif self.conf.test_case == 'all': + tunnel_uptime = 1800 + tunnels += [self.conf.app_manager.port, self.conf.inspector.port] + else: + tunnel_uptime = 600 + + for node_ip in self.controllers: + for port in tunnels: + self.log.info('tunnel for port %s' % port) + cmd = ("ssh -o UserKnownHostsFile=/dev/null" + " -o StrictHostKeyChecking=no" + " -i %s %s@%s -R %s:localhost:%s" + " sleep %s > ssh_tunnel.%s" + " 2>&1 < /dev/null " + % (self.key_file, + self.node_user_name, + node_ip, + port, + port, + tunnel_uptime, + node_ip)) + server = subprocess.Popen('exec ' + cmd, shell=True) + self.servers.append(server) + + def _get_ssh_key(self, client, key_path): + self.log.info('Get SSH keys from %s installer......' + % self.conf.installer.type) + + if self.key_file is not None: + self.log.info('Already have SSH keys from %s installer......' + % self.conf.installer.type) + return self.key_file + + ssh_key = '{0}/{1}'.format(get_doctor_test_root_dir(), 'instack_key') + client.scp(key_path, ssh_key, method='get') + user = getpass.getuser() + uid = pwd.getpwnam(user).pw_uid + gid = grp.getgrnam(user).gr_gid + os.chown(ssh_key, uid, gid) + os.chmod(ssh_key, stat.S_IREAD) + return ssh_key + + def _run_cmd_remote(self, client, command): + self.log.info('Run command=%s in %s installer......' + % (command, self.conf.installer.type)) + + ret, output = client.ssh(command) + if ret: + raise Exception('Exec command in %s installer failed,' + 'ret=%s, output=%s' + % (self.conf.installer.type, + ret, output)) + self.log.info('Output=%s command=%s in %s installer' + % (output, command, self.conf.installer.type)) + return output + + def _check_cmd_remote(self, client, command): + self.log.info('Check command=%s return in %s installer......' + % (command, self.conf.installer.type)) + + ret, output = client.ssh(command, raise_enabled=False) + self.log.info('return %s' % ret) + if ret == 0: + ret = True + else: + ret = False + return ret + + def _run_apply_patches(self, client, restart_cmd, script_names, + python='python3'): + installer_dir = os.path.dirname(os.path.realpath(__file__)) + + if isinstance(script_names, list): + for script_name in script_names: + script_abs_path = '{0}/{1}/{2}'.format(installer_dir, + 'common', script_name) + client.scp(script_abs_path, script_name) + cmd = 'sudo %s %s' % (python, script_name) + ret, output = client.ssh(cmd) + if ret: + raise Exception('Do the command in remote' + ' node failed, ret=%s, cmd=%s, output=%s' + % (ret, cmd, output)) + client.ssh(restart_cmd)