-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathruntime.py
More file actions
278 lines (227 loc) · 8.69 KB
/
runtime.py
File metadata and controls
278 lines (227 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import annotations
import argparse
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Optional
from timecapsulesmb.core.config import (
DEFAULTS,
AppConfig,
ConfigError,
load_app_config,
require_valid_app_config,
)
from timecapsulesmb.core.net import extract_host, ipv4_literal, is_link_local_ipv4, resolve_host_ipv4s
from timecapsulesmb.core.paths import resolve_app_paths
from timecapsulesmb.device.compat import (
DeviceCompatibility,
is_netbsd4_payload_family,
render_compatibility_message,
require_compatibility,
)
from timecapsulesmb.device.probe import (
ProbedDeviceState,
RemoteInterfaceProbeResult,
probe_connection_state,
probe_remote_interface_conn,
read_interface_ipv4_addrs_conn,
)
from timecapsulesmb.transport.ssh import SshConnection, ssh_opts_use_proxy
LogCallback = Optional[Callable[[str], None]]
class NonInteractivePromptError(RuntimeError):
"""Raised when a required confirmation cannot be read from stdin."""
@dataclass(frozen=True)
class ManagedTargetState:
connection: SshConnection
interface_probe: RemoteInterfaceProbeResult | None
probe_state: ProbedDeviceState | None
def add_config_argument(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--config",
type=Path,
default=None,
help="Path to the TimeCapsuleSMB config file. Overrides TCAPSULE_CONFIG and the repo-local .env.",
)
def config_path_from_args(args: argparse.Namespace) -> Path | None:
return getattr(args, "config", None)
def json_text(data: object) -> str:
return json.dumps(data, indent=2, sort_keys=True)
def print_json(data: object) -> None:
print(json_text(data))
def write_json_file(path: Path, data: object) -> None:
path.write_text(json_text(data) + "\n")
def prefixed_logger(prefix: str, *, enabled: bool) -> LogCallback:
if not enabled:
return None
def emit(message: str) -> None:
print(f"[{prefix}] {message}", flush=True)
return emit
def emit_progress(log: LogCallback, message: str) -> None:
if log is not None:
log(message)
def _confirm_suffix(default: bool) -> str:
return "[Y/n]" if default else "[y/N]"
def confirm(
prompt_text: str,
*,
default: bool,
eof_default: bool | None = None,
interrupt_default: bool | None = None,
noninteractive_message: str | None = None,
) -> bool:
while True:
try:
answer = input(f"{prompt_text} {_confirm_suffix(default)}: ").strip().lower()
except EOFError as exc:
if eof_default is not None:
return eof_default
raise NonInteractivePromptError(noninteractive_message or "Confirmation requires interactive stdin.") from exc
except KeyboardInterrupt:
if interrupt_default is not None:
print()
return interrupt_default
raise
if not answer:
return default
if answer in {"y", "yes"}:
return True
if answer in {"n", "no"}:
return False
print("Please answer 'y' or 'n'.")
def load_config_from_args(
args: argparse.Namespace,
*,
defaults: dict[str, str] | None = None,
) -> AppConfig:
return load_env_config(env_path=config_path_from_args(args), defaults=defaults)
def load_env_config(*, env_path: Path | None = None, defaults: dict[str, str] | None = None) -> AppConfig:
resolved_path = resolve_app_paths(config_path=env_path).config_path
return load_app_config(resolved_path, defaults=defaults)
def load_optional_env_config(
*,
env_path: Path | None = None,
defaults: dict[str, str] | None = None,
) -> AppConfig:
try:
resolved_path = resolve_app_paths(config_path=env_path).config_path
except Exception:
return AppConfig.missing(path=env_path or Path.cwd() / ".env")
if not resolved_path.exists():
return AppConfig.missing(path=resolved_path)
try:
return load_app_config(resolved_path, defaults=defaults)
except OSError:
return AppConfig.missing(path=resolved_path)
def resolve_ssh_credentials(
config: AppConfig,
*,
allow_empty_password: bool = False,
) -> tuple[str, str]:
host = config.require("TC_HOST")
password = config.get("TC_PASSWORD")
if not password and not allow_empty_password:
import getpass
password = getpass.getpass("Device root password: ")
return host, password
def resolve_env_connection(
config: AppConfig,
*,
required_keys: tuple[str, ...] = (),
allow_empty_password: bool = False,
) -> SshConnection:
for key in required_keys:
config.require(key)
host, password = resolve_ssh_credentials(config, allow_empty_password=allow_empty_password)
return SshConnection(host=host, password=password, ssh_opts=config.get("TC_SSH_OPTS", DEFAULTS["TC_SSH_OPTS"]))
def inspect_managed_connection(
connection: SshConnection,
iface: str,
*,
include_probe: bool = False,
) -> ManagedTargetState:
interface_probe = probe_remote_interface_conn(connection, iface)
probe_state = probe_connection_state(connection) if include_probe else None
return ManagedTargetState(connection=connection, interface_probe=interface_probe, probe_state=probe_state)
def ssh_target_link_local_resolution_error(
target: str,
ssh_opts: str,
*,
field_name: str = "Device SSH target",
) -> str | None:
if ssh_opts_use_proxy(ssh_opts):
return None
host = extract_host(target).strip()
if not host or ipv4_literal(host) is not None:
return None
link_local_ips = tuple(ip for ip in resolve_host_ipv4s(host) if is_link_local_ipv4(ip))
if not link_local_ips:
return None
noun = "address" if len(link_local_ips) == 1 else "addresses"
return (
f"{field_name} host {host} resolves to 169.254.x.x link-local IPv4 {noun} "
f"{', '.join(link_local_ips)}. Use the device's LAN IP or a hostname that resolves "
"to its LAN IP; 169.254.x.x is only suitable for temporary SSH recovery."
)
def resolve_validated_managed_target(
config: AppConfig,
*,
command_name: str,
profile: str,
include_probe: bool = False,
) -> ManagedTargetState:
require_valid_app_config(config, profile=profile, command_name=command_name)
resolution_error = ssh_target_link_local_resolution_error(
config.require("TC_HOST"),
config.get("TC_SSH_OPTS", DEFAULTS["TC_SSH_OPTS"]),
field_name="TC_HOST",
)
if resolution_error is not None:
raise ConfigError(resolution_error)
connection = resolve_env_connection(config)
if profile == "flash":
return ManagedTargetState(connection=connection, interface_probe=None, probe_state=None)
probe_state = probe_connection_state(connection) if include_probe else None
return ManagedTargetState(connection=connection, interface_probe=None, probe_state=probe_state)
def require_connection_compatibility(connection: SshConnection) -> DeviceCompatibility:
state = probe_connection_state(connection)
return require_compatibility(
state.compatibility,
fallback_error=state.probe_result.error or "Failed to determine remote device OS compatibility.",
)
def require_supported_device_compatibility(
command_context,
*,
allow_unsupported: bool = False,
json_output: bool = False,
) -> tuple[DeviceCompatibility, str]:
compatibility = command_context.require_compatibility()
compatibility_message = render_compatibility_message(compatibility)
if not compatibility.supported:
if not allow_unsupported:
raise SystemExit(compatibility_message)
if not json_output:
print(f"Warning: {compatibility_message}")
print("Continuing because --allow-unsupported was provided.")
elif not json_output:
print(compatibility_message)
return compatibility, compatibility_message
def require_netbsd4_device_compatibility(
command_context,
*,
command_name: str,
json_output: bool = False,
unsupported_message: str | None = None,
) -> tuple[DeviceCompatibility, str]:
compatibility, compatibility_message = require_supported_device_compatibility(
command_context,
json_output=json_output,
)
netbsd4_payload = is_netbsd4_payload_family(compatibility.payload_family)
command_context.update_fields(
compatibility_supported=compatibility.supported,
netbsd4_payload=netbsd4_payload,
)
if not netbsd4_payload:
message = unsupported_message or f"{command_name} is only supported for NetBSD4 AirPort storage devices."
raise SystemExit(message)
return compatibility, compatibility_message