def start_dns_server(asynchronous=False): try: # start local DNS server, if present from localstack_ext import config as config_ext from localstack_ext.services import dns_server if config_ext.DNS_ADDRESS in config.FALSE_STRINGS: return if is_port_open(PORT_DNS): return if is_root(): result = dns_server.start_servers() if not asynchronous: sleep_forever() return result env_vars = {} for env_var in config.CONFIG_ENV_VARS: if env_var.startswith("DNS_"): value = os.environ.get(env_var, None) if value is not None: env_vars[env_var] = value # note: running in a separate process breaks integration with Route53 (to be fixed for local dev mode!) return run_process_as_sudo("dns", PORT_DNS, asynchronous=asynchronous, env_vars=env_vars) except Exception: pass
def start_runtime_components(): from localstack.services.edge import start_edge # TODO: we want a composable LocalStack runtime (edge proxy, service manager, dns, ...) t = start_thread(start_edge, quiet=False) # TODO: properly encapsulate starting/stopping of edge server in a class if not poll_condition( lambda: is_port_open(config.get_edge_port_http()), timeout=15, interval=0.3): if LOG.isEnabledFor(logging.DEBUG): # make another call with quiet=False to print detailed error logs is_port_open(config.get_edge_port_http(), quiet=False) raise TimeoutError( f"gave up waiting for edge server on {config.EDGE_BIND_HOST}:{config.EDGE_PORT}" ) return t
def proxy_server(proxy_listener, host="127.0.0.1", port=None) -> str: """ Create a temporary proxy server on a random port (or the specified port) with the given proxy listener for the duration of the context manager. """ from localstack.services.generic_proxy import start_proxy_server host = host port = port or get_free_tcp_port() thread = start_proxy_server(port, bind_address=host, update_listener=proxy_listener) url = f"http://{host}:{port}" assert poll_condition(lambda: is_port_open(port), timeout=5), f"server on port {port} did not start" yield url thread.stop()
def http_server(handler, host="127.0.0.1", port=None) -> str: """ Create a temporary http server on a random port (or the specified port) with the given handler for the duration of the context manager. Example usage: def handler(request, data): print(request.method, request.path, data) with testutil.http_server(handler) as url: requests.post(url, json={"message": "hello"}) """ from localstack.utils.server.http2_server import run_server host = host port = port or get_free_tcp_port() thread = run_server(port, [host], handler=handler, asynchronous=True) url = f"http://{host}:{port}" assert poll_condition(lambda: is_port_open(port), timeout=5), f"server on port {port} did not start" yield url thread.stop()
def health(self): """ Runs a health check on the server. The default implementation performs is_port_open on the server URL. """ return is_port_open(self.url)