| 1 |
"""Configuration classes.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import dataclasses |
| 6 |
import enum |
| 7 |
import os |
| 8 |
import sys |
| 9 |
import typing as t |
| 10 |
|
| 11 |
from .util import ( |
| 12 |
verify_sys_executable, |
| 13 |
version_to_str, |
| 14 |
type_guard, |
| 15 |
) |
| 16 |
|
| 17 |
from .util_common import ( |
| 18 |
CommonConfig, |
| 19 |
) |
| 20 |
|
| 21 |
from .metadata import ( |
| 22 |
Metadata, |
| 23 |
DebuggerFlags, |
| 24 |
) |
| 25 |
|
| 26 |
from .data import ( |
| 27 |
data_context, |
| 28 |
PayloadConfig, |
| 29 |
) |
| 30 |
|
| 31 |
from .host_configs import ( |
| 32 |
ControllerConfig, |
| 33 |
ControllerHostConfig, |
| 34 |
HostConfig, |
| 35 |
HostSettings, |
| 36 |
OriginConfig, |
| 37 |
PythonConfig, |
| 38 |
VirtualPythonConfig, |
| 39 |
PowerShellConfig, |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
class TerminateMode(enum.Enum): |
| 44 |
"""When to terminate instances.""" |
| 45 |
|
| 46 |
ALWAYS = enum.auto() |
| 47 |
NEVER = enum.auto() |
| 48 |
SUCCESS = enum.auto() |
| 49 |
|
| 50 |
def __str__(self): |
| 51 |
return self.name.lower() |
| 52 |
|
| 53 |
|
| 54 |
@dataclasses.dataclass(frozen=True) |
| 55 |
class ModulesConfig: |
| 56 |
"""Configuration for modules.""" |
| 57 |
|
| 58 |
python_requires: str |
| 59 |
python_versions: tuple[str, ...] |
| 60 |
controller_only: bool |
| 61 |
|
| 62 |
|
| 63 |
@dataclasses.dataclass(frozen=True) |
| 64 |
class ContentConfig: |
| 65 |
"""Configuration for all content.""" |
| 66 |
|
| 67 |
modules: ModulesConfig |
| 68 |
python_versions: tuple[str, ...] |
| 69 |
|
| 70 |
|
| 71 |
class EnvironmentConfig(CommonConfig): |
| 72 |
"""Configuration common to all commands which execute in an environment.""" |
| 73 |
|
| 74 |
def __init__(self, args: t.Any, command: str) -> None: |
| 75 |
super().__init__(args, command) |
| 76 |
|
| 77 |
self.host_settings: HostSettings = args.host_settings |
| 78 |
self.host_path: t.Optional[str] = args.host_path |
| 79 |
self.containers: t.Optional[str] = args.containers |
| 80 |
self.pypi_proxy: bool = args.pypi_proxy |
| 81 |
self.pypi_endpoint: t.Optional[str] = args.pypi_endpoint |
| 82 |
|
| 83 |
# Populated by content_config.get_content_config on the origin. |
| 84 |
# Serialized and passed to delegated instances to avoid parsing a second time. |
| 85 |
self.content_config: t.Optional[ContentConfig] = None |
| 86 |
|
| 87 |
# Set by check_controller_python once HostState has been created by prepare_profiles. |
| 88 |
# This is here for convenience, to avoid needing to pass HostState to some functions which already have access to EnvironmentConfig. |
| 89 |
self.controller_python: t.Optional[PythonConfig] = None |
| 90 |
""" |
| 91 |
The Python interpreter used by the controller. |
| 92 |
Only available after delegation has been performed or skipped (if delegation is not required). |
| 93 |
""" |
| 94 |
|
| 95 |
# Set by check_controller_powershell once HostState has been created by prepare_profiles. |
| 96 |
# This is here for convenience, to avoid needing to pass HostState to some functions which already have access to EnvironmentConfig. |
| 97 |
self.controller_powershell: PowerShellConfig | None = None |
| 98 |
""" |
| 99 |
The PowerShell interpreter used by the controller. |
| 100 |
Only available after delegation has been performed or skipped (if delegation is not required). |
| 101 |
""" |
| 102 |
|
| 103 |
if self.host_path: |
| 104 |
self.delegate = False |
| 105 |
else: |
| 106 |
self.delegate = ( |
| 107 |
not isinstance(self.controller, OriginConfig) |
| 108 |
or isinstance(self.controller.python, VirtualPythonConfig) |
| 109 |
or self.controller.python.version != version_to_str(sys.version_info[:2]) |
| 110 |
or bool(verify_sys_executable(self.controller.python.path)) |
| 111 |
) |
| 112 |
|
| 113 |
self.docker_network: t.Optional[str] = args.docker_network |
| 114 |
self.docker_terminate: t.Optional[TerminateMode] = args.docker_terminate |
| 115 |
|
| 116 |
self.remote_endpoint: t.Optional[str] = args.remote_endpoint |
| 117 |
self.remote_stage: t.Optional[str] = args.remote_stage |
| 118 |
self.remote_terminate: t.Optional[TerminateMode] = args.remote_terminate |
| 119 |
|
| 120 |
self.prime_containers: bool = args.prime_containers |
| 121 |
|
| 122 |
self.requirements: bool = args.requirements |
| 123 |
|
| 124 |
self.delegate_args: list[str] = [] |
| 125 |
|
| 126 |
self.dev_systemd_debug: bool = args.dev_systemd_debug |
| 127 |
self.dev_probe_cgroups: t.Optional[str] = args.dev_probe_cgroups |
| 128 |
|
| 129 |
debugger_flags = DebuggerFlags( |
| 130 |
on_demand=args.dev_debug_on_demand, |
| 131 |
cli=args.dev_debug_cli, |
| 132 |
ansiballz=args.dev_debug_ansiballz, |
| 133 |
self=args.dev_debug_self, |
| 134 |
) |
| 135 |
|
| 136 |
self.metadata = Metadata.from_file(args.metadata) if args.metadata else Metadata(debugger_flags=debugger_flags) |
| 137 |
self.metadata_path: t.Optional[str] = None |
| 138 |
|
| 139 |
def metadata_callback(payload_config: PayloadConfig) -> None: |
| 140 |
"""Add the metadata file to the payload file list.""" |
| 141 |
config = self |
| 142 |
files = payload_config.files |
| 143 |
|
| 144 |
if config.metadata_path: |
| 145 |
files.append((os.path.abspath(config.metadata_path), config.metadata_path)) |
| 146 |
|
| 147 |
data_context().register_payload_callback(metadata_callback) |
| 148 |
|
| 149 |
def host_callback(payload_config: PayloadConfig) -> None: |
| 150 |
"""Add the host files to the payload file list.""" |
| 151 |
config = self |
| 152 |
|
| 153 |
if config.host_path: |
| 154 |
settings_path = os.path.join(config.host_path, 'settings.dat') |
| 155 |
state_path = os.path.join(config.host_path, 'state.dat') |
| 156 |
config_path = os.path.join(config.host_path, 'config.dat') |
| 157 |
|
| 158 |
files = payload_config.files |
| 159 |
|
| 160 |
files.append((os.path.abspath(settings_path), settings_path)) |
| 161 |
files.append((os.path.abspath(state_path), state_path)) |
| 162 |
files.append((os.path.abspath(config_path), config_path)) |
| 163 |
|
| 164 |
data_context().register_payload_callback(host_callback) |
| 165 |
|
| 166 |
@property |
| 167 |
def controller(self) -> ControllerHostConfig: |
| 168 |
"""Host configuration for the controller.""" |
| 169 |
return self.host_settings.controller |
| 170 |
|
| 171 |
@property |
| 172 |
def targets(self) -> list[HostConfig]: |
| 173 |
"""Host configuration for the targets.""" |
| 174 |
return self.host_settings.targets |
| 175 |
|
| 176 |
def only_target[THostConfig: HostConfig](self, target_type: t.Type[THostConfig]) -> THostConfig: |
| 177 |
""" |
| 178 |
Return the host configuration for the target. |
| 179 |
Requires that there is exactly one target of the specified type. |
| 180 |
""" |
| 181 |
targets = list(self.targets) |
| 182 |
|
| 183 |
if len(targets) != 1: |
| 184 |
raise Exception('There must be exactly one target.') |
| 185 |
|
| 186 |
target = targets.pop() |
| 187 |
|
| 188 |
if not isinstance(target, target_type): |
| 189 |
raise Exception(f'Target is {type(target_type)} instead of {target_type}.') |
| 190 |
|
| 191 |
return target |
| 192 |
|
| 193 |
def only_targets[THostConfig: HostConfig](self, target_type: t.Type[THostConfig]) -> list[THostConfig]: |
| 194 |
""" |
| 195 |
Return a list of target host configurations. |
| 196 |
Requires that there are one or more targets, all the specified type. |
| 197 |
""" |
| 198 |
if not self.targets: |
| 199 |
raise Exception('There must be one or more targets.') |
| 200 |
|
| 201 |
assert type_guard(self.targets, target_type) |
| 202 |
|
| 203 |
return t.cast(list[THostConfig], self.targets) |
| 204 |
|
| 205 |
@property |
| 206 |
def target_type(self) -> t.Type[HostConfig]: |
| 207 |
""" |
| 208 |
The true type of the target(s). |
| 209 |
If the target is the controller, the controller type is returned. |
| 210 |
Requires at least one target, and all targets must be of the same type. |
| 211 |
""" |
| 212 |
target_types = set(type(target) for target in self.targets) |
| 213 |
|
| 214 |
if len(target_types) != 1: |
| 215 |
raise Exception('There must be one or more targets, all of the same type.') |
| 216 |
|
| 217 |
target_type = target_types.pop() |
| 218 |
|
| 219 |
if issubclass(target_type, ControllerConfig): |
| 220 |
target_type = type(self.controller) |
| 221 |
|
| 222 |
return target_type |
| 223 |
|
| 224 |
|
| 225 |
class TestConfig(EnvironmentConfig): |
| 226 |
"""Configuration common to all test commands.""" |
| 227 |
|
| 228 |
def __init__(self, args: t.Any, command: str) -> None: |
| 229 |
super().__init__(args, command) |
| 230 |
|
| 231 |
self.coverage: bool = args.coverage |
| 232 |
self.coverage_check: bool = args.coverage_check |
| 233 |
self.include: list[str] = args.include or [] |
| 234 |
self.exclude: list[str] = args.exclude or [] |
| 235 |
self.require: list[str] = args.require or [] |
| 236 |
|
| 237 |
self.changed: bool = args.changed |
| 238 |
self.tracked: bool = args.tracked |
| 239 |
self.untracked: bool = args.untracked |
| 240 |
self.committed: bool = args.committed |
| 241 |
self.staged: bool = args.staged |
| 242 |
self.unstaged: bool = args.unstaged |
| 243 |
self.changed_from: str = args.changed_from |
| 244 |
self.changed_path: list[str] = args.changed_path |
| 245 |
self.base_branch: str = args.base_branch |
| 246 |
|
| 247 |
self.lint: bool = getattr(args, 'lint', False) |
| 248 |
self.junit: bool = getattr(args, 'junit', False) |
| 249 |
self.failure_ok: bool = getattr(args, 'failure_ok', False) |
| 250 |
|
| 251 |
if self.coverage_check: |
| 252 |
self.coverage = True |
| 253 |
|
| 254 |
|
| 255 |
class ShellConfig(EnvironmentConfig): |
| 256 |
"""Configuration for the shell command.""" |
| 257 |
|
| 258 |
def __init__(self, args: t.Any) -> None: |
| 259 |
super().__init__(args, 'shell') |
| 260 |
|
| 261 |
self.cmd: list[str] = args.cmd |
| 262 |
self.raw: bool = args.raw |
| 263 |
self.check_layout = self.delegate # allow shell to be used without a valid layout as long as no delegation is required |
| 264 |
self.interactive = sys.stdin.isatty() and not args.cmd # delegation should only be interactive when stdin is a TTY and no command was given |
| 265 |
self.export: t.Optional[str] = args.export |
| 266 |
self.display_stderr = True |
| 267 |
|
| 268 |
|
| 269 |
class SanityConfig(TestConfig): |
| 270 |
"""Configuration for the sanity command.""" |
| 271 |
|
| 272 |
def __init__(self, args: t.Any) -> None: |
| 273 |
super().__init__(args, 'sanity') |
| 274 |
|
| 275 |
self.test: list[str] = args.test |
| 276 |
self.skip_test: list[str] = args.skip_test |
| 277 |
self.list_tests: bool = args.list_tests |
| 278 |
self.allow_disabled: bool = args.allow_disabled |
| 279 |
self.enable_optional_errors: bool = args.enable_optional_errors |
| 280 |
self.prime_venvs: bool = args.prime_venvs |
| 281 |
self.fix: bool = getattr(args, 'fix', False) |
| 282 |
|
| 283 |
self.display_stderr = self.lint or self.list_tests |
| 284 |
|
| 285 |
|
| 286 |
class IntegrationConfig(TestConfig): |
| 287 |
"""Configuration for the integration command.""" |
| 288 |
|
| 289 |
def __init__(self, args: t.Any, command: str) -> None: |
| 290 |
super().__init__(args, command) |
| 291 |
|
| 292 |
self.start_at: str = args.start_at |
| 293 |
self.start_at_task: str = args.start_at_task |
| 294 |
self.allow_destructive: bool = args.allow_destructive |
| 295 |
self.allow_root: bool = args.allow_root |
| 296 |
self.allow_disabled: bool = args.allow_disabled |
| 297 |
self.allow_unstable: bool = args.allow_unstable |
| 298 |
self.allow_unstable_changed: bool = args.allow_unstable_changed |
| 299 |
self.allow_unsupported: bool = args.allow_unsupported |
| 300 |
self.retry_on_error: bool = args.retry_on_error |
| 301 |
self.continue_on_error: bool = args.continue_on_error |
| 302 |
self.debug_strategy: bool = args.debug_strategy |
| 303 |
self.changed_all_target: str = args.changed_all_target |
| 304 |
self.changed_all_mode: str = args.changed_all_mode |
| 305 |
self.list_targets: bool = args.list_targets |
| 306 |
self.tags = args.tags |
| 307 |
self.skip_tags = args.skip_tags |
| 308 |
self.diff = args.diff |
| 309 |
self.no_temp_workdir: bool = args.no_temp_workdir |
| 310 |
self.no_temp_unicode: bool = args.no_temp_unicode |
| 311 |
|
| 312 |
if self.list_targets: |
| 313 |
self.explain = True |
| 314 |
self.display_stderr = True |
| 315 |
|
| 316 |
def get_ansible_config(self) -> str: |
| 317 |
"""Return the path to the Ansible config for the given config.""" |
| 318 |
ansible_config_relative_path = os.path.join(data_context().content.integration_path, '%s.cfg' % self.command) |
| 319 |
ansible_config_path = os.path.join(data_context().content.root, ansible_config_relative_path) |
| 320 |
|
| 321 |
if not os.path.exists(ansible_config_path): |
| 322 |
# use the default empty configuration unless one has been provided |
| 323 |
ansible_config_path = super().get_ansible_config() |
| 324 |
|
| 325 |
return ansible_config_path |
| 326 |
|
| 327 |
|
| 328 |
class PosixIntegrationConfig(IntegrationConfig): |
| 329 |
"""Configuration for the posix integration command.""" |
| 330 |
|
| 331 |
def __init__(self, args: t.Any) -> None: |
| 332 |
super().__init__(args, 'integration') |
| 333 |
|
| 334 |
|
| 335 |
class WindowsIntegrationConfig(IntegrationConfig): |
| 336 |
"""Configuration for the windows integration command.""" |
| 337 |
|
| 338 |
def __init__(self, args: t.Any) -> None: |
| 339 |
super().__init__(args, 'windows-integration') |
| 340 |
|
| 341 |
|
| 342 |
class NetworkIntegrationConfig(IntegrationConfig): |
| 343 |
"""Configuration for the network integration command.""" |
| 344 |
|
| 345 |
def __init__(self, args: t.Any) -> None: |
| 346 |
super().__init__(args, 'network-integration') |
| 347 |
|
| 348 |
self.testcase: str = args.testcase |
| 349 |
|
| 350 |
|
| 351 |
class UnitsConfig(TestConfig): |
| 352 |
"""Configuration for the units command.""" |
| 353 |
|
| 354 |
def __init__(self, args: t.Any) -> None: |
| 355 |
super().__init__(args, 'units') |
| 356 |
|
| 357 |
self.collect_only: bool = args.collect_only |
| 358 |
self.num_workers: int = args.num_workers |
| 359 |
|
| 360 |
self.requirements_mode: str = getattr(args, 'requirements_mode', '') |
| 361 |
|
| 362 |
if self.requirements_mode == 'only': |
| 363 |
self.requirements = True |
| 364 |
elif self.requirements_mode == 'skip': |
| 365 |
self.requirements = False |