|
6 | 6 | from pathlib import Path |
7 | 7 | from typing import Any, Optional |
8 | 8 |
|
| 9 | +from podman.errors import PodmanConnectionError |
9 | 10 | from podman.api import cached_property |
10 | 11 | from podman.api.client import APIClient |
11 | 12 | from podman.api.path_utils import get_runtime_dir |
@@ -73,6 +74,18 @@ def __init__(self, **kwargs) -> None: |
73 | 74 | api_kwargs["base_url"] = "http+unix://" + path |
74 | 75 | self.api = APIClient(**api_kwargs) |
75 | 76 |
|
| 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 | + |
76 | 89 | def __enter__(self) -> "PodmanClient": |
77 | 90 | return self |
78 | 91 |
|
@@ -116,25 +129,45 @@ def from_env( |
116 | 129 | Raises: |
117 | 130 | ValueError when required environment variable is not set |
118 | 131 | """ |
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 | + ) |
138 | 171 |
|
139 | 172 | @cached_property |
140 | 173 | def containers(self) -> ContainersManager: |
|
0 commit comments