loft-cli 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 (61) hide show
  1. loft_cli/__init__.py +3 -0
  2. loft_cli/_builtins.py +269 -0
  3. loft_cli/addons/__init__.py +12 -0
  4. loft_cli/addons/goss/__init__.py +1 -0
  5. loft_cli/addons/goss/generator.py +202 -0
  6. loft_cli/addons/goss/renderer.py +207 -0
  7. loft_cli/addons/goss/shipper.py +174 -0
  8. loft_cli/agent_installer.py +65 -0
  9. loft_cli/checks/__init__.py +0 -0
  10. loft_cli/checks/compose.py +147 -0
  11. loft_cli/checks/container.py +24 -0
  12. loft_cli/checks/http.py +30 -0
  13. loft_cli/checks/nginx.py +21 -0
  14. loft_cli/checks/ports.py +24 -0
  15. loft_cli/checks/postgres.py +21 -0
  16. loft_cli/checks/ssh.py +66 -0
  17. loft_cli/checks/wireguard.py +21 -0
  18. loft_cli/cli.py +1161 -0
  19. loft_cli/compiler/__init__.py +0 -0
  20. loft_cli/compiler/normalizer.py +420 -0
  21. loft_cli/compiler/parser.py +42 -0
  22. loft_cli/compiler/planner.py +2543 -0
  23. loft_cli/local/__init__.py +0 -0
  24. loft_cli/local/ddl/__init__.py +0 -0
  25. loft_cli/local/ddl/bootstrap_tables.py +75 -0
  26. loft_cli/local/ddl/versionize_system.py +374 -0
  27. loft_cli/local/inventory.py +449 -0
  28. loft_cli/local/inventory_db.py +255 -0
  29. loft_cli/local/keys.py +45 -0
  30. loft_cli/local/remove.py +135 -0
  31. loft_cli/local/ssh_config.py +118 -0
  32. loft_cli/local/tunnel.py +228 -0
  33. loft_cli/local/wireguard_store.py +138 -0
  34. loft_cli/logs/__init__.py +0 -0
  35. loft_cli/logs/reader.py +55 -0
  36. loft_cli/logs/writer.py +58 -0
  37. loft_cli/runtime/__init__.py +0 -0
  38. loft_cli/runtime/agent_transport.py +115 -0
  39. loft_cli/runtime/executor.py +798 -0
  40. loft_cli/runtime/fabric_transport.py +68 -0
  41. loft_cli/runtime/secret_rotation.py +99 -0
  42. loft_cli/runtime/ssh.py +147 -0
  43. loft_cli/runtime/steps/__init__.py +0 -0
  44. loft_cli/runtime/steps/backup.py +72 -0
  45. loft_cli/runtime/steps/bootstrap.py +227 -0
  46. loft_cli/runtime/steps/compose.py +43 -0
  47. loft_cli/runtime/steps/container.py +40 -0
  48. loft_cli/runtime/steps/docker.py +15 -0
  49. loft_cli/runtime/steps/file_template.py +25 -0
  50. loft_cli/runtime/steps/nginx.py +126 -0
  51. loft_cli/runtime/steps/postgres.py +101 -0
  52. loft_cli/runtime/steps/postgres_ensure.py +141 -0
  53. loft_cli/runtime/steps/systemd.py +131 -0
  54. loft_cli/runtime/steps/wireguard.py +76 -0
  55. loft_cli/runtime/transport.py +43 -0
  56. loft_cli/updater.py +208 -0
  57. loft_cli-0.6.3.dist-info/METADATA +19 -0
  58. loft_cli-0.6.3.dist-info/RECORD +61 -0
  59. loft_cli-0.6.3.dist-info/WHEEL +5 -0
  60. loft_cli-0.6.3.dist-info/entry_points.txt +5 -0
  61. loft_cli-0.6.3.dist-info/top_level.txt +1 -0
File without changes
@@ -0,0 +1,420 @@
1
+ """Phase 2: Fill defaults and resolve computed values from spec."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import os
7
+ from dataclasses import dataclass, field
8
+ from pathlib import Path
9
+
10
+ from loft_cli_core.specs.backup_job_schema import BackupJobSpec
11
+ from loft_cli_core.specs.bootstrap_schema import BootstrapSpec
12
+ from loft_cli_core.specs.compose_project_schema import ComposeProjectSpec
13
+ from loft_cli_core.specs.file_template_schema import FileTemplateSpec
14
+ from loft_cli_core.specs.http_check_schema import HttpCheckSpec
15
+ from loft_cli_core.specs.postgres_ensure_schema import PostgresEnsureSpec
16
+ from loft_cli_core.specs.service_schema import ServiceSpec
17
+ from loft_cli_core.specs.stack_schema import StackSpec
18
+ from loft_cli_core.specs.systemd_timer_schema import SystemdTimerSpec
19
+ from loft_cli_core.specs.systemd_unit_schema import SystemdUnitSpec
20
+ from loft_cli_core.utils.files import expand_path, resolve_path
21
+
22
+ AnySpec = (
23
+ BootstrapSpec
24
+ | ServiceSpec
25
+ | FileTemplateSpec
26
+ | ComposeProjectSpec
27
+ | StackSpec
28
+ | HttpCheckSpec
29
+ | SystemdUnitSpec
30
+ | SystemdTimerSpec
31
+ | BackupJobSpec
32
+ | PostgresEnsureSpec
33
+ )
34
+
35
+
36
+ @dataclass
37
+ class NormalizedContext:
38
+ """Resolved values derived from the spec during normalization."""
39
+
40
+ spec: AnySpec
41
+ spec_dir: Path | None = (
42
+ None # directory containing the spec file; used for relative path resolution
43
+ )
44
+
45
+ # Template rendering results (file_template and compose_project)
46
+ rendered_templates: dict[str, str] = field(default_factory=dict) # dest -> rendered content
47
+ template_hashes: dict[str, str] = field(default_factory=dict) # dest -> sha256 of content
48
+ compose_file_content: str = "" # raw or rendered compose file content
49
+
50
+ # Bootstrap-specific resolved values
51
+ pubkey_contents: list[str] = field(default_factory=list)
52
+ wireguard_private_key: str = ""
53
+ wireguard_public_key: str = "" # derived via PyNaCl from server private key
54
+ wireguard_conf_content: str = "" # server config; populated by planner, used by executor
55
+ wg_client_private_key: str = "" # auto-generated client Curve25519 private key
56
+ wg_client_public_key: str = "" # derived via PyNaCl from client private key
57
+ wg_client_conf_content: str = "" # client wg-quick config; populated by planner, saved locally
58
+ ssh_conf_d_path: Path | None = None
59
+ db_path: Path | None = None
60
+ login_key_path: Path | None = None
61
+ login_password: str | None = None
62
+ admin_key_path: Path | None = None
63
+
64
+
65
+ def normalize(spec, spec_dir: Path | None = None) -> NormalizedContext:
66
+ """Resolve all paths, read key files, compute derived values.
67
+
68
+ Args:
69
+ spec: Parsed spec object.
70
+ spec_dir: Directory of the spec file. Relative paths in the spec are
71
+ resolved against this directory first. Falls back to CWD if None.
72
+ """
73
+ # Ensure built-in and addon kinds are registered (idempotent).
74
+ from loft_cli_core.registry import get_normalizer, load_addons
75
+
76
+ load_addons()
77
+
78
+ if isinstance(spec, list):
79
+ # Multi-document: normalize each spec independently, return list of contexts
80
+ return [normalize(s, spec_dir=spec_dir) for s in spec]
81
+
82
+ ctx = NormalizedContext(spec=spec, spec_dir=spec_dir)
83
+
84
+ normalizer = get_normalizer(spec.kind)
85
+ if normalizer is None:
86
+ raise RuntimeError(f"No normalizer registered for spec kind '{spec.kind}'")
87
+ normalizer(spec, ctx)
88
+
89
+ return ctx
90
+
91
+
92
+ def _generate_wg_private_key() -> str:
93
+ """Generate a fresh Curve25519 private key encoded as WireGuard base64."""
94
+ import nacl.public
95
+
96
+ priv = nacl.public.PrivateKey.generate()
97
+ return base64.b64encode(bytes(priv)).decode()
98
+
99
+
100
+ def _derive_wg_public_key(private_key_b64: str) -> str:
101
+ """Derive a WireGuard public key from a base64 Curve25519 private key.
102
+
103
+ Uses PyNaCl (libsodium) which is already a transitive dependency via
104
+ Fabric/Paramiko and is now an explicit dependency in pyproject.toml.
105
+ Returns an empty string if the key is invalid or PyNaCl is unavailable.
106
+ """
107
+ try:
108
+ import nacl.public
109
+
110
+ key_bytes = base64.b64decode(private_key_b64)
111
+ priv = nacl.public.PrivateKey(key_bytes)
112
+ return base64.b64encode(bytes(priv.public_key)).decode()
113
+ except Exception:
114
+ return ""
115
+
116
+
117
+ def _apply_state_dir(spec) -> None:
118
+ """Apply state_dir override from env var or spec field.
119
+
120
+ Priority (highest to lowest):
121
+ 1. LOFT_CLI_STATE_DIR environment variable
122
+ 2. local.state_dir spec field
123
+ 3. Built-in defaults (no action needed)
124
+
125
+ When a state_dir is active, re-registers LocalPathsConfig so all
126
+ downstream code (ssh_config, wireguard_store, etc.) picks it up.
127
+ """
128
+ from loft_cli_core.registry.local_paths import LocalPathsConfig, register_local_paths
129
+
130
+ env_state_dir = os.environ.get("LOFT_CLI_STATE_DIR")
131
+ spec_state_dir = spec.local.state_dir if spec.local.state_dir else None
132
+
133
+ effective = env_state_dir or spec_state_dir
134
+ if effective:
135
+ register_local_paths(LocalPathsConfig(state_dir=Path(effective)))
136
+
137
+
138
+ def _resolve_db_path(spec) -> Path:
139
+ """Resolve inventory database path respecting the priority order.
140
+
141
+ Priority (highest to lowest):
142
+ 1. Explicit spec field (local.inventory.db_path) if non-default
143
+ 2. state_dir-derived path (via get_local_paths())
144
+ 3. Built-in default (~/.loft-cli/inventory.db)
145
+ """
146
+ from loft_cli_core.registry.local_paths import get_local_paths
147
+
148
+ inv = spec.local.inventory
149
+ default_db = "~/.loft-cli/inventory.db"
150
+ if inv.db_path and inv.db_path != default_db:
151
+ # Explicit per-resource override in spec
152
+ return expand_path(inv.db_path)
153
+ # Use the centrally-resolved path (state_dir-aware)
154
+ return get_local_paths().inventory_db_path
155
+
156
+
157
+ def _normalize_bootstrap(spec: BootstrapSpec, ctx: NormalizedContext) -> None:
158
+ spec_dir = ctx.spec_dir
159
+
160
+ # Apply state_dir override before resolving any local paths
161
+ _apply_state_dir(spec)
162
+
163
+ # Resolve login private key path
164
+ ctx.login_key_path = (
165
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
166
+ )
167
+ ctx.login_password = spec.login.password or None
168
+
169
+ # Derive admin private key path from the first .pub entry in pubkeys
170
+ for pk_str in spec.admin_user.pubkeys:
171
+ pk_path = resolve_path(pk_str, spec_dir)
172
+ if pk_path.suffix == ".pub":
173
+ candidate = pk_path.with_suffix("")
174
+ if candidate.exists():
175
+ ctx.admin_key_path = candidate
176
+ break
177
+
178
+ # Read pubkey file contents (missing files are stored as placeholder for plan/docs)
179
+ for pk_path_str in spec.admin_user.pubkeys:
180
+ pk_path = resolve_path(pk_path_str, spec_dir)
181
+ if pk_path.exists():
182
+ ctx.pubkey_contents.append(pk_path.read_text(encoding="utf-8").strip())
183
+ else:
184
+ ctx.pubkey_contents.append(f"<key not found: {pk_path}>")
185
+
186
+ # Read WireGuard server private key and derive public key via PyNaCl (Curve25519).
187
+ # When private_key_file is omitted, the key is auto-generated using write-once
188
+ # semantics: reuse from local state if it exists, otherwise generate a fresh key.
189
+ # The key is persisted by save_wireguard_state after a successful apply.
190
+ if spec.wireguard.enabled:
191
+ if spec.wireguard.private_key_file:
192
+ wg_key_path = resolve_path(spec.wireguard.private_key_file, spec_dir)
193
+ if wg_key_path.exists():
194
+ ctx.wireguard_private_key = wg_key_path.read_text(encoding="utf-8").strip()
195
+ ctx.wireguard_public_key = _derive_wg_public_key(ctx.wireguard_private_key)
196
+ else:
197
+ ctx.wireguard_private_key = f"<key not found: {wg_key_path}>"
198
+ ctx.wireguard_public_key = ""
199
+ else:
200
+ # Auto-generate: reuse persisted key or generate fresh (write-once)
201
+ from loft_cli_core.registry.local_paths import get_local_paths
202
+
203
+ server_key_path = get_local_paths().wg_state_base / spec.host.name / "private.key"
204
+ if server_key_path.exists():
205
+ ctx.wireguard_private_key = server_key_path.read_text(encoding="utf-8").strip()
206
+ else:
207
+ ctx.wireguard_private_key = _generate_wg_private_key()
208
+ ctx.wireguard_public_key = _derive_wg_public_key(ctx.wireguard_private_key)
209
+
210
+ # Auto-generate (or reuse) WireGuard client key pair.
211
+ # The client private key is persisted to ~/.wg/loft-cli/{host}/client.key after
212
+ # a successful apply so that re-runs reuse the same key (stable peer identity).
213
+ # On first run the file won't exist yet — we generate it in memory here and the
214
+ # executor's save_wireguard_state step writes it to disk.
215
+ if spec.wireguard.enabled:
216
+ from loft_cli_core.registry.local_paths import get_local_paths
217
+
218
+ client_key_path = get_local_paths().wg_state_base / spec.host.name / "client.key"
219
+ if client_key_path.exists():
220
+ ctx.wg_client_private_key = client_key_path.read_text(encoding="utf-8").strip()
221
+ else:
222
+ ctx.wg_client_private_key = _generate_wg_private_key()
223
+ ctx.wg_client_public_key = _derive_wg_public_key(ctx.wg_client_private_key)
224
+
225
+ # Compute SSH conf.d path using the addon-overridable base directory
226
+ from loft_cli_core.registry.local_paths import get_local_paths
227
+
228
+ ssh_conf_d_base = get_local_paths().ssh_conf_d_base
229
+ ctx.ssh_conf_d_path = ssh_conf_d_base / f"{spec.host.name}.conf"
230
+
231
+ # If host_alias not set, default to host name
232
+ if not spec.local.ssh_config.host_alias:
233
+ spec.local.ssh_config.host_alias = spec.host.name
234
+
235
+ # Resolve inventory db path (state_dir-aware)
236
+ ctx.db_path = _resolve_db_path(spec)
237
+
238
+
239
+ def _normalize_service(spec: ServiceSpec, ctx: NormalizedContext) -> None:
240
+ spec_dir = ctx.spec_dir
241
+
242
+ # Apply state_dir override before resolving any local paths
243
+ _apply_state_dir(spec)
244
+
245
+ # Resolve login key
246
+ ctx.login_key_path = (
247
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
248
+ )
249
+ ctx.login_password = spec.login.password or None
250
+
251
+ # Resolve inventory (state_dir-aware)
252
+ ctx.db_path = _resolve_db_path(spec)
253
+
254
+ # Resolve postgres role password from env
255
+ if spec.postgres and spec.postgres.create_role:
256
+ role = spec.postgres.create_role
257
+ if role.password_env:
258
+ pw = os.environ.get(role.password_env, "")
259
+ role.password_env = pw # store resolved value
260
+
261
+
262
+ def _normalize_file_template(spec: FileTemplateSpec, ctx: NormalizedContext) -> None:
263
+ """Normalize a file_template spec: resolve paths, render templates at plan time."""
264
+ from loft_cli_core.utils.templates import content_hash, render_template_file
265
+
266
+ spec_dir = ctx.spec_dir
267
+
268
+ # Apply state_dir override before resolving any local paths
269
+ _apply_state_dir(spec)
270
+
271
+ # Resolve login key
272
+ ctx.login_key_path = (
273
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
274
+ )
275
+ ctx.login_password = spec.login.password or None
276
+
277
+ # Resolve inventory (state_dir-aware)
278
+ ctx.db_path = _resolve_db_path(spec)
279
+
280
+ # Render all templates with Jinja2 and store results on ctx
281
+ for t in spec.templates:
282
+ src_path = resolve_path(t.src, spec_dir)
283
+ rendered = render_template_file(src_path, spec.variables)
284
+ ctx.rendered_templates[t.dest] = rendered
285
+ ctx.template_hashes[t.dest] = content_hash(rendered)
286
+
287
+
288
+ def _normalize_compose_project(spec: ComposeProjectSpec, ctx: NormalizedContext) -> None:
289
+ """Normalize a compose_project spec: resolve paths, render templates, read compose file."""
290
+ from loft_cli_core.utils.templates import content_hash, render_template_file
291
+
292
+ spec_dir = ctx.spec_dir
293
+ p = spec.project
294
+
295
+ # Apply state_dir override before resolving any local paths
296
+ _apply_state_dir(spec)
297
+
298
+ # Resolve login key
299
+ ctx.login_key_path = (
300
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
301
+ )
302
+ ctx.login_password = spec.login.password or None
303
+
304
+ # Resolve inventory (state_dir-aware)
305
+ ctx.db_path = _resolve_db_path(spec)
306
+
307
+ # Render project templates (Jinja2)
308
+ for t in p.templates:
309
+ src_path = resolve_path(t.src, spec_dir)
310
+ rendered = render_template_file(src_path, p.variables)
311
+ # dest is relative to project directory — store with full remote path as key
312
+ if t.dest.startswith("/"):
313
+ full_dest = t.dest
314
+ else:
315
+ full_dest = f"{p.directory}/{t.dest}"
316
+ ctx.rendered_templates[full_dest] = rendered
317
+ ctx.template_hashes[full_dest] = content_hash(rendered)
318
+
319
+ # Read the compose file (static, not rendered through Jinja2)
320
+ compose_path = resolve_path(p.compose_file, spec_dir)
321
+ if compose_path.exists():
322
+ ctx.compose_file_content = compose_path.read_text(encoding="utf-8")
323
+ else:
324
+ ctx.compose_file_content = f"<compose file not found: {compose_path}>"
325
+
326
+
327
+ def _normalize_postgres_ensure(spec: PostgresEnsureSpec, ctx: NormalizedContext) -> None:
328
+ """Normalize a postgres_ensure spec: resolve login, paths, passwords."""
329
+ spec_dir = ctx.spec_dir
330
+
331
+ _apply_state_dir(spec)
332
+
333
+ ctx.login_key_path = (
334
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
335
+ )
336
+ ctx.login_password = spec.login.password or None
337
+
338
+ ctx.db_path = _resolve_db_path(spec)
339
+
340
+ # Resolve password_env for each user
341
+ for user in spec.users:
342
+ if user.password_env:
343
+ pw = os.environ.get(user.password_env, "")
344
+ user.password_env = pw # store resolved value
345
+
346
+
347
+ def _normalize_backup_job(spec: BackupJobSpec, ctx: NormalizedContext) -> None:
348
+ """Normalize a backup_job spec: resolve login, paths, state_dir."""
349
+ spec_dir = ctx.spec_dir
350
+
351
+ _apply_state_dir(spec)
352
+
353
+ ctx.login_key_path = (
354
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
355
+ )
356
+ ctx.login_password = spec.login.password or None
357
+
358
+ ctx.db_path = _resolve_db_path(spec)
359
+
360
+
361
+ def _normalize_systemd_unit(spec: SystemdUnitSpec, ctx: NormalizedContext) -> None:
362
+ """Normalize a systemd_unit spec: resolve login, paths, state_dir."""
363
+ spec_dir = ctx.spec_dir
364
+
365
+ _apply_state_dir(spec)
366
+
367
+ ctx.login_key_path = (
368
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
369
+ )
370
+ ctx.login_password = spec.login.password or None
371
+
372
+ ctx.db_path = _resolve_db_path(spec)
373
+
374
+
375
+ def _normalize_systemd_timer(spec: SystemdTimerSpec, ctx: NormalizedContext) -> None:
376
+ """Normalize a systemd_timer spec: resolve login, paths, state_dir."""
377
+ spec_dir = ctx.spec_dir
378
+
379
+ _apply_state_dir(spec)
380
+
381
+ ctx.login_key_path = (
382
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
383
+ )
384
+ ctx.login_password = spec.login.password or None
385
+
386
+ ctx.db_path = _resolve_db_path(spec)
387
+
388
+
389
+ def _normalize_http_check(spec: HttpCheckSpec, ctx: NormalizedContext) -> None:
390
+ """Normalize an http_check spec: resolve login, paths, state_dir."""
391
+ spec_dir = ctx.spec_dir
392
+
393
+ # Apply state_dir override before resolving any local paths
394
+ _apply_state_dir(spec)
395
+
396
+ # Resolve login key
397
+ ctx.login_key_path = (
398
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
399
+ )
400
+ ctx.login_password = spec.login.password or None
401
+
402
+ # Resolve inventory (state_dir-aware)
403
+ ctx.db_path = _resolve_db_path(spec)
404
+
405
+
406
+ def _normalize_stack(spec: StackSpec, ctx: NormalizedContext) -> None:
407
+ """Normalize a stack spec: resolve login, paths, state_dir."""
408
+ spec_dir = ctx.spec_dir
409
+
410
+ # Apply state_dir override before resolving any local paths
411
+ _apply_state_dir(spec)
412
+
413
+ # Resolve login key
414
+ ctx.login_key_path = (
415
+ resolve_path(spec.login.private_key, spec_dir) if spec.login.private_key else None
416
+ )
417
+ ctx.login_password = spec.login.password or None
418
+
419
+ # Resolve inventory (state_dir-aware)
420
+ ctx.db_path = _resolve_db_path(spec)
@@ -0,0 +1,42 @@
1
+ """Phase 1: Load YAML spec file into a typed model."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ from loft_cli_core.specs.bootstrap_schema import BootstrapSpec
8
+ from loft_cli_core.specs.compose_project_schema import ComposeProjectSpec
9
+ from loft_cli_core.specs.file_template_schema import FileTemplateSpec
10
+ from loft_cli_core.specs.loader import load_spec
11
+ from loft_cli_core.specs.service_schema import ServiceSpec
12
+ from loft_cli_core.specs.stack_schema import StackSpec
13
+
14
+ AnySpec = BootstrapSpec | ServiceSpec | FileTemplateSpec | ComposeProjectSpec | StackSpec
15
+
16
+
17
+ def parse(
18
+ spec_path: Path,
19
+ *,
20
+ strict_env: bool = True,
21
+ env_file: Path | None = None,
22
+ env_files: list[Path] | None = None,
23
+ ) -> AnySpec | list[AnySpec]:
24
+ """Load and parse a YAML spec file.
25
+
26
+ Parameters
27
+ ----------
28
+ spec_path:
29
+ Path to the YAML spec file.
30
+ strict_env:
31
+ When True (default), unresolved ``${VAR}`` references raise an error.
32
+ When False, they are left unchanged (passthrough mode).
33
+ env_file:
34
+ Optional path to a single ``.env`` file (backward-compatible).
35
+ env_files:
36
+ Optional list of ``.env`` file paths. Later files override earlier
37
+ ones; existing ``os.environ`` values always take precedence.
38
+
39
+ Returns a single spec for single-document files, or a list of specs
40
+ for multi-document files (separated by ``---``).
41
+ """
42
+ return load_spec(spec_path, strict_env=strict_env, env_file=env_file, env_files=env_files)