Skip to content

Commit 1c1b5c7

Browse files
zmheikodionhaefnerxalelax
authored
fix: use host port as api port (#330)
#### Description of changes - change `engine.serve` to use host port as api pot for unicorn - change `sdk.docker_client` and `sdk.cli` to not use host/api port - adapt multi-helloworld test and example #### Testing done - Tested serving locally --------- Co-authored-by: Dion Häfner <dion.haefner@simulation.science> Co-authored-by: Alessandro Angioi <alessandro.angioi@simulation.science>
1 parent 374e71f commit 1c1b5c7

File tree

10 files changed

+46
-24
lines changed

10 files changed

+46
-24
lines changed

examples/_multi-tesseract/multi_helloworld/README.md renamed to examples/_multi-tesseract/multi-helloworld/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ docker network create my_network
2222
This example can be executed using the Tesseract Python API or CLI. To run the example using the Python API, simply execute the Python script:
2323

2424
```bash
25-
$ cd ./examples/_multi-tesseract/multi_helloworld/
25+
$ cd ./examples/_multi-tesseract/multi-helloworld/
2626
$ python run_example.py
2727
```
2828

examples/_multi-tesseract/multi_helloworld/run_example.py renamed to examples/_multi-tesseract/multi-helloworld/run_example.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
with Tesseract.from_image(
77
"helloworld:latest", network="my_network", network_alias="helloworld"
88
) as helloworld_tess:
9-
payload = {"name": "YOU", "helloworld_tesseract_url": "http://helloworld:8000"}
9+
payload = {
10+
"name": "YOU",
11+
"helloworld_tesseract_url": f"http://helloworld:{helloworld_tess._serve_context['port']}",
12+
}
1013
result = multi_helloworld_tess.apply(inputs=payload)
1114
print(result["greeting"])

examples/_multi-tesseract/multi_helloworld/tesseract_api.py renamed to examples/_multi-tesseract/multi-helloworld/tesseract_api.py

File renamed without changes.

examples/_multi-tesseract/multi_helloworld/tesseract_config.yaml renamed to examples/_multi-tesseract/multi-helloworld/tesseract_config.yaml

File renamed without changes.

examples/_multi-tesseract/multi_helloworld/tesseract_requirements.txt renamed to examples/_multi-tesseract/multi-helloworld/tesseract_requirements.txt

File renamed without changes.

tesseract_core/sdk/cli.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,12 +699,13 @@ def _get_tesseract_env_vals(
699699
def _get_tesseract_network_meta(container: Container) -> dict:
700700
"""Retrieve network addresses from container."""
701701
network_meta = {}
702-
networks = container.attrs["NetworkSettings"].get("Networks", {})
703-
for network_name, network_info in networks.items():
704-
network_meta[network_name] = {
705-
"ip": f"{network_info['IPAddress']}",
706-
"port": 8000,
707-
}
702+
docker_network_ips = container.docker_network_ips
703+
if docker_network_ips:
704+
for network_name, ip in docker_network_ips.items():
705+
network_meta[network_name] = {
706+
"ip": ip,
707+
"port": container.api_port,
708+
}
708709
return network_meta
709710

710711

tesseract_core/sdk/docker_client.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,28 @@ def host_port(self) -> str | None:
328328
if self.attrs.get("NetworkSettings", None):
329329
ports = self.attrs["NetworkSettings"].get("Ports", None)
330330
if ports:
331-
api_port_key = "8000/tcp"
331+
api_port_key = f"{self.api_port}/tcp"
332332
if ports[api_port_key]:
333333
return ports[api_port_key][0].get("HostPort")
334334
return None
335335

336+
@property
337+
def api_port(self) -> str | None:
338+
"""Gets the api port of the container."""
339+
return self.attrs["Config"]["Cmd"][2]
340+
341+
@property
342+
def docker_network_ips(self) -> dict | None:
343+
"""Gets the host port of the container."""
344+
if self.attrs.get("NetworkSettings", None):
345+
networks = self.attrs["NetworkSettings"].get("Networks", None)
346+
if networks:
347+
network_ips = {}
348+
for network_name, network_info in networks.items():
349+
network_ips[network_name] = f"{network_info['IPAddress']}"
350+
return network_ips
351+
return None
352+
336353
@property
337354
def host_debugpy_port(self) -> str | None:
338355
"""Gets the host port which maps to debugpy server in the container."""
@@ -350,7 +367,7 @@ def host_ip(self) -> str | None:
350367
if self.attrs.get("NetworkSettings", None):
351368
ports = self.attrs["NetworkSettings"].get("Ports", None)
352369
if ports:
353-
api_port_key = "8000/tcp"
370+
api_port_key = f"{self.api_port}/tcp"
354371
if ports[api_port_key]:
355372
return ports[api_port_key][0].get("HostIp")
356373
return None

tesseract_core/sdk/engine.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,12 +564,6 @@ def serve(
564564
if output_format:
565565
environment["TESSERACT_OUTPUT_FORMAT"] = output_format
566566

567-
args = []
568-
container_api_port = "8000"
569-
container_debugpy_port = "5678"
570-
571-
args.extend(["--port", container_api_port])
572-
573567
if not port:
574568
port = str(get_free_port())
575569
else:
@@ -578,6 +572,12 @@ def serve(
578572
port_start, port_end = port.split("-")
579573
port = str(get_free_port(within_range=(int(port_start), int(port_end))))
580574

575+
args = []
576+
container_api_port = port
577+
container_debugpy_port = "5678"
578+
579+
args.extend(["--port", container_api_port])
580+
581581
if num_workers > 1:
582582
args.extend(["--num-workers", str(num_workers)])
583583

tesseract_core/sdk/templates/Dockerfile.base

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,5 @@ ENV TESSERACT_OUTPUT_PATH="/tesseract/output_data"
107107
RUN _TESSERACT_IS_BUILDING=1 tesseract-runtime check
108108
{% endif %}
109109

110-
EXPOSE 8000
111110
ENTRYPOINT ["tesseract-runtime"]
112111
CMD ["--help"]

tests/endtoend_tests/test_endtoend.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def serve_tesseract(alias: str):
665665
[
666666
"python",
667667
"-c",
668-
'import requests; requests.get("http://tess_2:8000/health").raise_for_status()',
668+
f'import requests; requests.get("http://tess_2:{tess_2.api_port}/health").raise_for_status()',
669669
]
670670
)
671671
assert returncode == 0, stdout.decode()
@@ -674,7 +674,7 @@ def serve_tesseract(alias: str):
674674
[
675675
"python",
676676
"-c",
677-
'import requests; requests.get("http://tess_1:8000/health").raise_for_status()',
677+
f'import requests; requests.get("http://tess_1:{tess_1.api_port}/health").raise_for_status()',
678678
]
679679
)
680680
assert returncode == 0, stdout.decode()
@@ -1079,12 +1079,12 @@ def test_multi_helloworld_endtoend(
10791079
dummy_network_name,
10801080
docker_cleanup,
10811081
):
1082-
"""Test that multi_helloworld example can be built, served, and executed."""
1082+
"""Test that multi-helloworld example can be built, served, and executed."""
10831083
cli_runner = CliRunner(mix_stderr=False)
10841084

10851085
# Build Tesseract images
10861086
img_names = []
1087-
for tess_name in ("_multi-tesseract/multi_helloworld", "helloworld"):
1087+
for tess_name in ("_multi-tesseract/multi-helloworld", "helloworld"):
10881088
img_name = build_tesseract(
10891089
docker_client,
10901090
unit_tesseracts_parent_dir / tess_name,
@@ -1123,17 +1123,19 @@ def test_multi_helloworld_endtoend(
11231123
)
11241124
assert result.exit_code == 0, result.output
11251125
docker_cleanup["containers"].append(json.loads(result.output)["container_name"])
1126-
1126+
api_port = json.loads(result.output)["containers"][0]["networks"][
1127+
dummy_network_name
1128+
]["port"]
11271129
payload = json.dumps(
11281130
{
11291131
"inputs": {
11301132
"name": "you",
1131-
"helloworld_tesseract_url": "http://helloworld:8000",
1133+
"helloworld_tesseract_url": f"http://helloworld:{api_port}",
11321134
}
11331135
}
11341136
)
11351137

1136-
# Run multi_helloworld Tesseract
1138+
# Run multi-helloworld Tesseract
11371139
result = cli_runner.invoke(
11381140
app,
11391141
[

0 commit comments

Comments
 (0)