def _CreateGceImageWithLocalFile(self, local_disk_image): """Create a Gce image with a local image file. The local disk image can be either a tar.gz file or a raw vmlinux image. e.g. /tmp/avd-system.tar.gz or /tmp/android_system_disk_syslinux.img If a raw vmlinux image is provided, it will be archived into a tar.gz file. The final tar.gz file will be uploaded to a cache bucket in storage. Args: local_disk_image: string, path to a local disk image, Returns: String, name of the Gce image that has been created. Raises: DriverError: if a file with an unexpected extension is given. """ logger.info("Creating a new gce image from a local file %s", local_disk_image) with utils.TempDir() as tempdir: if local_disk_image.endswith(self._cfg.disk_raw_image_extension): dest_tar_file = os.path.join(tempdir, self._cfg.disk_image_name) utils.MakeTarFile( src_dict={local_disk_image: self._cfg.disk_raw_image_name}, dest=dest_tar_file) local_disk_image = dest_tar_file elif not local_disk_image.endswith(self._cfg.disk_image_extension): raise errors.DriverError( "Wrong local_disk_image type, must be a *%s file or *%s file" % (self._cfg.disk_raw_image_extension, self._cfg.disk_image_extension)) disk_image_id = utils.GenerateUniqueName( suffix=self._cfg.disk_image_name) self._storage_client.Upload( local_src=local_disk_image, bucket_name=self._cfg.storage_bucket_name, object_name=disk_image_id, mime_type=self._cfg.disk_image_mime_type) disk_image_url = self._storage_client.GetUrl( self._cfg.storage_bucket_name, disk_image_id) try: image_name = self._compute_client.GenerateImageName() self._compute_client.CreateImage(image_name=image_name, source_uri=disk_image_url) finally: self._storage_client.Delete(self._cfg.storage_bucket_name, disk_image_id) return image_name
def _FetchSerialLogsFromDevices(compute_client, instance_names, output_file, port): """Fetch serial logs from a port for a list of devices to a local file. Args: compute_client: An object of android_compute_client.AndroidComputeClient instance_names: A list of instance names. output_file: A path to a file ending with "tar.gz" port: The number of serial port to read from, 0 for serial output, 1 for logcat. """ with utils.TempDir() as tempdir: src_dict = {} for instance_name in instance_names: serial_log = compute_client.GetSerialPortOutput( instance=instance_name, port=port) file_name = "%s.log" % instance_name file_path = os.path.join(tempdir, file_name) src_dict[file_path] = file_name with open(file_path, "w") as f: f.write(serial_log.encode("utf-8")) utils.MakeTarFile(src_dict, output_file)
def CollectSerialPortLogs(self, output_file, port=constants.DEFAULT_SERIAL_PORT): """Tar the instance serial logs into specified output_file. Args: output_file: String, the output tar file path port: The serial port number to be collected """ # For emulator, the serial log is the virtual host serial log. # For GCE AVD device, the serial log is the AVD device serial log. with utils.TempDir() as tempdir: src_dict = {} for device in self._devices: logger.info("Store instance %s serial port %s output to %s", device.instance_name, port, output_file) serial_log = self._compute_client.GetSerialPortOutput( instance=device.instance_name, port=port) file_name = "%s_serial_%s.log" % (device.instance_name, port) file_path = os.path.join(tempdir, file_name) src_dict[file_path] = file_name with open(file_path, "w") as f: f.write(serial_log.encode("utf-8")) utils.MakeTarFile(src_dict, output_file)