ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
fleet_ssh_config.py
Go to the documentation of this file.
1# SPDX-License-Identifier: MIT
2# Copyright (c) 2026 Brighton Sikarskie
3"""Install or render the declaration-derived fleet SSH configuration."""
4
5from __future__ import annotations
6
7from pathlib import Path
8from typing import Any
9
10import fleet_reach as fr
11
12
13def _ensure_include(config: Path) -> str:
14 """Put the generated fragment's Include line first in one SSH config."""
15 header = (
16 "# ra8-firmware: the CI fleet's machines, GENERATED from infra/fleet.yml.\n"
17 "# Refresh with `just infra::ssh_config`.\n"
18 f"{fr.SSH_INCLUDE_LINE}\n"
19 )
20 if not config.exists():
21 config.write_text(header, encoding="utf-8")
22 config.chmod(0o600)
23 return f"created {config} with the include line"
24 existing = config.read_text(encoding="utf-8")
25 if any(line.strip() == fr.SSH_INCLUDE_LINE for line in existing.splitlines()):
26 return f"{config} already includes it"
27 config.write_text(f"{header}\n{existing}", encoding="utf-8")
28 return f"prepended the include line to {config}"
29
30
31def command(data: dict[str, Any], *, install: bool) -> int:
32 """Print or install the fleet's generated SSH config fragment."""
33 body = fr.render_ssh_config(data)
34 if not install:
35 print(body, end="")
36 return 0
37 ssh_dir = Path.home() / ".ssh"
38 ssh_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
39 fragment = ssh_dir / fr.SSH_FRAGMENT_NAME
40 fragment.write_text(body, encoding="utf-8")
41 fragment.chmod(0o600)
42 print(f"wrote {fragment} ({len(data['hosts'])} host(s))")
43 print(_ensure_include(ssh_dir / "config"))
44 return 0