Esempio n. 1
0
 def copy(self, direction, local_path, remote_path, **kwargs):
     all_files = None
     if direction is 'to':
         msg = ('Copying file %s to %s:%s' % (local_path, self.node.name,
                                              remote_path))
         if self.node.is_dir(remote_path):
             pass
         elif remote_path[-1:] == '/':
             self.node.create_path_if_not_exsist(remote_path)
         else:
             # Remove the file
             self.execute('rm -f %s' % remote_path, as_root=True)
             self.node.create_path_if_not_exsist(
                 os.path.dirname(remote_path))
         if '*' in local_path:
             all_files = glob.glob(local_path)
     else:
         if local_path[-1:] == '/':
             execute('mkdir -p %s' % local_path)
         msg = ('Copying file from %s:%s to %s' % (self.node.name,
                                                   remote_path,
                                                   local_path))
     LOG.info(msg)
     if all_files:
         for one_file in all_files:
             return self._copy(direction, one_file, remote_path, **kwargs)
     else:
         return self._copy(direction, local_path, remote_path, **kwargs)
Esempio n. 2
0
 def _copy(src, dst, **kwargs):
     if os.path.isfile(src):
         if dst[-1:] == '/':
             shutil.mkdir_if_not_exsist(dst)
         putils.execute(['cp', src, dst], **kwargs)
     else:
         putils.execute(['cp', '-R', src, dst], **kwargs)
Esempio n. 3
0
 def get_virtual_node_name_from_mac(mac):
     vnode_names, _ = execute('virsh list|awk \'{print '
                              '$2}\'',
                              shell=True,
                              as_root=True)
     for node in vnode_names.split('\n'):
         if 'baremetal' in node:
             admin_net_mac, _ = execute(
                 'virsh domiflist %s |grep admin |awk \'{print $5}\'' %
                 node,
                 shell=True,
                 as_root=True)
             if admin_net_mac.replace('\n', '') == mac:
                 return node
     raise Exception(
         'Could not find corresponding virtual node for MAC: %s' % mac)
Esempio n. 4
0
 def execute(self, cmd, **kwargs):
     from flash_test.openstack.openstack_env import OpenstackEnv
     OpenstackEnv.gen_ssh_config(self.node)
     command_as_string = ' '.join(cmd)
     cmd = ['ssh', '-i', SshUtil.get_id_rsa(), '-F',
            SshUtil.get_config_file_path(),
            self.node.name]
     if self.node.password:
         cmd = ['sshpass', '-p', self.node.password] + cmd
     if not self.node.has_access:
         with open(SshUtil.get_id_rsa() + ".pub") as pub_key_file:
             pub_key = pub_key_file.read()
             try:
                 cmd.append('echo %s >> ~/.ssh/authorized_keys'
                            % pub_key)
                 execute(cmd)
             except Exception:
                 self.node.jump.execute(
                     'ssh %s '
                     '\'echo %s >> ~/.ssh/authorized_keys\''
                     % (self.node.address, pub_key))
         self.node.has_access = True
     cmd.append(command_as_string)
     return execute(cmd, **kwargs)
Esempio n. 5
0
 def _copy(self, direction, local_path, remote_path, **kwargs):
     # TODO create dir is not existing
     NodeManager.gen_ssh_config(self.node)
     cmd = ['scp', '-i', SshUtil.get_id_rsa(), '-F',
            SshUtil.get_config_file_path()]
     if direction == 'to':
         if os.path.isdir(local_path):
             cmd.append('-r')
         cmd = cmd + [local_path,
                      ('%s:%s') % (self.node.name, remote_path)]
     if direction == 'from':
         if self.node.is_dir(remote_path):
             cmd.append('-r')
         cmd = cmd + [('%s:%s') % (self.node.name, remote_path),
                      local_path]
     if self.node.password:
         cmd = ['sshpass', '-p', self.node.password] + cmd
     return execute(cmd, **kwargs)
Esempio n. 6
0
 def execute(self, cmd, **kwargs):
     if 'log_true' in kwargs:
         if kwargs['log_true']:
             LOG.info('Node: %s Executing: %s' % (self.node.name, cmd))
         kwargs.pop('log_true')
     NodeManager.gen_ssh_config(self.node)
     if not isinstance(cmd, str):
         cmd = ' '.join(cmd)
     cmd_addition = ['ssh', '-i', SshUtil.get_id_rsa(), '-F',
                     SshUtil.get_config_file_path(),
                     self.node.name]
     if self.node.password:
         cmd_addition = ['sshpass', '-p', self.node.password] + cmd_addition
     if 'as_root' in kwargs:
         kwargs.pop('as_root')
         cmd = 'sudo ' + cmd
     cmd_addition.append(cmd)
     return execute(cmd_addition, **kwargs)
Esempio n. 7
0
 def get_all_files_in_path(path):
     if os.path.exists(path):
         return putils.execute(['l', path])
Esempio n. 8
0
 def mv(src, dst):
     putils.execute(["mv", src, dst])
Esempio n. 9
0
 def rm(path, **kwargs):
     putils.execute(['rm', '-rf', path], **kwargs)
Esempio n. 10
0
 def mkdir_if_not_exist(path):
     if not path:
         raise Exception('Path should not be empty.')
     putils.execute(["mkdir", "-p", path])
Esempio n. 11
0
 def get_undercloud_ip():
     out, _ = execute('virsh domifaddr undercloud',
                      shell=True,
                      as_root=True)
     return re.findall('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', out)[0]