-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_docker
executable file
·73 lines (63 loc) · 2.54 KB
/
run_docker
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
#!/usr/bin/env python3
import argparse
import re
import shutil
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Optional
_ROS_ROOT_INSIDE_CONTAINER = "/home/user/catkin_ws/src/mocap_ros"
_TORCH_CACHE_DIR_INSIDE_CONTAINER = "/home/user/.cache/torch"
_TORCH_CACHE_DIR = Path.home() / ".cache" / "torch"
def add_prefix(file_path: Path, prefix: str) -> Path:
parent = file_path.parent
return parent / (prefix + file_path.name)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-launch", type=str, help="launch file name")
parser.add_argument(
"-host", type=str, default="pr1040", help="host name or ip-address"
)
parser.add_argument(
"launch_args",
nargs=argparse.REMAINDER,
help="launch args in ros style e.g. foo:=var",
)
args = parser.parse_args()
mount_path_str: Optional[str] = Path(__file__).resolve().parent / "launch"
assert mount_path_str is not None
mount_path = Path(mount_path_str)
launch_file_name: Optional[str] = args.launch
assert launch_file_name is not None
for launch_arg in args.launch_args:
assert bool(re.match(r".*:=.*", launch_arg))
launch_args = " ".join(args.launch_args)
with TemporaryDirectory() as td:
tmp_launch_path = Path(td) / "launch"
if mount_path.is_dir():
shutil.copytree(mount_path, tmp_launch_path)
else:
shutil.copyfile(mount_path, tmp_launch_path)
docker_run_command = """
docker run \
-v {node_scripts_dir}:{ros_root}/node_scripts \
-v {tmp_launch_path}:{ros_root}/launch \
-v {torch_cache_dir}:{torch_cache_dir_inside_container} \
--rm --net=host -it \
--gpus 1 mocap_ros:latest \
/bin/bash -i -c \
"source ~/.bashrc; \
roscd mocap_ros; \
rossetip; rossetmaster {host}; \
roslaunch mocap_ros {launch_file_name} {launch_args}"
""".format(
node_scripts_dir=Path(__file__).resolve().parent / "node_scripts",
tmp_launch_path=tmp_launch_path,
torch_cache_dir=_TORCH_CACHE_DIR,
torch_cache_dir_inside_container=_TORCH_CACHE_DIR_INSIDE_CONTAINER,
ros_root=_ROS_ROOT_INSIDE_CONTAINER,
host=args.host,
launch_file_name=launch_file_name,
launch_args=launch_args,
)
subprocess.call(docker_run_command, shell=True)