| 1 |
"""Configuration for the test hosts requested by the user.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import abc |
| 6 |
import dataclasses |
| 7 |
import enum |
| 8 |
import os |
| 9 |
import pickle |
| 10 |
import sys |
| 11 |
import typing as t |
| 12 |
|
| 13 |
from .constants import ( |
| 14 |
SUPPORTED_PYTHON_VERSIONS, |
| 15 |
) |
| 16 |
|
| 17 |
from .io import ( |
| 18 |
open_binary_file, |
| 19 |
) |
| 20 |
|
| 21 |
from .completion import ( |
| 22 |
AuditMode, |
| 23 |
CGroupVersion, |
| 24 |
CompletionConfig, |
| 25 |
docker_completion, |
| 26 |
DockerCompletionConfig, |
| 27 |
InventoryCompletionConfig, |
| 28 |
network_completion, |
| 29 |
NetworkRemoteCompletionConfig, |
| 30 |
PosixCompletionConfig, |
| 31 |
PosixRemoteCompletionConfig, |
| 32 |
PosixSshCompletionConfig, |
| 33 |
remote_completion, |
| 34 |
RemoteCompletionConfig, |
| 35 |
windows_completion, |
| 36 |
WindowsRemoteCompletionConfig, |
| 37 |
filter_completion, |
| 38 |
) |
| 39 |
|
| 40 |
from .util import ( |
| 41 |
find_python, |
| 42 |
get_available_python_versions, |
| 43 |
str_to_version, |
| 44 |
version_to_str, |
| 45 |
Architecture, |
| 46 |
find_executable, |
| 47 |
get_supported_powershell_versions, |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
@dataclasses.dataclass(frozen=True) |
| 52 |
class OriginCompletionConfig(PosixCompletionConfig): |
| 53 |
"""Pseudo completion config for the origin.""" |
| 54 |
|
| 55 |
def __init__(self) -> None: |
| 56 |
super().__init__(name='origin') |
| 57 |
|
| 58 |
@property |
| 59 |
def supported_pythons(self) -> list[str]: |
| 60 |
"""Return a list of the supported Python versions.""" |
| 61 |
current_version = version_to_str(sys.version_info[:2]) |
| 62 |
versions = [version for version in SUPPORTED_PYTHON_VERSIONS if version == current_version] + \ |
| 63 |
[version for version in SUPPORTED_PYTHON_VERSIONS if version != current_version] |
| 64 |
return versions |
| 65 |
|
| 66 |
def get_python_path(self, version: str) -> str: |
| 67 |
"""Return the path of the requested Python version.""" |
| 68 |
version = find_python(version) |
| 69 |
return version |
| 70 |
|
| 71 |
@property |
| 72 |
def supported_powershells(self) -> list[str]: |
| 73 |
"""Return a list of the supported PowerShell versions.""" |
| 74 |
return get_supported_powershell_versions() |
| 75 |
|
| 76 |
def get_powershell_path(self, version: str | None) -> str | None: |
| 77 |
"""Return the path of the requested PowerShell version, or None if PowerShell is not available.""" |
| 78 |
return find_executable(f'pwsh{version or ""}', required=bool(version)) |
| 79 |
|
| 80 |
@property |
| 81 |
def is_default(self) -> bool: |
| 82 |
"""True if the completion entry is only used for defaults, otherwise False.""" |
| 83 |
return False |
| 84 |
|
| 85 |
|
| 86 |
@dataclasses.dataclass(frozen=True) |
| 87 |
class HostContext: |
| 88 |
"""Context used when getting and applying defaults for host configurations.""" |
| 89 |
|
| 90 |
controller_config: t.Optional['PosixConfig'] |
| 91 |
|
| 92 |
@property |
| 93 |
def controller(self) -> bool: |
| 94 |
"""True if the context is for the controller, otherwise False.""" |
| 95 |
return not self.controller_config |
| 96 |
|
| 97 |
|
| 98 |
@dataclasses.dataclass |
| 99 |
class HostConfig(metaclass=abc.ABCMeta): |
| 100 |
"""Base class for host configuration.""" |
| 101 |
|
| 102 |
@abc.abstractmethod |
| 103 |
def get_defaults(self, context: HostContext) -> CompletionConfig: |
| 104 |
"""Return the default settings.""" |
| 105 |
|
| 106 |
@abc.abstractmethod |
| 107 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 108 |
"""Apply default settings.""" |
| 109 |
|
| 110 |
@property |
| 111 |
def is_managed(self) -> bool: |
| 112 |
""" |
| 113 |
True if the host is a managed instance, otherwise False. |
| 114 |
Managed instances are used exclusively by ansible-test and can safely have destructive operations performed without explicit permission from the user. |
| 115 |
""" |
| 116 |
return False |
| 117 |
|
| 118 |
|
| 119 |
@dataclasses.dataclass |
| 120 |
class PythonConfig(metaclass=abc.ABCMeta): |
| 121 |
"""Configuration for Python.""" |
| 122 |
|
| 123 |
version: t.Optional[str] = None |
| 124 |
path: t.Optional[str] = None |
| 125 |
|
| 126 |
@property |
| 127 |
def tuple(self) -> tuple[int, ...]: |
| 128 |
"""Return the Python version as a tuple.""" |
| 129 |
return str_to_version(self.version) |
| 130 |
|
| 131 |
@property |
| 132 |
def major_version(self) -> int: |
| 133 |
"""Return the Python major version.""" |
| 134 |
return self.tuple[0] |
| 135 |
|
| 136 |
def apply_defaults(self, context: HostContext, defaults: PosixCompletionConfig) -> None: |
| 137 |
"""Apply default settings.""" |
| 138 |
if self.version in (None, 'default'): |
| 139 |
self.version = defaults.get_default_python(context.controller) |
| 140 |
|
| 141 |
if self.path: |
| 142 |
if self.path.endswith('/'): |
| 143 |
self.path = os.path.join(self.path, f'python{self.version}') |
| 144 |
|
| 145 |
# FUTURE: If the host is origin, the python path could be validated here. |
| 146 |
else: |
| 147 |
self.path = defaults.get_python_path(self.version) |
| 148 |
|
| 149 |
@property |
| 150 |
@abc.abstractmethod |
| 151 |
def is_managed(self) -> bool: |
| 152 |
""" |
| 153 |
True if this Python is a managed instance, otherwise False. |
| 154 |
Managed instances are used exclusively by ansible-test and can safely have requirements installed without explicit permission from the user. |
| 155 |
""" |
| 156 |
|
| 157 |
|
| 158 |
@dataclasses.dataclass |
| 159 |
class NativePythonConfig(PythonConfig): |
| 160 |
"""Configuration for native Python.""" |
| 161 |
|
| 162 |
@property |
| 163 |
def is_managed(self) -> bool: |
| 164 |
""" |
| 165 |
True if this Python is a managed instance, otherwise False. |
| 166 |
Managed instances are used exclusively by ansible-test and can safely have requirements installed without explicit permission from the user. |
| 167 |
""" |
| 168 |
return False |
| 169 |
|
| 170 |
|
| 171 |
@dataclasses.dataclass |
| 172 |
class VirtualPythonConfig(PythonConfig): |
| 173 |
"""Configuration for Python in a virtual environment.""" |
| 174 |
|
| 175 |
system_site_packages: t.Optional[bool] = None |
| 176 |
|
| 177 |
def apply_defaults(self, context: HostContext, defaults: PosixCompletionConfig) -> None: |
| 178 |
"""Apply default settings.""" |
| 179 |
super().apply_defaults(context, defaults) |
| 180 |
|
| 181 |
if self.system_site_packages is None: |
| 182 |
self.system_site_packages = False |
| 183 |
|
| 184 |
@property |
| 185 |
def is_managed(self) -> bool: |
| 186 |
""" |
| 187 |
True if this Python is a managed instance, otherwise False. |
| 188 |
Managed instances are used exclusively by ansible-test and can safely have requirements installed without explicit permission from the user. |
| 189 |
""" |
| 190 |
return True |
| 191 |
|
| 192 |
|
| 193 |
@dataclasses.dataclass |
| 194 |
class PowerShellConfig: |
| 195 |
"""Configuration for PowerShell.""" |
| 196 |
|
| 197 |
version: str | None = None |
| 198 |
path: str | None = None |
| 199 |
|
| 200 |
def apply_defaults(self, defaults: PosixCompletionConfig) -> None: |
| 201 |
"""Apply default settings.""" |
| 202 |
if self.version in (None, 'default'): |
| 203 |
self.version = defaults.get_default_powershell() |
| 204 |
|
| 205 |
if self.path: |
| 206 |
if self.path.endswith('/'): |
| 207 |
self.path = os.path.join(self.path, f'pwsh{self.version or ""}') |
| 208 |
|
| 209 |
# FUTURE: If the host is origin, the pwsh path could be validated here. |
| 210 |
else: |
| 211 |
self.path = defaults.get_powershell_path(self.version) |
| 212 |
|
| 213 |
|
| 214 |
@dataclasses.dataclass |
| 215 |
class PosixConfig(HostConfig, metaclass=abc.ABCMeta): |
| 216 |
"""Base class for POSIX host configuration.""" |
| 217 |
|
| 218 |
python: t.Optional[PythonConfig] = None |
| 219 |
powershell: PowerShellConfig | None = None |
| 220 |
|
| 221 |
@property |
| 222 |
@abc.abstractmethod |
| 223 |
def have_root(self) -> bool: |
| 224 |
"""True if root is available, otherwise False.""" |
| 225 |
|
| 226 |
@abc.abstractmethod |
| 227 |
def get_defaults(self, context: HostContext) -> PosixCompletionConfig: |
| 228 |
"""Return the default settings.""" |
| 229 |
|
| 230 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 231 |
"""Apply default settings.""" |
| 232 |
assert isinstance(defaults, PosixCompletionConfig) |
| 233 |
|
| 234 |
super().apply_defaults(context, defaults) |
| 235 |
|
| 236 |
self.python = self.python or NativePythonConfig() |
| 237 |
self.python.apply_defaults(context, defaults) |
| 238 |
|
| 239 |
self.powershell = self.powershell or PowerShellConfig() |
| 240 |
self.powershell.apply_defaults(defaults) |
| 241 |
|
| 242 |
|
| 243 |
@dataclasses.dataclass |
| 244 |
class ControllerHostConfig(PosixConfig, metaclass=abc.ABCMeta): |
| 245 |
"""Base class for host configurations which support the controller.""" |
| 246 |
|
| 247 |
@abc.abstractmethod |
| 248 |
def get_default_targets(self, context: HostContext) -> list[ControllerConfig]: |
| 249 |
"""Return the default targets for this host config.""" |
| 250 |
|
| 251 |
|
| 252 |
@dataclasses.dataclass |
| 253 |
class RemoteConfig(HostConfig, metaclass=abc.ABCMeta): |
| 254 |
"""Base class for remote host configuration.""" |
| 255 |
|
| 256 |
name: t.Optional[str] = None |
| 257 |
provider: t.Optional[str] = None |
| 258 |
arch: t.Optional[str] = None |
| 259 |
|
| 260 |
@property |
| 261 |
def platform(self) -> str: |
| 262 |
"""The name of the platform.""" |
| 263 |
return self.name.partition('/')[0] |
| 264 |
|
| 265 |
@property |
| 266 |
def version(self) -> str: |
| 267 |
"""The version of the platform.""" |
| 268 |
return self.name.partition('/')[2] |
| 269 |
|
| 270 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 271 |
"""Apply default settings.""" |
| 272 |
assert isinstance(defaults, RemoteCompletionConfig) |
| 273 |
|
| 274 |
super().apply_defaults(context, defaults) |
| 275 |
|
| 276 |
if self.provider == 'default': |
| 277 |
self.provider = None |
| 278 |
|
| 279 |
self.provider = self.provider or defaults.provider or 'aws' |
| 280 |
self.arch = self.arch or defaults.arch or Architecture.X86_64 |
| 281 |
|
| 282 |
@property |
| 283 |
def is_managed(self) -> bool: |
| 284 |
""" |
| 285 |
True if this host is a managed instance, otherwise False. |
| 286 |
Managed instances are used exclusively by ansible-test and can safely have destructive operations performed without explicit permission from the user. |
| 287 |
""" |
| 288 |
return True |
| 289 |
|
| 290 |
|
| 291 |
@dataclasses.dataclass |
| 292 |
class PosixSshConfig(PosixConfig): |
| 293 |
"""Configuration for a POSIX SSH host.""" |
| 294 |
|
| 295 |
user: t.Optional[str] = None |
| 296 |
host: t.Optional[str] = None |
| 297 |
port: t.Optional[int] = None |
| 298 |
|
| 299 |
def get_defaults(self, context: HostContext) -> PosixSshCompletionConfig: |
| 300 |
"""Return the default settings.""" |
| 301 |
return PosixSshCompletionConfig( |
| 302 |
user=self.user, |
| 303 |
host=self.host, |
| 304 |
) |
| 305 |
|
| 306 |
@property |
| 307 |
def have_root(self) -> bool: |
| 308 |
"""True if root is available, otherwise False.""" |
| 309 |
return self.user == 'root' |
| 310 |
|
| 311 |
|
| 312 |
@dataclasses.dataclass |
| 313 |
class InventoryConfig(HostConfig): |
| 314 |
"""Configuration using inventory.""" |
| 315 |
|
| 316 |
path: t.Optional[str] = None |
| 317 |
|
| 318 |
def get_defaults(self, context: HostContext) -> InventoryCompletionConfig: |
| 319 |
"""Return the default settings.""" |
| 320 |
return InventoryCompletionConfig() |
| 321 |
|
| 322 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 323 |
"""Apply default settings.""" |
| 324 |
assert isinstance(defaults, InventoryCompletionConfig) |
| 325 |
|
| 326 |
|
| 327 |
@dataclasses.dataclass |
| 328 |
class DockerConfig(ControllerHostConfig, PosixConfig): |
| 329 |
"""Configuration for a docker host.""" |
| 330 |
|
| 331 |
name: t.Optional[str] = None |
| 332 |
image: t.Optional[str] = None |
| 333 |
memory: t.Optional[int] = None |
| 334 |
privileged: t.Optional[bool] = None |
| 335 |
seccomp: t.Optional[str] = None |
| 336 |
cgroup: t.Optional[CGroupVersion] = None |
| 337 |
audit: t.Optional[AuditMode] = None |
| 338 |
|
| 339 |
def get_defaults(self, context: HostContext) -> DockerCompletionConfig: |
| 340 |
"""Return the default settings.""" |
| 341 |
return filter_completion(docker_completion()).get(self.name) or DockerCompletionConfig( |
| 342 |
name=self.name, |
| 343 |
image=self.name, |
| 344 |
placeholder=True, |
| 345 |
) |
| 346 |
|
| 347 |
def get_default_targets(self, context: HostContext) -> list[ControllerConfig]: |
| 348 |
"""Return the default targets for this host config.""" |
| 349 |
if self.name in filter_completion(docker_completion()): |
| 350 |
defaults = self.get_defaults(context) |
| 351 |
pythons = {version: defaults.get_python_path(version) for version in defaults.supported_pythons} |
| 352 |
else: |
| 353 |
pythons = {context.controller_config.python.version: context.controller_config.python.path} |
| 354 |
|
| 355 |
return [ControllerConfig(python=NativePythonConfig(version=version, path=path)) for version, path in pythons.items()] |
| 356 |
|
| 357 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 358 |
"""Apply default settings.""" |
| 359 |
assert isinstance(defaults, DockerCompletionConfig) |
| 360 |
|
| 361 |
super().apply_defaults(context, defaults) |
| 362 |
|
| 363 |
self.name = defaults.name |
| 364 |
self.image = defaults.image |
| 365 |
|
| 366 |
if self.seccomp is None: |
| 367 |
self.seccomp = defaults.seccomp |
| 368 |
|
| 369 |
if self.cgroup is None: |
| 370 |
self.cgroup = defaults.cgroup_enum |
| 371 |
|
| 372 |
if self.audit is None: |
| 373 |
self.audit = defaults.audit_enum |
| 374 |
|
| 375 |
if self.privileged is None: |
| 376 |
self.privileged = False |
| 377 |
|
| 378 |
@property |
| 379 |
def is_managed(self) -> bool: |
| 380 |
""" |
| 381 |
True if this host is a managed instance, otherwise False. |
| 382 |
Managed instances are used exclusively by ansible-test and can safely have destructive operations performed without explicit permission from the user. |
| 383 |
""" |
| 384 |
return True |
| 385 |
|
| 386 |
@property |
| 387 |
def have_root(self) -> bool: |
| 388 |
"""True if root is available, otherwise False.""" |
| 389 |
return True |
| 390 |
|
| 391 |
|
| 392 |
@dataclasses.dataclass |
| 393 |
class PosixRemoteConfig(RemoteConfig, ControllerHostConfig, PosixConfig): |
| 394 |
"""Configuration for a POSIX remote host.""" |
| 395 |
|
| 396 |
become: t.Optional[str] = None |
| 397 |
|
| 398 |
def get_defaults(self, context: HostContext) -> PosixRemoteCompletionConfig: |
| 399 |
"""Return the default settings.""" |
| 400 |
# pylint: disable=unexpected-keyword-arg # see: https://github.com/PyCQA/pylint/issues/7434 |
| 401 |
return filter_completion(remote_completion()).get(self.name) or remote_completion().get(self.platform) or PosixRemoteCompletionConfig( |
| 402 |
name=self.name, |
| 403 |
placeholder=True, |
| 404 |
) |
| 405 |
|
| 406 |
def get_default_targets(self, context: HostContext) -> list[ControllerConfig]: |
| 407 |
"""Return the default targets for this host config.""" |
| 408 |
if self.name in filter_completion(remote_completion()): |
| 409 |
defaults = self.get_defaults(context) |
| 410 |
pythons = {version: defaults.get_python_path(version) for version in defaults.supported_pythons} |
| 411 |
else: |
| 412 |
pythons = {context.controller_config.python.version: context.controller_config.python.path} |
| 413 |
|
| 414 |
return [ControllerConfig(python=NativePythonConfig(version=version, path=path)) for version, path in pythons.items()] |
| 415 |
|
| 416 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 417 |
"""Apply default settings.""" |
| 418 |
assert isinstance(defaults, PosixRemoteCompletionConfig) |
| 419 |
|
| 420 |
super().apply_defaults(context, defaults) |
| 421 |
|
| 422 |
self.become = self.become or defaults.become |
| 423 |
|
| 424 |
if not defaults.is_default: |
| 425 |
self.name = defaults.name |
| 426 |
|
| 427 |
@property |
| 428 |
def have_root(self) -> bool: |
| 429 |
"""True if root is available, otherwise False.""" |
| 430 |
return True |
| 431 |
|
| 432 |
|
| 433 |
@dataclasses.dataclass |
| 434 |
class WindowsConfig(HostConfig, metaclass=abc.ABCMeta): |
| 435 |
"""Base class for Windows host configuration.""" |
| 436 |
|
| 437 |
|
| 438 |
@dataclasses.dataclass |
| 439 |
class WindowsRemoteConfig(RemoteConfig, WindowsConfig): |
| 440 |
"""Configuration for a remote Windows host.""" |
| 441 |
|
| 442 |
connection: t.Optional[str] = None |
| 443 |
powershell: PowerShellConfig | None = None |
| 444 |
|
| 445 |
def get_defaults(self, context: HostContext) -> WindowsRemoteCompletionConfig: |
| 446 |
"""Return the default settings.""" |
| 447 |
return filter_completion(windows_completion()).get(self.name) or windows_completion().get(self.platform) |
| 448 |
|
| 449 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 450 |
"""Apply default settings.""" |
| 451 |
assert isinstance(defaults, WindowsRemoteCompletionConfig) |
| 452 |
|
| 453 |
super().apply_defaults(context, defaults) |
| 454 |
|
| 455 |
self.connection = self.connection or defaults.connection |
| 456 |
|
| 457 |
if not defaults.is_default: |
| 458 |
self.name = defaults.name |
| 459 |
|
| 460 |
|
| 461 |
@dataclasses.dataclass |
| 462 |
class WindowsInventoryConfig(InventoryConfig, WindowsConfig): |
| 463 |
"""Configuration for Windows hosts using inventory.""" |
| 464 |
|
| 465 |
|
| 466 |
@dataclasses.dataclass |
| 467 |
class NetworkConfig(HostConfig, metaclass=abc.ABCMeta): |
| 468 |
"""Base class for network host configuration.""" |
| 469 |
|
| 470 |
|
| 471 |
@dataclasses.dataclass |
| 472 |
class NetworkRemoteConfig(RemoteConfig, NetworkConfig): |
| 473 |
"""Configuration for a remote network host.""" |
| 474 |
|
| 475 |
collection: t.Optional[str] = None |
| 476 |
connection: t.Optional[str] = None |
| 477 |
|
| 478 |
def get_defaults(self, context: HostContext) -> NetworkRemoteCompletionConfig: |
| 479 |
"""Return the default settings.""" |
| 480 |
return filter_completion(network_completion()).get(self.name) or NetworkRemoteCompletionConfig( |
| 481 |
name=self.name, |
| 482 |
placeholder=True, |
| 483 |
) |
| 484 |
|
| 485 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 486 |
"""Apply default settings.""" |
| 487 |
assert isinstance(defaults, NetworkRemoteCompletionConfig) |
| 488 |
|
| 489 |
super().apply_defaults(context, defaults) |
| 490 |
|
| 491 |
self.collection = self.collection or defaults.collection |
| 492 |
self.connection = self.connection or defaults.connection |
| 493 |
|
| 494 |
|
| 495 |
@dataclasses.dataclass |
| 496 |
class NetworkInventoryConfig(InventoryConfig, NetworkConfig): |
| 497 |
"""Configuration for network hosts using inventory.""" |
| 498 |
|
| 499 |
|
| 500 |
@dataclasses.dataclass |
| 501 |
class OriginConfig(ControllerHostConfig, PosixConfig): |
| 502 |
"""Configuration for the origin host.""" |
| 503 |
|
| 504 |
def get_defaults(self, context: HostContext) -> OriginCompletionConfig: |
| 505 |
"""Return the default settings.""" |
| 506 |
return OriginCompletionConfig() |
| 507 |
|
| 508 |
def get_default_targets(self, context: HostContext) -> list[ControllerConfig]: |
| 509 |
"""Return the default targets for this host config.""" |
| 510 |
return [ControllerConfig(python=NativePythonConfig(version=version, path=path)) for version, path in get_available_python_versions().items()] |
| 511 |
|
| 512 |
@property |
| 513 |
def have_root(self) -> bool: |
| 514 |
"""True if root is available, otherwise False.""" |
| 515 |
return os.getuid() == 0 |
| 516 |
|
| 517 |
|
| 518 |
@dataclasses.dataclass |
| 519 |
class ControllerConfig(PosixConfig): |
| 520 |
"""Configuration for the controller host.""" |
| 521 |
|
| 522 |
controller: t.Optional[PosixConfig] = None |
| 523 |
|
| 524 |
def get_defaults(self, context: HostContext) -> PosixCompletionConfig: |
| 525 |
"""Return the default settings.""" |
| 526 |
return context.controller_config.get_defaults(context) |
| 527 |
|
| 528 |
def apply_defaults(self, context: HostContext, defaults: CompletionConfig) -> None: |
| 529 |
"""Apply default settings.""" |
| 530 |
assert isinstance(defaults, PosixCompletionConfig) |
| 531 |
|
| 532 |
self.controller = context.controller_config |
| 533 |
|
| 534 |
if not self.python and not defaults.supported_pythons: |
| 535 |
# The user did not specify a target Python and supported Pythons are unknown, so use the controller Python specified by the user instead. |
| 536 |
self.python = context.controller_config.python |
| 537 |
|
| 538 |
if not self.powershell and not defaults.supported_powershells: |
| 539 |
# The user did not specify a target PowerShell and supported versions are unknown, so use the controller version specified by the user instead. |
| 540 |
self.powershell = context.controller_config.powershell |
| 541 |
|
| 542 |
super().apply_defaults(context, defaults) |
| 543 |
|
| 544 |
@property |
| 545 |
def is_managed(self) -> bool: |
| 546 |
""" |
| 547 |
True if the host is a managed instance, otherwise False. |
| 548 |
Managed instances are used exclusively by ansible-test and can safely have destructive operations performed without explicit permission from the user. |
| 549 |
""" |
| 550 |
return self.controller.is_managed |
| 551 |
|
| 552 |
@property |
| 553 |
def have_root(self) -> bool: |
| 554 |
"""True if root is available, otherwise False.""" |
| 555 |
return self.controller.have_root |
| 556 |
|
| 557 |
|
| 558 |
class FallbackReason(enum.Enum): |
| 559 |
"""Reason fallback was performed.""" |
| 560 |
|
| 561 |
ENVIRONMENT = enum.auto() |
| 562 |
PYTHON = enum.auto() |
| 563 |
|
| 564 |
|
| 565 |
@dataclasses.dataclass(frozen=True) |
| 566 |
class FallbackDetail: |
| 567 |
"""Details about controller fallback behavior.""" |
| 568 |
|
| 569 |
reason: FallbackReason |
| 570 |
message: str |
| 571 |
|
| 572 |
|
| 573 |
@dataclasses.dataclass(frozen=True) |
| 574 |
class HostSettings: |
| 575 |
"""Host settings for the controller and targets.""" |
| 576 |
|
| 577 |
controller: ControllerHostConfig |
| 578 |
targets: list[HostConfig] |
| 579 |
skipped_python_versions: list[str] |
| 580 |
filtered_args: list[str] |
| 581 |
controller_fallback: t.Optional[FallbackDetail] |
| 582 |
|
| 583 |
def serialize(self, path: str) -> None: |
| 584 |
"""Serialize the host settings to the given path.""" |
| 585 |
with open_binary_file(path, 'wb') as settings_file: |
| 586 |
pickle.dump(self, settings_file) |
| 587 |
|
| 588 |
@staticmethod |
| 589 |
def deserialize(path: str) -> HostSettings: |
| 590 |
"""Deserialize host settings from the path.""" |
| 591 |
with open_binary_file(path) as settings_file: |
| 592 |
return pickle.load(settings_file) |
| 593 |
|
| 594 |
def apply_defaults(self) -> None: |
| 595 |
"""Apply defaults to the host settings.""" |
| 596 |
context = HostContext(controller_config=None) |
| 597 |
self.controller.apply_defaults(context, self.controller.get_defaults(context)) |
| 598 |
|
| 599 |
for target in self.targets: |
| 600 |
context = HostContext(controller_config=self.controller) |
| 601 |
target.apply_defaults(context, target.get_defaults(context)) |