Example #1
0
def server(log_level=logging.INFO, config_file=''):
    # Initialize logging subsystem
    log = logging.getLogger('ajenti')
    log.setLevel(log_level)
    stderr = logging.StreamHandler()
    stderr.setLevel(log_level)
    if log_level == logging.DEBUG:
        formatter = logging.Formatter(
            '%(asctime)s %(levelname)-8s %(module)s.%(funcName)s(): %(message)s'
        )
    else:
        formatter = logging.Formatter(
            '%(asctime)s %(levelname)-8s %(message)s')
    stderr.setFormatter(formatter)
    log.addHandler(stderr)

    # Read config
    config = Config()
    if config_file:
        log.info('Using config file %s' % config_file)
        config.load(config_file)
    else:
        log.info('Using default settings')

    host = config.get('ajenti', 'bind_host')
    port = config.getint('ajenti', 'bind_port')
    log.info('Listening on %s:%d' % (host, port))
    # Add log handler to config, so all plugins could access it
    config.set('log_facility', log)

    # Load external plugins
    plugins.loader(config.get('ajenti', 'plugins'), log)

    CustomRequestHandler.log = log
    # Start server
    if config.getint('ajenti', 'ssl') == 1:
        CustomServer.cert_file = config.get('ajenti', 'cert_file')

    httpd = make_server(host, port,
                        AppDispatcher(config).dispatcher, CustomServer,
                        CustomRequestHandler)

    try:
        httpd.serve_forever()
    except KeyboardInterrupt, e:
        log.warn('Stopping by <Control-C>')
Example #2
0
def run_server(log_level=logging.INFO, config_file=''):
    log = make_log(debug=log_level == logging.DEBUG, log_level=log_level)

    # For the debugging purposes
    log.info('Ajenti %s' % version())

    # We need this early
    ajenti.utils.logger = log

    # Read config
    config = Config()
    if config_file:
        log.info('Using config file %s' % config_file)
        config.load(config_file)
    else:
        log.info('Using default settings')

    # Handle first-launch reconfiguration
    deployed.reconfigure(config)

    # Add log handler to config, so all plugins could access it
    config.set('log_facility', log)

    # Start recording log for the bug reports
    log.blackbox.start()

    platform = ajenti.utils.detect_platform()
    log.info('Detected platform: %s' % platform)

    # Load external plugins
    PluginLoader.initialize(log, config.get('ajenti', 'plugins'), platform)
    PluginLoader.load_plugins()

    # Start components
    app = Application(config)
    PluginLoader.register_mgr(app)  # Register permanent app
    ComponentManager.create(app)

    # Start server
    host = config.get('ajenti', 'bind_host')
    port = config.getint('ajenti', 'bind_port')
    log.info('Listening on %s:%d' % (host, port))

    # SSL params
    ssl = {}
    if config.getint('ajenti', 'ssl') == 1:
        ssl = {
            'keyfile': config.get('ajenti', 'cert_key'),
            'certfile': config.get('ajenti', 'cert_file'),
        }

    log.info('Using HTTP server: %s' % http_server)

    server = WSGIServer((host, port),
                        application=AppDispatcher(config).dispatcher,
                        **ssl)

    config.set('server', server)

    try:
        syslog.openlog(
            ident='ajenti',
            facility=syslog.LOG_AUTH,
        )
    except:
        syslog.openlog('ajenti')

    log.info('Starting server')

    server.serve_forever()

    ComponentManager.get().stop()

    if hasattr(server, 'restart_marker'):
        log.info('Restarting by request')

        fd = 20  # Close all descriptors. Creepy thing
        while fd > 2:
            try:
                os.close(fd)
                log.debug('Closed descriptor #%i' % fd)
            except:
                pass
            fd -= 1

        os.execv(sys.argv[0], sys.argv)
    else:
        log.info('Stopped by request')