def start(self): cmd = [ _IPERF3_BINARY.cmd, '--client', self._server_ip, '--version4', # only IPv4 '--time', str(self._test_time), '--parallel', str(self._threads), '--bind', self._bind_to, '--zerocopy', # use less cpu '--json' ] rc, self._raw_output, err = execCmd(cmd) if rc == 1 and 'No route to host' in self.out['error']: # it seems that it takes some time for the routes to get updated # on the os so that we don't get this error, hence the horrific # sleep here. # TODO: Investigate, understand, and remove this sleep. time.sleep(3) rc, self._raw_output, err = execCmd(cmd) if rc: raise Exception('iperf3 client failed: cmd=%s, rc=%s, out=%s, ' 'err=%s' % (' '.join(cmd), rc, self._raw_output, err))
def __enter__(self): rc, out, err = execCmd([_SSH_AGENT.cmd], raw=True) if rc != 0: raise V2VError('Error init ssh-agent, exit code: %r' ', out: %r, err: %r' % (rc, out, err)) m = self._ssh_auth_re.match(out) # looking for: SSH_AUTH_SOCK=/tmp/ssh-VEE74ObhTWBT/agent.29917 self._auth = {m.group(1): m.group(2)} self._agent_pid = m.group(3) try: rc, out, err = execCmd([_SSH_ADD.cmd], env=self._auth) except: self._kill_agent() raise if rc != 0: # 1 = general fail # 2 = no agnet if rc != 2: self._kill_agent() raise V2VError('Error init ssh-add, exit code: %r' ', out: %r, err: %r' % (rc, out, err))
def configure(): """ Set up the multipath daemon configuration to the known and supported state. The original configuration, if any, is saved """ if os.path.exists(_CONF_FILE): backup = _CONF_FILE + '.' + time.strftime("%Y%m%d%H%M") shutil.copyfile(_CONF_FILE, backup) sys.stdout.write("Backup previous multipath.conf to %r\n" % backup) with tempfile.NamedTemporaryFile(mode="wb", prefix=os.path.basename(_CONF_FILE) + ".tmp", dir=os.path.dirname(_CONF_FILE), delete=False) as f: try: f.write(_CONF_DATA) f.flush() selinux.restorecon(f.name) os.chmod(f.name, 0o644) os.rename(f.name, _CONF_FILE) except: os.unlink(f.name) raise # Flush all unused multipath device maps commands.execCmd([constants.EXT_MULTIPATH, "-F"]) try: service.service_reload("multipathd") except service.ServiceOperationError: status = service.service_status("multipathd", False) if status == 0: raise
def removeBridge(self, bridge): DynamicSourceRoute.addInterfaceTracking(bridge) ifdown(bridge.name) self._removeSourceRoute(bridge, StaticSourceRoute) commands.execCmd([constants.EXT_BRCTL, 'delbr', bridge.name]) self.configApplier.removeBridge(bridge.name) if bridge.port: bridge.port.remove()
def check_brctl(): try: execCmd([EXT_BRCTL, "show"]) except OSError as e: if e.errno == errno.ENOENT: raise SkipTest("Cannot run %r: %s\nDo you have bridge-utils " "installed?" % (EXT_BRCTL, e)) raise
def removeBridge(self, bridge): if not self.owned_device(bridge.name): IfcfgAcquire.acquire_device(bridge.name) DynamicSourceRoute.addInterfaceTracking(bridge) ifdown(bridge.name) self._removeSourceRoute(bridge, StaticSourceRoute) commands.execCmd([constants.EXT_BRCTL, "delbr", bridge.name]) self.configApplier.removeBridge(bridge.name) if bridge.port: bridge.port.remove()
def removeBridge(self, bridge): if not self.owned_device(bridge.name): self.normalize_device_filename(bridge.name) DynamicSourceRoute.addInterfaceTracking(bridge) ifdown(bridge.name) self._removeSourceRoute(bridge, StaticSourceRoute) commands.execCmd([constants.EXT_BRCTL, 'delbr', bridge.name]) self.configApplier.removeBridge(bridge.name) if bridge.port: bridge.port.remove()
def stop_devices(device_ifcfgs): for dev in reversed(_sort_device_ifcfgs(device_ifcfgs)): ifdown(dev) if os.path.exists("/sys/class/net/%s/bridge" % dev): # ifdown is not enough to remove nicless bridges commands.execCmd([constants.EXT_BRCTL, "delbr", dev]) if _is_bond_name(dev): if _is_running_bond(dev): with open(netinfo_bonding.BONDING_MASTERS, "w") as f: f.write("-%s\n" % dev)
def stop_devices(device_ifcfgs): for dev in reversed(_sort_device_ifcfgs(device_ifcfgs)): ifdown(dev) if os.path.exists('/sys/class/net/%s/bridge' % dev): # ifdown is not enough to remove nicless bridges commands.execCmd([constants.EXT_BRCTL, 'delbr', dev]) if _is_bond_name(dev): if _is_running_bond(dev): with open(netinfo_bonding.BONDING_MASTERS, 'w') as f: f.write("-%s\n" % dev)
def configureNic(self, nic, **opts): DynamicSourceRoute.addInterfaceTracking(nic) self.configApplier.setIfaceConfigAndUp(nic) self._addSourceRoute(nic) ethtool_opts = getEthtoolOpts(nic.name) if ethtool_opts: # We ignore ethtool's return code to maintain initscripts' # behaviour. execCmd( [_ETHTOOL_BINARY.cmd, '-K', nic.name] + ethtool_opts.split())
def teardown(self): ovsdb = create() bridges = ovsdb.list_bridge_info().execute() with ovsdb.transaction() as t: for bridge in bridges: if bridge in TEST_BRIDGES: t.add(ovsdb.del_br(bridge['name'])) if not self.ovs_init_state_is_up: execCmd([OVS_CTL, 'stop'])
def volumeEmptyCheck(volumeName, host=GLUSTER_VOL_HOST, port=GLUSTER_VOL_PORT, protocol=GLUSTER_VOL_PROTOCOL): module = "gluster.gfapi" command = [ constants.EXT_PYTHON, "-m", module, "-v", volumeName, "-p", str(port), "-H", host, "-t", protocol, "-c", "readdir", ] # to include /usr/share/vdsm in python path env = os.environ.copy() env["PYTHONPATH"] = "%s:%s" % (env.get("PYTHONPATH", ""), constants.P_VDSM) env["PYTHONPATH"] = ":".join(map(os.path.abspath, env["PYTHONPATH"].split(":"))) rc, out, err = commands.execCmd(command, raw=True, env=env) if rc != 0: raise ge.GlusterVolumeEmptyCheckFailedException(rc, [out], [err]) return out.upper() == "TRUE"
def testV2VOutput(self): cmd = [FAKE_VIRT_V2V.cmd, '-v', '-x', '-ic', self.vpx_url, '-o', 'vdsm', '-of', 'raw', '-oa', 'sparse', '--vdsm-image-uuid', self.image_id_a, '--vdsm-vol-uuid', self.volume_id_a, '--vdsm-image-uuid', self.image_id_b, '--vdsm-vol-uuid', self.volume_id_b, '--password-file', '/tmp/mypass', '--vdsm-vm-uuid', self.job_id, '--vdsm-ovf-output', '/usr/local/var/run/vdsm/v2v', '--machine-readable', '-os', '/rhev/data-center/%s/%s' % (self.pool_id, self.domain_id), self.vm_name] rc, output, error = execCmd(cmd, raw=True) self.assertEqual(rc, 0) with open('fake-virt-v2v.out', 'r') as f: self.assertEqual(output, f.read()) with open('fake-virt-v2v.err', 'r') as f: self.assertEqual(error, f.read())
def volumeStatvfs(volumeName, host=GLUSTER_VOL_HOST, port=GLUSTER_VOL_PORT, protocol=GLUSTER_VOL_PROTOCOL): module = "gluster.gfapi" command = [constants.EXT_PYTHON, '-m', module, '-v', volumeName, '-p', str(port), '-H', host, '-t', protocol, '-c', 'statvfs'] # to include /usr/share/vdsm in python path env = os.environ.copy() env['PYTHONPATH'] = "%s:%s" % ( env.get("PYTHONPATH", ""), constants.P_VDSM) env['PYTHONPATH'] = ":".join(map(os.path.abspath, env['PYTHONPATH'].split(":"))) rc, out, err = commands.execCmd(command, raw=True, env=env) if rc != 0: raise ge.GlfsStatvfsException(rc, [out], [err]) res = json.loads(out) return os.statvfs_result((res['f_bsize'], res['f_frsize'], res['f_blocks'], res['f_bfree'], res['f_bavail'], res['f_files'], res['f_ffree'], res['f_favail'], res['f_flag'], res['f_namemax']))
def testWriteLargeData(self): data = """The Doctor: Davros, if you had created a virus in your laboratory, something contagious and infectious that killed on contact, a virus that would destroy all other forms of life; would you allow its use? Davros: It is an interesting conjecture. The Doctor: Would you do it? Davros: The only living thing... The microscopic organism... reigning supreme... A fascinating idea. The Doctor: But would you do it? Davros: Yes; yes. To hold in my hand, a capsule that contained such power. To know that life and death on such a scale was my choice. To know that the tiny pressure on my thumb, enough to break the glass, would end everything. Yes! I would do it! That power would set me up above the gods! And through the Daleks, I shall have that power! """ # (C) BBC - Doctor Who data = data * 100 p = commands.execCmd([EXT_CAT], sync=False) self.log.info("Writing data to std out") p.stdin.write(data) p.stdin.flush() self.log.info("Written data reading") self.assertEquals(p.stdout.read(len(data)), data)
def __exit__(self, *args): rc, out, err = execCmd([_SSH_ADD.cmd, '-d'], env=self._auth) if rc != 0: logging.error('Error deleting ssh-add, exit code: %r' ', out: %r, err: %r' % (rc, out, err)) self._kill_agent()
def _start_virt_v2v(self): return execCmd(self._command(), sync=False, deathSignal=signal.SIGTERM, nice=NICENESS.HIGH, ioclass=IOCLASS.IDLE, env=self._environment())
def webhookDelete(url): command = [_glusterEventsApi.cmd, "webhook-del", url] rc, out, err = commands.execCmd(command) if rc: raise ge.GlusterWebhookDeleteException(rc, out, err) else: return True
def docker_net_remove(network): return commands.execCmd([ _DOCKER.cmd, 'network', 'rm', network, ])
def start(self): cmd = [_IPERF3_BINARY.cmd, '--server', '--bind', self._bind_to] if self._net_ns is not None: p = netns_exec(self._net_ns, cmd) else: p = execCmd(cmd, sync=False) self._pid = p.pid
def testResetAffinityByDefault(self): try: proc = commands.execCmd((EXT_SLEEP, '30s'), sync=False) self.assertEquals(taskset.get(proc.pid), taskset.get(os.getpid())) finally: proc.kill()
def _fail(self, t): proc = commands.execCmd(["sleep", str(t)], sync=False) def parse(rc, out, err): raise Exception("TEST!!!") return utils.AsyncProcessOperation(proc, parse)
def _echo(self, text): proc = commands.execCmd(["echo", "-n", "test"], sync=False) def parse(rc, out, err): return out return utils.AsyncProcessOperation(proc, parse)
def _kill_agent(self): rc, out, err = execCmd([_SSH_AGENT.cmd, '-k'], env={'SSH_AGENT_PID': self._agent_pid}) if rc != 0: logging.error('Error killing ssh-agent (PID=%r), exit code: %r' ', out: %r, err: %r' % (self._agent_pid, rc, out, err))
def _systemctl(*args): cmd = [_SYSTEMCTL.cmd] cmd.extend(args) rc, out, err = commands.execCmd(cmd, raw=True) if rc != 0: raise cmdutils.Error(cmd=cmd, rc=rc, out=out, err=err) return out
def rmAppropriateUSBDevice(bus, device): rule_file = _UDEV_RULE_FILE_NAME_USB % (bus, device) _log.debug("Removing rule %s", rule_file) try: os.remove(rule_file) except OSError as e: if e.errno != errno.ENOENT: raise _log.warning('Rule %s missing', rule_file) _log.debug( 'Changing ownership (to root:root) of device ' 'bus: %s, device:: %s', bus, device) device_path = _USB_DEVICE_PATH % (int(bus), int(device)) cmd = [EXT_CHOWN, 'root:root', device_path] rc, out, err = commands.execCmd(cmd) if err: raise OSError( errno.EINVAL, 'Could not change ownership' 'out %s\nerr %s' % (out, err)) # It's possible that the device was in input class or had rule # matched against it, trigger to make sure everything is fine. _udevTrigger(attr_matches=(('busnum', int(bus)), ('devnum', int(device))))
def uuid(): host_UUID = None try: if os.path.exists(constants.P_VDSM_NODE_ID): with open(constants.P_VDSM_NODE_ID) as f: host_UUID = f.readline().replace("\n", "") else: arch = cpuarch.real() if cpuarch.is_x86(arch): ret, out, err = execCmd( [constants.EXT_DMIDECODE, "-s", "system-uuid"], raw=True, sudo=True) out = '\n'.join(line for line in out.splitlines() if not line.startswith('#')) if ret == 0 and 'Not' not in out: # Avoid error string - 'Not Settable' or 'Not Present' host_UUID = out.strip() else: logging.warning('Could not find host UUID.') elif cpuarch.is_ppc(arch): # eg. output IBM,03061C14A try: with open('/proc/device-tree/system-id') as f: systemId = f.readline() host_UUID = systemId.rstrip('\0').replace(',', '') except IOError: logging.warning('Could not find host UUID.') except: logging.error("Error retrieving host UUID", exc_info=True) return host_UUID
def _execCmd(command): returnCode, output, error = execCmd(command) if returnCode: raise IPRoute2Error(error) return output
def testV2VOutput(self): cmd = [FAKE_VIRT_V2V.cmd, '-v', '-x', '-ic', self.vpx_url, '-o', 'vdsm', '-of', 'raw', '-oa', 'sparse', '--vdsm-image-uuid', self.image_id_a, '--vdsm-vol-uuid', self.volume_id_a, '--vdsm-image-uuid', self.image_id_b, '--vdsm-vol-uuid', self.volume_id_b, '--password-file', '/tmp/mypass', '--vdsm-vm-uuid', self.job_id, '--vdsm-ovf-output', '/usr/local/var/run/vdsm/v2v', '--machine-readable', '-os', '/rhev/data-center/%s/%s' % (self.pool_id, self.domain_id), self.vm_name] rc, output, error = execCmd(cmd, raw=True) self.assertEqual(rc, 0) with io.open('fake-virt-v2v.out', 'rb') as f: self.assertEqual(output, f.read()) with io.open('fake-virt-v2v.err', 'rb') as f: self.assertEqual(error, f.read())
def start(self, interface, dhcp_range_from=None, dhcp_range_to=None, dhcpv6_range_from=None, dhcpv6_range_to=None, router=None, ipv6_slaac_prefix=None): # --dhcp-authoritative The only DHCP server on network # -p 0 don't act as a DNS server # --dhcp-option=3,<router> advertise a specific gateway (or None) # --dhcp-option=6 don't reply with any DNS servers # -d don't daemonize and log to stderr # --bind-dynamic bind only the testing veth iface command = [ _DNSMASQ_BINARY.cmd, '--dhcp-authoritative', '-p', '0', '--dhcp-option=3' + (',{0}'.format(router) if router else ''), '--dhcp-option=6', '-i', interface, '-I', 'lo', '-d', '--bind-dynamic', ] if dhcp_range_from and dhcp_range_to: command += ['--dhcp-range={0},{1},2m'.format(dhcp_range_from, dhcp_range_to)] if dhcpv6_range_from and dhcpv6_range_to: command += ['--dhcp-range={0},{1},2m'.format(dhcpv6_range_from, dhcpv6_range_to)] if ipv6_slaac_prefix: command += ['--enable-ra'] command += ['--dhcp-range={0},slaac,2m'.format(ipv6_slaac_prefix)] self.proc = execCmd(command, sync=False) sleep(_START_CHECK_TIMEOUT) if self.proc.returncode: raise DhcpError('Failed to start dnsmasq DHCP server.\n%s\n%s' % (''.join(self.proc.stderr), ' '.join(command)))
def passwd_isconfigured(): script = (str(_SASLDBLISTUSERS2), '-f', _LIBVIRT_SASLDB) _, out, _ = commands.execCmd(script) for user in out: if SASL_USERNAME in user: return YES return NO
def snapshotScheduleDisable(): command = [_snapSchedulerPath.cmd, "disable_force"] rc, out, err = commands.execCmd(command) if rc not in [0, SNAP_SCHEDULER_ALREADY_DISABLED_RC]: raise ge.GlusterDisableSnapshotScheduleFailedException( rc) return True
def _start_helper(self): return execCmd(self._command(), sync=False, deathSignal=signal.SIGTERM, nice=NICENESS.HIGH, ioclass=IOCLASS.IDLE, env=self._environment())
def testWriteLargeData(self): data = """The Doctor: Davros, if you had created a virus in your laboratory, something contagious and infectious that killed on contact, a virus that would destroy all other forms of life; would you allow its use? Davros: It is an interesting conjecture. The Doctor: Would you do it? Davros: The only living thing... The microscopic organism... reigning supreme... A fascinating idea. The Doctor: But would you do it? Davros: Yes; yes. To hold in my hand, a capsule that contained such power. To know that life and death on such a scale was my choice. To know that the tiny pressure on my thumb, enough to break the glass, would end everything. Yes! I would do it! That power would set me up above the gods! And through the Daleks, I shall have that power! """ # (C) BBC - Doctor Who data = data * 100 p = commands.execCmd([EXT_CAT], sync=False) self.log.info("Writing data to std out") p.stdin.write(data) p.stdin.flush() self.log.info("Written data reading") self.assertEqual(p.stdout.read(len(data)), data)
def testExec(self): """ Tests that execCmd execs and returns the correct ret code """ ret, out, err = commands.execCmd([EXT_ECHO]) self.assertEquals(ret, 0)
def testSudo(self, cmd): checkSudo(['echo']) rc, out, _ = commands.execCmd(cmd(('grep', 'Uid', '/proc/self/status')), sudo=True) self.assertEquals(rc, 0) self.assertEquals(int(out[0].split()[2]), 0)
def _exec_cmd(command): rc, out, err = execCmd(command) if rc: raise NMCliError(rc, ' '.join(err)) return out
def docker_net_inspect(self, network): return commands.execCmd([ self._exes.docker.cmd, 'network', 'inspect', network, ], raw=True)
def testExec(self): """ Tests that execCmd execs and returns the correct ret code """ ret, out, err = commands.execCmd([EXT_ECHO]) self.assertEqual(ret, 0)
def webhookSync(): command = [_glusterEventsApi.cmd, "sync"] rc, out, err = commands.execCmd(command) if rc: raise ge.GlusterWebhookSyncException(rc, out, err) else: return True
def docker_net_inspect(network): return commands.execCmd([ _DOCKER.cmd, 'network', 'inspect', network, ], raw=True)
def _runcmd(cmd): rc, out, err = commands.execCmd(cmd, raw=True) if rc == 0: return raise MountError(rc, ";".join((out, err)))
def testSudo(self, cmd): checkSudo(['echo']) rc, out, _ = commands.execCmd(cmd( ('grep', 'Uid', '/proc/self/status')), sudo=True) self.assertEquals(rc, 0) self.assertEquals(int(out[0].split()[2]), 0)
def blkdiscard(device): cmd = [_blkdiscard.cmd] cmd.append(device) rc, out, err = commands.execCmd(cmd) if rc != 0: raise cmdutils.Error(cmd, rc, out, err)
def _run_command(args): cmd = [_SYSCTL.cmd] cmd.extend(args) rc, out, err = commands.execCmd(cmd, raw=True) if rc != 0: raise cmdutils.Error(cmd, rc, out, err) return out