From 72c3572123ff4748d4d19d0b6b63864dfa2076f1 Mon Sep 17 00:00:00 2001 From: Muayyad alsadi Date: Sun, 14 Nov 2021 00:59:41 +0200 Subject: [PATCH] #289: exit code and test for that --- podman_compose.py | 11 +++++++++-- tests/exit-from/README.md | 15 +++++++++++++++ tests/exit-from/docker-compose.yaml | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/exit-from/README.md create mode 100644 tests/exit-from/docker-compose.yaml diff --git a/podman_compose.py b/podman_compose.py index 9db255d..062ccb9 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -876,6 +876,8 @@ class Podman: return subprocess.check_output(cmd_ls) def run(self, podman_args, cmd='', cmd_args=None, wait=True, sleep=1, obj=None): + if obj is not None: + obj.exit_code = None cmd_args = list(map(str, cmd_args or [])) xargs = self.compose.get_podman_args(cmd) if cmd else [] cmd_ls = [self.podman_path, *podman_args, cmd] + xargs + cmd_args @@ -886,7 +888,7 @@ class Podman: p = subprocess.Popen(cmd_ls) if wait: exit_code = p.wait() - print(exit_code) + print("exit code:", exit_code) if obj is not None: obj.exit_code = exit_code @@ -1436,6 +1438,9 @@ def compose_up(compose, args): # TODO: handle already existing # TODO: if error creating do not enter loop # TODO: colors if sys.stdout.isatty() + exit_code_from = args.__dict__.get('exit_code_from', None) + if exit_code_from: + args.abort_on_container_exit=True threads = [] for cnt in compose.containers: @@ -1443,17 +1448,19 @@ def compose_up(compose, args): print("** skipping: ", cnt['name']) continue # TODO: remove sleep from podman.run - obj = compose if args.__dict__.get('exit_code_from', None) == cnt['name'] else None + obj = compose if exit_code_from == cnt['_service'] else None thread = Thread(target=compose.podman.run, args=[[], 'start', ['-a', cnt['name']]], kwargs={"obj":obj}, daemon=True, name=cnt['name']) thread.start() threads.append(thread) time.sleep(1) + while threads: for thread in threads: thread.join(timeout=1.0) if not thread.is_alive(): threads.remove(thread) if args.abort_on_container_exit: + time.sleep(1) exit_code = compose.exit_code if compose.exit_code is not None else -1 exit(exit_code) diff --git a/tests/exit-from/README.md b/tests/exit-from/README.md new file mode 100644 index 0000000..7f8cd30 --- /dev/null +++ b/tests/exit-from/README.md @@ -0,0 +1,15 @@ +We have service named sh1 that exits with code 1 and sh2 that exists with code 2 + +``` +podman-compose up --exit-code-from=sh1 +echo $? +``` + +the above should give 1. + +``` +podman-compose up --exit-code-from=sh2 +echo $? +``` + +the above should give 2. diff --git a/tests/exit-from/docker-compose.yaml b/tests/exit-from/docker-compose.yaml new file mode 100644 index 0000000..cfc3897 --- /dev/null +++ b/tests/exit-from/docker-compose.yaml @@ -0,0 +1,21 @@ +version: "3" +services: + too_long: + image: busybox + command: ["/bin/busybox", "sh", "-c", "sleep 3600; exit 0"] + tmpfs: + - /run + - /tmp + sh1: + image: busybox + command: ["/bin/busybox", "sh", "-c", "sleep 5; exit 1"] + tmpfs: + - /run + - /tmp + sh2: + image: busybox + command: ["/bin/busybox", "sh", "-c", "sleep 5; exit 2"] + tmpfs: + - /run + - /tmp +