-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdeploy.py
More file actions
508 lines (458 loc) · 22.4 KB
/
deploy.py
File metadata and controls
508 lines (458 loc) · 22.4 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from __future__ import annotations
import argparse
from contextlib import ExitStack
import tempfile
from pathlib import Path
from typing import Optional
from timecapsulesmb.cli.context import CommandContext
from timecapsulesmb.cli.flows import activate_deployed_runtime_flow, request_deploy_reboot_and_wait, verify_managed_runtime_flow
from timecapsulesmb.cli.runtime import (
add_config_argument,
load_env_config,
print_json,
require_supported_device_compatibility,
)
from timecapsulesmb.core.config import (
DEFAULTS,
MANAGED_PAYLOAD_DIR_NAME,
AppConfig,
airport_family_display_name_from_identity,
parse_bool,
shell_quote,
)
from timecapsulesmb.core.messages import NETBSD4_REBOOT_FOLLOWUP
from timecapsulesmb.core.paths import resolve_app_paths
from timecapsulesmb.core.release import CLI_VERSION_CODE, RELEASE_TAG
from timecapsulesmb.identity import ensure_install_id
from timecapsulesmb.deploy.artifact_resolver import resolve_payload_artifacts
from timecapsulesmb.deploy.artifacts import validate_artifacts
from timecapsulesmb.deploy.auth import render_smbpasswd
from timecapsulesmb.deploy.commands import RemoteAction, StopProcessAction
from timecapsulesmb.deploy.dry_run import deployment_plan_to_jsonable, format_deployment_plan
from timecapsulesmb.deploy.executor import flush_remote_filesystem_writes, run_remote_actions, upload_deployment_payload
from timecapsulesmb.deploy.planner import (
BINARY_MDNS_SOURCE,
BINARY_NBNS_SOURCE,
BINARY_SMBD_SOURCE,
DEFAULT_APPLE_MOUNT_WAIT_SECONDS,
DEFAULT_DISKD_USE_VOLUME_ATTEMPTS,
DEPLOY_STARTUP_ACTIVATE_NOW,
DEPLOY_STARTUP_REBOOT_THEN_ACTIVATE,
DEPLOY_STARTUP_REBOOT_THEN_VERIFY,
DeploymentStartupMode,
FileTransfer,
GENERATED_FLASH_CONFIG_SOURCE,
GENERATED_SMBPASSWD_SOURCE,
GENERATED_USERNAME_MAP_SOURCE,
PACKAGED_BOOT_SOURCE,
PACKAGED_COMMON_SH_SOURCE,
PACKAGED_DFREE_SH_SOURCE,
PACKAGED_MANAGER_SOURCE,
PACKAGED_RC_LOCAL_SOURCE,
build_deployment_plan,
)
from timecapsulesmb.deploy.boot_assets import (
boot_asset_path,
)
from timecapsulesmb.device.compat import is_netbsd4_payload_family, payload_family_description
from timecapsulesmb.device.storage import (
MAST_DISCOVERY_ATTEMPTS,
MAST_DISCOVERY_DELAY_SECONDS,
PayloadHome,
PayloadVerificationResult,
build_dry_run_payload_home,
verify_payload_home_conn,
)
from timecapsulesmb.telemetry import TelemetryClient
from timecapsulesmb.cli.util import color_green
REBOOT_NO_DOWN_MESSAGE = (
"Reboot was requested but the device did not go down.\n"
"The deploy stopped the managed runtime before reboot; power-cycle or rerun deploy."
)
def _no_mast_volumes_message(*, attempts: int, delay_seconds: int) -> str:
return (
f"No deployable HFS disk was found after {attempts} MaSt queries "
f"spaced {delay_seconds} seconds apart."
)
def _no_writable_mast_volumes_message(volume_count: int) -> str:
return f"MaSt found {volume_count} deployable HFS volume(s), but deploy could not write to any of them."
def _render_flash_config_assignment(key: str, value: str | int) -> str:
if isinstance(value, int):
return f"{key}={value}"
return f"{key}={shell_quote(value)}"
def _runtime_unsigned_config_value(config: AppConfig, key: str, default: str) -> str:
raw_value = config.get(key, default).strip()
if raw_value == "":
raw_value = default
if raw_value == "":
return ""
if not raw_value.isdigit():
raise ValueError(f"{key} must be a non-negative integer")
return str(int(raw_value))
def _runtime_unsigned_override_value(value: str | int) -> str | int:
if isinstance(value, int):
if value < 0:
raise ValueError("runtime setting override must be a non-negative integer")
return value
raw_value = value.strip()
if raw_value == "":
return ""
if not raw_value.isdigit():
raise ValueError("runtime setting override must be a non-negative integer")
return str(int(raw_value))
def render_flash_runtime_config(
config: AppConfig,
payload_home: PayloadHome,
*,
nbns_enabled: bool,
debug_logging: bool,
ata_idle_seconds: str | int | None = None,
ata_standby: str | int | None = None,
diskd_use_volume_attempts: int = DEFAULT_DISKD_USE_VOLUME_ATTEMPTS,
) -> str:
internal_root_default = config.get("TC_INTERNAL_SHARE_USE_DISK_ROOT", DEFAULTS["TC_INTERNAL_SHARE_USE_DISK_ROOT"])
any_protocol_default = config.get("TC_ANY_PROTOCOL", DEFAULTS["TC_ANY_PROTOCOL"])
runtime_ata_idle_seconds = (
_runtime_unsigned_config_value(config, "TC_ATA_IDLE_SECONDS", DEFAULTS["TC_ATA_IDLE_SECONDS"])
if ata_idle_seconds is None
else _runtime_unsigned_override_value(ata_idle_seconds)
)
runtime_ata_standby = (
_runtime_unsigned_config_value(config, "TC_ATA_STANDBY", DEFAULTS["TC_ATA_STANDBY"])
if ata_standby is None
else _runtime_unsigned_override_value(ata_standby)
)
values: list[tuple[str, str | int]] = [
("TC_CONFIG_VERSION", 2),
("TC_DEPLOY_RELEASE_TAG", RELEASE_TAG),
("TC_DEPLOY_CLI_VERSION_CODE", CLI_VERSION_CODE),
("INTERNAL_SHARE_USE_DISK_ROOT", 1 if parse_bool(internal_root_default) else 0),
("ANY_PROTOCOL", 1 if parse_bool(any_protocol_default) else 0),
("DISKD_USE_VOLUME_ATTEMPTS", diskd_use_volume_attempts),
("ATA_IDLE_SECONDS", runtime_ata_idle_seconds),
("ATA_STANDBY", runtime_ata_standby),
("NBNS_ENABLED", 1 if nbns_enabled else 0),
("SMBD_DEBUG_LOGGING", 1 if debug_logging else 0),
("MDNS_DEBUG_LOGGING", 1 if debug_logging else 0),
]
return "\n".join(_render_flash_config_assignment(key, value) for key, value in values) + "\n"
def _target_family_display_name(target) -> str:
probe = target.probe_state.probe_result if target.probe_state is not None else None
return airport_family_display_name_from_identity(
model=None if probe is None else probe.airport_model,
syap=None if probe is None else probe.airport_syap,
)
def _payload_verification_error(payload_home: PayloadHome, result: PayloadVerificationResult) -> str:
return f"managed payload verification failed at {payload_home.payload_dir}: {result.detail}"
def _startup_mode_for_deploy(*, no_reboot: bool, is_netbsd4: bool) -> DeploymentStartupMode:
if no_reboot:
return DEPLOY_STARTUP_ACTIVATE_NOW
if is_netbsd4:
return DEPLOY_STARTUP_REBOOT_THEN_ACTIVATE
return DEPLOY_STARTUP_REBOOT_THEN_VERIFY
def _activation_complete_message(*, is_netbsd4: bool) -> str:
if is_netbsd4:
return f"NetBSD4 activation complete. {NETBSD4_REBOOT_FOLLOWUP}"
return "Runtime activation complete."
def _non_negative_int(value: str) -> int:
try:
parsed = int(value)
except ValueError as e:
raise argparse.ArgumentTypeError("must be an integer") from e
if parsed < 0:
raise argparse.ArgumentTypeError("must be 0 or greater")
return parsed
def main(argv: Optional[list[str]] = None) -> int:
parser = argparse.ArgumentParser(description="Deploy the checked-in Samba 4 payload to an AirPort storage device.")
add_config_argument(parser)
parser.add_argument("--no-reboot", action="store_true", help="Do not reboot; activate the deployed runtime in place")
parser.add_argument("--yes", action="store_true", help="Do not prompt before reboot")
parser.add_argument("--dry-run", action="store_true", help="Print actions without making changes")
parser.add_argument("--json", action="store_true", help="Output the dry-run deployment plan as JSON")
parser.add_argument("--allow-unsupported", action="store_true", help="Proceed even if the detected device is not currently supported")
parser.add_argument("--no-nbns", action="store_true", help="Disable the bundled NBNS responder on the next boot")
parser.add_argument(
"--mount-wait",
type=_non_negative_int,
default=DEFAULT_APPLE_MOUNT_WAIT_SECONDS,
metavar="SECONDS",
help=f"Seconds for each deployment-time diskd.useVolume mount guard attempt to wait (default: {DEFAULT_APPLE_MOUNT_WAIT_SECONDS})",
)
parser.add_argument("--debug-logging", action="store_true", help=argparse.SUPPRESS)
args = parser.parse_args(argv)
if args.json and not args.dry_run:
parser.error("--json currently requires --dry-run")
nbns_enabled = not args.no_nbns
ensure_install_id()
app_paths = resolve_app_paths(config_path=args.config)
config = load_env_config(env_path=args.config)
telemetry = TelemetryClient.from_config(config, nbns_enabled=nbns_enabled)
with CommandContext(telemetry, "deploy", "deploy_started", "deploy_finished", config=config, args=args) as command_context:
command_context.update_fields(
nbns_enabled=nbns_enabled,
reboot_was_attempted=False,
device_came_back_after_reboot=False,
)
command_context.set_stage("resolve_managed_target")
if not args.json:
print("Resolving deployment target...", flush=True)
target = command_context.resolve_validated_managed_target(profile="deploy", include_probe=True)
connection = target.connection
host = connection.host
smb_password = connection.password
command_context.set_stage("validate_artifacts")
if not args.json:
print("Validating local artifacts...", flush=True)
artifact_results = validate_artifacts(app_paths.distribution_root)
failures = [message for _, ok, message in artifact_results if not ok]
if failures:
raise SystemExit("; ".join(failures))
command_context.set_stage("check_compatibility")
if not args.json:
print("Checking device compatibility...", flush=True)
compatibility, compatibility_message = require_supported_device_compatibility(
command_context,
allow_unsupported=args.allow_unsupported,
json_output=args.json,
)
if not compatibility.payload_family:
raise SystemExit(f"{compatibility_message}\nNo deployable payload is available for this detected device.")
payload_family = compatibility.payload_family
is_netbsd4 = is_netbsd4_payload_family(payload_family)
if is_netbsd4:
# Apple NetBSD 4 firmware can expose /usr/bin/scp but hang after
# writing the file. Use the SSH pipe upload fallback consistently.
connection.remote_has_scp = False
startup_mode = _startup_mode_for_deploy(no_reboot=args.no_reboot, is_netbsd4=is_netbsd4)
command_context.update_fields(deploy_startup_mode=startup_mode)
if not args.json:
print(f"Using {payload_family_description(payload_family)} payload.", flush=True)
apple_mount_wait_seconds = args.mount_wait
resolved_artifacts = resolve_payload_artifacts(app_paths.distribution_root, payload_family)
smbd_path = resolved_artifacts["smbd"].absolute_path
mdns_path = resolved_artifacts["mdns-advertiser"].absolute_path
nbns_path = resolved_artifacts["nbns-advertiser"].absolute_path
if args.dry_run:
payload_home = build_dry_run_payload_home(MANAGED_PAYLOAD_DIR_NAME)
else:
if not args.json:
print("Finding payload volume...", flush=True)
mast_discovery = command_context.wait_for_mast_volumes(
connection,
attempts=MAST_DISCOVERY_ATTEMPTS,
delay_seconds=MAST_DISCOVERY_DELAY_SECONDS,
)
mast_volumes = mast_discovery.volumes
if not mast_volumes:
raise SystemExit(
_no_mast_volumes_message(
attempts=MAST_DISCOVERY_ATTEMPTS,
delay_seconds=MAST_DISCOVERY_DELAY_SECONDS,
)
)
selection = command_context.select_payload_home(
connection,
mast_volumes,
MANAGED_PAYLOAD_DIR_NAME,
wait_seconds=apple_mount_wait_seconds,
)
if selection.payload_home is None:
raise SystemExit(_no_writable_mast_volumes_message(len(mast_volumes)))
payload_home = selection.payload_home
if not args.json:
print(f"Using payload directory {payload_home.payload_dir}.", flush=True)
command_context.set_stage("build_deployment_plan")
plan = build_deployment_plan(
host,
payload_home,
smbd_path,
mdns_path,
nbns_path,
startup_mode=startup_mode,
apple_mount_wait_seconds=apple_mount_wait_seconds,
)
command_context.add_debug_fields(
payload_volume_root=plan.volume_root,
payload_device_path=plan.device_path,
payload_dir=plan.payload_dir,
)
if args.dry_run:
if args.json:
print_json(deployment_plan_to_jsonable(plan))
else:
print(format_deployment_plan(plan))
command_context.succeed()
return 0
print("Deleting old deployed files...", flush=True)
print("Stopping existing runtime...", flush=True)
def report_pre_upload_action(action: RemoteAction, _index: int, _total: int) -> None:
if isinstance(action, StopProcessAction) and action.name == "nbns-advertiser":
print("Cleaning up previous deployment files...", flush=True)
command_context.set_stage("pre_upload_actions")
run_remote_actions(connection, plan.pre_upload_actions, on_action_done=report_pre_upload_action)
command_context.set_stage("prepare_deployment_files")
flash_config_text = render_flash_runtime_config(
config,
payload_home,
nbns_enabled=nbns_enabled,
debug_logging=args.debug_logging,
)
with tempfile.TemporaryDirectory(prefix="tc-deploy-") as tmp, ExitStack() as boot_assets:
tmpdir = Path(tmp)
generated_flash_config = tmpdir / "tcapsulesmb.conf"
generated_smbpasswd = tmpdir / "smbpasswd"
generated_username_map = tmpdir / "username.map"
generated_flash_config.write_text(flash_config_text)
smbpasswd_text, username_map_text = render_smbpasswd(smb_password)
generated_smbpasswd.write_text(smbpasswd_text)
generated_username_map.write_text(username_map_text)
upload_sources = {
BINARY_SMBD_SOURCE: plan.smbd_path,
BINARY_MDNS_SOURCE: plan.mdns_path,
BINARY_NBNS_SOURCE: plan.nbns_path,
GENERATED_SMBPASSWD_SOURCE: generated_smbpasswd,
GENERATED_USERNAME_MAP_SOURCE: generated_username_map,
GENERATED_FLASH_CONFIG_SOURCE: generated_flash_config,
PACKAGED_RC_LOCAL_SOURCE: boot_assets.enter_context(boot_asset_path("rc.local")),
PACKAGED_COMMON_SH_SOURCE: boot_assets.enter_context(boot_asset_path("common.sh")),
PACKAGED_BOOT_SOURCE: boot_assets.enter_context(boot_asset_path("boot.sh")),
PACKAGED_MANAGER_SOURCE: boot_assets.enter_context(boot_asset_path("manager.sh")),
PACKAGED_DFREE_SH_SOURCE: boot_assets.enter_context(boot_asset_path("dfree.sh")),
}
def report_uploaded_file(transfer: FileTransfer) -> None:
message = None
if transfer.source_id == BINARY_SMBD_SOURCE:
message = "Uploaded smbd."
elif transfer.source_id == BINARY_MDNS_SOURCE and transfer.mode == "flash_atomic":
message = "Uploaded mdns-advertiser."
elif transfer.source_id == BINARY_NBNS_SOURCE:
message = "Uploaded nbns-advertiser."
elif transfer.source_id == PACKAGED_DFREE_SH_SOURCE:
message = "Uploaded boot files."
elif transfer.source_id == GENERATED_FLASH_CONFIG_SOURCE:
message = "Uploaded runtime config."
elif transfer.source_id == GENERATED_USERNAME_MAP_SOURCE:
message = "Uploaded Samba account files."
if message is not None:
print(message, flush=True)
command_context.set_stage("upload_payload")
print("Uploading deployment payload...", flush=True)
upload_deployment_payload(
plan,
connection=connection,
source_resolver=upload_sources,
on_uploaded=report_uploaded_file,
)
print("Upload phase complete.", flush=True)
command_context.set_stage("post_upload_actions")
print("Applying file permissions...", flush=True)
run_remote_actions(connection, plan.post_upload_actions)
command_context.set_stage("verify_payload_upload")
print("Verifying uploaded payload...", flush=True)
payload_verification = verify_payload_home_conn(
connection,
payload_home,
wait_seconds=apple_mount_wait_seconds,
)
command_context.add_debug_fields(payload_upload_verification=payload_verification.detail)
if not payload_verification.ok:
raise SystemExit(_payload_verification_error(payload_home, payload_verification))
command_context.set_stage("flush_payload_upload")
if not args.json:
print("Flushing payload to disk...", flush=True)
flush_remote_filesystem_writes(connection)
# The immediate verification above can succeed from cache. Flush and
# verify again before any reboot so dirty HFS metadata cannot disappear
# under an ACP-triggered restart.
command_context.set_stage("verify_payload_upload_after_sync")
payload_verification = verify_payload_home_conn(
connection,
payload_home,
wait_seconds=apple_mount_wait_seconds,
)
command_context.add_debug_fields(payload_post_sync_verification=payload_verification.detail)
if not payload_verification.ok:
raise SystemExit(_payload_verification_error(payload_home, payload_verification))
print("Verified uploaded payload.", flush=True)
print(f"Deployed Samba payload to {plan.payload_dir}", flush=True)
print("Updated /mnt/Flash boot files.", flush=True)
if startup_mode == DEPLOY_STARTUP_ACTIVATE_NOW:
if not activate_deployed_runtime_flow(
connection,
command_context,
plan.activation_actions,
run_actions=run_remote_actions,
skip_if_ready=False,
already_active_message="Managed runtime already active; skipping rc.local.",
startup_in_progress_message="Managed runtime startup is already in progress; waiting for it to finish.",
activation_message="Starting deployed runtime without reboot.",
activation_stage="activate_runtime",
verification_stage="verify_runtime_activation",
verification_timeout_seconds=180,
verification_heading="Waiting for managed runtime to finish starting...",
failure_message="Managed runtime activation failed.",
):
return 1
print(_activation_complete_message(is_netbsd4=is_netbsd4))
print(color_green("Deploy Finished."))
command_context.succeed()
return 0
if not args.yes:
device_name = _target_family_display_name(target)
if startup_mode == DEPLOY_STARTUP_REBOOT_THEN_ACTIVATE:
prompt = f"This will reboot the {device_name}, then activate Samba after SSH returns. Continue?"
else:
prompt = f"This will reboot the {device_name} now. Continue?"
proceed = command_context.confirm_or_fail(
prompt,
default=True,
noninteractive_message="Running `deploy` with reboot requires confirmation when stdin is not interactive. Use `deploy --yes` to skip the prompt or `deploy --no-reboot`.",
)
if proceed is None:
return 1
if not proceed:
print("Deployment complete without reboot.", flush=True)
command_context.cancel_with_error("Cancelled by user at reboot confirmation prompt.")
return 0
print("Requesting reboot...", flush=True)
if not request_deploy_reboot_and_wait(
connection,
command_context,
reboot_no_down_message=REBOOT_NO_DOWN_MESSAGE,
):
return 1
if startup_mode == DEPLOY_STARTUP_REBOOT_THEN_ACTIVATE:
if not activate_deployed_runtime_flow(
connection,
command_context,
plan.activation_actions,
run_actions=run_remote_actions,
skip_if_ready=True,
already_active_message="Managed runtime already active after reboot; skipping rc.local.",
startup_in_progress_message="Managed runtime startup is already in progress after reboot; waiting for it to finish.",
activation_message="Activating deployed runtime after reboot.",
activation_stage="post_reboot_activation",
verification_stage="verify_runtime_activation",
verification_timeout_seconds=180,
verification_heading="Waiting for NetBSD 4 device activation, this can take a few minutes for Samba to start up...",
failure_message="NetBSD4 activation failed.",
):
return 1
print(_activation_complete_message(is_netbsd4=is_netbsd4))
print(color_green("Deploy Finished."))
command_context.succeed()
return 0
print("Waiting for managed runtime to finish starting...", flush=True)
if verify_managed_runtime_flow(
connection,
command_context,
stage="verify_runtime_reboot",
timeout_seconds=240,
heading="Wait for device to finish loading; it can take a few minutes for Samba to start up...",
failure_message="Managed runtime did not become ready after reboot.",
):
print(color_green("Deploy Finished."))
command_context.succeed()
return 0
return 1
return 1