From c7626dfa76d633465ed016fff9de657d00667640 Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 8 Apr 2025 20:25:37 +0800 Subject: [PATCH 1/5] Handle exit code when compose up -d --- podman_compose.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index 015880d..8f54eca 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2760,11 +2760,16 @@ def deps_from_container(args, cnt): @cmd_run(podman_compose, "up", "Create and start the entire stack or some of its services") async def compose_up(compose: PodmanCompose, args): excluded = get_excluded(compose, args) + exit_code = 0 + if not args.no_build: # `podman build` does not cache, so don't always build build_args = argparse.Namespace(if_not_exists=(not args.build), **args.__dict__) - if await compose.commands["build"](compose, build_args) != 0: + build_result = await compose.commands["build"](compose, build_args) + if build_result != 0: log.error("Build command failed") + if not args.dry_run: + return build_result hashes = ( ( @@ -2802,12 +2807,21 @@ async def compose_up(compose: PodmanCompose, args): compose, cnt, detached=args.detach, no_deps=args.no_deps ) subproc = await compose.podman.run([], podman_command, podman_args) + if subproc is not None and subproc != 0: + exit_code = subproc + if podman_command == "run" and subproc is not None: - await run_container( + container_result = await run_container( compose, cnt["name"], deps_from_container(args, cnt), ([], "start", [cnt["name"]]) ) - if args.no_start or args.detach or args.dry_run: - return + if container_result is not None and container_result != 0: + exit_code = container_result + + if args.dry_run: + return None + if args.no_start or args.detach: + return exit_code + # TODO: handle already existing # TODO: if error creating do not enter loop # TODO: colors if sys.stdout.isatty() From a3c2a4efc4655a2dd94a8a3026feb4211ecd49a9 Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 8 Apr 2025 21:24:03 +0800 Subject: [PATCH 2/5] Fix unittest --- .../in_pod/test_podman_compose_in_pod.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/integration/in_pod/test_podman_compose_in_pod.py b/tests/integration/in_pod/test_podman_compose_in_pod.py index 88b1a40..b83976e 100644 --- a/tests/integration/in_pod/test_podman_compose_in_pod.py +++ b/tests/integration/in_pod/test_podman_compose_in_pod.py @@ -96,7 +96,7 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin): ] try: - out, err = self.run_subprocess_assert_returncode(command_up) + out, err = self.run_subprocess_assert_returncode(command_up, 125) self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True) finally: @@ -221,7 +221,7 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin): ] try: - out, err = self.run_subprocess_assert_returncode(command_up) + out, err = self.run_subprocess_assert_returncode(command_up, 125) self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True) finally: @@ -255,7 +255,7 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin): ] try: - out, err = self.run_subprocess_assert_returncode(command_up) + out, err = self.run_subprocess_assert_returncode(command_up, 125) self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True) finally: @@ -334,7 +334,7 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin): ] try: - out, err = self.run_subprocess_assert_returncode(command_up) + out, err = self.run_subprocess_assert_returncode(command_up, 125) self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True) finally: @@ -368,7 +368,7 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin): ] try: - out, err = self.run_subprocess_assert_returncode(command_up) + out, err = self.run_subprocess_assert_returncode(command_up, 125) self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True) finally: @@ -402,7 +402,7 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin): ] try: - out, err = self.run_subprocess_assert_returncode(command_up) + out, err = self.run_subprocess_assert_returncode(command_up, 125) self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True) finally: @@ -482,7 +482,7 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin): ] try: - out, err = self.run_subprocess_assert_returncode(command_up) + out, err = self.run_subprocess_assert_returncode(command_up, 125) self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True) finally: From 5cce7ed015ee72ab826bf80d5e6895eca276c8fd Mon Sep 17 00:00:00 2001 From: Elsa Date: Wed, 9 Apr 2025 08:46:24 +0800 Subject: [PATCH 3/5] Format --- podman_compose.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index 8f54eca..0db1104 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2761,7 +2761,7 @@ def deps_from_container(args, cnt): async def compose_up(compose: PodmanCompose, args): excluded = get_excluded(compose, args) exit_code = 0 - + if not args.no_build: # `podman build` does not cache, so don't always build build_args = argparse.Namespace(if_not_exists=(not args.build), **args.__dict__) @@ -2809,14 +2809,14 @@ async def compose_up(compose: PodmanCompose, args): subproc = await compose.podman.run([], podman_command, podman_args) if subproc is not None and subproc != 0: exit_code = subproc - + if podman_command == "run" and subproc is not None: container_result = await run_container( compose, cnt["name"], deps_from_container(args, cnt), ([], "start", [cnt["name"]]) ) if container_result is not None and container_result != 0: exit_code = container_result - + if args.dry_run: return None if args.no_start or args.detach: From 188989c02fdb3af7ad567ff781e13e86f30dfaf4 Mon Sep 17 00:00:00 2001 From: Elsa Date: Wed, 9 Apr 2025 08:48:06 +0800 Subject: [PATCH 4/5] Rename variables related to exit_code --- podman_compose.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index 0db1104..69f3cbc 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2765,11 +2765,11 @@ async def compose_up(compose: PodmanCompose, args): if not args.no_build: # `podman build` does not cache, so don't always build build_args = argparse.Namespace(if_not_exists=(not args.build), **args.__dict__) - build_result = await compose.commands["build"](compose, build_args) - if build_result != 0: + build_exit_code = await compose.commands["build"](compose, build_args) + if build_exit_code != 0: log.error("Build command failed") if not args.dry_run: - return build_result + return build_exit_code hashes = ( ( @@ -2806,16 +2806,16 @@ async def compose_up(compose: PodmanCompose, args): podman_args = await container_to_args( compose, cnt, detached=args.detach, no_deps=args.no_deps ) - subproc = await compose.podman.run([], podman_command, podman_args) - if subproc is not None and subproc != 0: - exit_code = subproc + subproc_exit_code = await compose.podman.run([], podman_command, podman_args) + if subproc_exit_code is not None and subproc_exit_code != 0: + exit_code = subproc_exit_code - if podman_command == "run" and subproc is not None: - container_result = await run_container( + if podman_command == "run" and subproc_exit_code is not None: + container_exit_code = await run_container( compose, cnt["name"], deps_from_container(args, cnt), ([], "start", [cnt["name"]]) ) - if container_result is not None and container_result != 0: - exit_code = container_result + if container_exit_code is not None and container_exit_code != 0: + exit_code = container_exit_code if args.dry_run: return None From aa998cd26673ea899a569243a4c893f29a74e4e6 Mon Sep 17 00:00:00 2001 From: Elsa Date: Wed, 9 Apr 2025 08:51:39 +0800 Subject: [PATCH 5/5] Move variable location --- podman_compose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/podman_compose.py b/podman_compose.py index 69f3cbc..45d777f 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2760,7 +2760,6 @@ def deps_from_container(args, cnt): @cmd_run(podman_compose, "up", "Create and start the entire stack or some of its services") async def compose_up(compose: PodmanCompose, args): excluded = get_excluded(compose, args) - exit_code = 0 if not args.no_build: # `podman build` does not cache, so don't always build @@ -2799,6 +2798,7 @@ async def compose_up(compose: PodmanCompose, args): podman_command = "run" if args.detach and not args.no_start else "create" await create_pods(compose, args) + exit_code = 0 for cnt in compose.containers: if cnt["_service"] in excluded: log.debug("** skipping: %s", cnt["name"])