Skip to content

Commit

Permalink
ref(purge): Cleaning up purge network logic
Browse files Browse the repository at this point in the history
  • Loading branch information
IanWoodard committed Dec 23, 2024
1 parent 20a253f commit c944845
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
43 changes: 18 additions & 25 deletions devservices/commands/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from devservices.utils.console import Console
from devservices.utils.console import Status
from devservices.utils.docker import get_matching_containers
from devservices.utils.docker import get_matching_networks
from devservices.utils.docker import get_volumes_for_containers
from devservices.utils.docker import stop_containers
from devservices.utils.state import State
Expand Down Expand Up @@ -77,32 +78,24 @@ def purge(_args: Namespace) -> None:
console.failure(f"Failed to remove devservices volumes {e.stderr}")

console.warning("Removing any devservices networks")
devservices_networks = (
subprocess.check_output(
[
"docker",
"network",
"ls",
"--filter",
f"name={DOCKER_NETWORK_NAME}",
"--format",
"{{.ID}}",
],
text=True,
stderr=subprocess.DEVNULL,
)
.strip()
.splitlines()
)
try:
devservices_networks = get_matching_networks(DOCKER_NETWORK_NAME)
except DockerError as e:
console.failure(f"Failed to get devservices networks {e.stderr}")
exit(1)

Check warning on line 85 in devservices/commands/purge.py

View check run for this annotation

Codecov / codecov/patch

devservices/commands/purge.py#L83-L85

Added lines #L83 - L85 were not covered by tests
if len(devservices_networks) == 0:
console.success("No devservices networks found to remove")
for network in devservices_networks:
subprocess.run(
["docker", "network", "rm", network],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
console.success(f"Network {network} removed")
else:
try:
subprocess.run(
["docker", "network", "rm", *devservices_networks],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
console.success("All devservices networks removed")
except subprocess.CalledProcessError as e:
console.failure(f"Failed to remove devservices networks {e.stderr}")
exit(1)

Check warning on line 99 in devservices/commands/purge.py

View check run for this annotation

Codecov / codecov/patch

devservices/commands/purge.py#L97-L99

Added lines #L97 - L99 were not covered by tests

console.success("The local devservices cache and state has been purged")
32 changes: 32 additions & 0 deletions devservices/utils/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ def get_matching_containers(label: str) -> list[str]:
) from e


def get_matching_networks(name: str) -> list[str]:
"""
Returns a list of network IDs with the given name
"""
check_docker_daemon_running()
try:
return (
subprocess.check_output(
[
"docker",
"network",
"ls",
"--filter",
f"name={name}",
"--format",
"{{.ID}}",
],
text=True,
stderr=subprocess.DEVNULL,
)
.strip()
.splitlines()
)
except subprocess.CalledProcessError as e:
raise DockerError(

Check warning on line 76 in devservices/utils/docker.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker.py#L75-L76

Added lines #L75 - L76 were not covered by tests
command=f"docker network ls --filter name={name} --format '{{.ID}}'",
returncode=e.returncode,
stdout=e.stdout,
stderr=e.stderr,
) from e


def get_volumes_for_containers(containers: list[str]) -> set[str]:
"""
Returns a set of volume names for the given containers.
Expand Down
14 changes: 1 addition & 13 deletions tests/commands/test_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,7 @@ def test_purge_with_cache_and_state_and_running_containers_with_networks_and_vol
stderr=mock.ANY,
),
mock.call(
["docker", "network", "rm", "abc"],
check=True,
stdout=mock.ANY,
stderr=mock.ANY,
),
mock.call(
["docker", "network", "rm", "def"],
check=True,
stdout=mock.ANY,
stderr=mock.ANY,
),
mock.call(
["docker", "network", "rm", "ghe"],
["docker", "network", "rm", "abc", "def", "ghe"],
check=True,
stdout=mock.ANY,
stderr=mock.ANY,
Expand Down

0 comments on commit c944845

Please sign in to comment.