| 1 |
"""Provides compatibility with first-generation host delegation options in ansible-test.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import argparse |
| 6 |
import collections.abc as c |
| 7 |
import dataclasses |
| 8 |
import enum |
| 9 |
import os |
| 10 |
import types |
| 11 |
import typing as t |
| 12 |
|
| 13 |
from ..constants import ( |
| 14 |
CONTROLLER_PYTHON_VERSIONS, |
| 15 |
SUPPORTED_PYTHON_VERSIONS, |
| 16 |
) |
| 17 |
|
| 18 |
from ..util import ( |
| 19 |
ApplicationError, |
| 20 |
display, |
| 21 |
filter_args, |
| 22 |
sorted_versions, |
| 23 |
) |
| 24 |
|
| 25 |
from ..docker_util import ( |
| 26 |
docker_available, |
| 27 |
) |
| 28 |
|
| 29 |
from ..completion import ( |
| 30 |
docker_completion, |
| 31 |
remote_completion, |
| 32 |
filter_completion, |
| 33 |
windows_completion, |
| 34 |
) |
| 35 |
|
| 36 |
from ..host_configs import ( |
| 37 |
ControllerConfig, |
| 38 |
ControllerHostConfig, |
| 39 |
DockerConfig, |
| 40 |
FallbackDetail, |
| 41 |
FallbackReason, |
| 42 |
HostConfig, |
| 43 |
HostContext, |
| 44 |
HostSettings, |
| 45 |
NativePythonConfig, |
| 46 |
NetworkInventoryConfig, |
| 47 |
NetworkRemoteConfig, |
| 48 |
OriginConfig, |
| 49 |
PosixRemoteConfig, |
| 50 |
VirtualPythonConfig, |
| 51 |
WindowsInventoryConfig, |
| 52 |
WindowsRemoteConfig, |
| 53 |
) |
| 54 |
|
| 55 |
from ..data import ( |
| 56 |
data_context, |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
def filter_python(version: t.Optional[str], versions: t.Optional[c.Sequence[str]]) -> t.Optional[str]: |
| 61 |
"""If a Python version is given and is in the given version list, return that Python version, otherwise return None.""" |
| 62 |
return version if version in versions else None |
| 63 |
|
| 64 |
|
| 65 |
def controller_python(version: t.Optional[str]) -> t.Optional[str]: |
| 66 |
"""If a Python version is given and is supported by the controller, return that Python version, otherwise return None.""" |
| 67 |
return filter_python(version, CONTROLLER_PYTHON_VERSIONS) |
| 68 |
|
| 69 |
|
| 70 |
def get_fallback_remote_controller() -> str: |
| 71 |
"""Return the remote fallback platform for the controller.""" |
| 72 |
fallback = [item for name, item in filter_completion(remote_completion()).items() if item.controller_supported and name == "fedora/latest"][0] |
| 73 |
return fallback.name |
| 74 |
|
| 75 |
|
| 76 |
def get_option_name(name: str) -> str: |
| 77 |
"""Return a command-line option name from the given option name.""" |
| 78 |
if name == 'targets': |
| 79 |
name = 'target' |
| 80 |
|
| 81 |
return f'--{name.replace("_", "-")}' |
| 82 |
|
| 83 |
|
| 84 |
class PythonVersionUnsupportedError(ApplicationError): |
| 85 |
"""A Python version was requested for a context which does not support that version.""" |
| 86 |
|
| 87 |
def __init__(self, context: str, version: str, versions: c.Iterable[str]) -> None: |
| 88 |
super().__init__(f'Python {version} is not supported by environment `{context}`. Supported Python version(s) are: {", ".join(versions)}') |
| 89 |
|
| 90 |
|
| 91 |
class PythonVersionUnspecifiedError(ApplicationError): |
| 92 |
"""A Python version was not specified for a context which is unknown, thus the Python version is unknown.""" |
| 93 |
|
| 94 |
def __init__(self, context: str) -> None: |
| 95 |
super().__init__( |
| 96 |
f'Environment `{context}` is unknown. Use a predefined environment instead. ' |
| 97 |
f'Alternatively, to use an unknown environment, use the `--python` option to specify a Python version.' |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
class ControllerNotSupportedError(ApplicationError): |
| 102 |
"""Option(s) were specified which do not provide support for the controller and would be ignored because they are irrelevant for the target.""" |
| 103 |
|
| 104 |
def __init__(self, context: str) -> None: |
| 105 |
super().__init__(f'Environment `{context}` does not provide a Python version supported by the controller.') |
| 106 |
|
| 107 |
|
| 108 |
class OptionsConflictError(ApplicationError): |
| 109 |
"""Option(s) were specified which conflict with other options.""" |
| 110 |
|
| 111 |
def __init__(self, first: c.Iterable[str], second: c.Iterable[str]) -> None: |
| 112 |
super().__init__(f'Options `{" ".join(first)}` cannot be combined with options `{" ".join(second)}`.') |
| 113 |
|
| 114 |
|
| 115 |
@dataclasses.dataclass(frozen=True) |
| 116 |
class LegacyHostOptions: |
| 117 |
"""Legacy host options used prior to the availability of separate controller and target host configuration.""" |
| 118 |
|
| 119 |
python: t.Optional[str] = None |
| 120 |
python_interpreter: t.Optional[str] = None |
| 121 |
local: t.Optional[bool] = None |
| 122 |
venv: t.Optional[bool] = None |
| 123 |
venv_system_site_packages: t.Optional[bool] = None |
| 124 |
remote: t.Optional[str] = None |
| 125 |
remote_provider: t.Optional[str] = None |
| 126 |
remote_arch: t.Optional[str] = None |
| 127 |
docker: t.Optional[str] = None |
| 128 |
docker_privileged: t.Optional[bool] = None |
| 129 |
docker_seccomp: t.Optional[str] = None |
| 130 |
docker_memory: t.Optional[int] = None |
| 131 |
windows: t.Optional[list[str]] = None |
| 132 |
platform: t.Optional[list[str]] = None |
| 133 |
platform_collection: t.Optional[list[tuple[str, str]]] = None |
| 134 |
platform_connection: t.Optional[list[tuple[str, str]]] = None |
| 135 |
inventory: t.Optional[str] = None |
| 136 |
|
| 137 |
@staticmethod |
| 138 |
def create(namespace: t.Union[argparse.Namespace, types.SimpleNamespace]) -> LegacyHostOptions: |
| 139 |
"""Create legacy host options from the given namespace.""" |
| 140 |
kwargs = {field.name: getattr(namespace, field.name, None) for field in dataclasses.fields(LegacyHostOptions)} |
| 141 |
|
| 142 |
if kwargs['python'] == 'default': |
| 143 |
kwargs['python'] = None |
| 144 |
|
| 145 |
return LegacyHostOptions(**kwargs) |
| 146 |
|
| 147 |
@staticmethod |
| 148 |
def purge_namespace(namespace: t.Union[argparse.Namespace, types.SimpleNamespace]) -> None: |
| 149 |
"""Purge legacy host options fields from the given namespace.""" |
| 150 |
for field in dataclasses.fields(LegacyHostOptions): |
| 151 |
if hasattr(namespace, field.name): |
| 152 |
delattr(namespace, field.name) |
| 153 |
|
| 154 |
@staticmethod |
| 155 |
def purge_args(args: list[str]) -> list[str]: |
| 156 |
"""Purge legacy host options from the given command line arguments.""" |
| 157 |
fields: tuple[dataclasses.Field, ...] = dataclasses.fields(LegacyHostOptions) |
| 158 |
filters: dict[str, int] = {get_option_name(field.name): 0 if field.type is t.Optional[bool] else 1 for field in fields} |
| 159 |
|
| 160 |
return filter_args(args, filters) |
| 161 |
|
| 162 |
def get_options_used(self) -> tuple[str, ...]: |
| 163 |
"""Return a tuple of the command line options used.""" |
| 164 |
fields: tuple[dataclasses.Field, ...] = dataclasses.fields(self) |
| 165 |
options = tuple(sorted(get_option_name(field.name) for field in fields if getattr(self, field.name))) |
| 166 |
return options |
| 167 |
|
| 168 |
|
| 169 |
class TargetMode(enum.Enum): |
| 170 |
"""Type of provisioning to use for the targets.""" |
| 171 |
|
| 172 |
WINDOWS_INTEGRATION = enum.auto() # windows-integration |
| 173 |
NETWORK_INTEGRATION = enum.auto() # network-integration |
| 174 |
POSIX_INTEGRATION = enum.auto() # integration |
| 175 |
SANITY = enum.auto() # sanity |
| 176 |
UNITS = enum.auto() # units |
| 177 |
SHELL = enum.auto() # shell |
| 178 |
NO_TARGETS = enum.auto() # coverage |
| 179 |
|
| 180 |
@property |
| 181 |
def one_host(self) -> bool: |
| 182 |
"""Return True if only one host (the controller) should be used, otherwise return False.""" |
| 183 |
return self in (TargetMode.SANITY, TargetMode.UNITS, TargetMode.NO_TARGETS) |
| 184 |
|
| 185 |
@property |
| 186 |
def no_fallback(self) -> bool: |
| 187 |
"""Return True if no fallback is acceptable for the controller (due to options not applying to the target), otherwise return False.""" |
| 188 |
return self in (TargetMode.WINDOWS_INTEGRATION, TargetMode.NETWORK_INTEGRATION, TargetMode.NO_TARGETS) |
| 189 |
|
| 190 |
@property |
| 191 |
def multiple_pythons(self) -> bool: |
| 192 |
"""Return True if multiple Python versions are allowed, otherwise False.""" |
| 193 |
return self in (TargetMode.SANITY, TargetMode.UNITS) |
| 194 |
|
| 195 |
@property |
| 196 |
def has_python(self) -> bool: |
| 197 |
"""Return True if this mode uses Python, otherwise False.""" |
| 198 |
return self in (TargetMode.POSIX_INTEGRATION, TargetMode.SANITY, TargetMode.UNITS, TargetMode.SHELL) |
| 199 |
|
| 200 |
|
| 201 |
def convert_legacy_args( |
| 202 |
argv: list[str], |
| 203 |
args: t.Union[argparse.Namespace, types.SimpleNamespace], |
| 204 |
mode: TargetMode, |
| 205 |
) -> HostSettings: |
| 206 |
"""Convert pre-split host arguments in the given namespace to their split counterparts.""" |
| 207 |
old_options = LegacyHostOptions.create(args) |
| 208 |
old_options.purge_namespace(args) |
| 209 |
|
| 210 |
new_options = [ |
| 211 |
'--controller', |
| 212 |
'--target', |
| 213 |
'--target-python', |
| 214 |
'--target-posix', |
| 215 |
'--target-windows', |
| 216 |
'--target-network', |
| 217 |
] |
| 218 |
|
| 219 |
used_old_options = old_options.get_options_used() |
| 220 |
used_new_options = [name for name in new_options if name in argv] |
| 221 |
|
| 222 |
if used_old_options: |
| 223 |
if used_new_options: |
| 224 |
raise OptionsConflictError(used_old_options, used_new_options) |
| 225 |
|
| 226 |
controller, targets, controller_fallback = get_legacy_host_config(mode, old_options) |
| 227 |
|
| 228 |
if controller_fallback: |
| 229 |
if mode.one_host: |
| 230 |
display.info(controller_fallback.message, verbosity=1) |
| 231 |
else: |
| 232 |
display.warning(controller_fallback.message) |
| 233 |
|
| 234 |
used_default_pythons = mode in (TargetMode.SANITY, TargetMode.UNITS) and not native_python(old_options) |
| 235 |
else: |
| 236 |
controller = args.controller or OriginConfig() |
| 237 |
controller_fallback = None |
| 238 |
|
| 239 |
if mode == TargetMode.NO_TARGETS: |
| 240 |
targets = [] |
| 241 |
used_default_pythons = False |
| 242 |
elif args.targets: |
| 243 |
targets = args.targets |
| 244 |
used_default_pythons = False |
| 245 |
else: |
| 246 |
targets = default_targets(mode, controller) |
| 247 |
used_default_pythons = mode in (TargetMode.SANITY, TargetMode.UNITS) |
| 248 |
|
| 249 |
args.controller = controller |
| 250 |
args.targets = targets |
| 251 |
|
| 252 |
if used_default_pythons: |
| 253 |
control_targets = t.cast(list[ControllerConfig], targets) |
| 254 |
skipped_python_versions = sorted_versions(list(set(SUPPORTED_PYTHON_VERSIONS) - {target.python.version for target in control_targets})) |
| 255 |
else: |
| 256 |
skipped_python_versions = [] |
| 257 |
|
| 258 |
filtered_args = old_options.purge_args(argv) |
| 259 |
filtered_args = filter_args(filtered_args, {name: 1 for name in new_options}) |
| 260 |
|
| 261 |
host_settings = HostSettings( |
| 262 |
controller=controller, |
| 263 |
targets=targets, |
| 264 |
skipped_python_versions=skipped_python_versions, |
| 265 |
filtered_args=filtered_args, |
| 266 |
controller_fallback=controller_fallback, |
| 267 |
) |
| 268 |
|
| 269 |
return host_settings |
| 270 |
|
| 271 |
|
| 272 |
def controller_targets( |
| 273 |
mode: TargetMode, |
| 274 |
options: LegacyHostOptions, |
| 275 |
controller: ControllerHostConfig, |
| 276 |
) -> list[HostConfig]: |
| 277 |
"""Return the configuration for controller targets.""" |
| 278 |
python = native_python(options) |
| 279 |
|
| 280 |
targets: list[HostConfig] |
| 281 |
|
| 282 |
if python: |
| 283 |
targets = [ControllerConfig(python=python)] |
| 284 |
else: |
| 285 |
targets = default_targets(mode, controller) |
| 286 |
|
| 287 |
return targets |
| 288 |
|
| 289 |
|
| 290 |
def native_python(options: LegacyHostOptions) -> t.Optional[NativePythonConfig]: |
| 291 |
"""Return a NativePythonConfig for the given version if it is not None, otherwise return None.""" |
| 292 |
if not options.python and not options.python_interpreter: |
| 293 |
return None |
| 294 |
|
| 295 |
return NativePythonConfig(version=options.python, path=options.python_interpreter) |
| 296 |
|
| 297 |
|
| 298 |
def get_legacy_host_config( |
| 299 |
mode: TargetMode, |
| 300 |
options: LegacyHostOptions, |
| 301 |
) -> tuple[ControllerHostConfig, list[HostConfig], t.Optional[FallbackDetail]]: |
| 302 |
""" |
| 303 |
Returns controller and target host configs derived from the provided legacy host options. |
| 304 |
The goal is to match the original behavior, by using non-split testing whenever possible. |
| 305 |
When the options support the controller, use the options for the controller and use ControllerConfig for the targets. |
| 306 |
When the options do not support the controller, use the options for the targets and use a default controller config influenced by the options. |
| 307 |
""" |
| 308 |
venv_fallback = 'venv/default' |
| 309 |
docker_fallback = 'default' |
| 310 |
remote_fallback = get_fallback_remote_controller() |
| 311 |
|
| 312 |
controller_fallback: t.Optional[tuple[str, str, FallbackReason]] = None |
| 313 |
|
| 314 |
controller: t.Optional[ControllerHostConfig] |
| 315 |
targets: list[HostConfig] |
| 316 |
|
| 317 |
if options.venv: |
| 318 |
if controller_python(options.python) or not options.python: |
| 319 |
controller = OriginConfig(python=VirtualPythonConfig(version=options.python or 'default', system_site_packages=options.venv_system_site_packages)) |
| 320 |
else: |
| 321 |
controller_fallback = f'origin:python={venv_fallback}', f'--venv --python {options.python}', FallbackReason.PYTHON |
| 322 |
controller = OriginConfig(python=VirtualPythonConfig(version='default', system_site_packages=options.venv_system_site_packages)) |
| 323 |
|
| 324 |
if mode in (TargetMode.SANITY, TargetMode.UNITS): |
| 325 |
python = native_python(options) |
| 326 |
|
| 327 |
if python: |
| 328 |
control_targets = [ControllerConfig(python=python)] |
| 329 |
else: |
| 330 |
control_targets = controller.get_default_targets(HostContext(controller_config=controller)) |
| 331 |
|
| 332 |
# Target sanity tests either have no Python requirements or manage their own virtual environments. |
| 333 |
# Thus, there is no point in setting up virtual environments ahead of time for them. |
| 334 |
|
| 335 |
if mode == TargetMode.UNITS: |
| 336 |
targets = [ControllerConfig(python=VirtualPythonConfig(version=target.python.version, path=target.python.path, |
| 337 |
system_site_packages=options.venv_system_site_packages)) for target in control_targets] |
| 338 |
else: |
| 339 |
targets = t.cast(list[HostConfig], control_targets) |
| 340 |
else: |
| 341 |
targets = [ControllerConfig(python=VirtualPythonConfig(version=options.python or 'default', |
| 342 |
system_site_packages=options.venv_system_site_packages))] |
| 343 |
elif options.docker: |
| 344 |
docker_config = filter_completion(docker_completion()).get(options.docker) |
| 345 |
|
| 346 |
if docker_config: |
| 347 |
if options.python and options.python not in docker_config.supported_pythons: |
| 348 |
raise PythonVersionUnsupportedError(f'--docker {options.docker}', options.python, docker_config.supported_pythons) |
| 349 |
|
| 350 |
if docker_config.controller_supported: |
| 351 |
if controller_python(options.python) or not options.python: |
| 352 |
controller = DockerConfig(name=docker_config.name, python=native_python(options), |
| 353 |
privileged=options.docker_privileged, seccomp=options.docker_seccomp, memory=options.docker_memory) |
| 354 |
targets = controller_targets(mode, options, controller) |
| 355 |
else: |
| 356 |
controller_fallback = f'docker:{options.docker}', f'--docker {options.docker} --python {options.python}', FallbackReason.PYTHON |
| 357 |
controller = DockerConfig(name=docker_config.name) |
| 358 |
targets = controller_targets(mode, options, controller) |
| 359 |
else: |
| 360 |
controller_fallback = f'docker:{docker_fallback}', f'--docker {options.docker}', FallbackReason.ENVIRONMENT |
| 361 |
controller = DockerConfig(name=docker_fallback) |
| 362 |
targets = [DockerConfig(name=docker_config.name, python=native_python(options), |
| 363 |
privileged=options.docker_privileged, seccomp=options.docker_seccomp, memory=options.docker_memory)] |
| 364 |
else: |
| 365 |
if not options.python: |
| 366 |
raise PythonVersionUnspecifiedError(f'--docker {options.docker}') |
| 367 |
|
| 368 |
if controller_python(options.python): |
| 369 |
controller = DockerConfig(name=options.docker, python=native_python(options), |
| 370 |
privileged=options.docker_privileged, seccomp=options.docker_seccomp, memory=options.docker_memory) |
| 371 |
targets = controller_targets(mode, options, controller) |
| 372 |
else: |
| 373 |
controller_fallback = f'docker:{docker_fallback}', f'--docker {options.docker} --python {options.python}', FallbackReason.PYTHON |
| 374 |
controller = DockerConfig(name=docker_fallback) |
| 375 |
targets = [DockerConfig(name=options.docker, python=native_python(options), |
| 376 |
privileged=options.docker_privileged, seccomp=options.docker_seccomp, memory=options.docker_memory)] |
| 377 |
elif options.remote: |
| 378 |
remote_config = filter_completion(remote_completion()).get(options.remote) |
| 379 |
context, reason = None, None |
| 380 |
|
| 381 |
if remote_config: |
| 382 |
if options.python and options.python not in remote_config.supported_pythons: |
| 383 |
raise PythonVersionUnsupportedError(f'--remote {options.remote}', options.python, remote_config.supported_pythons) |
| 384 |
|
| 385 |
if remote_config.controller_supported: |
| 386 |
if controller_python(options.python) or not options.python: |
| 387 |
controller = PosixRemoteConfig(name=remote_config.name, python=native_python(options), provider=options.remote_provider, |
| 388 |
arch=options.remote_arch) |
| 389 |
targets = controller_targets(mode, options, controller) |
| 390 |
else: |
| 391 |
controller_fallback = f'remote:{options.remote}', f'--remote {options.remote} --python {options.python}', FallbackReason.PYTHON |
| 392 |
controller = PosixRemoteConfig(name=remote_config.name, provider=options.remote_provider, arch=options.remote_arch) |
| 393 |
targets = controller_targets(mode, options, controller) |
| 394 |
else: |
| 395 |
context, reason = f'--remote {options.remote}', FallbackReason.ENVIRONMENT |
| 396 |
controller = None |
| 397 |
targets = [PosixRemoteConfig(name=remote_config.name, python=native_python(options), provider=options.remote_provider, |
| 398 |
arch=options.remote_arch)] |
| 399 |
elif mode == TargetMode.SHELL and options.remote.startswith('windows/'): |
| 400 |
if options.python and options.python not in CONTROLLER_PYTHON_VERSIONS: |
| 401 |
raise ControllerNotSupportedError(f'--python {options.python}') |
| 402 |
|
| 403 |
name = resolve_windows_names([options.remote.removeprefix("windows/")])[0] |
| 404 |
controller = OriginConfig(python=native_python(options)) |
| 405 |
targets = [WindowsRemoteConfig(name=name, provider=options.remote_provider, arch=options.remote_arch)] |
| 406 |
else: |
| 407 |
if not options.python: |
| 408 |
raise PythonVersionUnspecifiedError(f'--remote {options.remote}') |
| 409 |
|
| 410 |
if controller_python(options.python): |
| 411 |
controller = PosixRemoteConfig(name=options.remote, python=native_python(options), provider=options.remote_provider, arch=options.remote_arch) |
| 412 |
targets = controller_targets(mode, options, controller) |
| 413 |
else: |
| 414 |
context, reason = f'--remote {options.remote} --python {options.python}', FallbackReason.PYTHON |
| 415 |
controller = None |
| 416 |
targets = [PosixRemoteConfig(name=options.remote, python=native_python(options), provider=options.remote_provider, arch=options.remote_arch)] |
| 417 |
|
| 418 |
if not controller: |
| 419 |
if docker_available(): |
| 420 |
controller_fallback = f'docker:{docker_fallback}', context, reason |
| 421 |
controller = DockerConfig(name=docker_fallback) |
| 422 |
else: |
| 423 |
controller_fallback = f'remote:{remote_fallback}', context, reason |
| 424 |
controller = PosixRemoteConfig(name=remote_fallback) |
| 425 |
else: # local/unspecified |
| 426 |
# There are several changes in behavior from the legacy implementation when using no delegation (or the `--local` option). |
| 427 |
# These changes are due to ansible-test now maintaining consistency between its own Python and that of controller Python subprocesses. |
| 428 |
# |
| 429 |
# 1) The `--python-interpreter` option (if different from sys.executable) now affects controller subprocesses and triggers re-execution of ansible-test. |
| 430 |
# Previously this option was completely ignored except when used with the `--docker` or `--remote` options. |
| 431 |
# 2) The `--python` option now triggers re-execution of ansible-test if it differs from sys.version_info. |
| 432 |
# Previously it affected Python subprocesses, but not ansible-test itself. |
| 433 |
|
| 434 |
if controller_python(options.python) or not options.python: |
| 435 |
controller = OriginConfig(python=native_python(options)) |
| 436 |
targets = controller_targets(mode, options, controller) |
| 437 |
else: |
| 438 |
controller_fallback = 'origin:python=default', f'--python {options.python}', FallbackReason.PYTHON |
| 439 |
controller = OriginConfig() |
| 440 |
targets = controller_targets(mode, options, controller) |
| 441 |
|
| 442 |
if controller_fallback: |
| 443 |
controller_option, context, reason = controller_fallback |
| 444 |
|
| 445 |
if mode.no_fallback: |
| 446 |
raise ControllerNotSupportedError(context) |
| 447 |
|
| 448 |
fallback_detail = FallbackDetail( |
| 449 |
reason=reason, |
| 450 |
message=f'Using `--controller {controller_option}` since `{context}` does not support the controller.', |
| 451 |
) |
| 452 |
else: |
| 453 |
fallback_detail = None |
| 454 |
|
| 455 |
if mode.one_host and any(not isinstance(target, ControllerConfig) for target in targets): |
| 456 |
raise ControllerNotSupportedError(controller_fallback[1]) |
| 457 |
|
| 458 |
if mode == TargetMode.NO_TARGETS: |
| 459 |
targets = [] |
| 460 |
else: |
| 461 |
targets = handle_non_posix_targets(mode, options, targets) |
| 462 |
|
| 463 |
return controller, targets, fallback_detail |
| 464 |
|
| 465 |
|
| 466 |
def handle_non_posix_targets( |
| 467 |
mode: TargetMode, |
| 468 |
options: LegacyHostOptions, |
| 469 |
targets: list[HostConfig], |
| 470 |
) -> list[HostConfig]: |
| 471 |
"""Return a list of non-POSIX targets if the target mode is non-POSIX.""" |
| 472 |
if mode == TargetMode.WINDOWS_INTEGRATION: |
| 473 |
if options.windows: |
| 474 |
names = resolve_windows_names(options.windows) |
| 475 |
targets = [WindowsRemoteConfig(name=name, provider=options.remote_provider, arch=options.remote_arch) for name in names] |
| 476 |
else: |
| 477 |
targets = [WindowsInventoryConfig(path=options.inventory)] |
| 478 |
elif mode == TargetMode.NETWORK_INTEGRATION: |
| 479 |
if options.platform: |
| 480 |
network_targets = [NetworkRemoteConfig(name=platform, provider=options.remote_provider, arch=options.remote_arch) for platform in options.platform] |
| 481 |
|
| 482 |
for platform, collection in options.platform_collection or []: |
| 483 |
for entry in network_targets: |
| 484 |
if entry.platform == platform: |
| 485 |
entry.collection = collection |
| 486 |
|
| 487 |
for platform, connection in options.platform_connection or []: |
| 488 |
for entry in network_targets: |
| 489 |
if entry.platform == platform: |
| 490 |
entry.connection = connection |
| 491 |
|
| 492 |
targets = t.cast(list[HostConfig], network_targets) |
| 493 |
else: |
| 494 |
targets = [NetworkInventoryConfig(path=options.inventory)] |
| 495 |
|
| 496 |
return targets |
| 497 |
|
| 498 |
|
| 499 |
def resolve_windows_names(versions: list[str]) -> list[str]: |
| 500 |
"""Resolve a list of Windows versions into version names, resolving any aliases.""" |
| 501 |
windows_completions = filter_completion(windows_completion()) |
| 502 |
|
| 503 |
names = [f'windows/{version}' for version in versions] # map versions to names |
| 504 |
names = [windows_completions[name].name if name in windows_completions else name for name in names] # resolve aliases |
| 505 |
|
| 506 |
return names |
| 507 |
|
| 508 |
|
| 509 |
def default_targets( |
| 510 |
mode: TargetMode, |
| 511 |
controller: ControllerHostConfig, |
| 512 |
) -> list[HostConfig]: |
| 513 |
"""Return a list of default targets for the given target mode.""" |
| 514 |
targets: list[HostConfig] |
| 515 |
|
| 516 |
if mode == TargetMode.WINDOWS_INTEGRATION: |
| 517 |
targets = [WindowsInventoryConfig(path=os.path.abspath(os.path.join(data_context().content.integration_path, 'inventory.winrm')))] |
| 518 |
elif mode == TargetMode.NETWORK_INTEGRATION: |
| 519 |
targets = [NetworkInventoryConfig(path=os.path.abspath(os.path.join(data_context().content.integration_path, 'inventory.networking')))] |
| 520 |
elif mode.multiple_pythons: |
| 521 |
targets = t.cast(list[HostConfig], controller.get_default_targets(HostContext(controller_config=controller))) |
| 522 |
else: |
| 523 |
targets = [ControllerConfig()] |
| 524 |
|
| 525 |
return targets |