Skip to content

Commit 07e97ee

Browse files
committed
Turn push and load into params for the build arg
1 parent e066ca0 commit 07e97ee

File tree

3 files changed

+39
-72
lines changed

3 files changed

+39
-72
lines changed

repo2docker/app.py

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -572,55 +572,6 @@ def initialize(self, *args, **kwargs):
572572
if self.volumes and not self.run:
573573
raise ValueError("Cannot mount volumes if container is not run")
574574

575-
def push_image(self):
576-
"""Push docker image to registry"""
577-
client = self.get_engine()
578-
# Build a progress setup for each layer, and only emit per-layer
579-
# info every 1.5s
580-
progress_layers = {}
581-
layers = {}
582-
last_emit_time = time.time()
583-
for chunk in client.push(self.output_image_spec):
584-
if client.string_output:
585-
self.log.info(chunk, extra=dict(phase=R2dState.PUSHING))
586-
continue
587-
# else this is Docker output
588-
589-
# each chunk can be one or more lines of json events
590-
# split lines here in case multiple are delivered at once
591-
for line in chunk.splitlines():
592-
line = line.decode("utf-8", errors="replace")
593-
try:
594-
progress = json.loads(line)
595-
except Exception as e:
596-
self.log.warning("Not a JSON progress line: %r", line)
597-
continue
598-
if "error" in progress:
599-
self.log.error(progress["error"], extra=dict(phase=R2dState.FAILED))
600-
raise ImageLoadError(progress["error"])
601-
if "id" not in progress:
602-
continue
603-
# deprecated truncated-progress data
604-
if "progressDetail" in progress and progress["progressDetail"]:
605-
progress_layers[progress["id"]] = progress["progressDetail"]
606-
else:
607-
progress_layers[progress["id"]] = progress["status"]
608-
# include full progress data for each layer in 'layers' data
609-
layers[progress["id"]] = progress
610-
if time.time() - last_emit_time > 1.5:
611-
self.log.info(
612-
"Pushing image\n",
613-
extra=dict(
614-
progress=progress_layers,
615-
layers=layers,
616-
phase=R2dState.PUSHING,
617-
),
618-
)
619-
last_emit_time = time.time()
620-
self.log.info(
621-
f"Successfully pushed {self.output_image_spec}",
622-
extra=dict(phase=R2dState.PUSHING),
623-
)
624575

625576
def run_image(self):
626577
"""Run docker container from built image
@@ -847,14 +798,20 @@ def build(self):
847798
extra=dict(phase=R2dState.BUILDING),
848799
)
849800

801+
extra_build_kwargs = self.extra_build_kwargs.copy()
802+
# Set "push" and "load" parameters in a backwards compat way, without
803+
# having to change the signature of every buildpack
804+
extra_build_kwargs["push"] = self.push
805+
extra_build_kwargs["load"] = self.run
806+
850807
for l in picked_buildpack.build(
851808
docker_client,
852809
self.output_image_spec,
853810
# This is deprecated, but passing it anyway to not break backwards compatibility
854811
self.build_memory_limit,
855812
build_args,
856813
self.cache_from,
857-
self.extra_build_kwargs,
814+
extra_build_kwargs,
858815
platform=self.platform,
859816
):
860817
if docker_client.string_output:
@@ -886,8 +843,5 @@ def build(self):
886843
def start(self):
887844
self.build()
888845

889-
if self.push:
890-
self.push_image()
891-
892846
if self.run:
893847
self.run_image()

repo2docker/docker.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
Docker container engine for repo2docker
33
"""
44

5+
from argparse import ArgumentError
56
import json
67
import os
78
import shutil
89
import subprocess
910
import tarfile
1011
import tempfile
11-
from contextlib import contextmanager
12+
from contextlib import ExitStack, contextmanager
1213
from pathlib import Path
1314

1415
from iso8601 import parse_date
@@ -89,6 +90,8 @@ class DockerEngine(ContainerEngine):
8990

9091
def build(
9192
self,
93+
push=False,
94+
load=False,
9295
*,
9396
buildargs=None,
9497
cache_from=None,
@@ -104,7 +107,15 @@ def build(
104107
):
105108
if not shutil.which("docker"):
106109
raise RuntimeError("The docker commandline client must be installed")
107-
args = ["docker", "buildx", "build", "--progress", "plain", "--load"]
110+
args = ["docker", "buildx", "build", "--progress", "plain"]
111+
if load:
112+
if push:
113+
raise ValueError("Setting push=True and load=True is currently not supported")
114+
args.append("--load")
115+
116+
if push:
117+
args.append("--push")
118+
108119
if buildargs:
109120
for k, v in buildargs.items():
110121
args += ["--build-arg", f"{k}={v}"]
@@ -129,19 +140,22 @@ def build(
129140
# place extra args right *before* the path
130141
args += self.extra_buildx_build_args
131142

132-
if fileobj:
133-
with tempfile.TemporaryDirectory() as d:
134-
tarf = tarfile.open(fileobj=fileobj)
135-
tarf.extractall(d)
143+
with ExitStack() as stack:
144+
if self.registry_credentials:
145+
stack.enter_context(self.docker_login(**self.registry_credentials))
146+
if fileobj:
147+
with tempfile.TemporaryDirectory() as d:
148+
tarf = tarfile.open(fileobj=fileobj)
149+
tarf.extractall(d)
136150

137-
args += [d]
151+
args += [d]
138152

139-
yield from execute_cmd(args, True)
140-
else:
141-
# Assume 'path' is passed in
142-
args += [path]
153+
yield from execute_cmd(args, True)
154+
else:
155+
# Assume 'path' is passed in
156+
args += [path]
143157

144-
yield from execute_cmd(args, True)
158+
yield from execute_cmd(args, True)
145159

146160
def inspect_image(self, image):
147161
"""
@@ -194,13 +208,6 @@ def docker_login(self, username, password, registry):
194208
else:
195209
del os.environ["DOCKER_CONFIG"]
196210

197-
def push(self, image_spec):
198-
if self.registry_credentials:
199-
with self.docker_login(**self.registry_credentials):
200-
yield from execute_cmd(["docker", "push", image_spec], capture=True)
201-
else:
202-
yield from execute_cmd(["docker", "push", image_spec], capture=True)
203-
204211
def run(
205212
self,
206213
image_spec,

repo2docker/engine.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ def __init__(self, *, parent):
212212

213213
def build(
214214
self,
215+
push=False,
216+
load=False,
215217
*,
216218
buildargs={},
217219
cache_from=[],
@@ -230,6 +232,10 @@ def build(
230232
231233
Parameters
232234
----------
235+
push: bool
236+
Push the resulting image to a registry
237+
load: bool
238+
Load the resulting image into the container store ready to be run
233239
buildargs : dict
234240
Dictionary of build arguments
235241
cache_from : list[str]

0 commit comments

Comments
 (0)