Пример #1
0
    def configure(self):
        """
        Configure the driver to use the stored configuration options
        Any store that needs special configuration should implement
        this method. If the store was not able to successfully configure
        itself, it should raise `exception.BadDriverConfiguration`
        """
        # Here we set up the various file-based image cache paths
        # that we need in order to find the files in different states
        # of cache management.
        self.set_paths()

        # We do a quick attempt to write a user xattr to a temporary file
        # to check that the filesystem is even enabled to support xattrs
        image_cache_dir = self.base_dir
        fake_image_filepath = os.path.join(image_cache_dir, 'checkme')
        with open(fake_image_filepath, 'wb') as fake_file:
            fake_file.write("XXX")
            fake_file.flush()
        try:
            set_xattr(fake_image_filepath, 'hits', '1')
        except IOError, e:
            if e.errno == errno.EOPNOTSUPP:
                msg = _("The device housing the image cache directory "
                        "%(image_cache_dir)s does not support xattr. It is "
                        "likely you need to edit your fstab and add the "
                        "user_xattr option to the appropriate line for the "
                        "device housing the cache directory.") % locals()
                logger.error(msg)
                raise exception.BadDriverConfiguration(driver="xattr",
                                                       reason=msg)
Пример #2
0
    def set_paths(self):
        """
        Creates all necessary directories under the base cache directory
        """

        self.base_dir = self.conf.image_cache_dir
        if self.base_dir is None:
            msg = _('Failed to read %s from config') % 'image_cache_dir'
            logger.error(msg)
            driver = self.__class__.__module__
            raise exception.BadDriverConfiguration(driver_name=driver,
                                                   reason=msg)

        self.incomplete_dir = os.path.join(self.base_dir, 'incomplete')
        self.invalid_dir = os.path.join(self.base_dir, 'invalid')
        self.queue_dir = os.path.join(self.base_dir, 'queue')

        dirs = [self.incomplete_dir, self.invalid_dir, self.queue_dir]

        for path in dirs:
            utils.safe_mkdirs(path)
Пример #3
0
 def create_db():
     try:
         conn = sqlite3.connect(self.db_path, check_same_thread=False,
                                factory=SqliteConnection)
         conn.executescript("""
             CREATE TABLE IF NOT EXISTS cached_images (
                 image_id TEXT PRIMARY KEY,
                 last_accessed REAL DEFAULT 0.0,
                 last_modified REAL DEFAULT 0.0,
                 size INTEGER DEFAULT 0,
                 hits INTEGER DEFAULT 0,
                 checksum TEXT
             );
         """)
         conn.close()
     except sqlite3.DatabaseError as e:
         msg = _("Failed to initialize the image cache database. "
                 "Got error: %s") % e
         LOG.error(msg)
         raise exception.BadDriverConfiguration(driver_name='sqlite',
                                                reason=msg)
Пример #4
0
 def initialize_db(self):
     db = self.options.get('image_cache_sqlite_db', DEFAULT_SQLITE_DB)
     self.db_path = os.path.join(self.base_dir, db)
     try:
         conn = sqlite3.connect(self.db_path, check_same_thread=False,
                                factory=SqliteConnection)
         conn.executescript("""
             CREATE TABLE IF NOT EXISTS cached_images (
                 image_id TEXT PRIMARY KEY,
                 last_accessed REAL DEFAULT 0.0,
                 last_modified REAL DEFAULT 0.0,
                 size INTEGER DEFAULT 0,
                 hits INTEGER DEFAULT 0,
                 checksum TEXT
             );
         """)
         conn.close()
     except sqlite3.DatabaseError, e:
         msg = _("Failed to initialize the image cache database. "
                 "Got error: %s") % e
         logger.error(msg)
         raise exception.BadDriverConfiguration(driver_name='sqlite',
                                                reason=msg)