Ejemplo n.º 1
0
    def __init__(self, name, user, daemonize):
        self.name = name
        self.run_dir = os.path.join(RUN_PATH, name)
        self.lib_dir = os.path.join(LIB_PATH, name)
        self.server = None
        self._shelf = None
        self.daemon = None
        if daemonize:
            out = file(os.path.join(LOG_PATH, '%s.stdout.log' % name), 'w')
            err = file(os.path.join(LOG_PATH, '%s.stderr.log' % name), 'w')
            self.daemon = daemon.DaemonContext(working_directory=self.run_dir,
                                 stdout=out,
                                 stderr=err,              
                                 pidfile=PIDFile(os.path.join(self.run_dir, '%s.pid' % name)))
            self.daemon.open()

        # Create run directory
        if not os.path.exists(self.run_dir):
            os.makedirs(self.run_dir)

        os.chdir(self.run_dir)

        # Create lib directory
        if not os.path.exists(self.lib_dir):
            os.makedirs(self.lib_dir)

        # Initialise logging
        if not os.path.exists(LOG_PATH):
            os.makedirs(LOG_PATH)
        logging.basicConfig(filename=os.path.join(LOG_PATH, '%s.log' % name), 
                            level=logging.DEBUG,
                            format="%(asctime)s - %(levelname)s - %(message)s")

        # Only log error messages from brisa
        logging.getLogger('RootLogger').setLevel(logging.ERROR)
        logging.getLogger('core').setLevel(logging.ERROR)
        logging.getLogger('upnp').setLevel(logging.ERROR)

        # Perform subclass initialisation while we still have some privileges.
        self.privileged_init()

        if os.getuid() == 0:
            p = pwd.getpwnam(user)
            os.chown(self.run_dir, p.pw_uid, p.pw_gid)
            os.chown(self.lib_dir, p.pw_uid, p.pw_gid)

            # Drop privileges
            os.setgid(p.pw_gid)
            os.setuid(p.pw_uid)
            os.environ['USER'] = user
            os.environ['HOME'] = self.run_dir

        from brisa.core.reactors._select import SelectReactor
        self.reactor = SelectReactor()
        self.reactor.add_after_stop_func(self.after_stop)
Ejemplo n.º 2
0
class SubSystem(object):
    def __init__(self, name, user, daemonize):
        self.name = name
        self.run_dir = os.path.join(RUN_PATH, name)
        self.lib_dir = os.path.join(LIB_PATH, name)
        self.server = None
        self._shelf = None
        self.daemon = None
        if daemonize:
            out = file(os.path.join(LOG_PATH, '%s.stdout.log' % name), 'w')
            err = file(os.path.join(LOG_PATH, '%s.stderr.log' % name), 'w')
            self.daemon = daemon.DaemonContext(working_directory=self.run_dir,
                                 stdout=out,
                                 stderr=err,              
                                 pidfile=PIDFile(os.path.join(self.run_dir, '%s.pid' % name)))
            self.daemon.open()

        # Create run directory
        if not os.path.exists(self.run_dir):
            os.makedirs(self.run_dir)

        os.chdir(self.run_dir)

        # Create lib directory
        if not os.path.exists(self.lib_dir):
            os.makedirs(self.lib_dir)

        # Initialise logging
        if not os.path.exists(LOG_PATH):
            os.makedirs(LOG_PATH)
        logging.basicConfig(filename=os.path.join(LOG_PATH, '%s.log' % name), 
                            level=logging.DEBUG,
                            format="%(asctime)s - %(levelname)s - %(message)s")

        # Only log error messages from brisa
        logging.getLogger('RootLogger').setLevel(logging.ERROR)
        logging.getLogger('core').setLevel(logging.ERROR)
        logging.getLogger('upnp').setLevel(logging.ERROR)

        # Perform subclass initialisation while we still have some privileges.
        self.privileged_init()

        if os.getuid() == 0:
            p = pwd.getpwnam(user)
            os.chown(self.run_dir, p.pw_uid, p.pw_gid)
            os.chown(self.lib_dir, p.pw_uid, p.pw_gid)

            # Drop privileges
            os.setgid(p.pw_gid)
            os.setuid(p.pw_uid)
            os.environ['USER'] = user
            os.environ['HOME'] = self.run_dir

        from brisa.core.reactors._select import SelectReactor
        self.reactor = SelectReactor()
        self.reactor.add_after_stop_func(self.after_stop)

    def privileged_init(self):
        pass

    @property
    def shelf(self):
        if self._shelf is None:
            self._shelf = shelve.open(os.path.join(self.lib_dir, 'shelf'), protocol=-1)
        return self._shelf

    def create_server(self, funcs):
        if self.server is None:
            from warmpi.ipc import IPCServer

            self.server = IPCServer(self.run_dir, funcs)
        else:
            self.server.funcs = funcs

    def run(self):
        try:
            self.reactor.main()
        except:
            logging.error('Exception in run', exc_info=True)


    def after_stop(self):
        if self._shelf is not None:
            self._shelf.close()

        if self.daemon is not None:
            self.daemon.close()