def wait(self, timeout=None, step=0.2): """ Wait for exit of subprocess. @param timeout: @return: Exit code. """ if timeout is None: return self.get_status(True) else: wait_for(lambda: not self.is_alive(), timeout, 0, step) return self.get_status()
def initialize(self): domain_ctl = self._machine.get_domain_ctl() if self._hwaddr: query = self._machine._rpc_call('get_devices_by_hwaddr', self._hwaddr) if len(query): msg = "Device with hwaddr %s already exists" % self._hwaddr raise MachineError(msg) else: mac_pool = self._machine.get_mac_pool() while True: self._hwaddr = normalize_hwaddr(mac_pool.get_addr()) query = self._machine._rpc_call('get_devices_by_hwaddr', self._hwaddr) if not len(query): break bridges = self._machine.get_network_bridges() if self._network in bridges: net_ctl = bridges[self._network] else: bridges[self._network] = net_ctl = VirtNetCtl() net_ctl.init() net_name = net_ctl.get_name() logging.info("Creating interface %s (%s) on machine %s", self.get_id(), self._hwaddr, self._machine.get_id()) self._orig_hwaddr = self._hwaddr domain_ctl.attach_interface(self._hwaddr, net_name, self._driver) # The sleep here is necessary, because udev sometimes renames the # newly created device and if the query for name comes too early, # the controller will then try to configure an nonexistent device sleep(1) ready = wait_for(self.is_ready, timeout=10) if not ready: msg = "Netdevice initialization failed." \ "Unable to create device %s (%s) on machine %s" \ % (self.get_id(), self._hwaddr, self._machine.get_id()) raise MachineError(msg) super(VirtualInterface, self).initialize()
def read_until_output_matches(self, patterns, internal_timeout=0.1, timeout=60): logging.log(self.logging_level, "Timeout: %d" % (timeout)) fd = self.fd out = "" end_time = time.time() + timeout p = select.poll() p.register(fd, select.POLLIN) while True: try: polled = p.poll((end_time - time.time()) * 1e3) except (select.error, TypeError): break if not polled: raise ShellProcess.ProcessTimeoutError(patterns, out) if polled and polled[0][1] == select.POLLHUP: break # Read data from child data = self.read_nonblocking(internal_timeout) if not data: break # Print it if necessary # Look for patterns out += data match = self.match_patterns(out, patterns) if match is not None: return match, out # Check if the child has terminated if wait_for(lambda: not self.is_alive(), 5, 0, 0.5): raise ShellProcess.ProcessTerminatedError(patterns, self.get_status(), out) else: # This shouldn't happen raise ShellProcess.ProcessError(patterns, out)
def read_until_output_matches(self, patterns, internal_timeout=0.1, timeout=60): logging.log(self.logging_level, "Timeout: %d" % (timeout)) fd = self.fd out = "" end_time = time.time() + timeout p = select.poll() p.register(fd, select.POLLIN) while True: try: polled = p.poll((end_time-time.time())*1e3) except (select.error, TypeError): break if not polled: raise ShellProcess.ProcessTimeoutError(patterns, out) if polled and polled[0][1] == select.POLLHUP: break # Read data from child data = self.read_nonblocking(internal_timeout) if not data: break # Print it if necessary # Look for patterns out += data match = self.match_patterns(out, patterns) if match is not None: return match, out # Check if the child has terminated if wait_for(lambda: not self.is_alive(), 5, 0, 0.5): raise ShellProcess.ProcessTerminatedError(patterns, self.get_status(), out) else: # This shouldn't happen raise ShellProcess.ProcessError(patterns, out)