-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_helper.py
More file actions
345 lines (313 loc) · 13.6 KB
/
Copy pathdocker_helper.py
File metadata and controls
345 lines (313 loc) · 13.6 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import os
import shutil
import subprocess
from dataclasses import dataclass
from urllib.parse import quote
@dataclass
class ExecResult:
stdout: str
stderr: str
returncode: int
@property
def ok(self) -> bool:
return self.returncode == 0
class DockerHelper:
def __init__(self, cli: str | None = None):
if cli is None:
# Treat an empty CONTAINER_CLI as unset so it falls back to auto-detection.
cli = os.environ.get("CONTAINER_CLI") or None
if cli is not None:
# An explicitly chosen CLI (argument or CONTAINER_CLI) must actually exist on
# PATH, otherwise every command later fails with an opaque FileNotFoundError.
if not shutil.which(cli):
raise RuntimeError(
f"Container CLI {cli!r} (from the cli argument or CONTAINER_CLI) was "
"not found on PATH. Install it or set CONTAINER_CLI correctly."
)
else:
for candidate in ("docker", "podman"):
if shutil.which(candidate):
cli = candidate
break
if not cli:
raise RuntimeError(
"No container CLI found. Install docker or podman, or set CONTAINER_CLI."
)
self.cli = cli
def _run(
self,
args: list[str],
check: bool = True,
input_text: str | None = None,
timeout: float | None = None,
) -> ExecResult:
"""Run a container CLI command and return its result, raising on failure when check is set.
A timeout (seconds) bounds how long the call may block; on timeout the process is killed and
a non-ok result is returned (or raised when check is set), so a hung connection can't stall forever.
"""
try:
proc = subprocess.run(
[self.cli, *args],
capture_output=True,
text=True,
input=input_text,
timeout=timeout,
)
except subprocess.TimeoutExpired as exc:
result = ExecResult(
stdout=exc.stdout or "",
stderr=(exc.stderr or "").strip() or f"timed out after {timeout}s",
returncode=124,
)
else:
result = ExecResult(stdout=proc.stdout, stderr=proc.stderr, returncode=proc.returncode)
if check and not result.ok:
raise RuntimeError(
f"{self.cli} {' '.join(args)} failed (exit {result.returncode})\n"
f"stdout: {result.stdout}\nstderr: {result.stderr}"
)
return result
def create(
self,
image: str,
name: str,
hostname: str | None = None,
environment: dict[str, str] | None = None,
volumes: list[str] | None = None,
network: str | None = None,
ports: list[str] | None = None,
entrypoint: str | None = None,
command: list[str] | None = None,
detach: bool = True,
restart: str | None = None,
platform: str | None = None,
cap_add: list[str] | None = None,
) -> ExecResult:
"""Create and start a long-lived (detached) container with the given config."""
# These containers are long-lived (no --rm), so a run that crashed before teardown
# leaves one with this name behind, making `run --name` fail with a conflict.
# Remove any leftover first so reruns are idempotent (named data volumes survive
# `rm -f -v`, so this only drops the stale container, not its data).
self.destroy(name)
args = ["run"]
if detach:
args.append("-d")
args.extend(["--name", name])
if platform:
# Needed for multi-arch manifest-list images with no native variant
# (e.g. an amd64-only image on Apple Silicon).
args.extend(["--platform", platform])
if hostname:
args.extend(["--hostname", hostname])
if entrypoint:
args.extend(["--entrypoint", entrypoint])
if restart:
args.extend(["--restart", restart])
for cap in cap_add or []:
args.extend(["--cap-add", cap])
for k, v in (environment or {}).items():
args.extend(["-e", f"{k}={v}"])
for vol in volumes or []:
args.extend(["-v", vol])
if network:
args.extend(["--network", network])
for port in ports or []:
args.extend(["-p", port])
args.append(image)
if command:
args.extend(command)
return self._run(args)
def run(
self,
image: str,
name: str | None = None,
network: str | None = None,
entrypoint: str | None = None,
command: list[str] | None = None,
volumes: list[str] | None = None,
user: str | None = None,
platform: str | None = None,
remove: bool = True,
check: bool = True,
) -> ExecResult:
"""Run a one-off (by default --rm) container, e.g. an ephemeral helper task."""
# --rm only cleans up on a clean exit; an interrupted prior run can leave a
# container with this name behind, making `run --name` fail with a conflict.
# Remove any leftover first so reruns are idempotent.
if name:
self.destroy(name)
args = ["run"]
if remove:
args.append("--rm")
if name:
args.extend(["--name", name])
if platform:
args.extend(["--platform", platform])
if user:
args.extend(["--user", user])
if entrypoint:
args.extend(["--entrypoint", entrypoint])
for vol in volumes or []:
args.extend(["-v", vol])
if network:
args.extend(["--network", network])
args.append(image)
if command:
args.extend(command)
return self._run(args, check=check)
def destroy(self, name: str) -> ExecResult:
"""Force-remove a container and its anonymous volumes, ignoring errors if absent.
The -v flag drops anonymous volumes the image declares (e.g. /var/log/mysql on
the server image, /etc/haproxy/pxc on the HAProxy image) that we never bind to a
named volume — otherwise they leak on every run. Named volumes (mounted by name)
are not affected and are removed explicitly via volume_remove.
"""
return self._run(["rm", "-f", "-v", name], check=False)
def start(self, name: str) -> ExecResult:
"""Start an existing stopped container."""
return self._run(["start", name])
def stop(self, name: str) -> ExecResult:
"""Stop a running container."""
return self._run(["stop", name])
def exec_command(self, name: str, command: str, check: bool = False) -> ExecResult:
"""Run a shell command inside a running container."""
return self._run(["exec", name, "sh", "-c", command], check=check)
def exec_mysql(
self,
name: str,
sql: str,
user: str = "root",
password: str = "rootpass",
database: str | None = None,
host: str | None = None,
port: int | None = None,
check: bool = True,
timeout: float | None = None,
) -> ExecResult:
"""Run a SQL statement inside a container using the mysql client.
With host/port omitted the client uses the container's local socket. Pass
host/port to connect over TCP instead (e.g. through a proxy). A timeout (seconds)
bounds the call so a connection that hangs (e.g. a proxy with no live backend) can't block.
"""
args = ["exec", name, "mysql", f"-u{user}", f"-p{password}", "-N", "-B"]
if host:
args.append(f"-h{host}")
if port:
# Force TCP so a host such as "localhost" is not silently swapped for the socket.
args.extend([f"-P{port}", "--protocol=TCP"])
if database:
args.extend(["-D", database])
args.extend(["-e", sql])
return self._run(args, check=check, timeout=timeout)
def exec_mysqlsh(
self,
name: str,
script: str,
user: str = "root",
password: str = "rootpass",
host: str = "localhost",
port: int = 3306,
language: str = "js",
check: bool = True,
timeout: float | None = None,
) -> ExecResult:
"""Run a MySQL Shell (mysqlsh) script inside a container against the given URI.
A timeout (seconds) bounds the call so a stuck mysqlsh (e.g. waiting on a
connection) can't hang the run until an outer pytest timeout.
"""
# Percent-encode the credentials so a user/password containing URI-reserved
# characters (@ : / # ?) doesn't make mysqlsh misparse the connection string.
uri = f"{quote(user, safe='')}:{quote(password, safe='')}@{host}:{port}"
args = [
"exec",
"-i",
name,
"mysqlsh",
"--no-wizard",
"--uri",
uri,
f"--{language}",
"-e",
script,
]
return self._run(args, check=check, timeout=timeout)
def network_create(self, name: str) -> ExecResult:
"""Create a container network, reusing it if one with the same name already exists."""
existing = self._run(
["network", "ls", "--filter", f"name=^{name}$", "--format", "{{.Name}}"],
check=False,
)
if existing.ok and existing.stdout.strip() == name:
return existing
return self._run(["network", "create", name])
def network_remove(self, name: str) -> ExecResult:
"""Remove a container network, ignoring errors if it does not exist."""
return self._run(["network", "rm", name], check=False)
def container_networks(self, name: str) -> list[str]:
"""Return the names of the networks a container is currently attached to.
Empty means attached to nothing — the normal state of a node that
network_disconnect() has isolated, not an error. An inspect that fails (no such
container, no daemon) raises rather than returning [], so callers can act on the
answer instead of guessing: reporting a failure as "attached to nothing" would make
network_disconnect() skip the disconnect and report success, leaving a partition
test reasoning about a partition that never happened.
"""
result = self._run(
[
"inspect",
"-f",
'{{range $net, $_ := .NetworkSettings.Networks}}{{$net}}{{"\\n"}}{{end}}',
name,
],
)
return [line.strip() for line in result.stdout.splitlines() if line.strip()]
def network_connect(self, network: str, name: str) -> ExecResult | None:
"""Attach a running container to a network, doing nothing if it is already attached.
The idempotence matters for reruns and for healing a partition that was only
partially applied: connecting twice otherwise fails with "already exists in network".
Returns None when the container was already attached.
"""
if network in self.container_networks(name):
return None
return self._run(["network", "connect", network, name])
def network_disconnect(self, network: str, name: str, force: bool = False) -> ExecResult | None:
"""Detach a running container from a network, doing nothing if it is not attached.
Unlike stop(), the process inside the container is untouched — it simply loses
connectivity — which is what makes this usable for network-partition tests.
Returns None when the container was not attached in the first place.
Note: reconnecting later does not restore the container's published host port
mappings (the -p flags given at create time). Nothing in this suite reaches nodes
from the host, but a healed node is no longer reachable on its host port.
"""
if network not in self.container_networks(name):
return None
args = ["network", "disconnect"]
if force:
args.append("--force")
args.extend([network, name])
return self._run(args)
def volume_remove(self, name: str) -> ExecResult:
"""Remove a container volume, ignoring errors if it does not exist."""
return self._run(["volume", "rm", name], check=False)
def container_ip(self, name: str, network: str) -> str:
"""Return a container's IPv4 address on the given network, or "" if it is not attached."""
template = f'{{{{(index .NetworkSettings.Networks "{network}").IPAddress}}}}'
result = self._run(["inspect", "-f", template, name], check=False)
return result.stdout.strip() if result.ok else ""
def container_state(self, name: str) -> str:
"""Return a container's status and restart count ("running restarts=0"), or "" if unknown.
Handy in a timeout message: it distinguishes a container that is up but not yet
serving from one that has died or is stuck in a restart loop.
"""
result = self._run(
["inspect", "-f", "{{.State.Status}} restarts={{.RestartCount}}", name],
check=False,
)
return result.stdout.strip() if result.ok else ""
def container_exists(self, name: str) -> bool:
"""Return True if a container with the exact given name exists (running or stopped)."""
result = self._run(
["ps", "-a", "--filter", f"name=^{name}$", "--format", "{{.Names}}"],
check=False,
)
return result.ok and result.stdout.strip() == name