def _start_webserver(outdir, port): """Start web server for report content.""" utils.drop_privileges() app = flask.Flask(__name__) @app.route('/checkout') def _get_listing(): """Return list of reports.""" files = os.listdir(outdir) template = """ <html> <body> {% for file in files %} <a href=/checkout/{{ file }}>{{ file }}</a><br> {% endfor %} </body> </html> """ return flask.render_template_string(template, files=reversed(files)) @app.route('/checkout/<path:path>') def _get_report(path): """Return checkout report.""" if not path: return _get_listing() complete_path = os.path.join(outdir, path) mimetype = 'text/html' try: with open(complete_path) as f: return flask.Response(f.read(), mimetype=mimetype) except IOError as err: return flask.Response(str(err), mimetype=mimetype) app.run('0.0.0.0', port=port)
def server(register, port, auth, modules, title, cors_origin): """Runs nodeinfo server.""" if port == 0: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('0.0.0.0', 0)) port = sock.getsockname()[1] sock.close() hostname = sysinfo.hostname() hostport = '%s:%s' % (hostname, port) if register: zkclient = context.GLOBAL.zk.conn zkclient.add_listener(zkutils.exit_on_lost) appname = 'root.%s#%010d' % (hostname, os.getpid()) path = z.path.endpoint(appname, 'tcp', 'nodeinfo') _LOGGER.info('register endpoint: %s %s', path, hostport) zkutils.create(zkclient, path, hostport, acl=[_SERVERS_ACL], ephemeral=True) _LOGGER.info('Starting nodeinfo server on port: %s', port) utils.drop_privileges() api_paths = [] if modules: api_paths = api.init(modules, title.replace('_', ' '), cors_origin) rest_server = rest.TcpRestServer(port, auth_type=auth, protect=api_paths) rest_server.run()
def server(approot, register, port, auth, modules, title, cors_origin): """Runs nodeinfo server.""" if port == 0: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('0.0.0.0', 0)) port = sock.getsockname()[1] sock.close() hostname = sysinfo.hostname() hostport = '%s:%s' % (hostname, port) if register: zkclient = context.GLOBAL.zk.conn zkclient.add_listener(zkutils.exit_on_lost) appname = 'root.%s#%010d' % (hostname, os.getpid()) app_pattern = 'root.%s#*' % (hostname) path = z.path.endpoint(appname, 'tcp', 'nodeinfo') _LOGGER.info('register endpoint: %s %s', path, hostport) zkutils.create(zkclient, path, hostport, acl=[zkclient.make_servers_acl()], ephemeral=True) # TODO: remove "legacy" endpoint registration once conversion is # complete. tm_env = appenv.AppEnvironment(approot) # TODO: need to figure out how to handle windows. assert os.name != 'nt' endpoints_mgr = endpoints.EndpointsMgr(tm_env.endpoints_dir) endpoints_mgr.unlink_all( app_pattern, endpoint='nodeinfo', proto='tcp' ) endpoints_mgr.create_spec( appname=appname, endpoint='nodeinfo', proto='tcp', real_port=port, pid=os.getpid(), port=port, owner='/proc/{}'.format(os.getpid()), ) _LOGGER.info('Starting nodeinfo server on port: %s', port) utils.drop_privileges() api_paths = [] if modules: api_modules = {module: None for module in modules} api_paths = api.init( api_modules, title.replace('_', ' '), cors_origin ) rest_server = rest.TcpRestServer(port, auth_type=auth, protect=api_paths) rest_server.run()
def server(approot, register, port, auth, modules, config, title, cors_origin, rate_limit_global, rate_limit_module, rate_limit_by): """Runs nodeinfo server.""" rate_limit = _get_rate_limit( rate_limit_global, rate_limit_module, rate_limit_by ) rest_server = rest.TcpRestServer(port, auth_type=auth, rate_limit=rate_limit) port = rest_server.port hostname = sysinfo.hostname() hostport = '%s:%s' % (hostname, port) if register: zkclient = context.GLOBAL.zk.conn zkclient.add_listener(zkutils.exit_on_lost) appname = 'root.%s#%010d' % (hostname, os.getpid()) app_pattern = 'root.%s#*' % (hostname) path = z.path.endpoint(appname, 'tcp', 'nodeinfo') _LOGGER.info('register endpoint: %s %s', path, hostport) zkutils.create(zkclient, path, hostport, acl=[zkclient.make_servers_acl()], ephemeral=True) # TODO: remove "legacy" endpoint registration once conversion is # complete. tm_env = appenv.AppEnvironment(approot) endpoints_mgr = endpoints.EndpointsMgr(tm_env.endpoints_dir) endpoints_mgr.unlink_all( app_pattern, endpoint='nodeinfo', proto='tcp' ) # On Linux endpoint for nodeinfo is a symlink pointing to # /proc/{pid}, on Windows it's just a regular file owner = '/proc/{}'.format(os.getpid()) if os.name == 'posix' \ else None endpoints_mgr.create_spec( appname=appname, endpoint='nodeinfo', proto='tcp', real_port=port, pid=os.getpid(), port=port, owner=owner, ) _LOGGER.info('Starting nodeinfo server on port: %s', port) utils.drop_privileges() if modules: api_modules = {module: None for module in modules} for module, cfg in config: if module not in api_modules: raise click.UsageError( 'Orphan config: %s, not in: %r' % (module, api_modules) ) api_modules[module] = yaml.load(stream=cfg) cfg.close() rest_server.protect = api.init( api_modules, title.replace('_', ' '), cors_origin ) rest_server.run()