Skip to content

Commit 3ed385c

Browse files
Patrick Schustertobifalk
Patrick Schuster
authored andcommitted
ui: Add python cli script to launch data replay
Signed-off-by: Tobias Falkenstein <[email protected]>
1 parent b9cdfe3 commit 3ed385c

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

ui/scripts/launch_replay.py

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env python3
2+
3+
import webbrowser
4+
import docker
5+
import click
6+
from pathlib import Path
7+
import subprocess
8+
from subprocess import CalledProcessError
9+
from time import sleep
10+
11+
12+
@click.command()
13+
@click.option(
14+
"-p",
15+
"--path",
16+
type=click.Path(exists=True, file_okay=True, dir_okay=False),
17+
required=True,
18+
help="""Path to the replay file. The file needs to either be a .JSON or a
19+
.JSON.gz file. The file is generated if the api_recording flag is
20+
set in the Cloe stackfile""",
21+
)
22+
@click.option(
23+
"-h",
24+
"--host",
25+
default="localhost",
26+
help="IP adress of host where ui and server run. (Default is localhost)",
27+
)
28+
@click.option("--cloe-ui-image", required=True, help="Cloe-ui docker image.")
29+
@click.option(
30+
"--webserver-image", required=True, help="Cloe-ui webserver docker image."
31+
)
32+
def main(path: str, host: str, cloe_ui_image: str, webserver_image: str):
33+
"""Replay simulation data with the Cloe web-ui.
34+
Docker containers which are used:
35+
- cloe-ui
36+
- cloe-ui-webserver
37+
If there are no containers running, this script will start both.
38+
If the images are not found locally, they will be pulled from the repository.
39+
"""
40+
41+
data_file_path = Path(path).resolve()
42+
dirname = data_file_path.parent.name
43+
return_code = launch_cloe_ui_webserver(data_file_path, dirname, webserver_image)
44+
if return_code == 0:
45+
print(
46+
"------------- Launched Webserver at http://localhost:4000 ------------- "
47+
)
48+
else:
49+
print(f"Failed to launch webserver: {return_code}")
50+
quit()
51+
52+
return_code = launch_cloe_ui(cloe_ui_image)
53+
if return_code == 0:
54+
print("------------- Launched Cloe UI at http://localhost:5000 ------------- ")
55+
else:
56+
print(f"Failed to launch web ui: {return_code}")
57+
quit()
58+
59+
sleep(3)
60+
open_browser(host, dirname, data_file_path.name)
61+
62+
63+
def open_browser(host, dir, filename):
64+
url = f"http://{host}:5000?id={dir}&name={filename}&host={host}"
65+
webbrowser.open_new(url)
66+
67+
68+
def launch_cloe_ui(docker_image):
69+
docker_container_name = "cloe-ui"
70+
# Stop cloe ui container if it is running.
71+
stop_container(docker_container_name)
72+
73+
commands = []
74+
commands.extend(
75+
[
76+
"docker",
77+
"run",
78+
"--rm",
79+
"-d",
80+
f"--name={docker_container_name}",
81+
"-p",
82+
"5000:5000",
83+
docker_image,
84+
]
85+
)
86+
87+
return run_process(commands)
88+
89+
90+
def stop_container(docker_container_name):
91+
docker_env = docker.from_env()
92+
running_container = docker_env.containers.list(
93+
filters={"status": "running", "name": docker_container_name}
94+
)
95+
for container in running_container:
96+
if container.name == docker_container_name:
97+
container.stop()
98+
99+
100+
def launch_cloe_ui_webserver(mounting_path, dir, docker_image):
101+
docker_container_name = "cloe-ui-webserver"
102+
103+
# Stop webserver container if it is running.
104+
stop_container(docker_container_name)
105+
106+
commands = []
107+
commands.extend(
108+
[
109+
"docker",
110+
"run",
111+
"--rm",
112+
"-d",
113+
f"--name={docker_container_name}",
114+
"-p",
115+
"4000:4000",
116+
"-v",
117+
f"{str(mounting_path.parent)}:/app/replay_data/{dir}",
118+
docker_image,
119+
]
120+
)
121+
122+
return run_process(commands)
123+
124+
125+
def run_process(commands: list, **kwargs) -> int:
126+
"""Run a process with the given commands
127+
128+
Return the return code in case of a thrown CalledProcessError, but doesn't raise.
129+
Raises Exception in case another exception was thrown.
130+
"""
131+
try:
132+
subprocess.check_call(commands, cwd=kwargs.get("cwd"))
133+
return 0
134+
except CalledProcessError as exc:
135+
return exc.returncode
136+
except Exception as exc:
137+
print(f"Unexpected error occurred while running process: {exc}")
138+
raise
139+
140+
141+
if __name__ == "__main__":
142+
main()

0 commit comments

Comments
 (0)