def _comment_keys_from_ifcfg_files(self, keys, interfaces=None, backup_file_suffix=".bak"): """ Comments the provided list of keys from all 'ifcfg-*' files. Optinally skips ifcfg files for interfaces not listed in 'interfaces'. """ if not interfaces: interfaces = [] scripts_dir = os.path.join(self._os_root_dir, "etc/sysconfig/network-scripts") all_ifcfg_files = utils.list_ssh_dir(self._ssh, scripts_dir) regex = "^(ifcfg-[a-z0-9]+)$" for ifcfgf in all_ifcfg_files: if not re.match(regex, ifcfgf): LOG.debug("Skipping ifcfg file with unknown filename '%s'." % ifcfgf) continue if interfaces and not any([i in ifcfgf for i in interfaces]): LOG.debug( "Skipping ifcfg file '%s' as it's not for one of the " "requested interfaces (%s)", ifcfgf, interfaces) continue fullpath = os.path.join(scripts_dir, ifcfgf) for key in keys: self._exec_cmd("sudo sed -i%s -E -e 's/^(%s=.*)$/# \\1/g' %s" % (backup_file_suffix, key, fullpath)) LOG.debug("Commented all %s references from '%s'" % (keys, fullpath))
def _find_dev_with_contents(self, devices, all_files=None, one_of_files=None): if all_files and one_of_files: raise exception.CoriolisException( "all_files and one_of_files are mutually exclusive") dev_name = None for dev_path in devices: dirs = None tmp_dir = self._exec_cmd('mktemp -d').decode().splitlines()[0] try: self._exec_cmd('sudo mount %s %s' % (dev_path, tmp_dir)) # NOTE: it's possible that the device was mounted successfully # but an I/O error occurs later along the line: dirs = utils.list_ssh_dir(self._ssh, tmp_dir) except Exception: self._event_manager.progress_update( "Failed to mount and scan device '%s'" % dev_path) LOG.warn( "Failed to mount and scan device '%s':\n%s", dev_path, utils.get_exception_details()) utils.ignore_exceptions(self._exec_cmd)( "sudo umount %s" % tmp_dir ) utils.ignore_exceptions(self._exec_cmd)( "sudo rmdir %s" % tmp_dir ) continue LOG.debug("Contents of device %s:\n%s", dev_path, dirs) if all_files and dirs: common = [i if i in dirs else None for i in all_files] if not all(common): self._exec_cmd('sudo umount %s' % tmp_dir) continue dev_name = dev_path self._exec_cmd('sudo umount %s' % tmp_dir) elif one_of_files and dirs: common = [i for i in one_of_files if i in dirs] if len(common) > 0: dev_name = dev_path self._exec_cmd('sudo umount %s' % tmp_dir) break else: self._exec_cmd('sudo umount %s' % tmp_dir) continue return dev_name
def _list_dir(self, chroot_path): path = os.path.join(self._os_root_dir, chroot_path) return utils.list_ssh_dir(self._ssh, path)