Example #1
0
    def __init__(self, cf_config_path):
        """Initialize a localInstance object.

        Args:
            cf_config_path: String, path to the cf runtime config.
        """
        self._cf_runtime_cfg = cvd_runtime_config.CvdRuntimeConfig(
            cf_config_path)
        self._instance_dir = self._cf_runtime_cfg.instance_dir
        self._virtual_disk_paths = self._cf_runtime_cfg.virtual_disk_paths
        self._local_instance_id = int(self._cf_runtime_cfg.instance_id)

        display = _DISPLAY_STRING % {
            "x_res": self._cf_runtime_cfg.x_res,
            "y_res": self._cf_runtime_cfg.y_res,
            "dpi": self._cf_runtime_cfg.dpi
        }
        # TODO(143063678), there's no createtime info in
        # cuttlefish_config.json so far.
        name = GetLocalInstanceName(self._local_instance_id)
        fullname = (_FULL_NAME_STRING % {
            "device_serial": "127.0.0.1:%s" % self._cf_runtime_cfg.adb_port,
            "instance_name": name,
            "elapsed_time": None
        })
        adb_device = AdbTools(self._cf_runtime_cfg.adb_port)
        device_information = None
        if adb_device.IsAdbConnected():
            device_information = adb_device.device_information

        super(LocalInstance,
              self).__init__(name=name,
                             fullname=fullname,
                             display=display,
                             ip="127.0.0.1",
                             status=constants.INS_STATUS_RUNNING,
                             adb_port=self._cf_runtime_cfg.adb_port,
                             vnc_port=self._cf_runtime_cfg.vnc_port,
                             createtime=None,
                             elapsed_time=None,
                             avd_type=constants.TYPE_CF,
                             is_local=True,
                             device_information=device_information,
                             zone=_LOCAL_ZONE)
Example #2
0
    def __init__(self, gce_instance):
        """Process the args into class vars.

        RemoteInstace initialized by gce dict object. We parse the required data
        from gce_instance to local variables.
        Reference:
        https://cloud.google.com/compute/docs/reference/rest/v1/instances/get

        We also gather more details on client side including the forwarding adb
        port and vnc port which will be used to determine the status of ssh
        tunnel connection.

        The status of gce instance will be displayed in _fullname property:
        - Connected: If gce instance and ssh tunnel and adb connection are all
         active.
        - No connected: If ssh tunnel or adb connection is not found.
        - Terminated: If we can't retrieve the public ip from gce instance.

        Args:
            gce_instance: dict object queried from gce.
        """
        name = gce_instance.get(constants.INS_KEY_NAME)

        create_time = gce_instance.get(constants.INS_KEY_CREATETIME)
        elapsed_time = _GetElapsedTime(create_time)
        status = gce_instance.get(constants.INS_KEY_STATUS)
        zone = self._GetZoneName(gce_instance.get(constants.INS_KEY_ZONE))

        ip = None
        for network_interface in gce_instance.get("networkInterfaces"):
            for access_config in network_interface.get("accessConfigs"):
                ip = access_config.get("natIP")

        # Get metadata
        display = None
        avd_type = None
        avd_flavor = None
        for metadata in gce_instance.get("metadata", {}).get("items", []):
            key = metadata["key"]
            value = metadata["value"]
            if key == constants.INS_KEY_DISPLAY:
                display = value
            elif key == constants.INS_KEY_AVD_TYPE:
                avd_type = value
            elif key == constants.INS_KEY_AVD_FLAVOR:
                avd_flavor = value

        # Find ssl tunnel info.
        adb_port = None
        vnc_port = None
        device_information = None
        if ip:
            forwarded_ports = self.GetAdbVncPortFromSSHTunnel(ip, avd_type)
            adb_port = forwarded_ports.adb_port
            vnc_port = forwarded_ports.vnc_port
            ssh_tunnel_is_connected = adb_port is not None

            adb_device = AdbTools(adb_port)
            if adb_device.IsAdbConnected():
                device_information = adb_device.device_information
                fullname = (_FULL_NAME_STRING % {
                    "device_serial": "127.0.0.1:%d" % adb_port,
                    "instance_name": name,
                    "elapsed_time": elapsed_time
                })
            else:
                fullname = (_FULL_NAME_STRING % {
                    "device_serial": "not connected",
                    "instance_name": name,
                    "elapsed_time": elapsed_time
                })
        # If instance is terminated, its ip is None.
        else:
            ssh_tunnel_is_connected = False
            fullname = (_FULL_NAME_STRING % {
                "device_serial": "terminated",
                "instance_name": name,
                "elapsed_time": elapsed_time
            })

        super(RemoteInstance,
              self).__init__(name=name,
                             fullname=fullname,
                             display=display,
                             ip=ip,
                             status=status,
                             adb_port=adb_port,
                             vnc_port=vnc_port,
                             ssh_tunnel_is_connected=ssh_tunnel_is_connected,
                             createtime=create_time,
                             elapsed_time=elapsed_time,
                             avd_type=avd_type,
                             avd_flavor=avd_flavor,
                             is_local=False,
                             device_information=device_information,
                             zone=zone)