mirror of
https://github.com/containers/podman-compose.git
synced 2025-05-18 21:20:53 +02:00
Add --abort-on-container-failure
Signed-off-by: gtebbutt <5956226+gtebbutt@users.noreply.github.com>
This commit is contained in:
parent
8bb43100b1
commit
083b95c860
@ -2890,9 +2890,20 @@ async def compose_up(compose: PodmanCompose, args):
|
|||||||
|
|
||||||
exit_code = 0
|
exit_code = 0
|
||||||
exiting = False
|
exiting = False
|
||||||
|
first_failed_task = None
|
||||||
|
|
||||||
while tasks:
|
while tasks:
|
||||||
done, tasks = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
|
done, tasks = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
|
||||||
if args.abort_on_container_exit:
|
|
||||||
|
if args.abort_on_container_failure and first_failed_task is None:
|
||||||
|
# Generally a single returned item when using asyncio.FIRST_COMPLETED, but that's not
|
||||||
|
# guaranteed. If multiple tasks finish at the exact same time the choice of which
|
||||||
|
# finished "first" is arbitrary
|
||||||
|
for t in done:
|
||||||
|
if t.result() != 0:
|
||||||
|
first_failed_task = t
|
||||||
|
|
||||||
|
if args.abort_on_container_exit or first_failed_task:
|
||||||
if not exiting:
|
if not exiting:
|
||||||
# If 2 containers exit at the exact same time, the cancellation of the other ones
|
# If 2 containers exit at the exact same time, the cancellation of the other ones
|
||||||
# cause the status to overwrite. Sleeping for 1 seems to fix this and make it match
|
# cause the status to overwrite. Sleeping for 1 seems to fix this and make it match
|
||||||
@ -2903,6 +2914,11 @@ async def compose_up(compose: PodmanCompose, args):
|
|||||||
t.cancel()
|
t.cancel()
|
||||||
t: Task
|
t: Task
|
||||||
exiting = True
|
exiting = True
|
||||||
|
if first_failed_task:
|
||||||
|
# Matches docker-compose behaviour, where the exit code of the task that triggered
|
||||||
|
# the cancellation is always propagated when aborting on failure
|
||||||
|
exit_code = first_failed_task.result()
|
||||||
|
else:
|
||||||
for t in done:
|
for t in done:
|
||||||
if t.get_name() == exit_code_from:
|
if t.get_name() == exit_code_from:
|
||||||
exit_code = t.result()
|
exit_code = t.result()
|
||||||
@ -3393,7 +3409,7 @@ def compose_up_parse(parser):
|
|||||||
"--detach",
|
"--detach",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Detached mode: Run container in the background, print new container name. \
|
help="Detached mode: Run container in the background, print new container name. \
|
||||||
Incompatible with --abort-on-container-exit.",
|
Incompatible with --abort-on-container-exit and --abort-on-container-failure.",
|
||||||
)
|
)
|
||||||
parser.add_argument("--no-color", action="store_true", help="Produce monochrome output.")
|
parser.add_argument("--no-color", action="store_true", help="Produce monochrome output.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@ -3434,7 +3450,14 @@ def compose_up_parse(parser):
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--abort-on-container-exit",
|
"--abort-on-container-exit",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Stops all containers if any container was stopped. Incompatible with -d.",
|
help="Stops all containers if any container was stopped. Incompatible with -d and "
|
||||||
|
"--abort-on-container-failure.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--abort-on-container-failure",
|
||||||
|
action="store_true",
|
||||||
|
help="Stops all containers if any container stops with a non-zero exit code. Incompatible "
|
||||||
|
"with -d and --abort-on-container-exit.",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-t",
|
"-t",
|
||||||
|
0
tests/integration/abort/__init__.py
Normal file
0
tests/integration/abort/__init__.py
Normal file
11
tests/integration/abort/docker-compose-fail-first.yaml
Normal file
11
tests/integration/abort/docker-compose-fail-first.yaml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
version: "3"
|
||||||
|
services:
|
||||||
|
sh1:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 1; exit 1"]
|
||||||
|
sh2:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 2; exit 0"]
|
||||||
|
sh3:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 3; exit 0"]
|
11
tests/integration/abort/docker-compose-fail-none.yaml
Normal file
11
tests/integration/abort/docker-compose-fail-none.yaml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
version: "3"
|
||||||
|
services:
|
||||||
|
sh1:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 1; exit 0"]
|
||||||
|
sh2:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 2; exit 0"]
|
||||||
|
sh3:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 3; exit 0"]
|
11
tests/integration/abort/docker-compose-fail-second.yaml
Normal file
11
tests/integration/abort/docker-compose-fail-second.yaml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
version: "3"
|
||||||
|
services:
|
||||||
|
sh1:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 1; exit 0"]
|
||||||
|
sh2:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 2; exit 1"]
|
||||||
|
sh3:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 3; exit 0"]
|
@ -0,0 +1,11 @@
|
|||||||
|
version: "3"
|
||||||
|
services:
|
||||||
|
sh1:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 1; exit 1"]
|
||||||
|
sh2:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 1; exit 0"]
|
||||||
|
sh3:
|
||||||
|
image: nopush/podman-compose-test
|
||||||
|
command: ["dumb-init", "/bin/busybox", "sh", "-c", "sleep 2; exit 0"]
|
76
tests/integration/abort/test_podman_compose_abort.py
Normal file
76
tests/integration/abort/test_podman_compose_abort.py
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
# SPDX-License-Identifier: GPL-2.0
|
||||||
|
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from tests.integration.test_utils import RunSubprocessMixin
|
||||||
|
from tests.integration.test_utils import podman_compose_path
|
||||||
|
from tests.integration.test_utils import test_path
|
||||||
|
|
||||||
|
|
||||||
|
def compose_yaml_path(failure_order):
|
||||||
|
return os.path.join(
|
||||||
|
test_path(),
|
||||||
|
"abort",
|
||||||
|
f"docker-compose-fail-{failure_order}.yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_abort_command(failure_order, abort_type):
|
||||||
|
return [
|
||||||
|
podman_compose_path(),
|
||||||
|
"-f",
|
||||||
|
compose_yaml_path(failure_order),
|
||||||
|
"up",
|
||||||
|
f"--abort-on-container-{abort_type}",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class TestComposeAbort(unittest.TestCase, RunSubprocessMixin):
|
||||||
|
def test_abort_on_exit_fail_first(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("first", "exit"),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_abort_on_failure_fail_first(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("first", "failure"),
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_abort_on_exit_fail_second(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("second", "exit"),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_abort_on_failure_fail_second(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("second", "failure"),
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_abort_on_exit_fail_simultaneous(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("simultaneous", "exit"),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_abort_on_failure_fail_simultaneous(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("simultaneous", "failure"),
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_abort_on_exit_fail_none(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("none", "exit"),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_abort_on_failure_fail_none(self):
|
||||||
|
self.run_subprocess_assert_returncode(
|
||||||
|
test_abort_command("none", "failure"),
|
||||||
|
0,
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user