-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.py
More file actions
184 lines (153 loc) · 6.16 KB
/
docker.py
File metadata and controls
184 lines (153 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import re
import logging
from functools import cache
from typing import Callable
import requests
from localstack.utils.docker_utils import DOCKER_CLIENT
from localstack.extensions.api import Extension, http
from localstack.http import Request
from localstack.utils.container_utils.container_client import (
PortMappings,
SimpleVolumeBind,
)
from localstack.utils.net import get_addressable_container_host
from localstack.utils.sync import retry
from rolo import route
from rolo.proxy import Proxy
from rolo.routing import RuleAdapter, WithHost
LOG = logging.getLogger(__name__)
logging.basicConfig()
# TODO: merge utils with code in TypeDB extension over time ...
class ProxiedDockerContainerExtension(Extension):
name: str
"""Name of this extension"""
image_name: str
"""Docker image name"""
container_name: str | None
"""Name of the Docker container spun up by the extension"""
container_ports: list[int]
"""List of network ports of the Docker container spun up by the extension"""
host: str | None
"""
Optional host on which to expose the container endpoints.
Can be either a static hostname, or a pattern like `<regex("(.+\.)?"):subdomain>myext.<domain>`
"""
path: str | None
"""Optional path on which to expose the container endpoints."""
command: list[str] | None
"""Optional command (and flags) to execute in the container."""
request_to_port_router: Callable[[Request], int] | None
"""Callable that returns the target port for a given request, for routing purposes"""
http2_ports: list[int] | None
"""List of ports for which HTTP2 proxy forwarding into the container should be enabled."""
volumes: list[SimpleVolumeBind] | None = (None,)
"""Optional volumes to mount into the container host."""
def __init__(
self,
image_name: str,
container_ports: list[int],
host: str | None = None,
path: str | None = None,
container_name: str | None = None,
command: list[str] | None = None,
request_to_port_router: Callable[[Request], int] | None = None,
http2_ports: list[int] | None = None,
volumes: list[SimpleVolumeBind] | None = None,
):
self.image_name = image_name
self.container_ports = container_ports
self.host = host
self.path = path
self.container_name = container_name
self.command = command
self.request_to_port_router = request_to_port_router
self.http2_ports = http2_ports
self.volumes = volumes
def update_gateway_routes(self, router: http.Router[http.RouteHandler]):
if self.path:
raise NotImplementedError(
"Path-based routing not yet implemented for this extension"
)
self.start_container()
# add resource for HTTP/1.1 requests
resource = RuleAdapter(ProxyResource(self))
if self.host:
resource = WithHost(self.host, [resource])
router.add(resource)
def on_platform_shutdown(self):
self._remove_container()
def _get_container_name(self) -> str:
if self.container_name:
return self.container_name
name = f"ls-ext-{self.name}"
name = re.sub(r"\W", "-", name)
return name
@cache
def start_container(self) -> None:
container_name = self._get_container_name()
LOG.debug("Starting extension container %s", container_name)
ports = PortMappings()
for port in self.container_ports:
ports.add(port)
kwargs = {}
if self.command:
kwargs["command"] = self.command
try:
DOCKER_CLIENT.run_container(
self.image_name,
detach=True,
remove=True,
name=container_name,
ports=ports,
volumes=self.volumes,
**kwargs,
)
except Exception as e:
LOG.debug("Failed to start container %s: %s", container_name, e)
raise
main_port = self.container_ports[0]
container_host = get_addressable_container_host()
def _ping_endpoint():
# TODO: allow defining a custom healthcheck endpoint ...
response = requests.get(
f"http://{container_host}:{main_port}/__admin/health"
)
assert response.ok
try:
retry(_ping_endpoint, retries=40, sleep=1)
except Exception as e:
LOG.info("Failed to connect to container %s: %s", container_name, e)
self._remove_container()
raise
LOG.debug("Successfully started extension container %s", container_name)
def _remove_container(self):
container_name = self._get_container_name()
LOG.debug("Stopping extension container %s", container_name)
DOCKER_CLIENT.remove_container(
container_name, force=True, check_existence=False
)
class ProxyResource:
"""
Simple proxy resource that forwards incoming requests from the
LocalStack Gateway to the target Docker container.
"""
extension: ProxiedDockerContainerExtension
def __init__(self, extension: ProxiedDockerContainerExtension):
self.extension = extension
@route("/<path:path>")
def index(self, request: Request, path: str, *args, **kwargs):
return self._proxy_request(request, forward_path=f"/{path}")
def _proxy_request(self, request: Request, forward_path: str, *args, **kwargs):
self.extension.start_container()
port = self.extension.container_ports[0]
container_host = get_addressable_container_host()
base_url = f"http://{container_host}:{port}"
proxy = Proxy(forward_base_url=base_url)
# update content length (may have changed due to content compression)
if request.method not in ("GET", "OPTIONS"):
request.headers["Content-Length"] = str(len(request.data))
# make sure we're forwarding the correct Host header
request.headers["Host"] = f"localhost:{port}"
# forward the request to the target
result = proxy.forward(request, forward_path=forward_path)
return result