Ejemplo n.º 1
0
class LogaggFuseRunner:
    def __init__(self):
        self.opts = None
        self.args = None
        self.fuse_server = None
        self.log_cache_dir = None
        self.state_file = None
        self.log_cache_dir = None
        self.runfs_thread = None
        self.log = Dummy()

    def _mkdir_logdir(self, parent_directory):

        log_dir = os.path.abspath(os.path.join(parent_directory,
                                "logs"))
        if not os.path.isdir(log_dir):
            self.log.debug('making_cache_directory', d=log_dir)
            os.makedirs(log_dir)
        return log_dir

    def _touch_statefile(self, parent_directory):

        state_file = os.path.abspath(os.path.join(parent_directory,
                                "trackfiles.txt"))
        if not os.path.exists(state_file):
            self.log.debug('making_state_file', f=state_file)
            open(state_file, 'a').close()
        return state_file

    def runfs(self):
        usage = """
    Logagg Log collection FUSE filesystem

    """ + Fuse.fusage
        #argument parsing
        server = LogaggFS(version="%prog " + fuse.__version__,
                     usage=usage,
                     dash_s_do='setsingle',
                     file_class=LogaggFSFile)
        self.fuse_server = server

        p = server.parser
        p.add_option(mountopt='root', metavar='PATH',
                                 help='mountpoint')
        p.add_option(mountopt='loglevel', metavar='DEBUG/INFO' ,default='INFO',
                                help='level of logger')
        p.add_option(mountopt='logfile', metavar='PATH', default='/tmp/fuse.log',
                                help='file path to store logs')

        server.parse(values=server, errex=1)
        self.opts, self.args = server.parser.parse_args()

        #initiating logger
        self.log = DUMMY_LOG
        if self.opts.logfile:
            self.log = init_logger(fpath=self.opts.logfile,
                                level=self.opts.loglevel)

        ldir = os.path.abspath(server.root)
        ldir = os.path.join(ldir, '')[:-1]
        self.log_cache_dir = ldir

        server.log_cache_dir = self.log_cache_dir
        LogaggFSFile.log_cache_dir = self.log_cache_dir

        server.log = self.log
        MirrorFSFile.log = self.log

        self.log.debug('starting_up')
        #FIXME: report bug of init_logger not working with fpath=None
        try:
            if server.fuse_args.mount_expected():
                os.chdir(server.log_cache_dir)
        except OSError:
            log.exception("cannot_enter_root_of_underlying_filesystem", file=sys.stderr)
            sys.exit(1)

        # mkdir logs directory and state file inside log cache directory
        self.log_dir = self._mkdir_logdir(parent_directory=self.log_cache_dir)
        self.state_file = self._touch_statefile(parent_directory=self.log_cache_dir)

        # create tracklist for monitoring log files
        tracklist = TrackList(state_file=self.state_file,
                        directory=self.log_dir,
                        log=self.log)
        LogaggFSFile.tracklist = tracklist
        LogaggFSFile.mountpoint = server.fuse_args.mountpoint

        server.main()

    def start(self):
        th = threading.Thread(target=self.runfs)
        th.daemon = True
        th.start()
        self.runfs_thread = th
        th.join()
Ejemplo n.º 2
0
class LogaggFuseRunner:
    """
    Initializes and runs LogaggFs file system
    """

    TRACKFILES_REFRESH_INTERVAL = (
        30)  # Seconds after which data inside trackfiles.txt is read regularly

    def __init__(self):
        self.opts = None
        self.args = None
        self.fuse_server = None
        self.log_cache_dir = None
        self.state_file = None
        self.log_cache_dir = None
        self.runfs_thread = None
        self.log = Dummy()

    def _mkdir_logdir(self, parent_directory):
        """
        Make logcache/logs dir if not present
        """
        # FIXME: use logagg_utils ensure_dir func

        log_dir = os.path.abspath(os.path.join(parent_directory, "logs"))
        if not os.path.isdir(log_dir):
            self.log.debug("making_cache_directory", d=log_dir)
            os.makedirs(log_dir)
        return log_dir

    def _touch_statefile(self, parent_directory):
        """
        Touch logcache/trackfiles.txt filr if not there
        """

        state_file = os.path.abspath(
            os.path.join(parent_directory, "trackfiles.txt"))
        if not os.path.exists(state_file):
            self.log.debug("making_state_file", f=state_file)
            open(state_file, "a").close()
        return state_file

    def runfs(self):
        usage = ("""
        Logagg Log collection FUSE filesystem
        """ + Fuse.fusage)
        # Argument parsing
        server = LogaggFS(
            version="%prog " + fuse.__version__,
            usage=usage,
            dash_s_do="setsingle",
            file_class=LogaggFSFile,
        )
        self.fuse_server = server

        p = server.parser
        p.add_option(mountopt="root", metavar="PATH", help="mountpoint")
        p.add_option(
            mountopt="loglevel",
            metavar="DEBUG/INFO",
            default="INFO",
            help="level of logger",
        )
        p.add_option(
            mountopt="logfile",
            metavar="PATH",
            default="/tmp/fuse.log",
            help="file path to store logs",
        )

        server.parse(values=server, errex=1)
        self.opts, self.args = server.parser.parse_args()

        # initiating logger
        self.log = DUMMY_LOG
        if self.opts.logfile:
            self.log = init_logger(fpath=self.opts.logfile,
                                   level=self.opts.loglevel)

        if not hasattr(server, "root"):
            sys.exit(0)
        ldir = os.path.abspath(server.root)
        ldir = os.path.join(ldir, "")[:-1]
        self.log_cache_dir = ldir

        server.log_cache_dir = self.log_cache_dir
        LogaggFSFile.log_cache_dir = self.log_cache_dir

        server.log = self.log
        MirrorFSFile.log = self.log

        self.log.debug("starting_up")
        # FIXME: report bug of init_logger not working with fpath=None
        try:
            if server.fuse_args.mount_expected():
                os.chdir(server.log_cache_dir)
        except OSError:
            log.exception("cannot_enter_root_of_underlying_filesystem",
                          file=sys.stderr)
            sys.exit(1)

        # mkdir logs directory and state file inside log cache directory
        self.log_dir = self._mkdir_logdir(parent_directory=self.log_cache_dir)
        self.state_file = self._touch_statefile(
            parent_directory=self.log_cache_dir)

        # Create tracklist for monitoring log files
        tracklist = TrackList(state_file=self.state_file,
                              directory=self.log_dir,
                              log=self.log)
        LogaggFSFile.tracklist = tracklist

        # LRU cache that expires in TRACKFILES_REFRESH_INTERVAL sec(s)
        clock = ExpiringCache(1,
                              default_timeout=self.TRACKFILES_REFRESH_INTERVAL)
        clock.put("timeout", "no")
        LogaggFSFile.clock = clock

        LogaggFSFile.mountpoint = server.fuse_args.mountpoint

        server.main()

    def start(self):
        th = threading.Thread(target=self.runfs)
        th.daemon = True
        th.start()
        self.runfs_thread = th
        th.join()