Ejemplo n.º 1
0
    def handle_api_languages(self, http_context):
        mgr = PluginManager.get(wu.context)
        languages = set()
        for id in mgr:
            locale_dir = mgr.get_content_path(id, 'locale')
            if os.path.isdir(locale_dir):
                for lang in os.listdir(locale_dir):
                    if lang != 'app.pot':
                        languages.add(lang)

        return sorted(list(languages))
Ejemplo n.º 2
0
 def get_augeas(self):
     return Augeas(
         modules=[{
             'name': 'Supervisor',
             'lens': 'Supervisor.lns',
             'incl': [
                 self.path,
             ]
         }],
         loadpath=PluginManager.get(wu.context).get_content_path('supervisor', ''),
     )
Ejemplo n.º 3
0
def make_report(e):
    """
    Formats a bug report.
    """
    import platform as _platform
    from wu import platform, platform_unmapped, platform_string, version, debug

    tb = traceback.format_exc(e)
    tb = '\n'.join('    ' + x for x in tb.splitlines())

    import gevent
    import greenlet
    import psutil
    from wu.plugins import PluginManager

    return """Webui bug report
--------------------


Info | Value
----- | -----
Webui | %s
Platform | %s / %s / %s
Architecture | %s
Python | %s
Debug | %s
Loaded plugins | %s

Library | Version
------- | -------
gevent | %s
greenlet | %s
psutil | %s


%s

            """ % (
        version,
        platform,
        platform_unmapped,
        platform_string,
        subprocess.check_output(['uname', '-mp']).strip(),
        '.'.join([str(x) for x in _platform.python_version_tuple()]),
        debug,
        ', '.join(
            sorted(PluginManager.get(wu.context).get_loaded_plugins_list())),
        gevent.__version__,
        greenlet.__version__,
        psutil.__version__,
        tb,
    )
Ejemplo n.º 4
0
 def handle_api_list_installed(self, http_context):
     r = []
     manager = PluginManager.get(wu.context)
     for name in manager:
         plugin = manager[name]
         r.append({
             'name': plugin['info']['name'],
             'imported': plugin['imported'],
             'crash': self.__serialize_exception(manager.get_crash(plugin['info']['name'])),
             'path': plugin['path'],
             'author': plugin['info']['author'],
             'author_email': plugin['info']['email'],
             'url': plugin['info']['url'],
             'icon': plugin['info']['icon'],
             'version': plugin['info']['version'],
             'title': plugin['info']['title'],
         })
     return r
Ejemplo n.º 5
0
    def handle_view(self, http_context):
        if wu.dev:
            rebuild_all = http_context.env.get('HTTP_CACHE_CONTROL',
                                               None) == 'no-cache'

            for provider in wu.plugin_providers:
                if isinstance(provider, DirectoryPluginProvider):
                    logging.debug('Building resources in %s', provider.path)
                    if rebuild_all:
                        cmd = ['webui-dev-multitool', '--rebuild']
                    else:
                        cmd = ['webui-dev-multitool', '--build']
                    p = subprocess.Popen(cmd,
                                         cwd=provider.path,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)
                    o, e = p.communicate()
                    if p.returncode != 0:
                        logging.error('Resource compilation failed')
                        logging.error(o + e)

        manager = PluginManager.get(wu.context)
        path = manager.get_content_path('core', 'templates/index.html')
        content = open(path).read() % {
            'prefix':
            http_context.prefix,
            'plugins':
            json.dumps(
                dict((manager[n]['info']['name'], manager[n]['info']['title'])
                     for n in manager)),
            'config':
            json.dumps(wu.config.data),
            'version':
            wu.version,
            'platform':
            wu.platform,
            'platformUnmapped':
            wu.platform_unmapped,
            'bootstrapColor':
            wu.config.data.get('color', None),
        }
        http_context.add_header('Content-Type', 'text/html')
        http_context.respond_ok()
        return content
 def __init__(self, http_context):
     self.cache = {}
     self.use_cache = not wu.debug
     self.mgr = PluginManager.get(wu.context)
 def handle_file(self, http_context, plugin=None, path=None):
     if '..' in path:
         return http_context.respond_not_found()
     return http_context.file(
         PluginManager.get(wu.context).get_content_path(plugin, path))
Ejemplo n.º 8
0
def run(config=None,
        plugin_providers=None,
        product_name='webui',
        dev_mode=False,
        debug_mode=False,
        autologin=False):
    """
    A global entry point for Webui.

    :param config: config file implementation instance to use
    :type  config: :class:`wu.config.BaseConfig`
    :param plugin_providers: list of plugin providers to load plugins from
    :type  plugin_providers: list(:class:`wu.plugins.PluginProvider`)
    :param str product_name: a product name to use
    :param bool dev_mode: enables dev mode (automatic resource recompilation)
    :param bool debug_mode: enables debug mode (verbose and extra logging)
    :param bool autologin: disables authentication and logs everyone in as the user running the panel. This is EXTREMELY INSECURE.
    """
    if config is None:
        raise TypeError('`config` can\'t be None')

    reload_module(sys)
    if hasattr(sys, 'setdefaultencoding'):
        sys.setdefaultencoding('utf8')

    wu.product = product_name
    wu.debug = debug_mode
    wu.dev = dev_mode
    wu.dev_autologin = autologin

    wu.init()
    wu.log.set_log_params(tag='master', master_pid=os.getpid())
    wu.context = Context()
    wu.config = config
    wu.plugin_providers = plugin_providers or []
    logging.info('Loading config from %s', wu.config)
    wu.config.load()
    wu.config.ensure_structure()

    if wu.debug:
        logging.warn('Debug mode')
    if wu.dev:
        logging.warn('Dev mode')

    try:
        locale.setlocale(locale.LC_ALL, '')
    except locale.Error:
        logging.warning('Couldn\'t set default locale')

    # install a passthrough gettext replacement since all localization is handled in frontend
    # and _() is here only for string extraction
    __builtins__['_'] = lambda x: x

    logging.info('Webui Core %s', wu.version)
    logging.info('Detected platform: %s / %s', wu.platform, wu.platform_string)

    # Load plugins
    PluginManager.get(wu.context).load_all_from(wu.plugin_providers)
    if len(PluginManager.get(wu.context)) == 0:
        logging.warn('No plugins were loaded!')

    if wu.config.data['bind']['mode'] == 'unix':
        path = wu.config.data['bind']['socket']
        if os.path.exists(path):
            os.unlink(path)
        listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        try:
            listener.bind(path)
        except OSError:
            logging.error('Could not bind to %s', path)
            sys.exit(1)

    if wu.config.data['bind']['mode'] == 'tcp':
        host = wu.config.data['bind']['host']
        port = wu.config.data['bind']['port']
        listener = socket.socket(
            socket.AF_INET6 if ':' in host else socket.AF_INET,
            socket.SOCK_STREAM)
        if wu.platform not in ['freebsd', 'osx']:
            try:
                listener.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 1)
            except socket.error:
                logging.warn('Could not set TCP_CORK')
        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        logging.info('Binding to [%s]:%s', host, port)
        try:
            listener.bind((host, port))
        except socket.error as e:
            logging.error('Could not bind: %s', str(e))
            sys.exit(1)

    # Fix stupid socketio bug (it tries to do *args[0][0])
    socket.socket.__getitem__ = lambda x, y: None

    listener.listen(10)

    gateway = GateMiddleware.get(wu.context)
    application = HttpRoot(HttpMiddlewareAggregator([gateway])).dispatch

    wu.server = SocketIOServer(
        listener,
        log=open(os.devnull, 'w'),
        application=application,
        handler_class=RequestHandler,
        policy_server=False,
        transports=[
            str('websocket'),
            str('flashsocket'),
            str('xhr-polling'),
            str('jsonp-polling'),
        ],
    )

    if wu.config.data['ssl']['enable'] and wu.config.data['bind'][
            'mode'] == 'tcp':
        try:
            context = SSL.Context(SSL.TLSv1_2_METHOD)
        except:
            context = SSL.Context(SSL.TLSv1_METHOD)
        context.set_session_id(str(id(context)))
        context.set_options(SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3)
        context.set_cipher_list(
            'ALL:!ADH:!EXP:!LOW:!RC2:!3DES:!SEED:!RC4:+HIGH:+MEDIUM')

        certificate = crypto.load_certificate(
            crypto.FILETYPE_PEM,
            open(wu.config.data['ssl']['certificate']).read())
        private_key = crypto.load_privatekey(
            crypto.FILETYPE_PEM,
            open(wu.config.data['ssl']['certificate']).read())

        context.use_certificate(certificate)
        context.use_privatekey(private_key)

        if wu.config.data['ssl']['client_auth']['enable']:
            # todo harden files
            logging.info('Enabling SSL client authentication')
            context.add_client_ca(certificate)
            context.get_cert_store().add_cert(certificate)
            verify_flags = SSL.VERIFY_PEER
            if wu.config.data['ssl']['client_auth']['force']:
                verify_flags |= SSL.VERIFY_FAIL_IF_NO_PEER_CERT
            context.set_verify(
                verify_flags,
                AuthenticationService.get(
                    wu.context).client_certificate_callback)
            context.set_verify_depth(0)

        wu.server.ssl_args = {'server_side': True}
        wu.server.wrap_socket = lambda socket, **ssl: SSLSocket(
            context, socket)
        logging.info('SSL enabled')

    # auth.log
    try:
        syslog.openlog(
            ident=str(wu.product),
            facility=syslog.LOG_AUTH,
        )
    except:
        syslog.openlog(wu.product)

    def cleanup():
        if hasattr(cleanup, 'started'):
            return
        cleanup.started = True
        logging.info('Process %s exiting normally', os.getpid())
        gevent.signal(signal.SIGINT, lambda: None)
        gevent.signal(signal.SIGTERM, lambda: None)
        if wu.master:
            gateway.destroy()

        p = psutil.Process(os.getpid())
        for c in p.children(recursive=True):
            try:
                os.killpg(c.pid, signal.SIGTERM)
                os.killpg(c.pid, signal.SIGKILL)
            except OSError:
                pass

    def signal_handler():
        cleanup()
        sys.exit(0)

    gevent.signal(signal.SIGINT, signal_handler)
    gevent.signal(signal.SIGTERM, signal_handler)

    wu.server.serve_forever()

    if not wu.master:
        # child process, server is stopped, wait until killed
        gevent.wait()

    if hasattr(wu.server, 'restart_marker'):
        logging.warn('Restarting by request')
        cleanup()

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

        logging.warn('Will restart the process now')
        if '-d' in sys.argv:
            sys.argv.remove('-d')
        os.execv(sys.argv[0], sys.argv)
    else:
        if wu.master:
            logging.debug('Server stopped')
            cleanup()