def _mark_unused_image(self, image_scanner, image_id, image_dir, unused_images): vmdk_filepath = os.path.join(image_dir, vmdk_add_suffix(image_id)) # If a file of the format: <image-id>.vmdk does not exists, we assume this is # the result of a partial image copy, it should be deleted, log a message # and continue if not os.path.isfile(vmdk_filepath): self._logger.info("No vmdk file found in image directory: %s", image_dir) return # If there is not already a marker file, create the file marker_pathname = os.path.join( image_dir, self._image_manager.UNUSED_IMAGE_MARKER_FILE_NAME) if not os.path.isfile(marker_pathname): # Write the content of _start_time to the marker file, any change occurred to # the image after _start_time, invalidates the image as a candidate for removal try: self._write_marker_file(marker_pathname, image_scanner.start_time_str) except Exception as ex: self._logger.warning("Failed to write maker file: %s, %s" % (marker_pathname, ex)) self._logger.info("Found unused image: %s" % image_id) unused_images[image_id] = image_dir
def _disk_matcher(self, disk_id): # On VMFS, device.backing.fileName is in the form of: '[ds_name] disk_[disk_id]/[disk_id].vmdk' # On VSAN, top-level folder is symlink to its internal object id, so the fileName field becomes # '[ds_name] [vsan object id]/[disk_id].vmdk'. # Since disk_id is unique, we only need to match [disk_id].vmdk. path = vmdk_add_suffix(disk_id) return lambda device: device.backing.fileName.endswith(path)
def detach_disk(self, cfg_info, disk_id): disks = self._get_devices_by_type(cfg_info, vim.vm.device.VirtualDisk) disk_vmdk = vmdk_add_suffix(disk_id) disk_to_detach = None for disk in disks: if disk.backing.fileName.endswith(disk_vmdk): disk_to_detach = disk if disk_to_detach is None: raise DeviceNotFoundException() self._remove_device(disk_to_detach)
def send_image_to_host(self, source_image_id, source_datastore, destination_image_id, destination_datastore, destination_host, destination_port): self._logger.info("transfer_image: connecting to remote agent") if self._auth_enabled: remote_agent_client = DirectClient("Host", Host.Client, destination_host, destination_port, 60, certfile=SSL_CERT_FILE, keyfile=SSL_KEY_FILE, capath=CA_PATH, ciphers=SSL_CIPHERS, validate=True) else: remote_agent_client = DirectClient("Host", Host.Client, destination_host, destination_port, 60, validate=False) remote_agent_client.connect() self._logger.info("transfer_image: getting ticket") nfc_ticket = self._get_nfc_ticket(remote_agent_client, destination_datastore) self._logger.info("transfer_image: creating remote image") if destination_image_id is None: destination_image_id = source_image_id upload_folder = self._create_remote_image(remote_agent_client, destination_image_id, destination_datastore) try: source_file_path = datastore_path(source_datastore, compond_path_join(IMAGE_FOLDER_NAME_PREFIX, source_image_id), vmdk_add_suffix(source_image_id)) destination_file_path = os.path.join(upload_folder, vmdk_add_suffix(destination_image_id)) self._logger.info("transfer_image: nfc copy image %s => (%s)%s, sslThumbprint=%s, ticket=%s", source_file_path, destination_host, destination_file_path, nfc_ticket.ssl_thumbprint, nfc_ticket.session_id) self._host_client.nfc_copy(source_file_path, destination_host, destination_file_path, nfc_ticket.ssl_thumbprint, nfc_ticket.session_id) self._logger.info("transfer_image: finalizing remote image") self._finalize_remote_image(remote_agent_client, destination_image_id, destination_datastore, upload_folder) except: self._logger.info("transfer_image: cleaning up failed transfer") self._cleanup_remote_image(remote_agent_client, destination_datastore, upload_folder) raise
def _mark_unused_image(self, image_scanner, image_id, image_dir, unused_images): vmdk_filepath = os.path.join(image_dir, vmdk_add_suffix(image_id)) # If a file of the format: <image-id>.vmdk does not exists, we assume this is # the result of a partial image copy, it should be deleted, log a message # and continue if not os.path.isfile(vmdk_filepath): self._logger.info("No vmdk file found in image directory: %s", image_dir) return # If there is not already a marker file, create the file marker_pathname = os.path.join(image_dir, self._image_manager.UNUSED_IMAGE_MARKER_FILE_NAME) if not os.path.isfile(marker_pathname): # Write the content of _start_time to the marker file, any change occurred to # the image after _start_time, invalidates the image as a candidate for removal try: self._write_marker_file(marker_pathname, image_scanner.start_time_str) except Exception as ex: self._logger.warning("Failed to write maker file: %s, %s" % (marker_pathname, ex)) self._logger.info("Found unused image: %s" % image_id) unused_images[image_id] = image_dir
def send_image_to_host(self, source_image_id, source_datastore, destination_image_id, destination_datastore, destination_host, destination_port): self._logger.info("transfer_image: connecting to remote agent") if self._auth_enabled: remote_agent_client = DirectClient("Host", Host.Client, destination_host, destination_port, 60, certfile=SSL_CERT_FILE, keyfile=SSL_KEY_FILE, capath=CA_PATH, ciphers=SSL_CIPHERS, validate=True) else: remote_agent_client = DirectClient("Host", Host.Client, destination_host, destination_port, 60, validate=False) remote_agent_client.connect() self._logger.info("transfer_image: getting ticket") nfc_ticket = self._get_nfc_ticket(remote_agent_client, destination_datastore) self._logger.info("transfer_image: creating remote image") if destination_image_id is None: destination_image_id = source_image_id upload_folder = self._create_remote_image(remote_agent_client, destination_image_id, destination_datastore) try: source_file_path = datastore_path( source_datastore, compond_path_join(IMAGE_FOLDER_NAME_PREFIX, source_image_id), vmdk_add_suffix(source_image_id)) destination_file_path = os.path.join( upload_folder, vmdk_add_suffix(destination_image_id)) self._logger.info( "transfer_image: nfc copy image %s => (%s)%s, sslThumbprint=%s, ticket=%s", source_file_path, destination_host, destination_file_path, nfc_ticket.ssl_thumbprint, nfc_ticket.session_id) self._host_client.nfc_copy(source_file_path, destination_host, destination_file_path, nfc_ticket.ssl_thumbprint, nfc_ticket.session_id) self._logger.info("transfer_image: finalizing remote image") self._finalize_remote_image(remote_agent_client, destination_image_id, destination_datastore, upload_folder) except: self._logger.info("transfer_image: cleaning up failed transfer") self._cleanup_remote_image(remote_agent_client, destination_datastore, upload_folder) raise