-
Notifications
You must be signed in to change notification settings - Fork 13
/
ddcterm
executable file
·260 lines (216 loc) · 8.11 KB
/
ddcterm
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#! /usr/bin/env python3
"""A tool that manages the terminal access of a development docker container.
This tool manages a build/test environment in a docker container during
code development. It is assumed that each directory is associated with one
development container.
The workflow is as follows:
To start the development container:
$ ./ddcterm up <docker_image_spec>
where <docker_image_spec> is the full docker image specification.
To enter the development container:
$ ./ddcterm enter
To end the development container:
$ ./ddcterm down
"""
import argparse
import getpass
import hashlib
import logging
import os
import os.path
import pwd
import shlex
import subprocess as sp
import sys
_CONTAINER_WORKDIR = "/workdir"
_CONTAINER_SHELL = "/bin/bash"
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
def _get_user_info_dict():
user_info_struct = pwd.getpwnam(getpass.getuser())
return {
"username": user_info_struct.pw_name,
"uid": user_info_struct.pw_uid,
"gid": user_info_struct.pw_gid
}
def _container_name_from_cwd():
return "{}-{}".format(
os.path.basename(__file__),
hashlib.sha256(os.getcwd().encode("latin-1")).hexdigest())
def _is_container_running(container_name):
docker_inspect_command = ("docker inspect "
"-f '{{{{.State.Running}}}}' "
"{container_name}").format(
container_name=container_name)
try:
sp.check_output(shlex.split(docker_inspect_command), stderr=sp.STDOUT)
except sp.CalledProcessError:
return False
return True
def _is_image_local(image_name):
docker_inspect_command = ("docker inspect {image_name}").format(
image_name=image_name)
try:
sp.check_output(shlex.split(docker_inspect_command), stderr=sp.STDOUT)
except sp.CalledProcessError:
return False
return True
def up(args):
"""Bring up the development container."""
container_name = _container_name_from_cwd()
if _is_container_running(container_name):
logging.warning("A container is already up: %s", container_name)
sys.exit(1)
if not _is_image_local(args.image_spec):
logging.error(
"%s is not found locally.\nPlease run `docker pull %s` and try again.",
args.image_spec, args.image_spec)
sys.exit(1)
def _docker_display_flags():
return "-v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY -e XAUTHORITY"
def _docker_gdb_enable_ptrace_flags():
return "--cap-add=SYS_PTRACE --security-opt seccomp=unconfined"
# Run the docker container based on the given image spec.
docker_run_config = {
"gpus":
"" if args.no_nvidia else "--gpus all",
"gdb_ptrace":
"" if args.no_gdb_ptrace else _docker_gdb_enable_ptrace_flags(),
"host_network":
"" if args.no_host_network else "--network host",
"host_display":
"" if args.no_host_display else _docker_display_flags(),
"host_sourcedir":
os.getcwd(),
"container_workdir":
_CONTAINER_WORKDIR,
"container_name":
container_name,
"image_spec":
args.image_spec,
"container_shell":
_CONTAINER_SHELL,
}
docker_run_command = ("docker run "
"-t -d --rm "
"{gpus} "
"{gdb_ptrace} "
"{host_network} "
"{host_display} "
"-v {host_sourcedir}:{container_workdir} "
"-w {container_workdir} "
"--name {container_name} "
"{image_spec} {container_shell}").format(
**docker_run_config)
try:
sp.check_output(shlex.split(docker_run_command))
except sp.CalledProcessError as e:
logging.error("%s", e.output.decode("UTF-8"))
sys.exit(1)
# Create a non-root user in the container that agrees with the host user.
user_info = _get_user_info_dict()
docker_exec_user_config = {
"username": user_info["username"],
"groupname": user_info["username"],
"uid": user_info["uid"],
"gid": user_info["gid"],
"container_name": docker_run_config["container_name"],
}
docker_exec_command = ("docker exec "
"{container_name} "
"groupadd -g {gid} "
"{groupname}").format(**docker_exec_user_config)
sp.check_output(shlex.split(docker_exec_command))
docker_exec_command = ("docker exec "
"{container_name} "
"useradd -g {gid} -u {uid} -m "
"{username}").format(**docker_exec_user_config)
sp.check_output(shlex.split(docker_exec_command))
logging.info("%s is up.", container_name)
def enter(args):
"""Enter the development container."""
container_name = _container_name_from_cwd()
if not _is_container_running(container_name):
logging.error("Container %s is not running.", container_name)
sys.exit(1)
user_info = _get_user_info_dict()
docker_exec_config = {
"container_name":
_container_name_from_cwd(),
"user_flag":
"" if args.root else "-u {uid}:{gid}".format(**user_info),
"alsologtostderr":
"" if args.no_alsologtostderr else "-e GLOG_alsologtostderr=1",
"gst_plugin_path":
"-e GST_PLUGIN_PATH={}".format(args.gst_plugin_path),
"container_shell":
_CONTAINER_SHELL,
}
logging.info("Entering container as \'%s\'",
"root" if args.root else user_info["username"])
docker_exec_command = ("docker exec "
"-it "
"{user_flag} "
"{alsologtostderr} "
"{gst_plugin_path} "
"{container_name} "
"{container_shell}").format(**docker_exec_config)
docker_exec_tokens = shlex.split(docker_exec_command)
os.execlp(docker_exec_tokens[0], *docker_exec_tokens)
def down(args): # pylint: disable=unused-argument
"""Bring down the development container."""
container_name = _container_name_from_cwd()
if not _is_container_running(container_name):
logging.error("Container %s is not running.", container_name)
sys.exit(1)
docker_stop_command = "docker container stop {}".format(container_name)
sp.check_output(shlex.split(docker_stop_command))
logging.info("%s is down.", container_name)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Docker development container terminal.")
subparsers = parser.add_subparsers()
# Options for "up" .
subparser_up = subparsers.add_parser("up")
subparser_up.add_argument(
"image_spec",
type=str,
help="The docker image spec in which the terminal will be run.")
subparser_up.add_argument(
"--no-gdb-ptrace",
action="store_true",
help="Do not enable ptrace for gdb.")
subparser_up.add_argument(
"--no-host-display",
action="store_true",
help="Do not connect to the host display.")
subparser_up.add_argument(
"--no-host-network",
action="store_true",
help="Run the container without using the host network interfaces.")
subparser_up.add_argument(
"--no-nvidia",
action="store_true",
help="Run the container without enabling GPUs.")
subparser_up.set_defaults(func=up)
# Options for "enter".
subparser_enter = subparsers.add_parser("enter")
subparser_enter.add_argument(
"--root", action="store_true", help="Enter the container as root.")
subparser_enter.add_argument(
"--no-alsologtostderr",
action="store_true",
help="Do not set GLOG_alsologtostderr")
subparser_enter.add_argument(
"--gst-plugin-path",
type=str,
default="/workdir/visionai-sdk/bazel-bin/visionai/gstreamer/plugins:${GST_PLUGIN_PATH}",
help="Value for GST_PLUGIN_PATH")
subparser_enter.set_defaults(func=enter)
# Options for "down".
subparser_down = subparsers.add_parser("down")
subparser_down.set_defaults(func=down)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
parsed_args = parser.parse_args()
parsed_args.func(parsed_args)