Esempio n. 1
0
    def get_images(self, datastore):
        """ Get image list from datastore
        :param datastore: datastore id
        :return: list of string, image id list
        """
        image_ids = []

        # image_folder is /vmfs/volumes/${datastore}/images
        image_folder = os_datastore_path(datastore, IMAGE_FOLDER_NAME)

        if not os.path.exists(image_folder):
            raise DatastoreNotFoundException()

        # prefix is the 2-digit prefix of image id
        for prefix in os.listdir(image_folder):
            # outer path is something like
            # /vmfs/volumes/${datastore}/images/${image_id}[0:2]
            outer_path = os.path.join(image_folder, prefix)
            if not os.path.isdir(outer_path):
                continue

            for image_id in os.listdir(outer_path):
                if self.check_image(image_id, datastore):
                    image_ids.append(image_id)

        return image_ids
    def normalize(self, ds_name_or_id):
        # if test does not set ds_name_id_map, simply return name as id
        if not self._ds_name_id_map:
            return ds_name_or_id

        if ds_name_or_id in self._ds_name_id_map:
            return self._ds_name_id_map[ds_name_or_id]
        if ds_name_or_id in self._ds_name_id_map.values():
            return ds_name_or_id
        raise DatastoreNotFoundException("%s not found" % ds_name_or_id)
Esempio n. 3
0
    def get_vm_images(self, image_scanner):
        vms_dir_path = os_datastore_path(image_scanner.datastore_id,
                                         VM_FOLDER_NAME)
        # Log messages with prefix: "IMAGE SCANNER" are for debugging
        # and will be removed after basic testing
        self._logger.info("IMAGE SCANNER: vms_dir: %s" % vms_dir_path)
        if not os.path.isdir(vms_dir_path):
            self._logger.info("get_vm_images: vms_dir: %s, doesn't exist" %
                              vms_dir_path)
            raise DatastoreNotFoundException("Image scanner, cannot find vms "
                                             "directory for datastore: %s" %
                                             image_scanner.datastore_id)

        return self._collect_active_images(image_scanner, vms_dir_path)
Esempio n. 4
0
    def delete_unused(self, image_sweeper):
        images_dir_path = os_datastore_path(image_sweeper.datastore_id,
                                            IMAGE_FOLDER_NAME)
        # Log messages with prefix: "IMAGE SWEEPER" are for debugging
        # and will be removed after basic testing
        self._logger.info("IMAGE SWEEPER: images_dir: %s" % images_dir_path)
        if not os.path.isdir(images_dir_path):
            self._logger.info(
                "images_dir_path: images_dir: %s, doesn't exist" %
                images_dir_path)
            raise DatastoreNotFoundException(
                "Image sweeper, cannot find image "
                "directory for datastore: %s" % image_sweeper.datastore_id)

        return self._delete_unused_images(image_sweeper, images_dir_path)
    def get_images(self, datastore):
        """ Get image list from datastore
        :param datastore: datastore id
        :return: list of string, image id list
        """
        image_ids = []

        if not os.path.exists(os_datastore_root(datastore)):
            raise DatastoreNotFoundException()

        # image_folder is /vmfs/volumes/${datastore}/images_*
        for dir in list_top_level_directory(datastore, IMAGE_FOLDER_NAME_PREFIX):
            image_id = dir.split(COMPOND_PATH_SEPARATOR)[1]
            if self.check_image(image_id, datastore):
                image_ids.append(image_id)

        return image_ids
Esempio n. 6
0
    def get_resource_ids(self, datastore=None):
        resources = set()
        if datastore:
            datastore_path = self._datastore_path(datastore)
            if not os.path.exists(datastore_path):
                raise DatastoreNotFoundException()

            if os.path.isdir(datastore_path):
                [
                    resources.add(disk[:-5])
                    for disk in os.listdir(datastore_path)
                ]
        else:
            for datastore in os.listdir(self.disk_path):
                resources = resources.union(self.get_resource_ids(datastore))

        return resources
Esempio n. 7
0
    def test_touch_image_timestamp(self):
        """Test touch image timestamp against mock"""
        handler = HostHandler(MagicMock())
        # no failure
        vm_id = uuid.uuid4()
        res = handler._touch_image_timestamp(vm_id, "ds", "image")
        assert_that(res.result, equal_to(CreateVmResultCode.OK))

        # image not found
        handler.hypervisor.image_manager.\
            touch_image_timestamp.side_effect = OSError()
        res = handler._touch_image_timestamp(vm_id, "ds", "image")
        assert_that(res.result, equal_to(CreateVmResultCode.IMAGE_NOT_FOUND))

        # invalid datastore, this must be last in the sequence
        handler.hypervisor.datastore_manager.\
            normalize.side_effect = DatastoreNotFoundException()
        res = handler._touch_image_timestamp(vm_id, "ds", "image")
        assert_that(res.result, equal_to(CreateVmResultCode.SYSTEM_ERROR))
Esempio n. 8
0
 def normalize(name):
     if name == "vm_path_b" or name == "disk_b":
         raise DatastoreNotFoundException()
     return name
Esempio n. 9
0
 def get_ds_in_cache(self, ds_name):
     for ds in self._ds_cache:
         if ds.name == ds_name:
             return ds
     raise DatastoreNotFoundException("Datastore '%s' not found on host." %
                                      ds_name)