forked from project-chip/certification-tool-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_shell_commands.py
More file actions
286 lines (232 loc) · 8.49 KB
/
Copy pathdocker_shell_commands.py
File metadata and controls
286 lines (232 loc) · 8.49 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#
# Copyright (c) 2025 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Utility functions to generate shell command equivalents for Docker API operations.
Useful for debugging and understanding what Docker operations are being performed.
"""
from pathlib import Path
from typing import Dict, List, Optional, Union
# Log message constants
SHELL_CMD_LOG_PREFIX = "Docker API call equivalent shell command:\n"
# Shell special characters that require escaping
SHELL_SPECIAL_CHARS = [
" ",
"'",
'"',
"$",
"`",
"\\",
"!",
"&",
"|",
";",
"(",
")",
"<",
">",
]
def escape_shell_arg(arg: Optional[str]) -> str:
"""
Escape shell argument if it contains spaces or special characters.
Uses single-quote wrapping for safety. Any single quotes in the argument
are escaped using the pattern: ' becomes '\''
(close quote, escaped quote, open quote).
Args:
arg: The argument to escape. Docker container/image names can be None
(e.g. a Container whose attrs have no "Name" key), so this is
tolerated and rendered as an empty string rather than raising.
Returns:
The argument wrapped in single quotes if it contains special characters,
otherwise returns the argument unchanged.
"""
if arg is None:
return ""
if any(c in arg for c in SHELL_SPECIAL_CHARS):
# Escape any single quotes: ' becomes '\''
escaped_arg = arg.replace("'", "'\\''")
# Wrap the entire argument in single quotes
return f"'{escaped_arg}'"
return arg
def docker_run_command(image_tag: str, parameters: Dict) -> str:
"""
Generate docker run command from API parameters.
Args:
image_tag: Docker image tag
parameters: Dictionary of parameters passed to docker.containers.run()
Returns:
String representation of equivalent shell command
"""
cmd_parts = ["docker run"]
# Handle privileged mode
if parameters.get("privileged"):
cmd_parts.append("--privileged")
# Handle detach mode
if parameters.get("detach"):
cmd_parts.append("-d")
# Handle network
if network := parameters.get("network"):
cmd_parts.append(f"--network {escape_shell_arg(network)}")
# Handle name
if name := parameters.get("name"):
cmd_parts.append(f"--name {escape_shell_arg(name)}")
# Handle volumes
if volumes := parameters.get("volumes"):
for host_path, mount_config in volumes.items():
bind_path = mount_config.get("bind")
mode = mount_config.get("mode", "rw")
volume_spec = f"{host_path}:{bind_path}:{mode}"
cmd_parts.append(f"-v {escape_shell_arg(volume_spec)}")
# Handle environment variables
if environment := parameters.get("environment"):
if isinstance(environment, dict):
for key, value in environment.items():
env_spec = f"{key}={value}"
cmd_parts.append(f"-e {escape_shell_arg(env_spec)}")
elif isinstance(environment, list):
for env_var in environment:
cmd_parts.append(f"-e {escape_shell_arg(env_var)}")
# Handle working directory
if working_dir := parameters.get("working_dir"):
cmd_parts.append(f"-w {escape_shell_arg(working_dir)}")
# Handle ports
if ports := parameters.get("ports"):
if isinstance(ports, dict):
for container_port, host_config in ports.items():
if isinstance(host_config, list):
for host_port_config in host_config:
host_port = host_port_config.get("HostPort")
port_spec = f"{host_port}:{container_port}"
cmd_parts.append(f"-p {escape_shell_arg(port_spec)}")
elif isinstance(host_config, tuple):
host_ip, host_port = host_config
port_spec = f"{host_ip}:{host_port}:{container_port}"
cmd_parts.append(f"-p {escape_shell_arg(port_spec)}")
else:
port_spec = f"{host_config}:{container_port}"
cmd_parts.append(f"-p {escape_shell_arg(port_spec)}")
# Handle user
if user := parameters.get("user"):
cmd_parts.append(f"--user {escape_shell_arg(user)}")
# Handle stdin_open
if parameters.get("stdin_open"):
cmd_parts.append("-i")
# Handle tty
if parameters.get("tty"):
cmd_parts.append("-t")
# Add image tag
cmd_parts.append(escape_shell_arg(image_tag))
# Handle command
if command := parameters.get("command"):
if isinstance(command, list):
cmd_parts.extend([escape_shell_arg(str(c)) for c in command])
else:
# For string commands, use sh -c to properly execute the command
cmd_parts.extend(["sh", "-c", escape_shell_arg(str(command))])
return " ".join(cmd_parts)
def docker_exec_command(
container_name: str,
command: Union[str, List[str]],
stdin: bool = False,
detach: bool = False,
) -> str:
"""
Generate docker exec command from API parameters.
Args:
container_name: Name or ID of the container
command: Command to execute (string or list)
stdin: Whether stdin is enabled
detach: Whether to run in detached mode
Returns:
String representation of equivalent shell command
"""
cmd_parts = ["docker exec"]
if stdin:
cmd_parts.append("-i")
if detach:
cmd_parts.append("-d")
cmd_parts.append(escape_shell_arg(container_name))
if isinstance(command, list):
cmd_parts.extend([escape_shell_arg(str(c)) for c in command])
else:
# Command is already a full string, use sh -c to execute it
cmd_parts.extend(["sh", "-c", escape_shell_arg(str(command))])
return " ".join(cmd_parts)
def docker_kill_command(container_name: Optional[str]) -> str:
"""
Generate docker kill command.
Args:
container_name: Name or ID of the container
Returns:
String representation of equivalent shell command
"""
return f"docker kill {escape_shell_arg(container_name)}"
def docker_stop_command(container_name: Optional[str]) -> str:
"""
Generate docker stop command.
Args:
container_name: Name or ID of the container
Returns:
String representation of equivalent shell command
"""
return f"docker stop {escape_shell_arg(container_name)}"
def docker_rm_command(container_name: Optional[str], force: bool = False) -> str:
"""
Generate docker rm command.
Args:
container_name: Name or ID of the container
force: Whether to force remove
Returns:
String representation of equivalent shell command
"""
cmd = "docker rm"
if force:
cmd += " -f"
cmd += f" {escape_shell_arg(container_name)}"
return cmd
def docker_cp_from_container_command(
container_name: str,
container_path: Path,
host_path: Path,
) -> str:
"""
Generate docker cp command for copying from container to host.
Args:
container_name: Name or ID of the container
container_path: Path inside the container
host_path: Destination path on host
Returns:
String representation of equivalent shell command
"""
source = f"{container_name}:{container_path}"
return f"docker cp {escape_shell_arg(source)} {escape_shell_arg(str(host_path))}"
def docker_cp_to_container_command(
container_name: str,
host_path: Path,
container_path: Path,
) -> str:
"""
Generate docker cp command for copying from host to container.
Args:
container_name: Name or ID of the container
host_path: Source path on host
container_path: Destination path inside the container
Returns:
String representation of equivalent shell command
"""
destination = f"{container_name}:{container_path}"
return (
f"docker cp {escape_shell_arg(str(host_path))} {escape_shell_arg(destination)}"
)