Ejemplo n.º 1
0
    if pid_file is None:
        pid_file = conf.get("PROVISION_PID_FILE", "/var/run/beaker-lab-controller/beaker-provision.pid")

    # HubProxy will try to log some stuff, even though we 
    # haven't configured our logging handlers yet. So we send logs to stderr 
    # temporarily here, and configure it again below.
    log_to_stream(sys.stderr, level=logging.WARNING)
    try:
        poller = CommandQueuePoller(conf=conf)
    except Exception, ex:
        sys.stderr.write('Error starting beaker-provision: %s\n' % ex)
        sys.exit(1)

    if opts.foreground:
        log_to_stream(sys.stderr, level=logging.DEBUG)
        main_loop(poller=poller, conf=conf)
    else:
        # See BZ#977269
        poller.close()
        with daemon.DaemonContext(pidfile=pidfile.TimeoutPIDLockFile(
                pid_file, acquire_timeout=0),detach_process=True):
            log_to_syslog('beaker-provision')
            try:
                main_loop(poller=poller, conf=conf)
            except Exception:
                logger.exception('Unhandled exception in main_loop')
                raise

if __name__ == '__main__':
    main()
Ejemplo n.º 2
0
def start_daemon():
    with daemon.DaemonContext(
            umask=0o002,  # -rw-r--r--
            pidfile=pidfile.TimeoutPIDLockFile(PID_FILE),
            working_directory=LOCAL_DIR) as context:
        run_jobs()
Ejemplo n.º 3
0
    uuid = get_uuid()  #inutile car uuid est global
    command = select_os(args)  #inutile car uuid est global
    while True:
        send_cached_payloads()
        client()
        sleep(int(args.interval))


if __name__ == "__main__":
    args = parser.parse_args()
    select_logging_mode(args)
    dir_path = os.path.dirname(
        os.path.realpath(__file__))  ## Get the directory path of the file
    error_logs_file = open(dir_path + '/../logs/error_logs.txt', 'a')
    debug_logs_file = open(dir_path + '/../logs/debug_logs.txt', 'a')

    if args.mac_os:
        context = daemon.DaemonContext(
            working_directory=dir_path,
            stderr=error_logs_file,
            stdout=debug_logs_file,
        )
    else:
        context = daemon.DaemonContext(
            working_directory=dir_path,
            stderr=error_logs_file,
            stdout=debug_logs_file,
            pidfile=pidfile.TimeoutPIDLockFile(PID_FILE))
    with context:
        main()
Ejemplo n.º 4
0
    # HubProxy will try to log some stuff, even though we
    # haven't configured our logging handlers yet. So we send logs to stderr
    # temporarily here, and configure it again below.
    log_to_stream(sys.stderr, level=logging.WARNING)
    try:
        watchdog = Watchdog(conf=conf)
    except Exception, ex:
        sys.stderr.write("Error starting beaker-watchdog: %s\n" % ex)
        sys.exit(1)

    if opts.foreground:
        log_to_stream(sys.stderr, level=logging.DEBUG)
        main_loop(watchdog, conf)
    else:
        # See BZ#977269
        watchdog.close()
        with daemon.DaemonContext(pidfile=pidfile.TimeoutPIDLockFile(
                pid_file, acquire_timeout=0),
                                  detach_process=True):
            log_to_syslog('beaker-watchdog')
            try:
                main_loop(watchdog, conf)
            except Exception:
                logger.exception('Unhandled exception in main_loop')
                raise


if __name__ == '__main__':
    main()
Ejemplo n.º 5
0
def start_daemon(pidf, logf):
    with daemon.DaemonContext(
        working_directory='/var/lib/remotfy_daemon',
        umask=0o002,
        pidfile=pidfile.TimeoutPIDLockFile(pidf)):
        process(logf)
Ejemplo n.º 6
0
def main():
    """Run the daemon from the command line"""

    opts = parse_arguments(sys.argv[1:])

    # Initialize logger
    lvl = logging.DEBUG if opts.debug else logging.INFO

    global logger
    logger = logging.getLogger("vncauthproxy")
    logger.setLevel(lvl)
    formatter = logging.Formatter(("%(asctime)s %(module)s[%(process)d] "
                                   " %(levelname)s: %(message)s"),
                                  "%Y-%m-%d %H:%M:%S")
    handler = logging.FileHandler(opts.log_file)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    try:
        # Create pidfile
        pidf = pidlockfile.TimeoutPIDLockFile(opts.pid_file, 10)

        # Init ephemeral port pool
        ports = range(opts.min_port, opts.max_port + 1)

        # Init VncAuthProxy class attributes
        VncAuthProxy.server_timeout = opts.server_timeout
        VncAuthProxy.connect_retries = opts.connect_retries
        VncAuthProxy.retry_wait = opts.retry_wait
        VncAuthProxy.connect_timeout = opts.connect_timeout
        VncAuthProxy.ports = ports

        VncAuthProxy.authdb = parse_auth_file(opts.auth_file)

        VncAuthProxy.keyfile = opts.key_file
        VncAuthProxy.certfile = opts.cert_file

        VncAuthProxy.proxy_address = opts.proxy_listen_address
        VncAuthProxy.fqdn = socket.getfqdn()

        sockets = get_listening_sockets(opts.listen_port,
                                        opts.listen_address,
                                        reuse_addr=True)

        wrap_ssl = lambda sock: sock
        if opts.enable_ssl:
            ssl_prot = ssl.PROTOCOL_TLSv1
            wrap_ssl = lambda sock: ssl.wrap_socket(sock,
                                                    server_side=True,
                                                    keyfile=opts.key_file,
                                                    certfile=opts.cert_file,
                                                    ssl_version=ssl_prot)

        # Become a daemon:
        # Redirect stdout and stderr to handler.stream to catch
        # early errors in the daemonization process [e.g., pidfile creation]
        # which will otherwise go to /dev/null.
        daemon_context = AllFilesDaemonContext(pidfile=pidf,
                                               umask=0022,
                                               stdout=handler.stream,
                                               stderr=handler.stream,
                                               files_preserve=[handler.stream])

        # Remove any stale PID files, left behind by previous invocations
        if daemon.runner.is_pidfile_stale(pidf):
            logger.warning("Removing stale PID lock file %s", pidf.path)
            pidf.break_lock()

        try:
            daemon_context.open()
        except (AlreadyLocked, LockTimeout):
            raise InternalError(("Failed to lock PID file %s, another "
                                 "instance running?"), pidf.path)

        logger.info("Became a daemon")

        # A fork() has occured while daemonizing,
        # we *must* reinit gevent
        gevent.reinit()

        # Catch signals to ensure graceful shutdown,
        #
        # Uses gevent.signal so the handler fires even during
        # gevent.socket.accept()
        gevent.signal(SIGINT, fatal_signal_handler, "SIGINT")
        gevent.signal(SIGTERM, fatal_signal_handler, "SIGTERM")
    except InternalError as err:
        logger.critical(err)
        sys.exit(1)
    except Exception as err:
        logger.critical("Unexpected error:")
        logger.exception(err)
        sys.exit(1)

    while True:
        try:
            client = None
            rlist, _, _ = select(sockets, [], [])
            for ctrl in rlist:
                client, _ = ctrl.accept()
                client = wrap_ssl(client)
                logger.info("New control connection")

                VncAuthProxy.spawn(logger, client)
            continue
        except Exception as err:
            logger.error("Unexpected error:")
            logger.exception(err)
            if client:
                client.close()
            continue
        except SystemExit:
            break

    try:
        logger.info("Closing control sockets")
        while sockets:
            sock = sockets.pop()
            sock.close()

        daemon_context.close()
        sys.exit(0)
    except Exception as err:
        logger.critical("Unexpected error:")
        logger.exception(err)
        sys.exit(1)
Ejemplo n.º 7
0
        if out:
            # Log the event
            logger.error(str(out))
            # Send a message
            client.send_message(out)
            # Remove it from the list until the deamon is restarted
            # so it doesn't keep messaging me
            ping.remove_server(out)
        # Wait one hour
        time.sleep(60*30)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Example daemon in Python")
    parser.add_argument('-t', '--test', dest='test', action='store_true')
    parser.add_argument('-p', '--pid-file', default='/var/run/DeadManText.pid')
    parser.add_argument('-l', '--log-file', default='/var/log/DeadManText.log')

    args = parser.parse_args()

    if args.test:
        main("local.out")
    else:
        with daemon.DaemonContext(working_directory='/var/lib/DeadManText',
                                  pidfile=pidfile.TimeoutPIDLockFile(
                                      args.pid_file),
                                  umask=0o002,
                                  ) as context:
            main(args.log_file)
Ejemplo n.º 8
0
def start_daemon():
    with daemon.DaemonContext(
            working_directory='./',
            pidfile=pidfile.TimeoutPIDLockFile('daemon.pid'),
    ) as context:
        main()
Ejemplo n.º 9
0
    formatter = logging.Formatter('%(name)-12s '
                                  '%(asctime)s '
                                  '%(levelname)-8s '
                                  '%(message)s')
    lh.setFormatter(formatter)
    my_logger.addHandler(lh)

    rabbit_pwd = pwd.getpwnam('rabbitmq')
    uid = rabbit_pwd.pw_uid
    gid = rabbit_pwd.pw_gid
    env = os.environ.copy()
    env['USER'] = USER
    env['MAIL'] = MAIL
    env['PWD'] = PWD
    env['HOME'] = HOME
    env['LOGNAME'] = LOGNAME

    pidfilename = '/var/run/rabbitmq/rabbit-fence.pid'
    pidfile = daemon_pidfile.TimeoutPIDLockFile(pidfilename, 10)
    try:
        with daemon.DaemonContext(files_preserve=[lh.socket.fileno()],
                                  pidfile=pidfile,
                                  uid=uid,
                                  gid=gid,
                                  umask=0o022):

            main()
    except Exception:
        my_logger.exception("A generic exception caught!")
        sys.exit(1)
Ejemplo n.º 10
0
            logger.error('Unhandled error:')
            logger.error(traceback.format_exc())
            try:
                agent.stop()
                zwave_network.stop()
            except Exception:
                pass
            logger.debug('Restarting in 3 sec...')
            time.sleep(3)


if __name__ == '__main__':
    arguments = docopt(__doc__)

    if arguments['--version']:
        print('Version: {}'.format(VERSION))
        exit(0)

    if arguments['run']:
        if arguments['--daemon']:
            from daemon import pidfile, DaemonContext
            pid = pidfile.TimeoutPIDLockFile('/var/run/zeyeagent.pid', 10)
            ctx = DaemonContext(working_directory='.', pidfile=pid)
            ctx.open()

        start_agent(device=arguments['--device'],
                    config_path=arguments['--config'],
                    user_path=arguments['--user_path'],
                    debug=arguments['--debug'] or arguments['--full-debug'],
                    pyagentx_debug=arguments['--full-debug'])
Ejemplo n.º 11
0
def start():
    """
	Start backend, as a daemon
	:return bool: True
	"""
    # only one instance may be running at a time
    if lockfile.is_file():
        with lockfile.open() as file:
            pid = int(file.read().strip())
        if pid in psutil.pids():
            print("...error: the 4CAT Backend Daemon is already running.")
            return False

    # start daemon in a separate process so we can continue doing stuff in this one afterwards
    new_pid = os.fork()
    if new_pid == 0:
        # create new daemon context and run bootstrapper inside it
        with daemon.DaemonContext(
                working_directory=os.path.abspath(os.path.dirname(__file__)),
                umask=0x002,
                stderr=open("4cat.stderr", "w"),
                pidfile=pidfile.TimeoutPIDLockFile(str(lockfile)),
                detach_process=True) as context:
            # clear module cache first so it will be regenerated
            # ideally we'd do this in ModuleLoader, but we need to do this
            # before that class is loaded...
            module_cache = Path(config.PATH_ROOT, "backend", "module_cache.pb")
            if module_cache.exists():
                module_cache.unlink()
            import backend.bootstrap as bootstrap
            bootstrap.run(as_daemon=True)
        sys.exit(0)
    else:
        # wait a few seconds and see if PIDfile was created and refers to a running process
        now = time.time()
        while time.time() < now + 10:
            if lockfile.is_file():
                break
            else:
                time.sleep(0.1)

        if not lockfile.is_file():
            print(
                "...error while starting 4CAT Backend Daemon (lockfile not found)."
            )
        else:
            with lockfile.open() as file:
                pid = int(file.read().strip())
                if pid in psutil.pids():
                    print("...4CAT Backend Daemon started.")
                else:
                    print("...error while starting 4CAT Backend Daemon.")

        if Path("4cat.stderr").is_file():
            with open("4cat.stderr") as errfile:
                stderr = errfile.read()
                if stderr:
                    print("---------------------------------\nstderr output:")
                    print(stderr)

    return True
Ejemplo n.º 12
0
    except Exception:
        print('error starting ros daemon: {}'.format(traceback.format_exc()))
    finally:
        if _daemon is not None:
            try:
                _daemon.close()
            except Exception:
                print('error closing ros daemon.')
        print('rosd complete.')

# ..............................................................................

with daemon.DaemonContext(
    stdout=sys.stdout,
    stderr=sys.stderr,
#   chroot_directory=None,
    working_directory='/home/pi/ros',
    umask=0o002,
    pidfile=pidfile.TimeoutPIDLockFile(PIDFILE), ) as context:
#   signal_map={
#       signal.SIGTERM: shutdown,
#       signal.SIGTSTP: shutdown
#   }) as context:
    main()

# call main ....................................................................
#if __name__== "__main__":
#    main()

#EOF
Ejemplo n.º 13
0
import os
import sys
import confparser
import server
import daemon
import daemon.pidfile as pidlockfile

DEFAULT_NAME = "config.json"

pidfile = pidlockfile.TimeoutPIDLockFile("/var/run/gluten.pid")
with daemon.DaemonContext(pidfile=pidfile):
    main()


def main():
    configParser = confparser.ConfigParser(DEFAULT_NAME)
    config = configParser.main_config
    server.serve(config)


if __name__ == "__main__":
    main()
Ejemplo n.º 14
0
def main():
    """The main entry point. """

    parser = ArgumentParser()
    parser.add_argument('-a', '--unix-socket-name',
                        default='/var/run/{}.sock'.format(DAEMON_NAME),
                        help='Unix domain socket file name',
                        metavar='UNIX_SOCKET_NAME')
    parser.add_argument('-d', '--daemonize', action='store_true',
                        help='daemonize the server')
    parser.add_argument('-H', '--redis-host', default='127.0.0.1',
                        help='Redis server host', metavar='REDIS_HOST')
    parser.add_argument('-l', '--log-file-prefix', default='',
                        help='path prefix for log files',
                        metavar='LOG_FILE_PREFIX')
    parser.add_argument('-n', '--channel-name', default='build_status_codes',
                        help='channel which is used for publishing build '
                             'status codes to',
                        metavar='CHANNEL_NAME')
    parser.add_argument('-p', '--pid',
                        default='/var/run/{}.pid'.format(DAEMON_NAME),
                        help='server pid file name',
                        metavar='PID_FILE_NAME')
    parser.add_argument('-P', '--redis-port', default='6379',
                        help='server pid file name', metavar='REDIS_PORT',
                        type=int)
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='turn on verbose mode')

    args = parser.parse_args()

    util.init_logger(LOGGER, logging.DEBUG if args.verbose else logging.INFO,
                     args.log_file_prefix)

    if os.path.exists(args.unix_socket_name):
        LOGGER.fatal('Address already in use')
        sys.exit(1)
    else:
        test_write_perm(os.path.dirname(args.unix_socket_name))

    if args.log_file_prefix:
        test_write_perm(os.path.dirname(args.log_file_prefix))
        LOGGER.debug('All log records will be forwarded to %s',
                     args.log_file_prefix)
    else:
        LOGGER.debug('No log file will be created')

    try:
        bscd = BSCServer(args.unix_socket_name, args.channel_name,
                         redis_host=args.redis_host,
                         redis_port=args.redis_port)
    except RedisError:
        LOGGER.fatal('Could not connect to Redis')
        sys.exit(1)

    if args.daemonize:
        test_write_perm(os.path.dirname(args.pid))

        LOGGER.debug('Server will be daemonized')

        # Preserve the fds related to the logger to prevent closing when
        # daemonizing the server.
        preserved_fds = []
        for handler in LOGGER.handlers:
            if hasattr(handler, 'stream'):
                preserved_fds.append(handler.stream.fileno())  # pylint: disable=no-member

        with DaemonContext(files_preserve=preserved_fds,
                           pidfile=pidfile.TimeoutPIDLockFile(args.pid),
                           working_directory=os.getcwd()):
            bscd.run()
    else:
        bscd.run()
Ejemplo n.º 15
0
def start(args):
    """Start an existing service. """
    _setup_logging(args)

    if args.daemon:
        if os.path.exists(args.pid_file):
            logging.error("Server already running. Quitting.")
            sys.exit(1)

        logger = logging.getLogger()

        stdout = sys.stdout
        stderr = sys.stderr

        context = daemon.DaemonContext(
            working_directory=args.home,
            pidfile=pidlockfile.TimeoutPIDLockFile(args.pid_file, 1),
            files_preserve=[file.stream for file in logger.handlers],
            stdout=stdout,
            stderr=stderr,
            umask=022)

        try:
            context.open()
        except (lockfile.LockTimeout, lockfile.AlreadyLocked):
            logging.error("Can't obtain a lock on '%s'. Quitting." %
                          args.pid_file)
            sys.exit(1)
    else:
        os.chdir(args.home)

    # This is our identification token. This will be sent to a core
    # and later used to verify if we talk between the same parties
    # or not (useful to determine if core/service was restarted).

    service_uuid = uuid.uuid4().hex

    application = tornado.web.Application([
        (r"/", handlers.MainHandler),
        (r"/core/?", handlers.CoreHandler),
        (r"/engine/?", handlers.EngineHandler),
    ],
                                          service_uuid=service_uuid)

    server = tornado.httpserver.HTTPServer(application)
    server.listen(args.port)

    logging.info("Started service at localhost:%s (pid=%s)" %
                 (args.port, os.getpid()))

    # We are ready for processing requests from a core server. However,
    # at this point no one knows about our existence, so lets tell a
    # selected core server that we exist and we would like to share our
    # computational resources.
    #
    # To achieve this, we will send notification (registration) request
    # to the selected core sever. The request consists of our URL, our
    # capabilities (e.g. what engine types we support) and a public key
    # that will allow later to authenticate the core server.

    def _on_registred_okay(args, result):
        """Gets executed when the core responds. """
        logging.info("Service has been registered at %s" % args.core_url)

    def _registration_callback(args):
        """Gets executed when IOLoop is started. """
        proxy = JSONRPCProxy(args.core_url, 'service')

        proxy.call(
            'register', {
                'url': args.service_url,
                'uuid': service_uuid,
                'provider': args.provider,
                'description': args.description,
            }, functools.partial(_on_registred_okay, args))

    ioloop = tornado.ioloop.IOLoop.instance()

    if args.core_url:
        ioloop.add_callback(functools.partial(_registration_callback, args))
    else:
        logging.warning("Couldn't register this service at any core server.")

    try:
        ioloop.start()
    except KeyboardInterrupt:
        print  # SIGINT prints '^C' so lets make logs more readable
    except SystemExit:
        pass

    # Make sure we clean up after this service here before we quit. It is
    # important that we explicitly terminate all child processes that were
    # spawned by this service. Note that in case of SIGKILL we can't do
    # much and those processes will be kept alive.

    processes.ProcessManager.instance().killall()

    logging.info("Stopped service at localhost:%s (pid=%s)" %
                 (args.port, os.getpid()))
Ejemplo n.º 16
0
def main():
    (opts, args) = parse_arguments(sys.argv[1:])

    # Rename this process so 'ps' output looks like this is a native
    # executable.  Can not seperate command-line arguments from actual name of
    # the executable by NUL bytes, so only show the name of the executable
    # instead.  setproctitle.setproctitle("\x00".join(sys.argv))
    setproctitle.setproctitle(sys.argv[0])
    setup_logging(opts)

    # Special case for the clean up queues action
    if opts.purge_queues:
        purge_queues()
        return

    # Special case for the clean up exch action
    if opts.purge_exchanges:
        purge_exchanges()
        return

    if opts.drain_queue:
        drain_queue(opts.drain_queue)
        return

    # Debug mode, process messages without daemonizing
    if opts.debug:
        debug_mode()
        return

    # Create pidfile,
    pidf = pidlockfile.TimeoutPIDLockFile(opts.pid_file, 10)

    if daemon.runner.is_pidfile_stale(pidf):
        log.warning("Removing stale PID lock file %s", pidf.path)
        pidf.break_lock()

    files_preserve = []
    for handler in log.handlers:
        stream = getattr(handler, 'stream')
        if stream and hasattr(stream, 'fileno'):
            files_preserve.append(handler.stream)

    stderr_stream = None
    for handler in log.handlers:
        stream = getattr(handler, 'stream')
        if stream and hasattr(handler, 'baseFilename'):
            stderr_stream = stream
            break

    daemon_context = daemon.DaemonContext(pidfile=pidf,
                                          umask=0022,
                                          stdout=stderr_stream,
                                          stderr=stderr_stream,
                                          files_preserve=files_preserve)

    try:
        daemon_context.open()
    except (pidlockfile.AlreadyLocked, LockTimeout):
        log.critical("Failed to lock pidfile %s, another instance running?",
                     pidf.path)
        sys.exit(1)

    log.info("Became a daemon")

    if 'gevent' in sys.modules:
        # A fork() has occured while daemonizing. If running in
        # gevent context we *must* reinit gevent
        log.debug("gevent imported. Reinitializing gevent")
        import gevent
        gevent.reinit()

    # Catch every exception, make sure it gets logged properly
    try:
        daemon_mode(opts)
    except Exception:
        log.exception("Unknown error")
        raise
Ejemplo n.º 17
0
def start(args):
    """Start an existing SDK server. """
    _setup_logging(args)

    if args.daemon:
        if os.path.exists(args.pid_file):
            logging.error("Server already running. Quitting.")
            sys.exit(1)

        stdout = sys.stdout
        stderr = sys.stderr

        context = daemon.DaemonContext(
            working_directory=args.home,
            pidfile=pidlockfile.TimeoutPIDLockFile(args.pid_file, 1),
            files_preserve=list(_iter_logger_streams()),
            stdout=stdout,
            stderr=stderr,
            umask=022)

        try:
            context.open()
        except (lockfile.LockTimeout, lockfile.AlreadyLocked):
            logging.error("Can't obtain a lock on '%s'. Quitting." %
                          args.pid_file)
            sys.exit(1)
    else:
        os.chdir(args.home)

    def walk(path):
        n = len(args.static_path)

        if not args.static_path.endswith('/'):
            n += 1

        for root, dirs, files in os.walk(path):
            has_hidden = True

            while has_hidden:
                for name in dirs:
                    if name.startswith('.') or name.startswith('_'):
                        dirs.remove(name)
                        break
                else:
                    has_hidden = False

            for name in files:
                if not name.startswith('.') and not name.startswith('_'):
                    yield os.path.join(root[n:], name)

    modules_css_path = os.path.join(args.static_path, 'css/modules')
    modules_js_path = os.path.join(args.static_path, 'js/modules')

    modules = []

    for module, cls in args.modules:
        css_files = sorted(walk(os.path.join(modules_css_path, module)))
        js_files = sorted(walk(os.path.join(modules_js_path, module)))
        modules.append((module, cls, css_files, js_files))
        logging.info("Enabled module '%s'" % module)

    app_settings = {
        'modules': modules,
        'static_path': args.static_path,
        'template_loader': tornado.template.Loader(args.templates_path),
    }

    from handlers import main, async, client, restful

    application = tornado.web.Application([
        (r"/", main.MainHandler, dict(debug=False)),
        (r"/debug/?", main.MainHandler, dict(debug=True)),
        (r"/async/?", async .AsyncHandler),
        (r"/client/?", client.ClientHandler),
        (r"/worksheets/([0-9a-f]+)/?", restful.PublishedWorksheetHandler),
    ], **app_settings)

    server = tornado.httpserver.HTTPServer(application)
    server.listen(args.port)

    logging.info("Started SDK at localhost:%s (pid=%s)" %
                 (args.port, os.getpid()))

    ioloop = tornado.ioloop.IOLoop.instance()

    try:
        ioloop.start()
    except KeyboardInterrupt:
        print  # SIGINT prints '^C' so lets make logs more readable
    except SystemExit:
        pass

    ProcessManager.instance().killall()

    logging.info("Stopped SDK at localhost:%s (pid=%s)" %
                 (args.port, os.getpid()))