From 529ad2d7772ced181d9273d4615d90c86ef3ba41 Mon Sep 17 00:00:00 2001 From: Rabi Mishra Date: Thu, 12 Sep 2019 10:53:48 +0530 Subject: [PATCH] Remove containers before removing associated storage We added --storage option in https://review.opendev.org/#/c/679793/ when removing containers with podman. However, it won't delete the exited containers, i.e only orphaned storage would be removed with it. So, we have to do both 'rm' and 'rm --storage' with podman to ensure that we don't get the container name already used error, Change-Id: I66ce37b19dd855e9798a7877494264f6a128d284 Depends-On: https://review.opendev.org/681989 Related-Bug: #1840691 --- common/container-puppet.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/common/container-puppet.py b/common/container-puppet.py index c0d90af42c..56102cd218 100755 --- a/common/container-puppet.py +++ b/common/container-puppet.py @@ -176,26 +176,31 @@ def rm_container(name): if cmd_stderr: log.debug(cmd_stderr) + def run_cmd(rm_cli_cmd): + subproc = subprocess.Popen(rm_cli_cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True) + cmd_stdout, cmd_stderr = subproc.communicate() + if cmd_stdout: + log.debug(cmd_stdout) + if cmd_stderr and \ + cmd_stderr != 'Error response from daemon: ' \ + 'No such container: {}\n'.format(name): + log.debug(cmd_stderr) + log.info('Removing container: %s' % name) rm_cli_cmd = [cli_cmd, 'rm'] - # --storage is used as a mitigation of + rm_cli_cmd.append(name) + run_cmd(rm_cli_cmd) + + # rm --storage is used as a mitigation of # https://github.com/containers/libpod/issues/3906 # Also look https://bugzilla.redhat.com/show_bug.cgi?id=1747885 if container_cli == 'podman': - rm_cli_cmd.extend(['--storage']) - rm_cli_cmd.append(name) - subproc = subprocess.Popen(rm_cli_cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True) - cmd_stdout, cmd_stderr = subproc.communicate() - if cmd_stdout: - log.debug(cmd_stdout) - if cmd_stderr and \ - cmd_stderr != 'Error response from daemon: ' \ - 'No such container: {}\n'.format(name): - log.debug(cmd_stderr) - + rm_storage_cli_cmd = [cli_cmd, 'rm', '--storage'] + rm_storage_cli_cmd.append(name) + run_cmd(rm_storage_cli_cmd) process_count = int(os.environ.get('PROCESS_COUNT', multiprocessing.cpu_count()))