loft-cli-core 0.6.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. loft_cli_core/__init__.py +1 -0
  2. loft_cli_core/agent_models.py +34 -0
  3. loft_cli_core/agent_paths.py +27 -0
  4. loft_cli_core/plan/__init__.py +0 -0
  5. loft_cli_core/plan/models.py +80 -0
  6. loft_cli_core/plan/render_diff.py +112 -0
  7. loft_cli_core/plan/render_markdown.py +189 -0
  8. loft_cli_core/plan/render_text.py +86 -0
  9. loft_cli_core/policy.py +218 -0
  10. loft_cli_core/registry/__init__.py +108 -0
  11. loft_cli_core/registry/executors.py +31 -0
  12. loft_cli_core/registry/hooks.py +46 -0
  13. loft_cli_core/registry/local_paths.py +147 -0
  14. loft_cli_core/registry/normalizers.py +21 -0
  15. loft_cli_core/registry/planners.py +21 -0
  16. loft_cli_core/registry/resolvers.py +65 -0
  17. loft_cli_core/registry/specs.py +20 -0
  18. loft_cli_core/registry/validators.py +21 -0
  19. loft_cli_core/specs/__init__.py +0 -0
  20. loft_cli_core/specs/backup_job_schema.py +95 -0
  21. loft_cli_core/specs/bootstrap_schema.py +124 -0
  22. loft_cli_core/specs/compose_project_schema.py +93 -0
  23. loft_cli_core/specs/file_template_schema.py +64 -0
  24. loft_cli_core/specs/http_check_schema.py +64 -0
  25. loft_cli_core/specs/loader.py +250 -0
  26. loft_cli_core/specs/postgres_ensure_schema.py +104 -0
  27. loft_cli_core/specs/service_schema.py +114 -0
  28. loft_cli_core/specs/stack_schema.py +74 -0
  29. loft_cli_core/specs/systemd_timer_schema.py +76 -0
  30. loft_cli_core/specs/systemd_unit_schema.py +86 -0
  31. loft_cli_core/specs/validators.py +755 -0
  32. loft_cli_core/state.py +31 -0
  33. loft_cli_core/utils/__init__.py +0 -0
  34. loft_cli_core/utils/files.py +41 -0
  35. loft_cli_core/utils/hashing.py +10 -0
  36. loft_cli_core/utils/os_detect.py +36 -0
  37. loft_cli_core/utils/templates.py +87 -0
  38. loft_cli_core-0.6.3.dist-info/METADATA +10 -0
  39. loft_cli_core-0.6.3.dist-info/RECORD +41 -0
  40. loft_cli_core-0.6.3.dist-info/WHEEL +5 -0
  41. loft_cli_core-0.6.3.dist-info/top_level.txt +1 -0
loft_cli_core/state.py ADDED
@@ -0,0 +1,31 @@
1
+ """Shared state models for the loft-cli agent.
2
+
3
+ These Pydantic models define the runtime state schema shared between
4
+ the client (for diff rendering) and the agent (for state tracking).
5
+
6
+ State management functions (load_state, save_state, etc.) live in
7
+ the loft_cli_agent package since they perform server-side I/O.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from pydantic import BaseModel, Field
13
+
14
+
15
+ class ResourceState(BaseModel):
16
+ """State of a single applied resource (step)."""
17
+
18
+ resource_id: str
19
+ content_hash: str # SHA-256 of step command + file_content + target_path
20
+ applied_at: str # ISO timestamp
21
+ status: str = "applied" # "applied" | "failed"
22
+
23
+
24
+ class RuntimeState(BaseModel):
25
+ """Complete runtime state of the agent on this server."""
26
+
27
+ version: str = "" # Agent version that last applied
28
+ last_applied: str = "" # ISO timestamp of last apply
29
+ spec_hash: str = "" # Hash of the last applied spec
30
+ plan_hash: str = "" # Hash of the last applied plan
31
+ resources: dict[str, ResourceState] = Field(default_factory=dict)
File without changes
@@ -0,0 +1,41 @@
1
+ from pathlib import Path
2
+
3
+
4
+ def expand_path(p: str) -> Path:
5
+ """Expand ~ and resolve to absolute path (relative to CWD)."""
6
+ return Path(p).expanduser().resolve()
7
+
8
+
9
+ def resolve_path(p: str, base_dir: Path | None = None) -> Path:
10
+ """Expand ~ and resolve a path, preferring spec-relative resolution.
11
+
12
+ Resolution order for relative paths:
13
+ 1. If the path starts with ~ or is absolute: resolve normally (CWD-independent).
14
+ 2. If base_dir is given and the path exists relative to base_dir: use that.
15
+ 3. Fall back to CWD-relative resolution (same as expand_path).
16
+
17
+ This ensures that `pubkeys: [.secrets/key.pub]` in a spec file resolves
18
+ correctly regardless of the working directory loft-cli is invoked from.
19
+ """
20
+ raw = Path(p)
21
+ if raw.is_absolute() or str(p).startswith("~"):
22
+ return raw.expanduser().resolve()
23
+ if base_dir is not None:
24
+ candidate = (base_dir / raw).resolve()
25
+ if candidate.exists():
26
+ return candidate
27
+ return raw.expanduser().resolve()
28
+
29
+
30
+ def ensure_dir(p: Path, mode: int = 0o700) -> None:
31
+ """Create directory and parents if they don't exist."""
32
+ p.mkdir(parents=True, exist_ok=True)
33
+ p.chmod(mode)
34
+
35
+
36
+ def read_text_safe(p: Path) -> str | None:
37
+ """Read file text, returning None if file does not exist."""
38
+ try:
39
+ return p.read_text(encoding="utf-8")
40
+ except FileNotFoundError:
41
+ return None
@@ -0,0 +1,10 @@
1
+ import hashlib
2
+ from pathlib import Path
3
+
4
+
5
+ def sha256_string(s: str) -> str:
6
+ return hashlib.sha256(s.encode()).hexdigest()
7
+
8
+
9
+ def sha256_file(path: Path) -> str:
10
+ return sha256_string(path.read_text(encoding="utf-8"))
@@ -0,0 +1,36 @@
1
+ from dataclasses import dataclass
2
+
3
+ SUPPORTED_OS_FAMILIES = {"debian", "ubuntu"}
4
+
5
+
6
+ @dataclass
7
+ class OSInfo:
8
+ distro: str
9
+ codename: str
10
+ version: str
11
+ family: str # "debian" for both Debian and Ubuntu
12
+
13
+
14
+ def detect_os(session) -> "OSInfo":
15
+ """Detect remote OS via /etc/os-release. Asserts Debian/Ubuntu in V1."""
16
+ result = session.run("cat /etc/os-release", hide=True)
17
+ info = {}
18
+ for line in result.stdout.splitlines():
19
+ if "=" in line:
20
+ k, _, v = line.partition("=")
21
+ info[k.strip()] = v.strip().strip('"')
22
+
23
+ distro = info.get("ID", "unknown").lower()
24
+ codename = info.get("VERSION_CODENAME", info.get("UBUNTU_CODENAME", "unknown"))
25
+ version = info.get("VERSION_ID", "unknown")
26
+
27
+ # Determine family
28
+ if distro in ("ubuntu",) or distro in ("debian",):
29
+ family = "debian"
30
+ else:
31
+ family = distro
32
+
33
+ if family not in SUPPORTED_OS_FAMILIES:
34
+ raise RuntimeError(f"Unsupported OS '{distro}'. loft-cli V1 supports Debian/Ubuntu only.")
35
+
36
+ return OSInfo(distro=distro, codename=codename, version=version, family=family)
@@ -0,0 +1,87 @@
1
+ """Shared Jinja2 template rendering for file_template and compose_project specs.
2
+
3
+ Templates are rendered at plan time so the rendered content appears in
4
+ step.file_content, making plans fully reviewable and deterministic.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from pathlib import Path
10
+
11
+ from jinja2 import Environment, FileSystemLoader, StrictUndefined, TemplateNotFound
12
+
13
+ from loft_cli_core.utils.hashing import sha256_string
14
+
15
+
16
+ class TemplateRenderError(Exception):
17
+ """Raised when template rendering fails."""
18
+
19
+
20
+ def render_template_file(
21
+ template_path: Path,
22
+ variables: dict[str, str],
23
+ ) -> str:
24
+ """Render a Jinja2 template file with the given variables.
25
+
26
+ The template is loaded from its parent directory so that Jinja2's
27
+ include/extends directives work with sibling files.
28
+
29
+ Args:
30
+ template_path: Absolute path to the Jinja2 template file.
31
+ variables: Key-value pairs passed to the template context.
32
+
33
+ Returns:
34
+ The rendered template content as a string.
35
+
36
+ Raises:
37
+ TemplateRenderError: If the template cannot be found or rendered.
38
+ """
39
+ if not template_path.exists():
40
+ raise TemplateRenderError(f"Template file not found: {template_path}")
41
+
42
+ try:
43
+ env = Environment(
44
+ loader=FileSystemLoader(str(template_path.parent)),
45
+ undefined=StrictUndefined,
46
+ keep_trailing_newline=True,
47
+ autoescape=False,
48
+ )
49
+ template = env.get_template(template_path.name)
50
+ return template.render(**variables)
51
+ except TemplateNotFound as e:
52
+ raise TemplateRenderError(f"Template not found: {e}") from e
53
+ except Exception as e:
54
+ raise TemplateRenderError(f"Failed to render template '{template_path}': {e}") from e
55
+
56
+
57
+ def render_template_string(
58
+ template_str: str,
59
+ variables: dict[str, str],
60
+ ) -> str:
61
+ """Render a Jinja2 template string with the given variables.
62
+
63
+ Args:
64
+ template_str: Jinja2 template content as a string.
65
+ variables: Key-value pairs passed to the template context.
66
+
67
+ Returns:
68
+ The rendered content as a string.
69
+
70
+ Raises:
71
+ TemplateRenderError: If the template cannot be rendered.
72
+ """
73
+ try:
74
+ env = Environment(
75
+ undefined=StrictUndefined,
76
+ keep_trailing_newline=True,
77
+ autoescape=False,
78
+ )
79
+ template = env.from_string(template_str)
80
+ return template.render(**variables)
81
+ except Exception as e:
82
+ raise TemplateRenderError(f"Failed to render template string: {e}") from e
83
+
84
+
85
+ def content_hash(content: str) -> str:
86
+ """Return the SHA-256 hash of rendered content for change detection."""
87
+ return sha256_string(content)
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: loft-cli-core
3
+ Version: 0.6.3
4
+ Summary: Shared models, specs, and registry infrastructure for loft-cli
5
+ Requires-Python: >=3.11
6
+ Requires-Dist: pydantic>=2.5
7
+ Requires-Dist: pyyaml>=6.0
8
+ Requires-Dist: jinja2>=3.1
9
+ Requires-Dist: rich>=13.0
10
+ Requires-Dist: python-dotenv>=1.0
@@ -0,0 +1,41 @@
1
+ loft_cli_core/__init__.py,sha256=zYiFHqR7JwbvdK9dvKrh-RTNfUqjHUwC4CTcFAPVYLc,22
2
+ loft_cli_core/agent_models.py,sha256=6v9vIac5PlZqQRfP-5EY7tLB916LLtKtWu6siq6l2W8,821
3
+ loft_cli_core/agent_paths.py,sha256=vIeoLxkPWgQVPvBEaKpmqA2p0U6lRULAB8_Dxk3Wwyo,872
4
+ loft_cli_core/policy.py,sha256=HjakmtbE_f22-FdcXZfCmZGV74SY3dKqRRuYRM6I1GY,6950
5
+ loft_cli_core/state.py,sha256=MxrHC05uCi6YXB53dL-icby_2HWi9m3pPmigwwM5e2s,1065
6
+ loft_cli_core/plan/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ loft_cli_core/plan/models.py,sha256=PURsNXwIz9-yF_Di9Nu3oJ5TKHmrDYmHmsqR89S6eEA,2589
8
+ loft_cli_core/plan/render_diff.py,sha256=E3Z1d606y6JsnHinqRRwOMwVNruc0sim1SSudBzkcGs,3401
9
+ loft_cli_core/plan/render_markdown.py,sha256=ZChswQExiX2qUnuJSyH4Pt4GanauDYUpPC7sAhZUEDk,5096
10
+ loft_cli_core/plan/render_text.py,sha256=Kie2nXk2PxP-ATtLDTnhr7MZK1KpWbVMGW_9kpH9Jfk,2749
11
+ loft_cli_core/registry/__init__.py,sha256=AREvqWtT3-7bZWYap580HkCHOYeWI7DtR1ZNREGV-bw,3538
12
+ loft_cli_core/registry/executors.py,sha256=17OHTsabuPisuNmSPQI-EL6ac0AYJCmhtO_ukAUsQ4w,1063
13
+ loft_cli_core/registry/hooks.py,sha256=7dGvr7txtpP8gG6UpDDvASDozM8rOrcFNvranIJv-Fc,1590
14
+ loft_cli_core/registry/local_paths.py,sha256=oL6TfBit9ePTgzCYXHdFsgdNM36ffDS0doYxxqr_fPM,5579
15
+ loft_cli_core/registry/normalizers.py,sha256=F6elumm3KO7l0rrOFxoRfj9rDgdDzpPM31Gc8gceCmU,606
16
+ loft_cli_core/registry/planners.py,sha256=Il3SHGSw69Wsq4_f82orj-dCjVhnO8WTnoGS9nzGfHs,597
17
+ loft_cli_core/registry/resolvers.py,sha256=_ARL9FE2OsMlcG7gS0K777U4Xwq27RYhVu9jGrCCB4I,2386
18
+ loft_cli_core/registry/specs.py,sha256=HVnskIz8hVUPSVnidOiCrInMi9iVxB9FrQzfZI92yjY,597
19
+ loft_cli_core/registry/validators.py,sha256=4R3MW_2OW1lMnp-ZlcXZC-WBO50gdAey_cc6WCCM_20,589
20
+ loft_cli_core/specs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
+ loft_cli_core/specs/backup_job_schema.py,sha256=NxjK0FUhZbEew88GwQQjiHldhMWOm21AX07HhJp9uSg,2541
22
+ loft_cli_core/specs/bootstrap_schema.py,sha256=0pspRXiy8XfY8GLA7QtNWD3L2neD2Rn_6Iuaylv5wdA,3712
23
+ loft_cli_core/specs/compose_project_schema.py,sha256=-Awt0SCNLtcuo_W2R5zHWSevJ20Q5lPRapvbIELHLgg,2892
24
+ loft_cli_core/specs/file_template_schema.py,sha256=IiHaQsw27j3JzttChtvwhScFSho8RH0R8T2fjnCrUz4,1921
25
+ loft_cli_core/specs/http_check_schema.py,sha256=uZnsyKbC4CDha1Lsuq_kGLntCb1M_t1pWoBo1lnbHQs,1830
26
+ loft_cli_core/specs/loader.py,sha256=BO2cWzasKi2VZrt5QC1Tn_DU2jY7vGjiFUL71a6FHwk,8505
27
+ loft_cli_core/specs/postgres_ensure_schema.py,sha256=PujooB04HagDv6Io_KGLtUhz4B_DInmjZ1Gtg-SUkEU,2869
28
+ loft_cli_core/specs/service_schema.py,sha256=y9mH22j1P_awBcxm_qTzSFv2v40XbawYee0-Y-iqOUs,2798
29
+ loft_cli_core/specs/stack_schema.py,sha256=vw-D2MmzUdYU4IpyLGvWgkpqf9_rSIx8oJNWeQ5ZItQ,2314
30
+ loft_cli_core/specs/systemd_timer_schema.py,sha256=xdrNagm81IEyJAF8VF4CAXfIM2p6kcC659FIcbErBtU,2205
31
+ loft_cli_core/specs/systemd_unit_schema.py,sha256=xo54_fkjAQK6dr6pinNPupUPeMXXfvUgTvMTVlIzAmY,2670
32
+ loft_cli_core/specs/validators.py,sha256=ZTgDXNyhd4bzi2zLu4UwkRq6Ls7BHCW2inCGAIR6Ojw,23737
33
+ loft_cli_core/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
+ loft_cli_core/utils/files.py,sha256=ic7pRcSQSFnNbCy0rYAHQRPgh1cDAGUqyUXWPGsF6DM,1416
35
+ loft_cli_core/utils/hashing.py,sha256=k5cvaDZho5SQ71ee4wv6ZvhhAZaZdoL9rXhAhJFwXYw,223
36
+ loft_cli_core/utils/os_detect.py,sha256=L_oNJ7uRM4hAncyjd5g1R98EAO2rbxwn1Dmcani0HDw,1110
37
+ loft_cli_core/utils/templates.py,sha256=tHN9kjO_ZBPF3vWrdrurW5qK-0dCl1JxOgndjg5jOAg,2708
38
+ loft_cli_core-0.6.3.dist-info/METADATA,sha256=Mr68aqQYI5I1TvWaM46WN8J16rNDGcFty4u4OjNrv5E,296
39
+ loft_cli_core-0.6.3.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
40
+ loft_cli_core-0.6.3.dist-info/top_level.txt,sha256=PlWgdNGCEGgX7KFDt2Tc-rkFbAf5MT1V_d4oHXmJmdM,14
41
+ loft_cli_core-0.6.3.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ loft_cli_core