Remove named volumes during "down -v"

Fixes containers#105

Signed-off-by: Luiz Carvalho <lucarval@redhat.com>
This commit is contained in:
Luiz Carvalho 2021-11-22 13:34:40 -05:00 committed by Muayyad alsadi
parent d38aeaa713
commit 5f40f4df31

View File

@ -820,6 +820,13 @@ class Podman:
time.sleep(sleep)
return p
def volume_inspect_all(self):
output = self.output(["volume", "inspect", "--all"]).decode('utf-8')
return json.loads(output)
def volume_rm(self, name):
return self.run(["volume", "rm", name])
def normalize_service(service):
for key in ("env_file", "security_opt"):
if key not in service: continue
@ -1414,6 +1421,12 @@ def compose_down(compose, args):
return
for pod in compose.pods:
compose.podman.run([], "pod", ["rm", pod["name"]], sleep=0)
if args.volumes:
volumes = compose.podman.volume_inspect_all()
for volume in volumes:
project = volume.get("Labels", {}).get("io.podman.compose.project")
if project == compose.project_name:
compose.podman.volume_rm(volume["Name"])
@cmd_run(podman_compose, 'ps', 'show status of containers')
def compose_ps(compose, args):
@ -1573,6 +1586,12 @@ def compose_up_parse(parser):
parser.add_argument("--exit-code-from", metavar='SERVICE', type=str, default=None,
help="Return the exit code of the selected service container. Implies --abort-on-container-exit.")
@cmd_parse(podman_compose, 'down')
def compose_down_parse(parser):
parser.add_argument("-v", "--volumes", action='store_true', default=False,
help="Remove named volumes declared in the `volumes` section of the Compose file and "
"anonymous volumes attached to containers.")
@cmd_parse(podman_compose, 'run')
def compose_run_parse(parser):
parser.add_argument("-d", "--detach", action='store_true',