Last active
November 7, 2020 04:10
-
-
Save omaskery/bd81ccbdcb027b31794e521b6016ebc5 to your computer and use it in GitHub Desktop.
Script for waiting for a docker compose to exit, before checking all services exited cleanly, returning 0 if so and 1 if any returned non-zero.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess | |
import json | |
import sys | |
class Colours: | |
Green = '\033[92m' | |
Amber = '\033[93m' | |
Red = '\033[91m' | |
Reset = '\033[0m' | |
def main(): | |
ids = subprocess.check_output(["docker-compose", "ps", "-q"]).strip().splitlines() | |
print(f"{Colours.Amber}waiting for {len(ids)} containers to exit...{Colours.Reset}") | |
subprocess.check_call(["docker", "wait", *ids], stdout=subprocess.DEVNULL) | |
status = json.loads(subprocess.check_output(["docker", "inspect", *ids])) | |
failure = False | |
print("outcome:") | |
for entry in status: | |
name = entry["Name"] | |
exit_code = entry["State"]["ExitCode"] | |
if exit_code != 0: | |
failure = True | |
colour = Colours.Green if exit_code == 0 else Colours.Red | |
print(f"{colour} - {name} exited with {exit_code}{Colours.Reset}") | |
if failure: | |
print("non-zero exit(s) detected, reporting failure") | |
sys.exit(0 if not failure else 1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment