ansible-test - Fix container error handling. (#78739)

pull/76313/merge
Matt Clay 2 years ago committed by GitHub
parent 2e5e8026cc
commit 79f67ed561
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
bugfixes:
- ansible-test - Always remove containers after failing to create/run them.
This avoids leaving behind created containers when using podman.

@ -139,7 +139,7 @@ def run_support_container(
if current_container_id:
publish_ports = False # publishing ports is pointless if already running in a docker container
options = (options or []) + ['--name', name]
options = (options or [])
if start:
options.append('-d')
@ -183,7 +183,7 @@ def run_support_container(
else:
display.info('Starting new "%s" container.' % name)
docker_pull(args, image)
support_container_id = docker_run(args, image, options, create_only=not start, cmd=cmd)
support_container_id = docker_run(args, image, name, options, create_only=not start, cmd=cmd)
running = start
existing = False

@ -287,13 +287,14 @@ def docker_cp_to(args: EnvironmentConfig, container_id: str, src: str, dst: str)
def docker_run(
args: EnvironmentConfig,
image: str,
name: str,
options: t.Optional[list[str]],
cmd: t.Optional[list[str]] = None,
create_only: bool = False,
) -> str:
"""Run a container using the given docker image."""
if not options:
options = []
options = list(options or [])
options.extend(['--name', name])
if not cmd:
cmd = []
@ -322,6 +323,7 @@ def docker_run(
except SubprocessError as ex:
display.error(ex.message)
display.warning('Failed to run docker image "%s". Waiting a few seconds before trying again.' % image)
docker_rm(args, name) # podman doesn't remove containers after create if run fails
time.sleep(3)
raise ApplicationError('Failed to run docker image "%s".' % image)

Loading…
Cancel
Save