Skip to content

Commit

Permalink
fix: Improved check for the PodmanConnectionError
Browse files Browse the repository at this point in the history
Signed-off-by: Kanishk Pachauri <itskanishkp.py@gmail.com>
  • Loading branch information
Mr-Sunglasses committed Feb 16, 2025
1 parent cdbdf0d commit e56d71a
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions podman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,38 @@ def __init__(self, **kwargs) -> None:
super().__init__()
config = PodmanConfig()

api_kwargs = kwargs.copy()
self.api_kwargs = kwargs.copy()

if "connection" in api_kwargs:
connection = config.services[api_kwargs.get("connection")]
api_kwargs["base_url"] = connection.url.geturl()
if "connection" in self.api_kwargs:
connection = config.services[self.api_kwargs.get("connection")]
self.api_kwargs["base_url"] = connection.url.geturl()

# Override configured identity, if provided in arguments
api_kwargs["identity"] = kwargs.get("identity", str(connection.identity))
elif "base_url" not in api_kwargs:
self.api_kwargs["identity"] = kwargs.get("identity", str(connection.identity))
elif "base_url" not in self.api_kwargs:
path = str(Path(get_runtime_dir()) / "podman" / "podman.sock")
api_kwargs["base_url"] = "http+unix://" + path
self.api = APIClient(**api_kwargs)
self.api_kwargs["base_url"] = "http+unix://" + path

# Check if the connection to the Podman service is successful
try:
SystemManager(client=self.api).version()
self.api = APIClient(**self.api_kwargs)
response = self.api.get("_ping")

if response.status_code != 200:
raise PodmanConnectionError(
message=f"Unexpected response from Podman service: {response.status_code}",
environment=os.environ,
host=self.api_kwargs.get("base_url"),
original_error=None,
)
except PodmanConnectionError:
raise
except Exception as e:
error_msg = "Failed to connect to Podman service"
raise PodmanConnectionError(
message=error_msg,
message=f"Failed to connect to Podman service: {str(e)}",
environment=os.environ,
host=api_kwargs.get("base_url"),
host=self.api_kwargs.get("base_url"),
original_error=e,
)
) from e

def __enter__(self) -> "PodmanClient":
return self
Expand Down

0 comments on commit e56d71a

Please sign in to comment.