def list_all_irqs(): cmd_line = 'tuna --show_irqs' cmd = Command(cmd_line) cmd.run() output, _ = cmd.get_output() return output
def install_packages(self): pkgs = self.conf.get_subset(m_type=model.system.Package) install_cmd = self._INSTALLER + ' '.join([str(pkg.value) for pkg in pkgs]) c = Command(install_cmd) c.run() out, retcode = c.watch_output() logger.info(out)
def test_no_bash(self): input_str = 'asdf | grep asdf' cmd = Command(f'echo -n {input_str}') cmd.run() out, ret = cmd.get_output() self.assertEqual(out, input_str) self.assertEqual(ret, 0)
def up_interface(cls, name): logger.info('enabling interface %s' % (name)) link_cmd_line = 'ip link set %s up ' % (name) link_cmd = Command(link_cmd_line) link_cmd.run() _, retcode = link_cmd.get_output() return retcode
def rename_interface(cls, old_name, new_name): logger.info('renaming interface %s -> %s' % (old_name, new_name)) link_cmd_line = 'ip link set %s name %s' % (old_name, new_name) link_cmd = Command(link_cmd_line) link_cmd.run() _, retcode = link_cmd.get_output() return retcode
def discover_topology(cls, interfaces): def get_val_from_lldp_output(lldp_out): lines = lldp_out.split('\n') if len(lines) > 1: val = lines[1][1:] else: val = None return val logger.info('discovering up lldp topology') output_table = [] mine_hostname = Uname.get_hostname() for iface in interfaces: if cls._is_enabled(iface): iface_values = [] for cmd in cls.GET_LLDP_CMDS: c = Command(cmd % iface.name) c.run() out, _ = c.get_output() value = get_val_from_lldp_output(out) if value: iface_values.append(value) output_table.append([mine_hostname, iface.name, iface_values]) out_string = 'discovered lldp topology:\n' for line in output_table: out_string += '%s %s <=connected=> %s \n' % (line[0], line[1], line[2]) return out_string
def show(cls, human_readable=True): if human_readable: cmd = Command(f'{cls.PREFIX_CMD} show') else: cmd = Command('nmcli -t con show') out = cmd.run().watch_and_log_error()[0] return out
def get_all_interfaces(cls): logger.debug('Getting all interfaces') link_cmd = Command('ip link') link_cmd.run() out, _ = link_cmd.get_output() return re.findall(r'^[0-9]*: (.*):', out, re.MULTILINE)
def install_special_packages(self): spec_pkgs = self.conf.get_subset(m_type=model.system.SpecialPackage) for pkg in spec_pkgs: install_cmd = self._INSTALLER_COMMAND_TEMPLATE.render(installer=self._INSTALLER, pkg=pkg) c = Command(install_cmd) c.run() out, retcode = c.watch_output() logger.info(out)
def install_packages(self): pkgs = self.conf.get_subset(m_type=model.system.Package) for pkg in pkgs: install_cmd = self._INSTALLER + pkg.value c = Command(install_cmd) c.run() out, _ = c.watch_output() logger.info(out)
def submit_log(cls, filename): if not Environment.in_rstrnt: logger.warning('Skipping method, NOT in rstrnt environment!!!') return logger.info(f'submiting log using rstrnt: {filename}') c = Command(f'rstrnt-report-log --filename {filename}').run() print(c.get_output())
def down(cls, interface: Interface): logger.info(f'Disabling interface {interface}') uuid = cls.get_interface_uuid(interface) if uuid: cmd = Command(f'{cls.PREFIX_CMD} down {uuid}') cmd.run().watch_and_log_error() else: logger.error(f'Cannot find interface uuid: {interface}')
def set_persistent_max_cpus(guest: VirtualGuest): logger.info('Setting persistent maximum cpus : %s on guest %s' % (guest.cpu_count, guest.name)) cmd = 'virsh setvcpus %s %s --config --maximum' % (guest.name, guest.cpu_count) c = Command(cmd) c.run() _, retcode = c.get_output() assert not retcode return retcode
def modprobe(module: KernelModule): opts = ' '.join([f'{k}={v}' for k, v in module.options.items()]) cmd = Command(f'modprobe {module.name} {opts}') cmd.run() out, ret = cmd.get_output() if ret: logger.error( f'Error during modprobe module {module.name} with options {module.options}' )
def sync_set(cls, state): if not Environment.in_rstrnt: logger.warning('Skipping method, NOT in rstrnt environment!!!') return logger.info( 'rstrnt synchronization: setting synchronization state: %s', state) c = Command('rstrnt-sync-set -s %s' % state).run() print(c.get_output())
def set_persistent_max_mem(guest: VirtualGuest): mem_size = guest.mem_size * 1024 # conversion from MB to Kb because of virsh logger.info('Setting persistent maximum memory size : %s kB on guest %s' % (mem_size, guest.name)) cmd = 'virsh setmaxmem %s %s --config' % (guest.name, mem_size) c = Command(cmd) c.run() output, retcode = c.get_output() assert not retcode return retcode
def get_src_name_from_file(cls, path) -> Optional[str]: rpm_cmd = Command(f'{cls.CMD_RPM} -qif {path}').run() out, _ = rpm_cmd.get_output() re_match = re.search(r'Source\s+RPM\s*:\s+(?P<all_src_name>.*[^\n])', out, re.MULTILINE) if re_match: return re_match.group('all_src_name') else: return None
def get_interface_name(cls, mac): mac_regex = r'[0-9]*: (.*):.*\n.*link/ether (%s)' % mac link_cmd = Command('ip link') link_cmd.run() out, _ = link_cmd.get_output() m = re.search(mac_regex, out) if m is None: return m return m.group(1)
def attach_device(guest: VirtualGuest, dfile, persistent=True): logger.info('Attaching device specified in %s to guest %s', guest.name, dfile) cmd = 'virsh attach-device %s %s' % (guest.name, dfile) if persistent: cmd += ' --persistent' c = Command(cmd) c.run() _, retcode = c.get_output() return retcode
def dell_bridge(bridge): bridge_name = bridge.name logger.info('deleting ovs bridge %s' % bridge_name) cmd_line = 'ovs-vsctl del-br %s' % bridge_name cmd = Command(cmd_line) cmd.run() output, retcode = cmd.get_output() if retcode: logger.error(output) return retcode
def get_package_version(cls, package) -> Optional[str]: rpm_cmd = Command('%s -qa %s' % (cls.CMD_RPM, package)) rpm_cmd.run() out, ret_code = rpm_cmd.watch_output() if ret_code == 0 and len(out): first_match = out.split()[0] # strip whitespaces # "kernel-4.18.0-67.el8.x86_64".split("kernel-") -> [[], ['4.18.0-67.el8.x86_64']] return first_match.split('%s-' % package)[1] else: return None
def destroy(guest: VirtualGuest): logger.info('Destroying guest : %s!' % guest.name) cmd = 'virsh destroy %s' % guest.name c = Command(cmd) c.run() output, return_code = c.get_output() if return_code: logger.warning(output) else: logger.info(output) return return_code
def start(guest: VirtualGuest): logger.info('Starting guest %s!' % guest.name) cmd = 'virsh start %s' % guest.name c = Command(cmd) c.run() output, return_code = c.get_output() if return_code: logger.error('Virsh >> %s' % output) else: logger.info('Virsh >> %s' % output) return return_code
def get_src_name(cls, pkg_nvr) -> Optional[str]: rpm_cmd = Command('%s -q -i %s' % (cls.CMD_RPM, pkg_nvr)) rpm_cmd.run() out, _ = rpm_cmd.get_output() re_match = re.search(r'Source\s+RPM\s*:\s+(?P<all_src_name>.*[^\n])', out, re.MULTILINE) if re_match: src_name = re_match.group('all_src_name') return src_name else: return None
def get_irq_of_interface(interface): cmd_line = 'tuna --show_irqs | grep %s' % interface cmd = Command(cmd_line) cmd.run() all_irq, _ = cmd.get_output() output = [] for i in all_irq.split('\n'): if len(i): output.append(i.split()[0]) return output
def detach_interface(guest: VirtualGuest, itype, mac, persistent=True): logger.info('Detaching interface type %s with mac %s from guest %s', guest.name, itype, mac) cmd = 'virsh detach-interface %s %s' % (guest.name, itype) if mac: cmd += ' --mac %s' % mac if persistent: cmd += ' --persistent' c = Command(cmd) c.run() _, retcode = c.get_output() return retcode
def get_profile(): cmd = Command('tuned-adm active') cmd.run() out, ret = cmd.watch_output() if ret: return None re_match = re.search(r'.*: (.*)\n', out) if re_match: return re_match.group(1) else: return None
def generate_pcp_config(self): for pcp in self.conf.get_subset(m_type=model.system.PCPConfiguration): logger.info(f'Generating PCP configuration: {pcp}') if os.path.exists(pcp.config_path): logger.info('PCP config already exists >> Deleting ') os.remove(pcp.config_path) os.makedirs(pcp.log_path, exist_ok=True) cmd = Command(f'pmlogconf -c {pcp.config_path}').run() out, ret_code = cmd.watch_output() if ret_code: logger.error(f'PCP cannot generate configuration! Log: {out}')
def dell_port(bridge, interface): bridge_name = bridge.get_name() interface_name = interface.get_name() logger.info('removing interface %s from bridge %s' % (interface_name, bridge_name)) cmd_line = 'ovs-vsctl del-port %s %s' % (bridge_name, interface_name) cmd = Command(cmd_line) cmd.run() output, retcode = cmd.get_output() if retcode: logger.error(output) return retcode
def add_port(bridge, interface): bridge_name = bridge.name interface_name = interface.name logger.info('adding interface %s to bridge %s' % (interface_name, bridge_name)) cmd_line = 'ovs-vsctl add-port %s %s' % (bridge_name, interface_name) cmd = Command(cmd_line) cmd.run() output, retcode = cmd.get_output() if retcode: logger.error(output) return retcode