Beispiel #1
0
def run_component(component_cls, listen_all, location, daemon, pidfile_name,
                  to_file, cfile, argv=None, register=True,
                  state_name=False, cls_kwargs={}, extra_getopt='', time_out=10,
                  protocol='xmlrpc/ssl', certfile=None, keyfile=None, ca=None):

    # default settings
    level = logging.INFO

    logging.getLogger().setLevel(level)
    Bcfg2.Logger.setup_logging(component_cls.implementation,
                               to_console=True,
                               to_syslog=True,
                               to_file=to_file,
                               level=level)

    if daemon:
        child_pid = os.fork()
        if child_pid != 0:
            return

        os.setsid()

        child_pid = os.fork()
        if child_pid != 0:
            os._exit(0)

        redirect_file = open("/dev/null", "w+")
        os.dup2(redirect_file.fileno(), sys.__stdin__.fileno())
        os.dup2(redirect_file.fileno(), sys.__stdout__.fileno())
        os.dup2(redirect_file.fileno(), sys.__stderr__.fileno())

        os.chdir(os.sep)

        pidfile = open(pidfile_name or "/dev/null", "w")
        fprint(os.getpid(), pidfile)
        pidfile.close()

    component = component_cls(cfile=cfile, **cls_kwargs)
    up = urlparse(location)
    port = tuple(up[1].split(':'))
    port = (port[0], int(port[1]))
    try:
        server = XMLRPCServer(listen_all,
                              port,
                              keyfile=keyfile,
                              certfile=certfile,
                              register=register,
                              timeout=time_out,
                              ca=ca,
                              protocol=protocol)
    except:
        logger.error("Server startup failed")
        os._exit(1)
    server.register_instance(component)

    try:
        server.serve_forever()
    finally:
        server.server_close()
    component.shutdown()
Beispiel #2
0
 def traceit(frame, event, arg):
     if event == "line":
         lineno = frame.f_lineno
         filename = frame.f_globals["__file__"]
         if (filename.endswith(".pyc") or
             filename.endswith(".pyo")):
             filename = filename[:-1]
         name = frame.f_globals["__name__"]
         line = linecache.getline(filename, lineno)
         fprint("%s:%s: %s" % (name, lineno, line.rstrip()), log_file)
     return traceit