def check_image(self, image_id, datastore): image_dir = os_vmdk_path(datastore, image_id, IMAGE_FOLDER_NAME_PREFIX) try: return os.path.exists(image_dir) except: self._logger.exception("Error looking up %s" % image_dir) return False
def _check_image_repair(self, image_id, datastore): vmdk_pathname = os_vmdk_path(datastore, image_id, IMAGE_FOLDER_NAME_PREFIX) image_dirname = os.path.dirname(vmdk_pathname) try: # Check vmdk file if not os.path.exists(vmdk_pathname): self._logger.info("Vmdk path doesn't exists: %s" % vmdk_pathname) return False except Exception as ex: self._logger.exception("Exception validating %s, %s" % (image_dirname, ex)) return False # Check timestamp file timestamp_pathname = os.path.join(image_dirname, self.IMAGE_TIMESTAMP_FILE_NAME) try: if os.path.exists(timestamp_pathname): self._logger.info("Timestamp file exists: %s" % timestamp_pathname) return True except Exception as ex: self._logger.exception("Exception validating %s, %s" % (timestamp_pathname, ex)) # The timestamp file is not accessible, try creating one try: self._create_image_timestamp_file(image_dirname) except Exception as ex: self._logger.exception("Exception creating %s, %s" % (timestamp_pathname, ex)) return False self._logger.info("Image repaired: %s" % image_dirname) return True
def _delete_image_on_vsan(self, datastore_id, image_id): self._logger.info("_delete_image_on_vsan: datastore_id=%s, image_id=%s" % (datastore_id, image_id)) # clear ddb.deletable flag in .vmdk file which would otherwise cause # Vmacore::File::PermissionDeniedException (PR 1704935) vmdk_path = os_vmdk_path(datastore_id, image_id, IMAGE_FOLDER_NAME_PREFIX) temp_path = "%s~" % vmdk_path pattern = re.compile("^ddb.deletable = ") disk_file = open(vmdk_path) temp_file = open(temp_path, "w+") for line in disk_file: if not pattern.match(line): temp_file.write(line) else: self._logger.info("_delete_image_on_vsan: skip %s" % line) temp_file.close() disk_file.close() os.rename(temp_path, vmdk_path) # delete vdisk self._host_client.delete_file(vmdk_path) # delete folder content which would otherwise cause vim.fault.DirectoryNotEmpty (PR 1721520) image_dir = self._image_directory(datastore_id, image_id) for entry in os.listdir(image_dir): if not entry.startswith('.') or entry.endswith(".lck"): self._logger.info("_delete_image_on_vsan: delete %s" % os.path.join(image_dir, entry)) entry_full_path = os.path.join(image_dir, entry) if os.path.isdir(entry_full_path): rm_rf(entry_full_path) else: os.unlink(entry_full_path) # delete folder (osfs namespace) self._host_client.delete_file(image_dir)
def check_image_dir(self, image_id, datastore): image_path = os_vmdk_path(datastore, image_id, IMAGE_FOLDER_NAME_PREFIX) try: return os.path.exists(os.path.dirname(image_path)) except: self._logger.error( "Error looking up %s" % image_path, exc_info=True) return False
def get_datastore(self, disk_id): for datastore in self._ds_manager.get_datastore_ids(): disk = os_vmdk_path(datastore, disk_id) if os.path.isfile(disk): return datastore # Extra logging to help debug failures where host2 cannot find disk created by host1 on a shared datastore self._logger.error("get_disk_datastore failed: disk=%s, datastores=%s" % (disk_id, self._ds_manager.get_datastore_ids())) for datastore in self._ds_manager.get_datastore_ids(): p1 = os_datastore_root(datastore) p2 = os_datastore_path(datastore, compond_path_join(DISK_FOLDER_NAME_PREFIX, disk_id)) p3 = os_vmdk_path(datastore, disk_id) self._logger.error("get_disk_datastore check_path: %s:%s, %s:%s, %s:%s" % (p1, os.path.isdir(p1), p2, os.path.isdir(p2), p3, os.path.isfile(p3))) return None
def touch_image_timestamp(self, ds_id, image_id): image_path = os.path.dirname(os_vmdk_path(ds_id, image_id, IMAGE_FOLDER_NAME_PREFIX)) # Touch the timestamp file timestamp_pathname = os.path.join(image_path, self.IMAGE_TIMESTAMP_FILE_NAME) try: os.utime(timestamp_pathname, None) except Exception as ex: self._logger.exception("Exception looking up %s, %s" % (timestamp_pathname, ex)) raise ex
def get_datastore(self, disk_id): for datastore in self._ds_manager.get_datastore_ids(): disk = os_vmdk_path(datastore, disk_id) if os.path.isfile(disk): return datastore # Extra logging to help debug failures where host2 cannot find disk created by host1 on a shared datastore self._logger.error( "get_disk_datastore failed: disk=%s, datastores=%s" % (disk_id, self._ds_manager.get_datastore_ids())) for datastore in self._ds_manager.get_datastore_ids(): p1 = os_datastore_root(datastore) p2 = os_datastore_path( datastore, compond_path_join(DISK_FOLDER_NAME_PREFIX, disk_id)) p3 = os_vmdk_path(datastore, disk_id) self._logger.error( "get_disk_datastore check_path: %s:%s, %s:%s, %s:%s" % (p1, os.path.isdir(p1), p2, os.path.isdir(p2), p3, os.path.isfile(p3))) return None
def image_size(self, image_id): for image_ds in self._ds_manager.image_datastores(): if self._ds_manager.datastore_type(image_ds) is DatastoreType.VSAN: if os.path.exists(os_vmdk_path(image_ds, image_id, IMAGE_FOLDER_NAME_PREFIX)): # VSAN does not have flat.vmdk so we cannot get file size. Default to 1GB. return 1024 ** 3 else: try: image_path = os_vmdk_flat_path(image_ds, image_id, IMAGE_FOLDER_NAME_PREFIX) return os.path.getsize(image_path) except os.error: pass self._logger.info("Image %s not found in DataStore %s" % (image_id, image_ds)) self._logger.warning("Failed to get image size:", exc_info=True) # Failed to access shared image. raise NoSuchResourceException(ResourceType.IMAGE, "Image does not exist.")
def check_and_validate_image(self, image_id, ds_id): image_dir = os.path.dirname(os_vmdk_path(ds_id, image_id, IMAGE_FOLDER_NAME_PREFIX)) try: if not os.path.exists(image_dir): return False except: self._logger.exception("Error looking up %s" % image_dir) return False # Check the existence of the timestamp file timestamp_pathname = os.path.join(image_dir, self.IMAGE_TIMESTAMP_FILE_NAME) try: if os.path.exists(timestamp_pathname): return True except Exception as ex: self._logger.exception("Exception looking up %s, %s" % (timestamp_pathname, ex)) return False return False
def patched_os_vmdk_path(self, datastore, disk_id, folder): folder = self.dir0 ret = os_vmdk_path(datastore, disk_id, folder) return ret
def _vmdk_rmdir(self, datastore, disk_id): path = os.path.dirname(os_vmdk_path(datastore, disk_id)) self._host_client.delete_file(path)
def _vmdk_mkdir(self, datastore, disk_id): path = os.path.dirname(os_vmdk_path(datastore, disk_id)) self._host_client.make_directory(path)
def _image_directory(self, datastore_id, image_id): return os.path.dirname(os_vmdk_path(datastore_id, image_id, IMAGE_FOLDER_NAME_PREFIX))
def get_image_path(self, datastore_id, image_id): return os_vmdk_path(datastore_id, image_id, IMAGE_FOLDER_NAME_PREFIX)