From 822b0f20462c728fa054925acb061b828fd55885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Wed, 14 Aug 2024 13:17:43 +0200 Subject: [PATCH] bib: make build the default subcommand Prior this commit, the bootc-image-builder container image had a custom entrypoint that hardcoded the use of the build subcommand. This meant that if a user wanted to use a different subcommand, they had to overwrite the entrypoint. This commit changes the cobra code in bib to fallback to build if no subcommand was given. This is slighly ugly, but it allows us to remove the custom entrypoint, streamlining the use of subcommands. Let's see an example of calling the version subcommand: Before: podman run --rm -it --entrypoint=/usr/bin/bootc-image-builder \ quay.io/centos-bootc/bootc-image-builder:latest version After: sudo podman run --rm -it \ quay.io/centos-bootc/bootc-image-builder:latest version Kudos to https://github.com/IKukhta for his code from https://github.com/spf13/cobra/issues/823#issuecomment-870027246 --- Containerfile | 3 +-- README.md | 3 +-- bib/cmd/bootc-image-builder/main.go | 8 ++++++++ devel/Containerfile | 3 +-- entrypoint.sh | 5 ----- test/test_manifest.py | 16 ++-------------- test/test_opts.py | 2 -- 7 files changed, 13 insertions(+), 27 deletions(-) delete mode 100755 entrypoint.sh diff --git a/Containerfile b/Containerfile index 5b9fb3d46..8e0d35c8f 100644 --- a/Containerfile +++ b/Containerfile @@ -16,10 +16,9 @@ COPY ./group_osbuild-osbuild-fedora.repo /etc/yum.repos.d/ COPY ./package-requires.txt . RUN grep -vE '^#' package-requires.txt | xargs dnf install -y && rm -f package-requires.txt && dnf clean all COPY --from=builder /build/bin/* /usr/bin/ -COPY entrypoint.sh / COPY bib/data /usr/share/bootc-image-builder -ENTRYPOINT ["/entrypoint.sh"] +ENTRYPOINT ["/usr/bin/bootc-image-builder"] VOLUME /output WORKDIR /output VOLUME /store diff --git a/README.md b/README.md index 1e7b314f1..0e8ddd748 100644 --- a/README.md +++ b/README.md @@ -473,8 +473,7 @@ The contents of the file `$(pwd)/wheel-passwordless-sudo` should be Please report bugs to the [Bug Tracker](https://github.com/osbuild/bootc-image-builder/issues) and include instructions to reproduce and the output of: ``` -$ sudo podman run --rm -it --entrypoint=/usr/bin/bootc-image-builder \ - quay.io/centos-bootc/bootc-image-builder:latest version +$ sudo podman run --rm -it quay.io/centos-bootc/bootc-image-builder:latest version ``` ## 📊 Project diff --git a/bib/cmd/bootc-image-builder/main.go b/bib/cmd/bootc-image-builder/main.go index 0fe0bd3f1..db1f2f4fc 100644 --- a/bib/cmd/bootc-image-builder/main.go +++ b/bib/cmd/bootc-image-builder/main.go @@ -13,6 +13,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/pflag" "golang.org/x/exp/slices" "github.com/osbuild/images/pkg/arch" @@ -638,6 +639,13 @@ func run() error { } buildCmd.MarkFlagsRequiredTogether("aws-region", "aws-bucket", "aws-ami-name") + // If no subcommand is given, assume the user wants to use the build subcommand + // See https://github.com/spf13/cobra/issues/823#issuecomment-870027246 + cmd, _, err := rootCmd.Find(os.Args[1:]) + if err == nil && cmd.Use == rootCmd.Use && cmd.Flags().Parse(os.Args[1:]) != pflag.ErrHelp { + args := append([]string{buildCmd.Use}, os.Args[1:]...) + rootCmd.SetArgs(args) + } return rootCmd.Execute() } diff --git a/devel/Containerfile b/devel/Containerfile index a3a1a8797..044213934 100644 --- a/devel/Containerfile +++ b/devel/Containerfile @@ -27,9 +27,8 @@ COPY --from=osbuild-builder /build/rpmbuild/RPMS/noarch/*.rpm /rpms/ COPY ./package-requires.txt . RUN grep -vE '^#' package-requires.txt | xargs dnf install -y && rm -f package-requires.txt && dnf install -y /rpms/*.rpm && dnf clean all COPY --from=bib-builder /build/bin/bootc-image-builder /usr/bin/bootc-image-builder -COPY entrypoint.sh /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] +ENTRYPOINT ["/usr/bin/bootc-image-builder"] VOLUME /output WORKDIR /output VOLUME /store diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100755 index a2d9290c5..000000000 --- a/entrypoint.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -euo pipefail -# TODO: This script only exists for legacy reasons, the plan is to start requiring -# a e.g. `build-image` entrypoint. -/usr/bin/bootc-image-builder build "$@" diff --git a/test/test_manifest.py b/test/test_manifest.py index a86851220..be92fde8f 100644 --- a/test/test_manifest.py +++ b/test/test_manifest.py @@ -33,7 +33,6 @@ def find_image_size_from(manifest_str): def test_manifest_smoke(build_container, tc): output = subprocess.check_output([ *testutil.podman_run_common, - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", *tc.bib_rootfs_args(), @@ -53,7 +52,6 @@ def test_manifest_smoke(build_container, tc): def test_iso_manifest_smoke(build_container, tc): output = subprocess.check_output([ *testutil.podman_run_common, - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", "--type=anaconda-iso", f"{tc.container_ref}", @@ -82,7 +80,6 @@ def test_manifest_disksize(tmp_path, build_container, tc): print(f"using {container_tag}") manifest_str = subprocess.check_output([ *testutil.podman_run_common, - "--entrypoint", "/usr/bin/bootc-image-builder", build_container, "manifest", "--local", *tc.bib_rootfs_args(), @@ -102,7 +99,6 @@ def test_manifest_local_checks_containers_storage_errors(build_container): "podman", "run", "--rm", "--privileged", "--security-opt", "label=type:unconfined_t", - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", "--local", "arg-not-used", ], check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf8") @@ -121,7 +117,6 @@ def test_manifest_local_checks_containers_storage_works(tmp_path, build_containe with make_container(tmp_path) as container_tag: subprocess.run([ *testutil.podman_run_common, - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", "--local", *tc.bib_rootfs_args(), @@ -141,7 +136,6 @@ def test_manifest_cross_arch_check(tmp_path, build_container): with pytest.raises(subprocess.CalledProcessError) as exc: subprocess.run([ *testutil.podman_run_common, - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", "--target-arch=aarch64", "--local", f"localhost/{container_tag}" @@ -165,7 +159,6 @@ def test_manifest_rootfs_respected(build_container, tc): # TODO: derive container and fake "bootc install print-configuration"? output = subprocess.check_output([ *testutil.podman_run_common, - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", f"{tc.container_ref}", ]) @@ -183,7 +176,6 @@ def test_manifest_rootfs_override(build_container): output = subprocess.check_output([ *testutil.podman_run_common, - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", "--rootfs", "btrfs", f"{container_ref}", ]) @@ -216,7 +208,6 @@ def test_manifest_user_customizations_toml(tmp_path, build_container): output = subprocess.check_output([ *testutil.podman_run_common, "-v", f"{config_toml_path}:/config.toml:ro", - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", f"{container_ref}", ]) @@ -243,7 +234,6 @@ def test_manifest_installer_customizations(tmp_path, build_container): output = subprocess.check_output([ *testutil.podman_run_common, "-v", f"{config_toml_path}:/config.toml:ro", - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", "--type=anaconda-iso", f"{container_ref}", ]) @@ -297,7 +287,6 @@ def test_mount_ostree_error(tmpdir_factory, build_container): subprocess.check_output([ *testutil.podman_run_common, "-v", f"{output_path}:/output", - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", f"{container_ref}", "--config", "/output/config.json", @@ -316,8 +305,9 @@ def test_manifest_checks_build_container_is_bootc(build_container, container_ref def check_image_ref(): subprocess.check_output([ *testutil.podman_run_common, - f'--entrypoint=["/usr/bin/bootc-image-builder", "manifest", "{container_ref}"]', build_container, + "manifest", + container_ref, ], stderr=subprocess.PIPE, encoding="utf8") if should_error: with pytest.raises(subprocess.CalledProcessError) as exc: @@ -332,7 +322,6 @@ def test_manifest_target_arch_smoke(build_container, tc): # TODO: actually build an image too output = subprocess.check_output([ *testutil.podman_run_common, - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", *tc.bib_rootfs_args(), @@ -385,7 +374,6 @@ def test_manifest_anaconda_module_customizations(tmpdir_factory, build_container output = subprocess.check_output([ *testutil.podman_run_common, "-v", f"{output_path}:/output", - "--entrypoint=/usr/bin/bootc-image-builder", build_container, "manifest", "--config", "/output/config.json", diff --git a/test/test_opts.py b/test/test_opts.py index e95ec22ae..188b4f1fd 100644 --- a/test/test_opts.py +++ b/test/test_opts.py @@ -122,7 +122,6 @@ def test_bib_help_hides_config(tmp_path, container_storage, build_fake_container "--security-opt", "label=type:unconfined_t", "-v", f"{container_storage}:/var/lib/containers/storage", "-v", f"{output_path}:/output", - "--entrypoint=/usr/bin/bootc-image-builder", build_fake_container, "manifest", "--help", ], check=True, capture_output=True, text=True) @@ -159,7 +158,6 @@ def test_bib_version(tmp_path, container_storage, build_fake_container): "--security-opt", "label=type:unconfined_t", "-v", f"{container_storage}:/var/lib/containers/storage", "-v", f"{output_path}:/output", - "--entrypoint=/usr/bin/bootc-image-builder", build_fake_container, "version", ], check=True, capture_output=True, text=True)