Skip to content

Commit 18535a2

Browse files
hack/build-image: add --archive action
Add an --archive=LOCATION command line argument that is somewhat like push but instead uses the archive tarballs. This could be use in a situation where you are building on a fast machine or different arch than the machine you will ultimately be pushing from. Signed-off-by: John Mulligan <[email protected]>
1 parent 63ea608 commit 18535a2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

hack/build-image

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Usage:
3636
"""
3737

3838
import argparse
39+
import functools
3940
import logging
4041
import os
4142
import pathlib
@@ -304,6 +305,16 @@ def container_id(cli, target):
304305
return res.stdout.decode("utf8").strip()
305306

306307

308+
def maybe_container_id(cli, target):
309+
"""Same as container_id but returns None if no match for the target was
310+
found.
311+
"""
312+
try:
313+
return container_id(cli, target)
314+
except subprocess.CalledProcessError:
315+
return None
316+
317+
307318
def container_tag(cli, target, tag, *tags):
308319
"""Add additional tags to the existing target image."""
309320
if isinstance(target, str):
@@ -552,6 +563,20 @@ def push(cli, target):
552563
container_push(cli, push_name)
553564

554565

566+
def archive(cli, target, location):
567+
"""Write tarballs to archive location."""
568+
# reuse push_stage flag. TODO rename flag
569+
if cli.push_state == "rebuild" or (
570+
cli.push_state == "exists" and not maybe_container_id(cli, target)
571+
):
572+
build(cli, target)
573+
574+
eng = container_engine(cli)
575+
fname = pathlib.Path(location) / f"{target.flat_name()}.tar"
576+
args = [eng, "save", f"-o{fname}", target.image_name()]
577+
run(cli, args, check=True)
578+
579+
555580
def retag(cli, target):
556581
"""Command to regenerate any missing unqualified tags."""
557582
cid = container_id(cli, target)
@@ -590,6 +615,16 @@ def print_tags(cli, target):
590615
print(f"{prefix}{name}")
591616

592617

618+
def _kwbind(func, key, conversion=None):
619+
"""Attach an argument value to a command-linked argument."""
620+
621+
def _capture(arg_value):
622+
value = conversion(arg_value) if conversion else arg_value
623+
return functools.partial(func, **{key: value})
624+
625+
return _capture
626+
627+
593628
def main():
594629
parser = argparse.ArgumentParser()
595630
parser.add_argument(
@@ -757,6 +792,13 @@ def main():
757792
" for a given FQIN. Requires FQIN to already exist locally."
758793
),
759794
)
795+
behaviors.add_argument(
796+
"--archive",
797+
metavar="LOCATION",
798+
dest="main_action",
799+
type=_kwbind(archive, "location"),
800+
help="Write archive files (tarballs) to a specified location",
801+
)
760802
cli = parser.parse_args()
761803

762804
if os.environ.get("BUILD_IMAGE_DEBUG") in ("1", "yes"):

0 commit comments

Comments
 (0)