-
Notifications
You must be signed in to change notification settings - Fork 14
/
run_container.py
executable file
·73 lines (61 loc) · 2.24 KB
/
run_container.py
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
# NOTE: please use only standard libraries
import argparse
import os
import re
import shutil
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Optional
_DETIC_ROS_ROOT_INSIDE_CONTAINER = "/home/user/detic_ws/src/detic_ros"
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(
"-mount", type=str, help="mount source launch file or directory"
)
parser.add_argument("-name", 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] = args.mount
assert mount_path_str is not None
mount_path = Path(mount_path_str)
launch_file_name: Optional[str] = args.name
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 {tmp_launch_path}:{detic_ros_root}/launch \
--rm --net=host -it \
--gpus 1 detic_ros:latest \
/bin/bash -i -c \
"source ~/.bashrc; \
roscd detic_ros; \
rossetip; rossetmaster {host}; \
roslaunch detic_ros {launch_file_name} {launch_args}"
""".format(
tmp_launch_path=tmp_launch_path,
detic_ros_root=_DETIC_ROS_ROOT_INSIDE_CONTAINER,
host=args.host,
launch_file_name=launch_file_name,
launch_args=launch_args,
)
subprocess.call(docker_run_command, shell=True)