def _ConnectToTarget(self): logging.info('Connecting to Fuchsia using ffx.') # Prefer connecting via node name over address:port. if self._node_name: # Assume that ffx already knows about the target, so there's no need to # add/remove it. self._ffx_target = ffx_session.FfxTarget(self._ffx_runner, self._node_name) else: # The target may not be known by ffx. Probe to see if it has already been # added. ffx_target = ffx_session.FfxTarget( self._ffx_runner, ffx_session.format_host_port(self._host, self._port)) try: ffx_target.get_ssh_address() # If we could lookup the address, the target must be reachable. Do not # open a new scoped_target_context, as that will `ffx target add` now # and then `ffx target remove` later, which will break subsequent # interactions with a persistent emulator. self._ffx_target = ffx_target except subprocess.CalledProcessError: # The target is not known, so take on responsibility of adding and # removing it. self._target_context = self._ffx_runner.scoped_target_context( self._host, self._port) self._ffx_target = self._target_context.__enter__() self._ffx_target.wait(ATTACH_RETRY_SECONDS) return super(DeviceTarget, self)._ConnectToTarget()
def _Discover(self): """Queries mDNS for the IP address of a booted Fuchsia instance whose name matches |_node_name| on the local area network. If |_node_name| is not specified and there is only one device on the network, |_node_name| is set to that device's name. Returns: True if exactly one device is found, after setting |_host| and |_port| to its SSH address. False if no devices are found. Raises: Exception: If more than one device is found. """ if not self._node_name: # Get the node name of a single attached target. targets = None try: targets = self._ffx_runner.list_targets() except subprocess.CalledProcessError: # A failure to list targets could mean that the device is in zedboot. # Return false in this case so that Start() will attempt to provision. return False if not targets: return False if len(targets) > 1: raise Exception( 'More than one device was discovered on the network. ' 'Use --node-name <name> to specify the device to use.' 'List of devices: {}'.format(targets)) assert len(targets) == 1 node_name = targets[0].get('nodename') if not node_name or node_name == '<unknown>': return False self._node_name = node_name # Get the ssh address of the target. ffx_target = ffx_session.FfxTarget(self._ffx_runner, self._node_name) try: self._host, self._port = ffx_target.get_ssh_address() except subprocess.CalledProcessError: return False logging.info('Found device "%s" at %s.' % (self._node_name, ffx_session.format_host_port(self._host, self._port))) # TODO(crbug.com/1307220): Remove this once the telemetry scripts can handle # specifying the port for a device that is not listening on localhost. if self._port == 22: self._port = None return True
def _ConnectToTarget(self): logging.info('Connecting to Fuchsia using ffx.') # Prefer connecting via node name over address:port. if self._node_name: # Assume that ffx already knows about the target, so there's no need to # add/remove it. self._ffx_target = ffx_session.FfxTarget(self._ffx_runner, self._node_name) else: # The target may not be known by ffx, so tell it how to find it. self._target_context = self._ffx_runner.scoped_target_context( self._host, self._port) self._ffx_target = self._target_context.__enter__() self._ffx_target.wait(ATTACH_RETRY_SECONDS) return super(DeviceTarget, self)._ConnectToTarget()
def _ConnectToTarget(self): with_network = self._HasNetworking() if not with_network: # The target was started without networking, so tell ffx how to find it. logging.info('Connecting to Fuchsia using ffx.') _, host_ssh_port = self._GetEndpoint() self._target_context = self._ffx_runner.scoped_target_context( '127.0.0.1', host_ssh_port) self._ffx_target = self._target_context.__enter__() self._ffx_target.wait(common.ATTACH_RETRY_SECONDS) super(EmuTarget, self)._ConnectToTarget() if with_network: # Interact with the target via its node name, which ffx should now know # about. address, port = self._GetNetworkAddress() self._ffx_target = ffx_session.FfxTarget( self._ffx_runner, self._ffx_runner.get_node_name(address, port))