Esempio n. 1
0
 def __init__(self, remote_ip_lst=None):
     """
     Constructor
     """
     self.sync_files = SyncFiles()
     if remote_ip_lst is None:
         self.remote_ip_lst = []
     self.remote_ip_lst = remote_ip_lst
Esempio n. 2
0
 def __init__(self, remote_ip_lst=None):
     """
     Constructor
     """
     self.sync_files = SyncFiles()
     if remote_ip_lst is None:
         self.remote_ip_lst = []
     self.remote_ip_lst = remote_ip_lst
Esempio n. 3
0
    def my_init(self, cond=None, eventq=None, retryq=None, remote_ip_lst=None):
        """init vars

        @param cond: condition var
        @type cond: threading.Condition
        @param eventq: event queue
        @type eventq: collections.deque
        @param retryq: retry queue
        @type retryq: Queue.Queue
        """
        self.cond = cond
        self.eventq = eventq
        self.retryq = retryq  # 同步重试命令队列
        self.remote_ip_lst = remote_ip_lst
        self._stop_event = threading.Event()
        self.sync_files = SyncFiles()
        self.encrypt_set = self.sync_files.encrypt_set
Esempio n. 4
0
class SyncServer(object):
    """Real-time sync server
    """

    def __init__(self, remote_ip_lst=None):
        """
        Constructor
        """
        self.sync_files = SyncFiles()
        if remote_ip_lst is None:
            self.remote_ip_lst = []
        self.remote_ip_lst = remote_ip_lst

    def start(self):
        """启动实时同步配置文件接受端"""
        # 初始化rsync配置文件
        self.sync_files.update_rsyncd_conf(self.remote_ip_lst)
        rsync_serv_cmd = ['rsync', '--daemon',
                           '--config=%s' % self.sync_files.rsync_cfg_path]
        execute_cmd(rsync_serv_cmd)
Esempio n. 5
0
class SyncServer(object):
    """Real-time sync server
    """
    def __init__(self, remote_ip_lst=None):
        """
        Constructor
        """
        self.sync_files = SyncFiles()
        if remote_ip_lst is None:
            self.remote_ip_lst = []
        self.remote_ip_lst = remote_ip_lst

    def start(self):
        """启动实时同步配置文件接受端"""
        # 初始化rsync配置文件
        self.sync_files.update_rsyncd_conf(self.remote_ip_lst)
        rsync_serv_cmd = [
            'rsync', '--daemon',
            '--config=%s' % self.sync_files.rsync_cfg_path
        ]
        execute_cmd(rsync_serv_cmd)
Esempio n. 6
0
 def __init__(self, remote_ip_lst, local_ip=''):
     """
     Constructor
     """
     self.sync_files = SyncFiles()
     self.local_ip = local_ip
     self.remote_ip_lst = remote_ip_lst
     self.client_notifier = None
     self.sync_manager = None
     self.retry_thread = None
     self.server_notifier = None
     self.full_sync_thread = None
Esempio n. 7
0
 def full_sync(remote_ip):
     LOGGER.debug('Start full sync...')
     sync_files = SyncFiles()
     sync_files.sync_full_files(remote_ip)
Esempio n. 8
0
class SyncThread(threading.Thread):
    """
    Sync cmd thread ,get event from sync queue.(consumer thread)
    When sync failed, insert event into retry queue.
    """
    def __init__(self, name, **kwargs):
        threading.Thread.__init__(self, name=name)
        self.my_init(**kwargs)

    def my_init(self, cond=None, eventq=None, retryq=None, remote_ip_lst=None):
        """init vars

        @param cond: condition var
        @type cond: threading.Condition
        @param eventq: event queue
        @type eventq: collections.deque
        @param retryq: retry queue
        @type retryq: Queue.Queue
        """
        self.cond = cond
        self.eventq = eventq
        self.retryq = retryq  # 同步重试命令队列
        self.remote_ip_lst = remote_ip_lst
        self._stop_event = threading.Event()
        self.sync_files = SyncFiles()
        self.encrypt_set = self.sync_files.encrypt_set

    def sync_file(self, event):
        """sync file or directory

        @param event: event
        @type event: pyinotify.Event
        """
        sync_cmds = []
        if event.mask & (IN_DELETE | IN_MOVED_FROM):
            # get sync delete remote file cmd
            for remote_ip in self.remote_ip_lst:
                sync_cmd = self.sync_files.combine_del_cmd(
                    remote_ip, event.pathname, event.dir)
                sync_cmds.append(sync_cmd)
        else:
            # get sync create or modify file cmd
            is_crypt = self.sync_files.is_encrypt_file(event.pathname,
                                                       self.encrypt_set)
            for remote_ip in self.remote_ip_lst:
                sync_cmd = self.sync_files.combine_other_cmd(
                    remote_ip, event.pathname, event.dir, is_crypt)
                sync_cmds.append(sync_cmd)
        LOGGER.debug('sync_cmds: %s', sync_cmds)
        for sync_cmd in sync_cmds:
            if sync_cmd:
                process = subprocess.Popen(sync_cmd,
                                           shell=True,
                                           stderr=subprocess.PIPE)
                retcode = process.wait()
                if retcode != 0:
                    LOGGER.warning(
                        'sync failed:%s.Insert cmd into retry queue!',
                        process.stderr.read())
                    sync_dict = {}
                    sync_dict['cmd'] = sync_cmd
                    sync_dict['time'] = int(time.time())
                    self.retryq.put(sync_dict)

    def stop(self):
        """
        Stop sync's loop. Join the thread.
        """
        LOGGER.info('Thread %s stop', self.name)
        self._stop_event.set()
        self.cond.acquire()
        self.cond.notify_all()
        self.cond.release()
        threading.Thread.join(self, 8)

    def loop(self):
        while not self._stop_event.isSet():
            self.cond.acquire()
            try:
                if not self.eventq:
                    LOGGER.debug(
                        "Nothing in queue, sync consumer %s is waiting.",
                        self.name)
                    self.cond.wait()
                    LOGGER.debug(
                        "Producer added something to queue and notified"
                        " the consumer %s", self.name)
                else:
                    event = self.eventq.popleft()
                    self.cond.notify()
                    self.sync_file(event)
                    LOGGER.debug("%s is consuming. %s in the queue is consumed!/n" \
                        % (self.getName(), event.pathname))
            finally:
                self.cond.release()
                time.sleep(0.01)

        LOGGER.info("Thread %s exit monitoring", self.name)
        while 1:
            self.cond.acquire()
            try:
                if not self.eventq:
                    self.cond.notifyAll()
                    time.sleep(0.5)
                    break
                else:
                    event = self.eventq.popleft()
                    self.sync_file(event)
                    time.sleep(0.01)
                    LOGGER.debug("%s:%s is consuming. %s in the queue is consumed!/n" \
                        % (time.ctime(), self.name, event.pathname))
                    self.cond.notify()
            finally:
                self.cond.release()

    def run(self):
        self.loop()