def sync(devices=[]): ''' Run continuous synchronization between CouchDB instances. ''' if len(devices) == 0: devices = local_config.get_default_devices() for name in devices: (url, path) = local_config.get_config(name) (device_id, device_password) = local_config.get_device_config(name) (db_login, db_password) = local_config.get_db_credentials(name) print 'Start continuous replication from Cozy to device.' replication.replicate(name, url, name, device_password, device_id, db_login, db_password, to_local=True) print 'Start continuous replication from device to Cozy.' replication.replicate(name, url, name, device_password, device_id, db_login, db_password) print 'Continuous replications started.' print 'Running daemon for binary synchronization...' try: context = local_config.get_daemon_context(name, 'sync') with context: replication.BinaryReplication(name) except KeyboardInterrupt: print ' Binary Synchronization interrupted.'
def init_replication(name): ''' Run initial replications then start continutous replication. Write device information in database. ''' (url, path) = local_config.get_config(name) (device_id, password) = local_config.get_device_config(name) (db_login, db_password) = local_config.get_db_credentials(name) print 'Replication from remote to local...' replication.replicate( name, url, name, password, device_id, db_login, db_password, to_local=True, continuous=False, deleted=False) print 'Init device...' dbutils.init_device(name, url, path, password, device_id) print 'Replication from local to remote...' replication.replicate( name, url, name, password, device_id, db_login, db_password, to_local=False, continuous=False) print 'Continuous replication from remote to local setting...' replication.replicate(name, url, name, password, device_id, db_login, db_password, to_local=True) print 'Continuous replication from local to remote setting...' replication.replicate(name, url, name, password, device_id, db_login, db_password) print 'Metadata replications are done.'
def __init__(self, db_name, *args, **kwargs): ''' Set database connetors on current instance. Run binary replication. ''' (self.username, self.password) = \ local_config.get_db_credentials(db_name) (self.db, self.server) = dbutils.get_db_and_server(db_name) self.db_name = db_name self.replicate_file_changes()
def get_db_and_server(database): ''' Get or create given database from/in CouchDB. ''' try: server = Server('http://localhost:5984/') server.resource.credentials = local_config.get_db_credentials(database) db = server[database] return (db, server) except Exception: logging.exception('[DB] Cannot connect to the database %s' % database) return (None, None)
def get_db(database, credentials=True): ''' Get or create given database from/in CouchDB. ''' try: server = Server('http://localhost:5984/') if credentials: server.resource.credentials = \ local_config.get_db_credentials(database) return server[database] except Exception: logging.exception('[DB] Cannot connect to the database') return None
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')
def cache_folder(device, path, add=True): ''' Download target file from remote Cozy to local folder. ''' # Get configuration. (device_url, device_mount_path) = local_config.get_config(device) (device_id, device_password) = local_config.get_device_config(device) (db_username, db_password) = local_config.get_db_credentials(device) # Built target device url. device_url = "http://%s:%s@localhost:5984/%s" % ( db_username, db_password, device ) # Ensure that path corresponds to a mounted folder. abs_path = os.path.abspath(path) device_mount_path = os.path.abspath(device_mount_path) device_mount_path_len = len(device_mount_path) device_config_path = os.path.join(local_config.CONFIG_FOLDER, device) if add: print "Start %s caching folder." % abs_path else: print "Start %s uncaching folder." % abs_path if abs_path[:device_mount_path_len] == device_mount_path: # Cache object binary_cache = binarycache.BinaryCache( device, device_config_path, device_url, device_mount_path) # Walk through given folder and run cache operation on each file found. for (dirpath, dirnames, filenames) in os.walk(abs_path): for filename in filenames: file_path = os.path.join(dirpath, filename) file_path = file_path[device_mount_path_len:] file_path = couchmount._normalize_path(file_path) if add: binary_cache.add(file_path) print "File %s successfully cached." % file_path else: binary_cache.remove(file_path) print "File %s successfully uncached." % file_path else: print 'This is not a folder synchronized with your Cozy'
def sync(name): ''' Run continuous synchronization between CouchDB instances. ''' (url, path) = local_config.get_config(name) (device_id, device_password) = local_config.get_device_config(name) (db_login, db_password) = local_config.get_db_credentials(name) replication.replicate(name, url, name, device_password, device_id, db_login, db_password, to_local=True) replication.replicate(name, url, name, device_password, device_id, db_login, db_password) print 'Continuous replications started.' print 'Running daemon for binary synchronization...' try: replication.BinaryReplication(name) except local_config.DaemonAlreadyRunning, e: print e
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 = {}
def sync(name): ''' Run continuous synchronization between CouchDB instances. ''' (url, path) = local_config.get_config(name) (device_id, device_password) = local_config.get_device_config(name) (db_login, db_password) = local_config.get_db_credentials(name) replication.replicate(name, url, name, device_password, device_id, db_login, db_password, to_local=True) replication.replicate(name, url, name, device_password, device_id, db_login, db_password) print 'Continuous replications started.' print 'Running daemon for binary synchronization...' try: context = local_config.get_daemon_context(name, 'sync') with context: replication.BinaryReplication(name) except KeyboardInterrupt: print ' Binary Synchronization interrupted.'
def cache_file(device, path, add=True): ''' Download target file from remote Cozy to local cache. ''' # Get configuration. (device_url, device_mount_path) = local_config.get_config(device) (device_id, device_password) = local_config.get_device_config(device) (db_username, db_password) = local_config.get_db_credentials(device) # Built target device url. device_url = "http://%s:%s@localhost:5984/%s" % ( db_username, db_password, device ) # Ensure that path corresponds to a mounted file. abs_path = os.path.abspath(path) device_mount_path = os.path.abspath(device_mount_path) device_mount_path_len = len(device_mount_path) device_config_path = os.path.join(local_config.CONFIG_FOLDER, device) path = abs_path[device_mount_path_len:] path = couchmount._normalize_path(path) print "Start %s caching." % abs_path if abs_path[:device_mount_path_len] == device_mount_path: binary_cache = binarycache.BinaryCache( device, device_config_path, device_url, device_mount_path) if add: binary_cache.add(path) print "File %s successfully cached." % abs_path else: binary_cache.remove(path) print "File %s successfully uncached." % abs_path else: print "Wrong path, that doesn't match any file in your device folder"