Skip to content

Commit

Permalink
Add fake "podman network exists" command
Browse files Browse the repository at this point in the history
Motivation:

podman-compose uses the "podman network exists" command to avoid
creating the same network twice.  This command was added with podman
v3.1.0.

Debian stable has an older version of podman (v3.0.1) that doesn't
support the "podman network exists" command.

A symptom of this problem is podman-compose failing with lines like:

    subprocess.CalledProcessError: Command '['podman', 'network', 'exists', 'scicatlive_default']' returned non-zero exit status 125.

    During handling of the above exception, another exception occurred:

    [...]
    subprocess.CalledProcessError: Command '['podman', 'network', 'create', '--labelect=scicatlive', 'scicatlive_default']' returned non-zero exit status 125.

Modification:

Abstract the two places where podman-compose checks if a network already
exists.  This is now handled by a specific method.

Check the podman version.  If the podman version is earlier than v3.1.0
then simulate the "podman network exists" command by parsing the output
from "podman network ls", otherwise simply call the "podman network
exists" command directly.

Result:

podman-compose is now able to create a network with versions of podman
before v3.1.0.

Signed-off-by: Paul Millar <[email protected]>
  • Loading branch information
paulmillar committed Jan 4, 2023
1 parent 08ffcf6 commit 1d1bae9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
21 changes: 19 additions & 2 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from threading import Thread

import shlex
import semantic_version as sv

try:
from shlex import quote as cmd_quote
Expand Down Expand Up @@ -669,6 +670,22 @@ def norm_ports(ports_in):
ports_out.append(port)
return ports_out

def assert_network_exists(compose, net_name):
podman_version = sv.Version(compose.podman_version)
if sv.SimpleSpec('<3.1.0').match(podman_version):
output = compose.podman.output([], "network", ["ls"])
offset = None
for raw_line in output.splitlines():
line = raw_line.decode("utf-8")
if not offset:
offset = line.find("VERSION")
else:
this_net_name = line[:offset].rstrip()
if (this_net_name == net_name):
return ''
raise subprocess.CalledProcessError(1, "docker network exists " + net_name)
else:
return compose.podman.output([], "network", ["exists", net_name])

def assert_cnt_nets(compose, cnt):
"""
Expand All @@ -693,7 +710,7 @@ def assert_cnt_nets(compose, cnt):
ext_desc.get("name", None) or net_desc.get("name", None) or default_net_name
)
try:
compose.podman.output([], "network", ["exists", net_name])
assert_network_exists(compose, net_name)
except subprocess.CalledProcessError as e:
if is_ext:
raise RuntimeError(
Expand Down Expand Up @@ -735,7 +752,7 @@ def assert_cnt_nets(compose, cnt):
args.extend(("--gateway", gateway))
args.append(net_name)
compose.podman.output([], "network", args)
compose.podman.output([], "network", ["exists", net_name])
assert_network_exists(compose, net_name)


def get_net_args(compose, cnt):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

pyyaml
python-dotenv
semantic-version

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
install_requires=[
"pyyaml",
"python-dotenv",
"semantic-version",
],
# test_suite='tests',
# tests_require=[
Expand Down

0 comments on commit 1d1bae9

Please sign in to comment.