示例#1
0
    def __init__(self, device_name, mountpoint, uri=None, *args, **kwargs):
        '''
        Configure file system, device and store remote Cozy informations.
        '''
        logger.info('Configuring CouchDB Fuse...')

        # Configure fuse
        fuse.Fuse.__init__(self, *args, **kwargs)
        self.fuse_args.mountpoint = mountpoint
        self.fuse_args.add('allow_other')
        self.currentFile = None
        logger.info('- Fuse configured')

        # Configure device
        self.device = device_name
        (self.db, self.server) = dbutils.get_db_and_server(device_name)
        logger.info('- Database configured')

        # Configure Cozy
        device = dbutils.get_device(device_name)
        self.urlCozy = device.get('url', '')
        self.passwordCozy = device.get('password', '')
        self.loginCozy = device_name
        logger.info('- Cozy configured')

        # Configure replication urls.
        (self.db_username, self.db_password) = \
            local_config.get_db_credentials(device_name)
        self.rep_source = 'http://%s:%s@localhost:5984/%s' % (
            self.db_username,
            self.db_password,
            self.device
        )
        self.rep_target = "https://%s:%s@%s/cozy" % (
            self.loginCozy,
            self.passwordCozy,
            self.urlCozy.split('/')[2]
        )
        logger.info('- Replication configured')

        # Configure cache and create required folders
        device_path = os.path.join(CONFIG_FOLDER, device_name)
        self.binary_cache = binarycache.BinaryCache(
            device_name, device_path, self.rep_source, mountpoint)
        self.file_size_cache = cache.Cache()
        self.attr_cache = cache.Cache()
        self.name_cache = cache.Cache()
        self.fd_cache = cache.Cache()

        logger.info('- Cache configured')
示例#2
0
    def __init__(self, database, mountpoint, uri=None, *args, **kwargs):
        '''
        Configure file system, database and store remote Cozy informations.
        '''
        logger.info('Mounting folder...')

        # Configure fuse
        fuse.Fuse.__init__(self, *args, **kwargs)
        self.fuse_args.mountpoint = mountpoint
        self.fuse_args.add('allow_other')
        self.currentFile = None

        # Configure database
        self.database = database
        (self.db, self.server) = dbutils.get_db_and_server(database)

        ## Configure Cozy
        device = dbutils.get_device(database)
        self.urlCozy = device['url']
        self.passwordCozy = device['password']
        self.loginCozy = device['login']

        # Configure replication urls.
        (self.db_username, self.db_password) = \
            local_config.get_db_credentials(database)
        string_data = (
            self.db_username,
            self.db_password,
            self.database
        )
        self.rep_source = 'http://%s:%s@localhost:5984/%s' % string_data
        string_data = (
            self.loginCozy,
            self.passwordCozy,
            self.urlCozy.split('/')[2]
        )
        self.rep_target = "https://%s:%s@%s/cozy" % string_data

        # init cache
        self.dirs = {}
        self.descriptors = {}
        self.writeBuffers = {}
示例#3
0
    def replicate_file_changes(self):
        '''
        Replicate all changes related to files and binaries to stored devices.
        '''

        device = dbutils.get_device(self.db_name)
        self.urlCozy = device['url']
        self.loginCozy = device['login']
        self.passwordCozy = device['password']

        self.ids = {}
        for res in self.db.view("file/all"):
            if 'binary' in res.value and 'file' in res.value['binary']:
                id_binary = res.value['binary']['file']['id']
                if id_binary in self.db:
                    binary = self.db[id_binary]
                    self.ids[res.id] = [id_binary, binary.rev]
                else:
                    self.ids[res.id] = [id_binary, ""]

        changes = self.db.changes(feed='continuous',
                                  heartbeat='1000',
                                  since=device['change'],
                                  include_docs=True)

        for line in changes:
            if not self._is_device(line):
                device['change'] = line['seq']
                self.db.save(device)

                if self._is_deleted(line):
                    self._delete_file(line)
                elif self._is_new(line):
                    self._add_file(line)
                else:
                    self._update_file(line)
示例#4
0
    def replicate_file_changes(self):
        '''
        Replicate all changes related to files and binaries to stored devices.
        '''
        device = dbutils.get_device(self.db_name)
        self.urlCozy = device['url']
        self.loginCozy = device['login']
        self.passwordCozy = device['password']

        # Initialize sequence number to avoid full replication
        # every time.
        if 'seq' not in device:
            device['seq'] = 0
        new_seq = device['seq']

        # We create an infinite loop which will get file changes
        # every 10 seconds, and fetch related binaries if needed.
        while True:
            changes = self.db.changes(since=device['seq'],
                                      filter='file/all',
                                      include_docs=True)

            binary_ids = []

            # Iterate over changes
            for line in changes['results']:

                # Save last sequence number
                new_seq = line['seq']

                # Find related binary and add its ID to the list
                # of files to replicate.
                doc = line['doc']
                if self._is_deleted(line):
                    logger.info("Deleting file %s..." % line['id'])
                    try:
                        # Delete file locally
                        self.db.delete(self.db[doc['binary']['file']['id']])
                    except http.ResourceNotFound:
                        # Already deleted
                        pass
                elif self._is_new(line):
                    logger.info("Creating file %s..." % doc['name'])
                else:
                    logger.info("Updating file %s..." % doc['name'])
                if 'binary' in doc:
                    binary_ids.append(doc['binary']['file']['id'])

            # Replicate related binaries
            if len(binary_ids) > 0:
                try:
                    self._replicate_to_local(binary_ids)
                except http.ResourceConflict:
                    #TODO: Handle comparison
                    pass
                except:
                    logging.exception(
                        'An error occured while replicating doc %s'
                        % line['id']
                    )

            # Save last sequence number along with the device
            if new_seq != device['seq']:
                device = dbutils.get_device(self.db_name)
                device['seq'] = new_seq
                self.db.save(device)

            # Wait until further potential changes
            time.sleep(10)