def add_images_from_csv_to_mock(path):
     """Add images to the mock, reading the csv files saved by the backup
      tool.
     :param path: The directory where the csv files are.
     :return: This method does not return nothing.
     Each file in path has this pattern: backup_<regionname>.csv.
     """
     for file in glob.glob(path + '/*.csv'):
         region_name = os.path.basename(file)[7:-4]
         if region_name not in ServersFacade.images:
             if ServersFacade.use_persistence:
                 ServersFacade.images[region_name] =\
                     shelve.open(ServersFacade.dir_persist + '/_persist_' +
                                 region_name)
             else:
                 ServersFacade.images[region_name] = dict()
         with open(file) as f:
             for row in csv.reader(f):
                 # ignore blank lines
                 if len(row) == 0:
                     continue
                 image = GlanceSyncImage.from_field_list(row)
                 ServersFacade.images[region_name][image.id] = image
         if ServersFacade.use_persistence:
             ServersFacade.images[region_name].sync()
 def add_images_from_csv_to_mock(path):
     """Add images to the mock, reading the csv files saved by the backup
      tool.
     :param path: The directory where the csv files are.
     :return: This method does not return nothing.
     Each file in path has this pattern: backup_<regionname>.csv.
     """
     for file in glob.glob(path + '/*.csv'):
         region_name = os.path.basename(file)[7:-4]
         if region_name not in ServersFacade.images:
             if ServersFacade.use_persistence:
                 ServersFacade.images[region_name] =\
                     shelve.open(ServersFacade.dir_persist + '/_persist_' +
                                 region_name)
             else:
                 ServersFacade.images[region_name] = dict()
         with open(file) as f:
             for row in csv.reader(f):
                 # ignore blank lines
                 if len(row) == 0:
                     continue
                 image = GlanceSyncImage.from_field_list(row)
                 ServersFacade.images[region_name][image.id] = image
         if ServersFacade.use_persistence:
             ServersFacade.images[region_name].sync()
    def upload_image(self, regionobj, image):
        """Upload the image to the glance server on the specified region.

        :param regionobj: GlanceSyncRegion object; the region where the image
          will be upload.
        :param image: GlanceSyncImage object; the image to be uploaded.
        :return: The UUID of the new image.
        """
        count = 1
        if regionobj.fullname not in ServersFacade.images:
            ServersFacade.images[regionobj.fullname] = dict()
        imageid = '1$' + image.name
        while imageid in ServersFacade.images[regionobj.fullname]:
            count += 1
            imageid = str(count) + '$' + image.name
        owner = regionobj.target['tenant'] + 'id'
        new_image = GlanceSyncImage(image.name, imageid, regionobj.fullname,
                                    owner, image.is_public, image.checksum,
                                    image.size, image.status,
                                    dict(image.user_properties))

        ServersFacade.images[regionobj.fullname][imageid] = new_image
        if ServersFacade.use_persistence:
            ServersFacade.images[regionobj.fullname].sync()

        return imageid
示例#4
0
    def __upload_image(self, master_image, images_dict, regionobj):
        new_image = copy.deepcopy(master_image)
        # update kernel_id & ramdisk_id if necessary.
        glancesync_ami.update_kernelramdisk_id(new_image, master_image,
                                               images_dict)
        # filter properties to upload
        metadata_set = set(regionobj.target['metadata_set'])
        properties = set(new_image.user_properties.keys())
        if len(metadata_set) > 0:
            diff = properties - metadata_set - set(['kernel_id', 'ramdisk_id'])
            for p in diff:
                del new_image.user_properties[p]

        # upload
        uuid = regionobj.target['facade'].upload_image(regionobj, new_image)

        # update images_dict with the new image (needed for pending_ami images)
        images_dict[new_image.name] = GlanceSyncImage(new_image.name, uuid,
                                                      regionobj.fullname)
    def add_image_to_mock(image):
        """Add the image to the mock
        :param image: The image to add. If can be a GlanceSyncImage or a list
        :return: This method does not return nothing.
        """
        if type(image) == list:
            image = GlanceSyncImage.from_field_list(image)
        else:
            image = copy.deepcopy(image)

        if image.region not in ServersFacade.images:
            if ServersFacade.use_persistence:
                ServersFacade.images[image.region] =\
                    shelve.open(ServersFacade.dir_persist + '/_persist_' +
                                image.region)
            else:
                ServersFacade.images[image.region] = dict()
        ServersFacade.images[image.region][image.id] = image
        if ServersFacade.use_persistence:
            ServersFacade.images[image.region].sync()
    def add_image_to_mock(image):
        """Add the image to the mock
        :param image: The image to add. If can be a GlanceSyncImage or a list
        :return: This method does not return nothing.
        """
        if type(image) == list:
            image = GlanceSyncImage.from_field_list(image)
        else:
            image = copy.deepcopy(image)

        if image.region not in ServersFacade.images:
            if ServersFacade.use_persistence:
                ServersFacade.images[image.region] =\
                    shelve.open(ServersFacade.dir_persist + '/_persist_' +
                                image.region)
            else:
                ServersFacade.images[image.region] = dict()
        ServersFacade.images[image.region][image.id] = image
        if ServersFacade.use_persistence:
            ServersFacade.images[image.region].sync()
示例#7
0
    def get_imagelist(self, regionobj):
        """return a image list from the glance of the specified region

        :param regionobj: The GlanceSyncRegion object of the region to list
        :return: a list of GlanceSyncImage objects
        """
        client = self._get_glanceclient(regionobj.region)
        try:
            target = regionobj.target
            # We need a Pool to implement a timeout. Unfortunately
            # setting client.images.client.timeout does nothing.
            if 'list_images_timeout' in target:
                timeout = target['list_images_timeout']
            else:
                timeout = _default_timeout
            pool = Pool(1)
            result = pool.apply_async(_getrawimagelist, (client, ))
            images = result.get(timeout=timeout)
            image_list = list()
            for image in images:
                i = GlanceSyncImage(image['name'], image['id'],
                                    regionobj.fullname, image['owner'],
                                    image['is_public'], image['checksum'],
                                    image['size'], image['status'],
                                    image['properties'], image)

                image_list.append(i)

        except TimeoutError:
            msg = regionobj.fullname + \
                ': Timeout while retrieving image list.'
            self.logger.error(msg)
            raise GlanceFacadeException(msg)
        except Exception, e:
            cause = str(e)
            if not cause:
                cause = repr(e)
            msg = regionobj.fullname + \
                ': Error retrieving image list. Cause: ' + cause
            self.logger.error(msg)
            raise GlanceFacadeException(msg)