Skip to content

Commit 556901a

Browse files
committed
feat: Add PodmanConnectionError check in client.py
Signed-off-by: Kanishk Pachauri <[email protected]>
1 parent c1a042a commit 556901a

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

podman/client.py

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
from typing import Any, Optional
88

9+
from podman.errors import PodmanConnectionError
910
from podman.api import cached_property
1011
from podman.api.client import APIClient
1112
from podman.api.path_utils import get_runtime_dir
@@ -73,6 +74,18 @@ def __init__(self, **kwargs) -> None:
7374
api_kwargs["base_url"] = "http+unix://" + path
7475
self.api = APIClient(**api_kwargs)
7576

77+
# Check if the connection to the Podman service is successful
78+
try:
79+
SystemManager(client=self.api).version()
80+
except Exception as e:
81+
error_msg = "Failed to connect to Podman service"
82+
raise PodmanConnectionError(
83+
message=error_msg,
84+
environment=os.environ,
85+
host=api_kwargs.get("base_url"),
86+
original_error=e,
87+
)
88+
7689
def __enter__(self) -> "PodmanClient":
7790
return self
7891

@@ -116,25 +129,45 @@ def from_env(
116129
Raises:
117130
ValueError when required environment variable is not set
118131
"""
119-
environment = environment or os.environ
120-
credstore_env = credstore_env or {}
121-
122-
if version == "auto":
123-
version = None
124-
125-
kwargs = {
126-
'version': version,
127-
'timeout': timeout,
128-
'tls': False,
129-
'credstore_env': credstore_env,
130-
'max_pool_size': max_pool_size,
131-
}
132-
133-
host = environment.get("CONTAINER_HOST") or environment.get("DOCKER_HOST") or None
134-
if host is not None:
135-
kwargs['base_url'] = host
136-
137-
return PodmanClient(**kwargs)
132+
try:
133+
environment = environment or os.environ
134+
credstore_env = credstore_env or {}
135+
136+
if version == "auto":
137+
version = None
138+
139+
kwargs = {
140+
"version": version,
141+
"timeout": timeout,
142+
"tls": False,
143+
"credstore_env": credstore_env,
144+
"max_pool_size": max_pool_size,
145+
}
146+
147+
host = (
148+
environment.get("CONTAINER_HOST")
149+
or environment.get("DOCKER_HOST")
150+
or None
151+
)
152+
if host is not None:
153+
kwargs["base_url"] = host
154+
155+
return PodmanClient(**kwargs)
156+
except ValueError as e:
157+
error_msg = "Invalid environment configuration for Podman client"
158+
raise PodmanConnectionError(
159+
message=error_msg, environment=environment, host=host, original_error=e
160+
)
161+
except (ConnectionError, TimeoutError) as e:
162+
error_msg = "Failed to connect to Podman service"
163+
raise PodmanConnectionError(
164+
message=error_msg, environment=environment, host=host, original_error=e
165+
)
166+
except Exception as e:
167+
error_msg = "Failed to initialize Podman client from environment"
168+
raise PodmanConnectionError(
169+
message=error_msg, environment=environment, host=host, original_error=e
170+
)
138171

139172
@cached_property
140173
def containers(self) -> ContainersManager:

0 commit comments

Comments
 (0)