|
36 | 36 | """
|
37 | 37 |
|
38 | 38 | import argparse
|
| 39 | +import functools |
39 | 40 | import logging
|
40 | 41 | import os
|
41 | 42 | import pathlib
|
@@ -304,6 +305,16 @@ def container_id(cli, target):
|
304 | 305 | return res.stdout.decode("utf8").strip()
|
305 | 306 |
|
306 | 307 |
|
| 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 | + |
307 | 318 | def container_tag(cli, target, tag, *tags):
|
308 | 319 | """Add additional tags to the existing target image."""
|
309 | 320 | if isinstance(target, str):
|
@@ -552,6 +563,20 @@ def push(cli, target):
|
552 | 563 | container_push(cli, push_name)
|
553 | 564 |
|
554 | 565 |
|
| 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 | + |
555 | 580 | def retag(cli, target):
|
556 | 581 | """Command to regenerate any missing unqualified tags."""
|
557 | 582 | cid = container_id(cli, target)
|
@@ -590,6 +615,16 @@ def print_tags(cli, target):
|
590 | 615 | print(f"{prefix}{name}")
|
591 | 616 |
|
592 | 617 |
|
| 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 | + |
593 | 628 | def main():
|
594 | 629 | parser = argparse.ArgumentParser()
|
595 | 630 | parser.add_argument(
|
@@ -757,6 +792,13 @@ def main():
|
757 | 792 | " for a given FQIN. Requires FQIN to already exist locally."
|
758 | 793 | ),
|
759 | 794 | )
|
| 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 | + ) |
760 | 802 | cli = parser.parse_args()
|
761 | 803 |
|
762 | 804 | if os.environ.get("BUILD_IMAGE_DEBUG") in ("1", "yes"):
|
|
0 commit comments