| 1 |
"""Command line parsing for test environments.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import argparse |
| 6 |
import enum |
| 7 |
import functools |
| 8 |
import os |
| 9 |
import typing as t |
| 10 |
|
| 11 |
from ..constants import ( |
| 12 |
CONTROLLER_PYTHON_VERSIONS, |
| 13 |
REMOTE_PROVIDERS, |
| 14 |
SECCOMP_CHOICES, |
| 15 |
SUPPORTED_PYTHON_VERSIONS, |
| 16 |
) |
| 17 |
|
| 18 |
from ..util import ( |
| 19 |
REMOTE_ARCHITECTURES, |
| 20 |
) |
| 21 |
|
| 22 |
from ..completion import ( |
| 23 |
docker_completion, |
| 24 |
network_completion, |
| 25 |
remote_completion, |
| 26 |
windows_completion, |
| 27 |
filter_completion, |
| 28 |
) |
| 29 |
|
| 30 |
from ..cli.argparsing import ( |
| 31 |
CompositeAction, |
| 32 |
CompositeActionCompletionFinder, |
| 33 |
) |
| 34 |
|
| 35 |
from ..cli.argparsing.actions import ( |
| 36 |
EnumAction, |
| 37 |
) |
| 38 |
|
| 39 |
from ..cli.actions import ( |
| 40 |
DelegatedControllerAction, |
| 41 |
NetworkSshTargetAction, |
| 42 |
NetworkTargetAction, |
| 43 |
OriginControllerAction, |
| 44 |
PosixSshTargetAction, |
| 45 |
PosixTargetAction, |
| 46 |
SanityPythonTargetAction, |
| 47 |
UnitsPythonTargetAction, |
| 48 |
WindowsSshTargetAction, |
| 49 |
WindowsTargetAction, |
| 50 |
) |
| 51 |
|
| 52 |
from ..cli.compat import ( |
| 53 |
TargetMode, |
| 54 |
) |
| 55 |
|
| 56 |
from ..config import ( |
| 57 |
TerminateMode, |
| 58 |
) |
| 59 |
|
| 60 |
from .completers import ( |
| 61 |
complete_choices, |
| 62 |
register_completer, |
| 63 |
) |
| 64 |
|
| 65 |
from .converters import ( |
| 66 |
key_value_type, |
| 67 |
) |
| 68 |
|
| 69 |
from .epilog import ( |
| 70 |
get_epilog, |
| 71 |
) |
| 72 |
|
| 73 |
from ..ci import ( |
| 74 |
get_ci_provider, |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
class ControllerMode(enum.Enum): |
| 79 |
"""Type of provisioning to use for the controller.""" |
| 80 |
|
| 81 |
NO_DELEGATION = enum.auto() |
| 82 |
ORIGIN = enum.auto() |
| 83 |
DELEGATED = enum.auto() |
| 84 |
|
| 85 |
|
| 86 |
def add_environments( |
| 87 |
parser: argparse.ArgumentParser, |
| 88 |
completer: CompositeActionCompletionFinder, |
| 89 |
controller_mode: ControllerMode, |
| 90 |
target_mode: TargetMode, |
| 91 |
) -> None: |
| 92 |
"""Add arguments for the environments used to run ansible-test and commands it invokes.""" |
| 93 |
no_environment = controller_mode == ControllerMode.NO_DELEGATION and target_mode == TargetMode.NO_TARGETS |
| 94 |
|
| 95 |
parser.set_defaults(no_environment=no_environment) |
| 96 |
|
| 97 |
if no_environment: |
| 98 |
return |
| 99 |
|
| 100 |
parser.set_defaults(target_mode=target_mode) |
| 101 |
|
| 102 |
add_global_options(parser, controller_mode) |
| 103 |
add_legacy_environment_options(parser, controller_mode, target_mode) |
| 104 |
action_types = add_composite_environment_options(parser, completer, controller_mode, target_mode) |
| 105 |
|
| 106 |
sections = [f'{heading}\n{content}' |
| 107 |
for action_type, documentation_state in CompositeAction.documentation_state.items() if action_type in action_types |
| 108 |
for heading, content in documentation_state.sections.items()] |
| 109 |
|
| 110 |
if not get_ci_provider().supports_core_ci_auth(): |
| 111 |
sections.append('Remote provisioning options have been hidden since no Ansible Core CI API key was found.') |
| 112 |
|
| 113 |
sections.append(get_epilog(completer)) |
| 114 |
|
| 115 |
parser.formatter_class = argparse.RawDescriptionHelpFormatter |
| 116 |
parser.epilog = '\n\n'.join(sections) |
| 117 |
|
| 118 |
|
| 119 |
def add_global_options( |
| 120 |
parser: argparse.ArgumentParser, |
| 121 |
controller_mode: ControllerMode, |
| 122 |
): |
| 123 |
"""Add global options for controlling the test environment that work with both the legacy and composite options.""" |
| 124 |
global_parser = t.cast(argparse.ArgumentParser, parser.add_argument_group(title='global environment arguments')) |
| 125 |
|
| 126 |
global_parser.add_argument( |
| 127 |
'--containers', |
| 128 |
metavar='JSON', |
| 129 |
help=argparse.SUPPRESS, |
| 130 |
) |
| 131 |
|
| 132 |
global_parser.add_argument( |
| 133 |
'--pypi-proxy', |
| 134 |
action='store_true', |
| 135 |
help=argparse.SUPPRESS, |
| 136 |
) |
| 137 |
|
| 138 |
global_parser.add_argument( |
| 139 |
'--pypi-endpoint', |
| 140 |
metavar='URI', |
| 141 |
help=argparse.SUPPRESS, |
| 142 |
) |
| 143 |
|
| 144 |
global_parser.add_argument( |
| 145 |
'--requirements', |
| 146 |
action='store_true', |
| 147 |
default=False, |
| 148 |
help='install command requirements', |
| 149 |
) |
| 150 |
|
| 151 |
global_parser.add_argument( |
| 152 |
'--host-path', |
| 153 |
help=argparse.SUPPRESS, # for internal use only by ansible-test |
| 154 |
) |
| 155 |
|
| 156 |
global_parser.add_argument( |
| 157 |
'--metadata', |
| 158 |
default=os.environ.get('ANSIBLE_TEST_METADATA_PATH'), |
| 159 |
help=argparse.SUPPRESS, # for internal use only by ansible-test |
| 160 |
) |
| 161 |
|
| 162 |
add_global_remote(global_parser, controller_mode) |
| 163 |
add_global_docker(global_parser, controller_mode) |
| 164 |
add_global_debug(global_parser) |
| 165 |
|
| 166 |
|
| 167 |
def add_composite_environment_options( |
| 168 |
parser: argparse.ArgumentParser, |
| 169 |
completer: CompositeActionCompletionFinder, |
| 170 |
controller_mode: ControllerMode, |
| 171 |
target_mode: TargetMode, |
| 172 |
) -> list[t.Type[CompositeAction]]: |
| 173 |
"""Add composite options for controlling the test environment.""" |
| 174 |
composite_parser = t.cast(argparse.ArgumentParser, parser.add_argument_group( |
| 175 |
title='composite environment arguments (mutually exclusive with "environment arguments" above)')) |
| 176 |
|
| 177 |
action_types: list[t.Type[CompositeAction]] = [] |
| 178 |
|
| 179 |
def register_action_type(action_type: t.Type[CompositeAction]) -> t.Type[CompositeAction]: |
| 180 |
"""Register the provided composite action type and return it.""" |
| 181 |
action_types.append(action_type) |
| 182 |
return action_type |
| 183 |
|
| 184 |
if controller_mode == ControllerMode.NO_DELEGATION: |
| 185 |
composite_parser.set_defaults(controller=None) |
| 186 |
else: |
| 187 |
register_completer(composite_parser.add_argument( |
| 188 |
'--controller', |
| 189 |
metavar='OPT', |
| 190 |
action=register_action_type(DelegatedControllerAction if controller_mode == ControllerMode.DELEGATED else OriginControllerAction), |
| 191 |
help='configuration for the controller', |
| 192 |
), completer.completer) |
| 193 |
|
| 194 |
if target_mode == TargetMode.NO_TARGETS: |
| 195 |
composite_parser.set_defaults(targets=[]) |
| 196 |
elif target_mode == TargetMode.SHELL: |
| 197 |
group = composite_parser.add_mutually_exclusive_group() |
| 198 |
|
| 199 |
register_completer(group.add_argument( |
| 200 |
'--target-posix', |
| 201 |
metavar='OPT', |
| 202 |
action=register_action_type(PosixSshTargetAction), |
| 203 |
help='configuration for the target', |
| 204 |
), completer.completer) |
| 205 |
|
| 206 |
suppress = None if get_ci_provider().supports_core_ci_auth() else argparse.SUPPRESS |
| 207 |
|
| 208 |
register_completer(group.add_argument( |
| 209 |
'--target-windows', |
| 210 |
metavar='OPT', |
| 211 |
action=WindowsSshTargetAction if suppress else register_action_type(WindowsSshTargetAction), |
| 212 |
help=suppress or 'configuration for the target', |
| 213 |
), completer.completer) |
| 214 |
|
| 215 |
register_completer(group.add_argument( |
| 216 |
'--target-network', |
| 217 |
metavar='OPT', |
| 218 |
action=NetworkSshTargetAction if suppress else register_action_type(NetworkSshTargetAction), |
| 219 |
help=suppress or 'configuration for the target', |
| 220 |
), completer.completer) |
| 221 |
else: |
| 222 |
if target_mode.multiple_pythons: |
| 223 |
target_option = '--target-python' |
| 224 |
target_help = 'configuration for the target python interpreter(s)' |
| 225 |
elif target_mode == TargetMode.POSIX_INTEGRATION: |
| 226 |
target_option = '--target' |
| 227 |
target_help = 'configuration for the target' |
| 228 |
else: |
| 229 |
target_option = '--target' |
| 230 |
target_help = 'configuration for the target(s)' |
| 231 |
|
| 232 |
target_actions = { |
| 233 |
TargetMode.POSIX_INTEGRATION: PosixTargetAction, |
| 234 |
TargetMode.WINDOWS_INTEGRATION: WindowsTargetAction, |
| 235 |
TargetMode.NETWORK_INTEGRATION: NetworkTargetAction, |
| 236 |
TargetMode.SANITY: SanityPythonTargetAction, |
| 237 |
TargetMode.UNITS: UnitsPythonTargetAction, |
| 238 |
} |
| 239 |
|
| 240 |
target_action = target_actions[target_mode] |
| 241 |
|
| 242 |
register_completer(composite_parser.add_argument( |
| 243 |
target_option, |
| 244 |
metavar='OPT', |
| 245 |
action=register_action_type(target_action), |
| 246 |
help=target_help, |
| 247 |
), completer.completer) |
| 248 |
|
| 249 |
return action_types |
| 250 |
|
| 251 |
|
| 252 |
def add_legacy_environment_options( |
| 253 |
parser: argparse.ArgumentParser, |
| 254 |
controller_mode: ControllerMode, |
| 255 |
target_mode: TargetMode, |
| 256 |
): |
| 257 |
"""Add legacy options for controlling the test environment.""" |
| 258 |
environment: argparse.ArgumentParser = parser.add_argument_group( # type: ignore[assignment] # real type private |
| 259 |
title='environment arguments (mutually exclusive with "composite environment arguments" below)', |
| 260 |
) |
| 261 |
|
| 262 |
add_environments_python(environment, target_mode) |
| 263 |
add_environments_host(environment, controller_mode, target_mode) |
| 264 |
|
| 265 |
|
| 266 |
def add_environments_python( |
| 267 |
environments_parser: argparse.ArgumentParser, |
| 268 |
target_mode: TargetMode, |
| 269 |
) -> None: |
| 270 |
"""Add environment arguments to control the Python version(s) used.""" |
| 271 |
python_versions: tuple[str, ...] |
| 272 |
|
| 273 |
if target_mode.has_python: |
| 274 |
python_versions = SUPPORTED_PYTHON_VERSIONS |
| 275 |
else: |
| 276 |
python_versions = CONTROLLER_PYTHON_VERSIONS |
| 277 |
|
| 278 |
environments_parser.add_argument( |
| 279 |
'--python', |
| 280 |
metavar='X.Y', |
| 281 |
choices=python_versions + ('default',), |
| 282 |
help='python version: %s' % ', '.join(python_versions), |
| 283 |
) |
| 284 |
|
| 285 |
environments_parser.add_argument( |
| 286 |
'--python-interpreter', |
| 287 |
metavar='PATH', |
| 288 |
help='path to the python interpreter', |
| 289 |
) |
| 290 |
|
| 291 |
|
| 292 |
def add_environments_host( |
| 293 |
environments_parser: argparse.ArgumentParser, |
| 294 |
controller_mode: ControllerMode, |
| 295 |
target_mode: TargetMode, |
| 296 |
) -> None: |
| 297 |
"""Add environment arguments for the given host and argument modes.""" |
| 298 |
environments_exclusive_group: argparse.ArgumentParser = environments_parser.add_mutually_exclusive_group() # type: ignore[assignment] # real type private |
| 299 |
|
| 300 |
add_environment_local(environments_exclusive_group) |
| 301 |
add_environment_venv(environments_exclusive_group, environments_parser) |
| 302 |
|
| 303 |
if controller_mode == ControllerMode.DELEGATED: |
| 304 |
add_environment_remote(environments_exclusive_group, environments_parser, target_mode) |
| 305 |
add_environment_docker(environments_exclusive_group, environments_parser, target_mode) |
| 306 |
|
| 307 |
if target_mode == TargetMode.WINDOWS_INTEGRATION: |
| 308 |
add_environment_windows(environments_parser) |
| 309 |
|
| 310 |
if target_mode == TargetMode.NETWORK_INTEGRATION: |
| 311 |
add_environment_network(environments_parser) |
| 312 |
|
| 313 |
|
| 314 |
def add_environment_network( |
| 315 |
environments_parser: argparse.ArgumentParser, |
| 316 |
) -> None: |
| 317 |
"""Add environment arguments for running on a windows host.""" |
| 318 |
register_completer(environments_parser.add_argument( |
| 319 |
'--platform', |
| 320 |
metavar='PLATFORM', |
| 321 |
action='append', |
| 322 |
help='network platform/version', |
| 323 |
), complete_network_platform) |
| 324 |
|
| 325 |
register_completer(environments_parser.add_argument( |
| 326 |
'--platform-collection', |
| 327 |
type=key_value_type, |
| 328 |
metavar='PLATFORM=COLLECTION', |
| 329 |
action='append', |
| 330 |
help='collection used to test platform', |
| 331 |
), complete_network_platform_collection) |
| 332 |
|
| 333 |
register_completer(environments_parser.add_argument( |
| 334 |
'--platform-connection', |
| 335 |
type=key_value_type, |
| 336 |
metavar='PLATFORM=CONNECTION', |
| 337 |
action='append', |
| 338 |
help='connection used to test platform', |
| 339 |
), complete_network_platform_connection) |
| 340 |
|
| 341 |
environments_parser.add_argument( |
| 342 |
'--inventory', |
| 343 |
metavar='PATH', |
| 344 |
help='path to inventory used for tests', |
| 345 |
) |
| 346 |
|
| 347 |
|
| 348 |
def add_environment_windows( |
| 349 |
environments_parser: argparse.ArgumentParser, |
| 350 |
) -> None: |
| 351 |
"""Add environment arguments for running on a windows host.""" |
| 352 |
register_completer(environments_parser.add_argument( |
| 353 |
'--windows', |
| 354 |
metavar='VERSION', |
| 355 |
action='append', |
| 356 |
help='windows version', |
| 357 |
), complete_windows) |
| 358 |
|
| 359 |
environments_parser.add_argument( |
| 360 |
'--inventory', |
| 361 |
metavar='PATH', |
| 362 |
help='path to inventory used for tests', |
| 363 |
) |
| 364 |
|
| 365 |
|
| 366 |
def add_environment_local( |
| 367 |
exclusive_parser: argparse.ArgumentParser, |
| 368 |
) -> None: |
| 369 |
"""Add environment arguments for running on the local (origin) host.""" |
| 370 |
exclusive_parser.add_argument( |
| 371 |
'--local', |
| 372 |
action='store_true', |
| 373 |
help='run from the local environment', |
| 374 |
) |
| 375 |
|
| 376 |
|
| 377 |
def add_environment_venv( |
| 378 |
exclusive_parser: argparse.ArgumentParser, |
| 379 |
environments_parser: argparse.ArgumentParser, |
| 380 |
) -> None: |
| 381 |
"""Add environment arguments for running in ansible-test managed virtual environments.""" |
| 382 |
exclusive_parser.add_argument( |
| 383 |
'--venv', |
| 384 |
action='store_true', |
| 385 |
help='run from a virtual environment', |
| 386 |
) |
| 387 |
|
| 388 |
environments_parser.add_argument( |
| 389 |
'--venv-system-site-packages', |
| 390 |
action='store_true', |
| 391 |
help='enable system site packages', |
| 392 |
) |
| 393 |
|
| 394 |
|
| 395 |
def add_global_docker( |
| 396 |
parser: argparse.ArgumentParser, |
| 397 |
controller_mode: ControllerMode, |
| 398 |
) -> None: |
| 399 |
"""Add global options for Docker.""" |
| 400 |
if controller_mode != ControllerMode.DELEGATED: |
| 401 |
parser.set_defaults( |
| 402 |
docker_network=None, |
| 403 |
docker_terminate=None, |
| 404 |
prime_containers=False, |
| 405 |
dev_systemd_debug=False, |
| 406 |
dev_probe_cgroups=None, |
| 407 |
) |
| 408 |
|
| 409 |
return |
| 410 |
|
| 411 |
parser.add_argument( |
| 412 |
'--docker-network', |
| 413 |
metavar='NET', |
| 414 |
help='run using the specified network', |
| 415 |
) |
| 416 |
|
| 417 |
parser.add_argument( |
| 418 |
'--docker-terminate', |
| 419 |
metavar='T', |
| 420 |
default=TerminateMode.ALWAYS, |
| 421 |
type=TerminateMode, |
| 422 |
action=EnumAction, |
| 423 |
help='terminate the container: %(choices)s (default: %(default)s)', |
| 424 |
) |
| 425 |
|
| 426 |
parser.add_argument( |
| 427 |
'--prime-containers', |
| 428 |
action='store_true', |
| 429 |
help='download containers without running tests', |
| 430 |
) |
| 431 |
|
| 432 |
# Docker support isn't related to ansible-core-ci. |
| 433 |
# However, ansible-core-ci support is a reasonable indicator that the user may need the `--dev-*` options. |
| 434 |
suppress = None if get_ci_provider().supports_core_ci_auth() else argparse.SUPPRESS |
| 435 |
|
| 436 |
parser.add_argument( |
| 437 |
'--dev-systemd-debug', |
| 438 |
action='store_true', |
| 439 |
help=suppress or 'enable systemd debugging in containers', |
| 440 |
) |
| 441 |
|
| 442 |
parser.add_argument( |
| 443 |
'--dev-probe-cgroups', |
| 444 |
metavar='DIR', |
| 445 |
nargs='?', |
| 446 |
const='', |
| 447 |
help=suppress or 'probe container cgroups, with optional log dir', |
| 448 |
) |
| 449 |
|
| 450 |
|
| 451 |
def add_global_debug( |
| 452 |
parser: argparse.ArgumentParser, |
| 453 |
) -> None: |
| 454 |
"""Add global debug options.""" |
| 455 |
# These `--dev-*` options are experimental features that may change or be removed without regard for backward compatibility. |
| 456 |
# Additionally, they're features that are not likely to be used by most users. |
| 457 |
# To avoid confusion, they're hidden from `--help` and tab completion by default, except for ansible-core-ci users. |
| 458 |
suppress = None if get_ci_provider().supports_core_ci_auth() else argparse.SUPPRESS |
| 459 |
|
| 460 |
parser.add_argument( |
| 461 |
'--dev-debug-on-demand', |
| 462 |
action='store_true', |
| 463 |
default=False, |
| 464 |
help=suppress or 'enable remote debugging only under a debugger', |
| 465 |
) |
| 466 |
|
| 467 |
parser.add_argument( |
| 468 |
'--dev-debug-cli', |
| 469 |
action='store_true', |
| 470 |
default=False, |
| 471 |
help=suppress or 'enable remote debugging for the Ansible CLI', |
| 472 |
) |
| 473 |
|
| 474 |
parser.add_argument( |
| 475 |
'--dev-debug-ansiballz', |
| 476 |
action='store_true', |
| 477 |
default=False, |
| 478 |
help=suppress or 'enable remote debugging for AnsiballZ modules', |
| 479 |
) |
| 480 |
|
| 481 |
parser.add_argument( |
| 482 |
'--dev-debug-self', |
| 483 |
action='store_true', |
| 484 |
default=False, |
| 485 |
help=suppress or 'enable remote debugging for ansible-test', |
| 486 |
) |
| 487 |
|
| 488 |
|
| 489 |
def add_environment_docker( |
| 490 |
exclusive_parser: argparse.ArgumentParser, |
| 491 |
environments_parser: argparse.ArgumentParser, |
| 492 |
target_mode: TargetMode, |
| 493 |
) -> None: |
| 494 |
"""Add environment arguments for running in docker containers.""" |
| 495 |
if target_mode in (TargetMode.POSIX_INTEGRATION, TargetMode.SHELL): |
| 496 |
docker_images = sorted(filter_completion(docker_completion())) |
| 497 |
else: |
| 498 |
docker_images = sorted(filter_completion(docker_completion(), controller_only=True)) |
| 499 |
|
| 500 |
register_completer(exclusive_parser.add_argument( |
| 501 |
'--docker', |
| 502 |
metavar='IMAGE', |
| 503 |
nargs='?', |
| 504 |
const='default', |
| 505 |
help='run from a docker container', |
| 506 |
), functools.partial(complete_choices, docker_images)) |
| 507 |
|
| 508 |
environments_parser.add_argument( |
| 509 |
'--docker-privileged', |
| 510 |
action='store_true', |
| 511 |
help='run docker container in privileged mode', |
| 512 |
) |
| 513 |
|
| 514 |
environments_parser.add_argument( |
| 515 |
'--docker-seccomp', |
| 516 |
metavar='SC', |
| 517 |
choices=SECCOMP_CHOICES, |
| 518 |
help='set seccomp confinement for the test container: %(choices)s', |
| 519 |
) |
| 520 |
|
| 521 |
environments_parser.add_argument( |
| 522 |
'--docker-memory', |
| 523 |
metavar='INT', |
| 524 |
type=int, |
| 525 |
help='memory limit for docker in bytes', |
| 526 |
) |
| 527 |
|
| 528 |
|
| 529 |
def add_global_remote( |
| 530 |
parser: argparse.ArgumentParser, |
| 531 |
controller_mode: ControllerMode, |
| 532 |
) -> None: |
| 533 |
"""Add global options for remote instances.""" |
| 534 |
if controller_mode != ControllerMode.DELEGATED: |
| 535 |
parser.set_defaults( |
| 536 |
remote_stage=None, |
| 537 |
remote_endpoint=None, |
| 538 |
remote_terminate=None, |
| 539 |
) |
| 540 |
|
| 541 |
return |
| 542 |
|
| 543 |
suppress = None if get_ci_provider().supports_core_ci_auth() else argparse.SUPPRESS |
| 544 |
|
| 545 |
register_completer(parser.add_argument( |
| 546 |
'--remote-stage', |
| 547 |
metavar='STAGE', |
| 548 |
default='prod', |
| 549 |
help=suppress or 'remote stage to use: prod, dev', |
| 550 |
), complete_remote_stage) |
| 551 |
|
| 552 |
parser.add_argument( |
| 553 |
'--remote-endpoint', |
| 554 |
metavar='EP', |
| 555 |
help=suppress or 'remote provisioning endpoint to use', |
| 556 |
) |
| 557 |
|
| 558 |
parser.add_argument( |
| 559 |
'--remote-terminate', |
| 560 |
metavar='T', |
| 561 |
default=TerminateMode.NEVER, |
| 562 |
type=TerminateMode, |
| 563 |
action=EnumAction, |
| 564 |
help=suppress or 'terminate the remote instance: %(choices)s (default: %(default)s)', |
| 565 |
) |
| 566 |
|
| 567 |
|
| 568 |
def add_environment_remote( |
| 569 |
exclusive_parser: argparse.ArgumentParser, |
| 570 |
environments_parser: argparse.ArgumentParser, |
| 571 |
target_mode: TargetMode, |
| 572 |
) -> None: |
| 573 |
"""Add environment arguments for running in ansible-core-ci provisioned remote virtual machines.""" |
| 574 |
if target_mode == TargetMode.POSIX_INTEGRATION: |
| 575 |
remote_platforms = get_remote_platform_choices() |
| 576 |
elif target_mode == TargetMode.SHELL: |
| 577 |
remote_platforms = sorted(set(get_remote_platform_choices()) | set(get_windows_platform_choices())) |
| 578 |
else: |
| 579 |
remote_platforms = get_remote_platform_choices(True) |
| 580 |
|
| 581 |
suppress = None if get_ci_provider().supports_core_ci_auth() else argparse.SUPPRESS |
| 582 |
|
| 583 |
register_completer(exclusive_parser.add_argument( |
| 584 |
'--remote', |
| 585 |
metavar='NAME', |
| 586 |
help=suppress or 'run from a remote instance', |
| 587 |
), functools.partial(complete_choices, remote_platforms)) |
| 588 |
|
| 589 |
environments_parser.add_argument( |
| 590 |
'--remote-provider', |
| 591 |
metavar='PR', |
| 592 |
choices=REMOTE_PROVIDERS, |
| 593 |
help=suppress or 'remote provider to use: %(choices)s', |
| 594 |
) |
| 595 |
|
| 596 |
environments_parser.add_argument( |
| 597 |
'--remote-arch', |
| 598 |
metavar='ARCH', |
| 599 |
choices=REMOTE_ARCHITECTURES, |
| 600 |
help=suppress or 'remote arch to use: %(choices)s', |
| 601 |
) |
| 602 |
|
| 603 |
|
| 604 |
def complete_remote_stage(prefix: str, **_) -> list[str]: |
| 605 |
"""Return a list of supported stages matching the given prefix.""" |
| 606 |
return [stage for stage in ('prod', 'dev') if stage.startswith(prefix)] |
| 607 |
|
| 608 |
|
| 609 |
def complete_windows(prefix: str, parsed_args: argparse.Namespace, **_) -> list[str]: |
| 610 |
"""Return a list of supported Windows versions matching the given prefix, excluding versions already parsed from the command line.""" |
| 611 |
return [i for i in get_windows_version_choices() if i.startswith(prefix) and (not parsed_args.windows or i not in parsed_args.windows)] |
| 612 |
|
| 613 |
|
| 614 |
def complete_network_platform(prefix: str, parsed_args: argparse.Namespace, **_) -> list[str]: |
| 615 |
"""Return a list of supported network platforms matching the given prefix, excluding platforms already parsed from the command line.""" |
| 616 |
images = sorted(filter_completion(network_completion())) |
| 617 |
|
| 618 |
return [i for i in images if i.startswith(prefix) and (not parsed_args.platform or i not in parsed_args.platform)] |
| 619 |
|
| 620 |
|
| 621 |
def complete_network_platform_collection(prefix: str, parsed_args: argparse.Namespace, **_) -> list[str]: |
| 622 |
"""Return a list of supported network platforms matching the given prefix, excluding collection platforms already parsed from the command line.""" |
| 623 |
left = prefix.split('=')[0] |
| 624 |
images = sorted(set(image.platform for image in filter_completion(network_completion()).values())) |
| 625 |
|
| 626 |
return [i + '=' for i in images if i.startswith(left) and (not parsed_args.platform_collection or i not in [x[0] for x in parsed_args.platform_collection])] |
| 627 |
|
| 628 |
|
| 629 |
def complete_network_platform_connection(prefix: str, parsed_args: argparse.Namespace, **_) -> list[str]: |
| 630 |
"""Return a list of supported network platforms matching the given prefix, excluding connection platforms already parsed from the command line.""" |
| 631 |
left = prefix.split('=')[0] |
| 632 |
images = sorted(set(image.platform for image in filter_completion(network_completion()).values())) |
| 633 |
|
| 634 |
return [i + '=' for i in images if i.startswith(left) and (not parsed_args.platform_connection or i not in [x[0] for x in parsed_args.platform_connection])] |
| 635 |
|
| 636 |
|
| 637 |
def get_remote_platform_choices(controller: bool = False) -> list[str]: |
| 638 |
"""Return a list of supported remote platforms matching the given prefix.""" |
| 639 |
return sorted(filter_completion(remote_completion(), controller_only=controller)) |
| 640 |
|
| 641 |
|
| 642 |
def get_windows_platform_choices() -> list[str]: |
| 643 |
"""Return a list of supported Windows version names.""" |
| 644 |
return sorted(filter_completion(windows_completion())) |
| 645 |
|
| 646 |
|
| 647 |
def get_windows_version_choices() -> list[str]: |
| 648 |
"""Return a list of supported Windows versions.""" |
| 649 |
return sorted(name.removeprefix("windows/") for name in get_windows_platform_choices()) |