Skip to content

Commit 0e2b4d0

Browse files
committed
refactor(docker): cleanup
- Extract get_image_label_sha() helper, dedup docker inspect label extraction in pull_image and _get_image_relay_sha - Extract _print_indented() helper for show_docker_df and _dump_docker_logs - Remove redundant metavar="RELAY" and metavar="SERVICE" - Simplify prune: positional level arg instead of --deep/--all flags - Clarify ensure_docker() docstring (why nesting is set here)
1 parent 3c8627e commit 0e2b4d0

1 file changed

Lines changed: 32 additions & 36 deletions

File tree

src/cmlxc/driver_docker.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ def image_tag(sha):
4444
def ensure_docker(ct):
4545
"""Install Docker engine in container if not present.
4646
47-
Enables security.nesting (required for Docker-in-LXC)
48-
and restarts the container if needed.
47+
Sets security.nesting (required for Docker-in-LXC) unconditionally
48+
because this is also called from standalone subcommands (docker build,
49+
docker pull) where the container may not have been launched with
50+
DockerDriver.NESTING_CONFIG.
4951
"""
5052
if ct.bash("docker info >/dev/null 2>&1", check=False) is not None:
5153
return
@@ -204,13 +206,8 @@ def pull_image(ct, tag, out):
204206
out.red(f" Failed to pull {ref}")
205207
return None
206208
ct.bash(f"docker tag {ref} {DOCKER_IMAGE_TAG}:latest")
207-
sha = ct.bash(
208-
f"docker inspect {ref}"
209-
" --format '{{index .Config.Labels \"org.opencontainers.image.revision\"}}'",
210-
check=False,
211-
)
212-
if sha and sha.strip():
213-
sha = sha.strip()
209+
sha = get_image_label_sha(ct, ref)
210+
if sha:
214211
local_tag = image_tag(sha)
215212
ct.bash(f"docker tag {ref} {local_tag}")
216213
out.print(f" Tagged as {local_tag}")
@@ -219,6 +216,16 @@ def pull_image(ct, tag, out):
219216
return None
220217

221218

219+
def get_image_label_sha(ct, tag):
220+
"""Read the relay commit SHA from a Docker image's OCI labels."""
221+
sha = ct.bash(
222+
f"docker inspect {tag}"
223+
" --format '{{index .Config.Labels \"org.opencontainers.image.revision\"}}'",
224+
check=False,
225+
)
226+
return sha.strip() if sha and sha.strip() else None
227+
228+
222229
def auto_prune_images(bld_ct, out, keep=3):
223230
"""Keep newest ``keep`` chatmail-relay images, delete the rest."""
224231
raw = bld_ct.bash(
@@ -241,12 +248,17 @@ def auto_prune_images(bld_ct, out, keep=3):
241248
bld_ct.bash(f"docker rmi {DOCKER_IMAGE_TAG}:{tag}", check=False)
242249

243250

251+
def _print_indented(out, text):
252+
"""Print each line of *text* with two-space indent."""
253+
for line in text.strip().splitlines():
254+
out.print(f" {line}")
255+
256+
244257
def show_docker_df(bld_ct, out):
245258
"""Display docker disk usage summary from builder."""
246259
raw = bld_ct.bash("docker system df", check=False)
247260
if raw:
248-
for line in raw.strip().splitlines():
249-
out.print(f" {line}")
261+
_print_indented(out, raw)
250262

251263

252264
def prune_relay_containers(ix, level, out):
@@ -491,7 +503,6 @@ def ps_docker_cmd(args, out):
491503
def shell_docker_cmd_options(parser, completer=None):
492504
relay_arg = parser.add_argument(
493505
"relay",
494-
metavar="RELAY",
495506
help="Relay container name (e.g. dock0).",
496507
)
497508
if completer:
@@ -500,7 +511,6 @@ def shell_docker_cmd_options(parser, completer=None):
500511
"service",
501512
nargs="?",
502513
default=DOCKER_COMPOSE_SERVICE,
503-
metavar="SERVICE",
504514
help=f"Docker Compose service (default: {DOCKER_COMPOSE_SERVICE}).",
505515
)
506516
parser.add_argument(
@@ -571,17 +581,13 @@ def pull_docker_cmd(args, out):
571581

572582

573583
def prune_docker_cmd_options(parser):
574-
group = parser.add_mutually_exclusive_group()
575-
group.add_argument(
576-
"--deep",
577-
action="store_true",
578-
help="Also prune dangling build cache, unused volumes, and relay containers.",
579-
)
580-
group.add_argument(
581-
"--all",
582-
dest="prune_all",
583-
action="store_true",
584-
help="Remove ALL unused images, build cache, volumes, and relay resources.",
584+
parser.add_argument(
585+
"level",
586+
nargs="?",
587+
default="default",
588+
choices=["default", "deep", "all"],
589+
help="Prune level: default (old images), deep (+cache/volumes/relays),"
590+
" all (everything). Default: default.",
585591
)
586592
parser.add_argument(
587593
"--dry-run",
@@ -606,12 +612,7 @@ def prune_docker_cmd(args, out):
606612
if args.dry_run:
607613
return 0
608614

609-
if args.prune_all:
610-
level = "all"
611-
elif args.deep:
612-
level = "deep"
613-
else:
614-
level = "default"
615+
level = args.level
615616

616617
images = list_images(bld_ct)
617618
if images:
@@ -1051,12 +1052,7 @@ def _setup_docker_ssh_forwarding(self):
10511052

10521053
def _get_image_relay_sha(self):
10531054
"""Read the relay commit SHA from the running Docker image's OCI labels."""
1054-
sha = self.ct.bash(
1055-
f"docker inspect {DOCKER_IMAGE_TAG}:latest"
1056-
" --format '{{index .Config.Labels \"org.opencontainers.image.revision\"}}'",
1057-
check=False,
1058-
)
1059-
return sha.strip() if sha and sha.strip() else None
1055+
return get_image_label_sha(self.ct, f"{DOCKER_IMAGE_TAG}:latest")
10601056

10611057
def run_tests(self, second_domain=None):
10621058
"""Execute the cmdeploy test suite against the Docker relay.

0 commit comments

Comments
 (0)