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.
- loft_cli/__init__.py +3 -0
- loft_cli/_builtins.py +269 -0
- loft_cli/addons/__init__.py +12 -0
- loft_cli/addons/goss/__init__.py +1 -0
- loft_cli/addons/goss/generator.py +202 -0
- loft_cli/addons/goss/renderer.py +207 -0
- loft_cli/addons/goss/shipper.py +174 -0
- loft_cli/agent_installer.py +65 -0
- loft_cli/checks/__init__.py +0 -0
- loft_cli/checks/compose.py +147 -0
- loft_cli/checks/container.py +24 -0
- loft_cli/checks/http.py +30 -0
- loft_cli/checks/nginx.py +21 -0
- loft_cli/checks/ports.py +24 -0
- loft_cli/checks/postgres.py +21 -0
- loft_cli/checks/ssh.py +66 -0
- loft_cli/checks/wireguard.py +21 -0
- loft_cli/cli.py +1161 -0
- loft_cli/compiler/__init__.py +0 -0
- loft_cli/compiler/normalizer.py +420 -0
- loft_cli/compiler/parser.py +42 -0
- loft_cli/compiler/planner.py +2543 -0
- loft_cli/local/__init__.py +0 -0
- loft_cli/local/ddl/__init__.py +0 -0
- loft_cli/local/ddl/bootstrap_tables.py +75 -0
- loft_cli/local/ddl/versionize_system.py +374 -0
- loft_cli/local/inventory.py +449 -0
- loft_cli/local/inventory_db.py +255 -0
- loft_cli/local/keys.py +45 -0
- loft_cli/local/remove.py +135 -0
- loft_cli/local/ssh_config.py +118 -0
- loft_cli/local/tunnel.py +228 -0
- loft_cli/local/wireguard_store.py +138 -0
- loft_cli/logs/__init__.py +0 -0
- loft_cli/logs/reader.py +55 -0
- loft_cli/logs/writer.py +58 -0
- loft_cli/runtime/__init__.py +0 -0
- loft_cli/runtime/agent_transport.py +115 -0
- loft_cli/runtime/executor.py +798 -0
- loft_cli/runtime/fabric_transport.py +68 -0
- loft_cli/runtime/secret_rotation.py +99 -0
- loft_cli/runtime/ssh.py +147 -0
- loft_cli/runtime/steps/__init__.py +0 -0
- loft_cli/runtime/steps/backup.py +72 -0
- loft_cli/runtime/steps/bootstrap.py +227 -0
- loft_cli/runtime/steps/compose.py +43 -0
- loft_cli/runtime/steps/container.py +40 -0
- loft_cli/runtime/steps/docker.py +15 -0
- loft_cli/runtime/steps/file_template.py +25 -0
- loft_cli/runtime/steps/nginx.py +126 -0
- loft_cli/runtime/steps/postgres.py +101 -0
- loft_cli/runtime/steps/postgres_ensure.py +141 -0
- loft_cli/runtime/steps/systemd.py +131 -0
- loft_cli/runtime/steps/wireguard.py +76 -0
- loft_cli/runtime/transport.py +43 -0
- loft_cli/updater.py +208 -0
- loft_cli-0.6.3.dist-info/METADATA +19 -0
- loft_cli-0.6.3.dist-info/RECORD +61 -0
- loft_cli-0.6.3.dist-info/WHEEL +5 -0
- loft_cli-0.6.3.dist-info/entry_points.txt +5 -0
- loft_cli-0.6.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"""Render goss validation results as a human-readable Rich table.
|
|
2
|
+
|
|
3
|
+
The output is intentionally concise: one row per check, colour-coded status,
|
|
4
|
+
a summary line, and — if there were failures — a separate panel listing only
|
|
5
|
+
the failing checks with their details so the operator can act immediately.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
from rich.panel import Panel
|
|
14
|
+
from rich.table import Table
|
|
15
|
+
from rich.text import Text
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
# Goss resource-type display names (goss JSON uses lower-case keys)
|
|
21
|
+
_TYPE_LABELS: dict[str, str] = {
|
|
22
|
+
"User": "user",
|
|
23
|
+
"File": "file",
|
|
24
|
+
"Service": "service",
|
|
25
|
+
"Port": "port",
|
|
26
|
+
"Package": "package",
|
|
27
|
+
"Command": "command",
|
|
28
|
+
"Interface": "interface",
|
|
29
|
+
"Addr": "addr",
|
|
30
|
+
"DNS": "dns",
|
|
31
|
+
"HTTP": "http",
|
|
32
|
+
"Process": "process",
|
|
33
|
+
"Kernel": "kernel-param",
|
|
34
|
+
"Mount": "mount",
|
|
35
|
+
"Gossfile": "gossfile",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def render_goss_results(goss_result: dict, console: Console | None = None) -> None:
|
|
40
|
+
"""Pretty-print goss validation results to the terminal.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
goss_result: dict returned by ``shipper.ship_and_run()``.
|
|
44
|
+
console: Rich Console to write to (creates a new one if None).
|
|
45
|
+
"""
|
|
46
|
+
if console is None:
|
|
47
|
+
console = Console()
|
|
48
|
+
|
|
49
|
+
# ------------------------------------------------------------------ #
|
|
50
|
+
# Guard: shipper-level error (goss never ran)
|
|
51
|
+
# ------------------------------------------------------------------ #
|
|
52
|
+
if goss_result.get("error"):
|
|
53
|
+
console.print(
|
|
54
|
+
Panel(
|
|
55
|
+
f"[bold red]Goss could not run:[/bold red]\n{goss_result['error']}",
|
|
56
|
+
title="[bold red]Goss Error[/bold red]",
|
|
57
|
+
border_style="red",
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
return
|
|
61
|
+
|
|
62
|
+
results: list[dict] = goss_result.get("results", [])
|
|
63
|
+
summary: dict = goss_result.get("summary", {})
|
|
64
|
+
|
|
65
|
+
total = summary.get("test-count", len(results))
|
|
66
|
+
passed = summary.get("success-count", sum(1 for r in results if r.get("successful")))
|
|
67
|
+
failed = summary.get("failed-count", sum(1 for r in results if not r.get("successful")))
|
|
68
|
+
skipped = summary.get("skipped-count", 0)
|
|
69
|
+
duration = summary.get("total-duration", 0)
|
|
70
|
+
duration_s = f"{duration / 1e9:.2f}s" if duration else ""
|
|
71
|
+
|
|
72
|
+
all_pass = failed == 0
|
|
73
|
+
|
|
74
|
+
# ------------------------------------------------------------------ #
|
|
75
|
+
# Main results table
|
|
76
|
+
# ------------------------------------------------------------------ #
|
|
77
|
+
table = Table(
|
|
78
|
+
show_header=True,
|
|
79
|
+
header_style="bold dim",
|
|
80
|
+
border_style="bright_blue",
|
|
81
|
+
box=_rich_box(),
|
|
82
|
+
padding=(0, 1),
|
|
83
|
+
expand=False,
|
|
84
|
+
)
|
|
85
|
+
table.add_column("Resource type", min_width=12, no_wrap=True)
|
|
86
|
+
table.add_column("Resource ID", min_width=30)
|
|
87
|
+
table.add_column("Property", min_width=16)
|
|
88
|
+
table.add_column("Status", min_width=9, justify="center")
|
|
89
|
+
table.add_column("Details", min_width=24)
|
|
90
|
+
|
|
91
|
+
for r in results:
|
|
92
|
+
resource_type = r.get("resource-type", "")
|
|
93
|
+
resource_id = r.get("resource-id", "")
|
|
94
|
+
property_name = r.get("property", "")
|
|
95
|
+
successful = r.get("successful", False)
|
|
96
|
+
expected = r.get("expected", [])
|
|
97
|
+
found = r.get("found", [])
|
|
98
|
+
msg = r.get("msg", "")
|
|
99
|
+
|
|
100
|
+
label = _TYPE_LABELS.get(resource_type, resource_type.lower())
|
|
101
|
+
|
|
102
|
+
if successful:
|
|
103
|
+
status_text = Text("✓ PASS", style="bold green")
|
|
104
|
+
details = Text("")
|
|
105
|
+
else:
|
|
106
|
+
status_text = Text("✗ FAIL", style="bold red")
|
|
107
|
+
# Produce a compact diff-style detail string
|
|
108
|
+
exp_str = _short(expected)
|
|
109
|
+
found_str = _short(found)
|
|
110
|
+
if msg and msg not in ("", "none"):
|
|
111
|
+
details = Text(msg[:60], style="red")
|
|
112
|
+
elif exp_str != found_str:
|
|
113
|
+
details = Text(f"want {exp_str} got {found_str}", style="red")
|
|
114
|
+
else:
|
|
115
|
+
details = Text(exp_str[:60], style="red")
|
|
116
|
+
|
|
117
|
+
table.add_row(
|
|
118
|
+
Text(label, style="dim"),
|
|
119
|
+
Text(_truncate(resource_id, 40), style=""),
|
|
120
|
+
Text(property_name, style="dim"),
|
|
121
|
+
status_text,
|
|
122
|
+
details,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
# ------------------------------------------------------------------ #
|
|
126
|
+
# Summary footer text
|
|
127
|
+
# ------------------------------------------------------------------ #
|
|
128
|
+
if all_pass:
|
|
129
|
+
summary_style = "bold green"
|
|
130
|
+
summary_icon = "✓"
|
|
131
|
+
summary_label = "All checks passed"
|
|
132
|
+
else:
|
|
133
|
+
summary_style = "bold red"
|
|
134
|
+
summary_icon = "✗"
|
|
135
|
+
summary_label = f"{failed} check{'s' if failed != 1 else ''} failed"
|
|
136
|
+
|
|
137
|
+
counts = (
|
|
138
|
+
f"{total} total • "
|
|
139
|
+
f"[green]{passed} passed[/green] • "
|
|
140
|
+
f"{'[red]' if failed else '[dim]'}{failed} failed{'[/red]' if failed else '[/dim]'} • "
|
|
141
|
+
f"[dim]{skipped} skipped[/dim]" + (f" • [dim]{duration_s}[/dim]" if duration_s else "")
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
title = f"[{summary_style}]{summary_icon} Goss Verification — {summary_label}[/{summary_style}]"
|
|
145
|
+
|
|
146
|
+
console.print(Panel(table, title=title, border_style="green" if all_pass else "red"))
|
|
147
|
+
console.print(f" {counts}\n")
|
|
148
|
+
|
|
149
|
+
# ------------------------------------------------------------------ #
|
|
150
|
+
# Failure detail panel — only shown when there are failures
|
|
151
|
+
# ------------------------------------------------------------------ #
|
|
152
|
+
if not all_pass:
|
|
153
|
+
failed_lines: list[str] = []
|
|
154
|
+
for r in results:
|
|
155
|
+
if r.get("successful"):
|
|
156
|
+
continue
|
|
157
|
+
rtype = _TYPE_LABELS.get(r.get("resource-type", ""), r.get("resource-type", "").lower())
|
|
158
|
+
rid = r.get("resource-id", "")
|
|
159
|
+
prop = r.get("property", "")
|
|
160
|
+
exp = _short(r.get("expected", []))
|
|
161
|
+
fnd = _short(r.get("found", []))
|
|
162
|
+
msg = r.get("msg", "")
|
|
163
|
+
line = f"[bold]{rtype}[/bold].[dim]{_truncate(rid, 35)}[/dim] {prop}"
|
|
164
|
+
if msg and msg not in ("", "none"):
|
|
165
|
+
line += f"\n [red]{msg}[/red]"
|
|
166
|
+
else:
|
|
167
|
+
line += f"\n expected [green]{exp}[/green] got [red]{fnd}[/red]"
|
|
168
|
+
failed_lines.append(line)
|
|
169
|
+
|
|
170
|
+
console.print(
|
|
171
|
+
Panel(
|
|
172
|
+
"\n\n".join(failed_lines),
|
|
173
|
+
title="[bold red]Failed checks — action required[/bold red]",
|
|
174
|
+
border_style="red",
|
|
175
|
+
padding=(1, 2),
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# ------------------------------------------------------------------ #
|
|
181
|
+
# Helpers
|
|
182
|
+
# ------------------------------------------------------------------ #
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _short(value) -> str:
|
|
186
|
+
"""Compact representation of goss expected/found lists."""
|
|
187
|
+
if isinstance(value, list):
|
|
188
|
+
if len(value) == 0:
|
|
189
|
+
return "[]"
|
|
190
|
+
if len(value) == 1:
|
|
191
|
+
return str(value[0])
|
|
192
|
+
return "[" + ", ".join(str(v) for v in value[:3]) + (", …" if len(value) > 3 else "") + "]"
|
|
193
|
+
return str(value)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _truncate(s: str, n: int) -> str:
|
|
197
|
+
return s if len(s) <= n else s[: n - 1] + "…"
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _rich_box():
|
|
201
|
+
"""Return a simple box style compatible with all Rich versions."""
|
|
202
|
+
try:
|
|
203
|
+
from rich import box
|
|
204
|
+
|
|
205
|
+
return box.SIMPLE_HEAVY
|
|
206
|
+
except Exception:
|
|
207
|
+
return None
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""Ship a goss spec to the remote server and run it.
|
|
2
|
+
|
|
3
|
+
Responsibilities:
|
|
4
|
+
1. Ensure goss binary is present on the remote server (install via curl if missing).
|
|
5
|
+
2. Create ~/.goss/ directory on the remote (resolved dynamically via $HOME
|
|
6
|
+
so it works for any connecting user, not just root).
|
|
7
|
+
3. Upload the generated goss YAML to ~/.goss/<spec_name>.yaml.
|
|
8
|
+
4. Read-modify-write the master gossfile ~/.goss/goss.yaml so every shipped
|
|
9
|
+
spec is included in a single umbrella run.
|
|
10
|
+
5. Run `goss -g ~/.goss/goss.yaml validate --format json` and return the
|
|
11
|
+
parsed result dict, plus the raw stdout for debugging.
|
|
12
|
+
|
|
13
|
+
The caller (executor) decides what to do with the result dict.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
from typing import TYPE_CHECKING
|
|
20
|
+
|
|
21
|
+
import yaml
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from loft_cli.runtime.ssh import SSHSession
|
|
25
|
+
|
|
26
|
+
# Install command: idempotent — only runs curl if goss is not already on PATH.
|
|
27
|
+
# Installs to /usr/local/bin so it is available system-wide.
|
|
28
|
+
_GOSS_INSTALL_CMD = (
|
|
29
|
+
"command -v goss >/dev/null 2>&1 || (curl -fsSL https://goss.rocks/install | sudo sh)"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _resolve_goss_remote_dir(session: SSHSession) -> str:
|
|
34
|
+
"""Return the absolute path to the goss directory on the remote.
|
|
35
|
+
|
|
36
|
+
Runs ``echo $HOME`` on the remote to determine the connecting user's home
|
|
37
|
+
directory, then appends ``/.goss``. This mirrors the ``~`` expansion in
|
|
38
|
+
``executor._execute_ssh_upload()`` and ensures goss files land in the
|
|
39
|
+
correct home regardless of whether we connect as root or as the admin user
|
|
40
|
+
(e.g. on an idempotent re-run where credential fallback activates).
|
|
41
|
+
|
|
42
|
+
Falls back to ``/root/.goss`` if the probe fails (should never happen in
|
|
43
|
+
practice — $HOME is always set for SSH sessions).
|
|
44
|
+
"""
|
|
45
|
+
result = session.run("echo $HOME", sudo=False, warn=True)
|
|
46
|
+
if result.ok and result.stdout.strip():
|
|
47
|
+
return f"{result.stdout.strip()}/.goss"
|
|
48
|
+
return "/root/.goss"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def ship_and_run(
|
|
52
|
+
session: SSHSession,
|
|
53
|
+
spec_name: str,
|
|
54
|
+
goss_yaml_content: str,
|
|
55
|
+
admin_user: str,
|
|
56
|
+
) -> dict:
|
|
57
|
+
"""Ship spec, update master gossfile, run validate.
|
|
58
|
+
|
|
59
|
+
Returns a dict with keys:
|
|
60
|
+
- "results": list of individual check result dicts (from goss JSON)
|
|
61
|
+
- "summary": top-level summary dict from goss JSON
|
|
62
|
+
- "raw_output": raw stdout string (for debugging)
|
|
63
|
+
- "exit_ok": bool — True if goss reported zero failures
|
|
64
|
+
- "error": str — non-empty when something failed before goss ran
|
|
65
|
+
"""
|
|
66
|
+
# ------------------------------------------------------------------ #
|
|
67
|
+
# 0. Resolve the remote goss directory for the connecting user
|
|
68
|
+
# ------------------------------------------------------------------ #
|
|
69
|
+
goss_dir = _resolve_goss_remote_dir(session)
|
|
70
|
+
goss_master = f"{goss_dir}/goss.yaml"
|
|
71
|
+
|
|
72
|
+
# ------------------------------------------------------------------ #
|
|
73
|
+
# 1. Install goss if absent
|
|
74
|
+
# ------------------------------------------------------------------ #
|
|
75
|
+
install_result = session.run(_GOSS_INSTALL_CMD, sudo=False, warn=True)
|
|
76
|
+
if not install_result.ok:
|
|
77
|
+
return _error(f"goss install failed: {install_result.stderr}")
|
|
78
|
+
|
|
79
|
+
# ------------------------------------------------------------------ #
|
|
80
|
+
# 2. Ensure ~/.goss/ directory exists
|
|
81
|
+
# ------------------------------------------------------------------ #
|
|
82
|
+
mkdir_result = session.run(f"mkdir -p {goss_dir}", sudo=False, warn=True)
|
|
83
|
+
if not mkdir_result.ok:
|
|
84
|
+
return _error(f"mkdir {goss_dir} failed: {mkdir_result.stderr}")
|
|
85
|
+
|
|
86
|
+
# ------------------------------------------------------------------ #
|
|
87
|
+
# 3. Upload the generated goss spec
|
|
88
|
+
# ------------------------------------------------------------------ #
|
|
89
|
+
spec_remote_path = f"{goss_dir}/{spec_name}.yaml"
|
|
90
|
+
try:
|
|
91
|
+
session.upload_content(goss_yaml_content, spec_remote_path, sudo=False)
|
|
92
|
+
except Exception as exc:
|
|
93
|
+
return _error(f"upload of goss spec failed: {exc}")
|
|
94
|
+
|
|
95
|
+
# ------------------------------------------------------------------ #
|
|
96
|
+
# 4. Read-modify-write the master gossfile
|
|
97
|
+
# ------------------------------------------------------------------ #
|
|
98
|
+
master_result = session.run(
|
|
99
|
+
f"cat {goss_master} 2>/dev/null || echo '{{}}'",
|
|
100
|
+
sudo=False,
|
|
101
|
+
warn=True,
|
|
102
|
+
)
|
|
103
|
+
try:
|
|
104
|
+
existing_master = yaml.safe_load(master_result.stdout or "{}") or {}
|
|
105
|
+
except yaml.YAMLError:
|
|
106
|
+
existing_master = {}
|
|
107
|
+
|
|
108
|
+
if "gossfile" not in existing_master or not isinstance(existing_master["gossfile"], dict):
|
|
109
|
+
existing_master["gossfile"] = {}
|
|
110
|
+
|
|
111
|
+
# Register this spec in the master; key is the remote path
|
|
112
|
+
existing_master["gossfile"][spec_remote_path] = {}
|
|
113
|
+
|
|
114
|
+
master_yaml = yaml.dump(existing_master, default_flow_style=False, sort_keys=True)
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
session.upload_content(master_yaml, goss_master, sudo=False)
|
|
118
|
+
except Exception as exc:
|
|
119
|
+
return _error(f"upload of master gossfile failed: {exc}")
|
|
120
|
+
|
|
121
|
+
# ------------------------------------------------------------------ #
|
|
122
|
+
# 5. Run goss validate (JSON output for machine-readable results)
|
|
123
|
+
# ------------------------------------------------------------------ #
|
|
124
|
+
# sudo=True because goss internally runs privileged commands (e.g.
|
|
125
|
+
# ``ufw status verbose``, ``wg show``). On a first run the session
|
|
126
|
+
# connects as root so sudo is a no-op; on an idempotent re-run the
|
|
127
|
+
# session connects as the admin user who has passwordless sudo.
|
|
128
|
+
goss_cmd = f"goss -g {goss_master} validate --format json --no-color"
|
|
129
|
+
goss_result = session.run(goss_cmd, sudo=True, warn=True)
|
|
130
|
+
|
|
131
|
+
raw_output = goss_result.stdout.strip()
|
|
132
|
+
|
|
133
|
+
# goss exits non-zero when checks fail, but still emits valid JSON.
|
|
134
|
+
# We parse regardless and use the summary to determine pass/fail.
|
|
135
|
+
try:
|
|
136
|
+
parsed = json.loads(raw_output)
|
|
137
|
+
except json.JSONDecodeError:
|
|
138
|
+
# goss may not be in PATH for non-interactive sessions on some distros
|
|
139
|
+
# (e.g. /usr/local/bin not in PATH). Try the full path.
|
|
140
|
+
goss_result2 = session.run(
|
|
141
|
+
f"/usr/local/bin/goss -g {goss_master} validate --format json --no-color",
|
|
142
|
+
sudo=True,
|
|
143
|
+
warn=True,
|
|
144
|
+
)
|
|
145
|
+
raw_output = goss_result2.stdout.strip()
|
|
146
|
+
try:
|
|
147
|
+
parsed = json.loads(raw_output)
|
|
148
|
+
except json.JSONDecodeError:
|
|
149
|
+
return _error(
|
|
150
|
+
f"goss produced non-JSON output. stderr: {goss_result2.stderr!r}. "
|
|
151
|
+
f"stdout: {raw_output[:300]!r}"
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
results = parsed.get("results", [])
|
|
155
|
+
summary = parsed.get("summary", {})
|
|
156
|
+
failed_count = summary.get("failed-count", 0)
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
"results": results,
|
|
160
|
+
"summary": summary,
|
|
161
|
+
"raw_output": raw_output,
|
|
162
|
+
"exit_ok": failed_count == 0,
|
|
163
|
+
"error": "",
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _error(msg: str) -> dict:
|
|
168
|
+
return {
|
|
169
|
+
"results": [],
|
|
170
|
+
"summary": {},
|
|
171
|
+
"raw_output": "",
|
|
172
|
+
"exit_ok": False,
|
|
173
|
+
"error": msg,
|
|
174
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Agent installation utilities.
|
|
2
|
+
|
|
3
|
+
Handles uploading and installing the loft-cli-agent binary on target servers,
|
|
4
|
+
and verifying agent availability.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import shutil
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from loft_cli_core.agent_paths import (
|
|
14
|
+
AGENT_BINARY_PATH,
|
|
15
|
+
AGENT_CONFIG_DIR,
|
|
16
|
+
AGENT_LOG_DIR,
|
|
17
|
+
AGENT_STATE_DIR,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from loft_cli.runtime.transport import Transport
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def detect_agent(transport: Transport) -> str | None:
|
|
25
|
+
"""Check if loft-cli-agent is installed on the target and return its version.
|
|
26
|
+
|
|
27
|
+
Returns the version string, or None if not installed.
|
|
28
|
+
"""
|
|
29
|
+
result = transport.run(f"{AGENT_BINARY_PATH} version", sudo=False, warn=True)
|
|
30
|
+
if result.ok and result.stdout.strip():
|
|
31
|
+
# Output format: "loft-cli-agent X.Y.Z"
|
|
32
|
+
parts = result.stdout.strip().split()
|
|
33
|
+
return parts[-1] if parts else result.stdout.strip()
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def install_agent_commands() -> list[str]:
|
|
38
|
+
"""Return the shell commands to create agent directories on the target.
|
|
39
|
+
|
|
40
|
+
The actual binary upload is handled by the planner as an ssh_upload step.
|
|
41
|
+
These commands are embedded in plan steps.
|
|
42
|
+
"""
|
|
43
|
+
dirs = [str(AGENT_CONFIG_DIR), str(AGENT_STATE_DIR), str(AGENT_LOG_DIR)]
|
|
44
|
+
return [f"mkdir -p {d}" for d in dirs]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def get_local_agent_binary() -> Path | None:
|
|
48
|
+
"""Locate the loft-cli-agent binary on the local system.
|
|
49
|
+
|
|
50
|
+
Checks: 1) alongside the current loft-cli binary, 2) on PATH.
|
|
51
|
+
Returns None if not found.
|
|
52
|
+
"""
|
|
53
|
+
# Check alongside the running loft-cli binary
|
|
54
|
+
import sys
|
|
55
|
+
|
|
56
|
+
current = Path(sys.executable).parent / "loft-cli-agent"
|
|
57
|
+
if current.exists():
|
|
58
|
+
return current
|
|
59
|
+
|
|
60
|
+
# Check PATH
|
|
61
|
+
found = shutil.which("loft-cli-agent")
|
|
62
|
+
if found:
|
|
63
|
+
return Path(found)
|
|
64
|
+
|
|
65
|
+
return None
|
|
File without changes
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""Docker Compose container health check.
|
|
2
|
+
|
|
3
|
+
Polls ``docker compose ps --format json`` to verify all containers in a
|
|
4
|
+
compose project are healthy/running. Used by the COMPOSE_HEALTH_CHECK
|
|
5
|
+
step kind.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
from loft_cli.checks.ssh import CheckResult
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def check_compose_health(
|
|
17
|
+
session,
|
|
18
|
+
directory: str,
|
|
19
|
+
compose_file: str,
|
|
20
|
+
project_name: str,
|
|
21
|
+
timeout: int = 120,
|
|
22
|
+
interval: int = 5,
|
|
23
|
+
) -> CheckResult:
|
|
24
|
+
"""Poll compose container health until all are healthy or timeout expires.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
session: SSH session for running remote commands.
|
|
28
|
+
directory: Remote project directory.
|
|
29
|
+
compose_file: Compose filename (relative to directory).
|
|
30
|
+
project_name: Docker Compose project name.
|
|
31
|
+
timeout: Maximum seconds to wait for healthy state.
|
|
32
|
+
interval: Seconds between polls.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
CheckResult with per-container status in details.
|
|
36
|
+
"""
|
|
37
|
+
deadline = time.monotonic() + timeout
|
|
38
|
+
last_containers: list[dict] = []
|
|
39
|
+
|
|
40
|
+
cmd = (
|
|
41
|
+
f"bash -c 'cd {directory} && "
|
|
42
|
+
f"docker compose -f {compose_file} -p {project_name} ps --format json'"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
while time.monotonic() < deadline:
|
|
46
|
+
result = session.run(cmd, sudo=True, warn=True)
|
|
47
|
+
if not result.ok:
|
|
48
|
+
time.sleep(interval)
|
|
49
|
+
continue
|
|
50
|
+
|
|
51
|
+
containers = _parse_compose_ps(result.stdout)
|
|
52
|
+
if not containers:
|
|
53
|
+
time.sleep(interval)
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
last_containers = containers
|
|
57
|
+
|
|
58
|
+
# Check if all containers are in an acceptable state
|
|
59
|
+
all_healthy = all(_is_container_healthy(c) for c in containers)
|
|
60
|
+
|
|
61
|
+
if all_healthy:
|
|
62
|
+
summary = {c["name"]: _container_status(c) for c in containers}
|
|
63
|
+
return CheckResult(
|
|
64
|
+
passed=True,
|
|
65
|
+
check_type="compose_health",
|
|
66
|
+
message=f"All {len(containers)} containers healthy/running",
|
|
67
|
+
details={"containers": summary},
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
time.sleep(interval)
|
|
71
|
+
|
|
72
|
+
# Timeout — report which containers are not healthy
|
|
73
|
+
unhealthy = {
|
|
74
|
+
c["name"]: _container_status(c) for c in last_containers if not _is_container_healthy(c)
|
|
75
|
+
}
|
|
76
|
+
summary = {c["name"]: _container_status(c) for c in last_containers}
|
|
77
|
+
return CheckResult(
|
|
78
|
+
passed=False,
|
|
79
|
+
check_type="compose_health",
|
|
80
|
+
message=(
|
|
81
|
+
f"Timeout after {timeout}s: {len(unhealthy)} container(s) not healthy: "
|
|
82
|
+
+ ", ".join(f"{n}={s}" for n, s in unhealthy.items())
|
|
83
|
+
),
|
|
84
|
+
details={"containers": summary, "unhealthy": unhealthy},
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _parse_compose_ps(stdout: str) -> list[dict]:
|
|
89
|
+
"""Parse docker compose ps --format json output.
|
|
90
|
+
|
|
91
|
+
Docker Compose v2 outputs one JSON object per line (NDJSON format).
|
|
92
|
+
Earlier versions may output a JSON array.
|
|
93
|
+
"""
|
|
94
|
+
containers: list[dict] = []
|
|
95
|
+
stdout = stdout.strip()
|
|
96
|
+
if not stdout:
|
|
97
|
+
return containers
|
|
98
|
+
|
|
99
|
+
# Try JSON array first
|
|
100
|
+
if stdout.startswith("["):
|
|
101
|
+
try:
|
|
102
|
+
return json.loads(stdout)
|
|
103
|
+
except json.JSONDecodeError:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
# NDJSON: one JSON object per line
|
|
107
|
+
for line in stdout.splitlines():
|
|
108
|
+
line = line.strip()
|
|
109
|
+
if not line:
|
|
110
|
+
continue
|
|
111
|
+
try:
|
|
112
|
+
obj = json.loads(line)
|
|
113
|
+
# Normalize field names (docker compose v2 uses different casing)
|
|
114
|
+
containers.append(
|
|
115
|
+
{
|
|
116
|
+
"name": obj.get("Name") or obj.get("name", "unknown"),
|
|
117
|
+
"state": obj.get("State") or obj.get("state", "unknown"),
|
|
118
|
+
"health": obj.get("Health") or obj.get("health", ""),
|
|
119
|
+
"service": obj.get("Service") or obj.get("service", ""),
|
|
120
|
+
}
|
|
121
|
+
)
|
|
122
|
+
except json.JSONDecodeError:
|
|
123
|
+
continue
|
|
124
|
+
|
|
125
|
+
return containers
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _container_status(container: dict) -> str:
|
|
129
|
+
"""Return a human-readable status string including health when present."""
|
|
130
|
+
state = container.get("state", "unknown")
|
|
131
|
+
health = container.get("health", "")
|
|
132
|
+
if health:
|
|
133
|
+
return f"{state} ({health})"
|
|
134
|
+
return state
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _is_container_healthy(container: dict) -> bool:
|
|
138
|
+
"""Determine if a container is in an acceptable running state."""
|
|
139
|
+
state = container.get("state", "").lower()
|
|
140
|
+
health = container.get("health", "").lower()
|
|
141
|
+
|
|
142
|
+
# If health check is defined, it must be "healthy"
|
|
143
|
+
if health and health != "healthy":
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
# State must be "running"
|
|
147
|
+
return state == "running"
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Docker container status check."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from loft_cli.checks.ssh import CheckResult
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def check_container_running(session, name: str) -> CheckResult:
|
|
9
|
+
"""Check that a Docker container is in running state."""
|
|
10
|
+
result = session.run(
|
|
11
|
+
f"docker inspect --format='{{{{.State.Running}}}}' {name}",
|
|
12
|
+
warn=True,
|
|
13
|
+
)
|
|
14
|
+
if result.ok and "true" in result.stdout.lower():
|
|
15
|
+
return CheckResult(
|
|
16
|
+
passed=True,
|
|
17
|
+
check_type="container_running",
|
|
18
|
+
message=f"Container '{name}' is running",
|
|
19
|
+
)
|
|
20
|
+
return CheckResult(
|
|
21
|
+
passed=False,
|
|
22
|
+
check_type="container_running",
|
|
23
|
+
message=f"Container '{name}' is not running: {result.stderr or result.stdout}",
|
|
24
|
+
)
|
loft_cli/checks/http.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""HTTP health check."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from loft_cli.checks.ssh import CheckResult
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def check_http(url: str, expect_status: int = 200, timeout: int = 10) -> CheckResult:
|
|
9
|
+
"""Check that an HTTP endpoint returns the expected status code."""
|
|
10
|
+
try:
|
|
11
|
+
import requests
|
|
12
|
+
|
|
13
|
+
response = requests.get(url, timeout=timeout)
|
|
14
|
+
if response.status_code == expect_status:
|
|
15
|
+
return CheckResult(
|
|
16
|
+
passed=True,
|
|
17
|
+
check_type="http",
|
|
18
|
+
message=f"HTTP {url} returned {response.status_code}",
|
|
19
|
+
)
|
|
20
|
+
return CheckResult(
|
|
21
|
+
passed=False,
|
|
22
|
+
check_type="http",
|
|
23
|
+
message=f"HTTP {url} returned {response.status_code}, expected {expect_status}",
|
|
24
|
+
)
|
|
25
|
+
except Exception as e:
|
|
26
|
+
return CheckResult(
|
|
27
|
+
passed=False,
|
|
28
|
+
check_type="http",
|
|
29
|
+
message=f"HTTP check failed for {url}: {e}",
|
|
30
|
+
)
|
loft_cli/checks/nginx.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Nginx readiness check."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from loft_cli.checks.ssh import CheckResult
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def check_nginx_ready(session) -> CheckResult:
|
|
9
|
+
"""Check that nginx configuration is valid and service is running."""
|
|
10
|
+
result = session.run("nginx -t 2>&1 && systemctl is-active nginx", warn=True)
|
|
11
|
+
if result.ok:
|
|
12
|
+
return CheckResult(
|
|
13
|
+
passed=True,
|
|
14
|
+
check_type="nginx_ready",
|
|
15
|
+
message="Nginx is running with valid configuration",
|
|
16
|
+
)
|
|
17
|
+
return CheckResult(
|
|
18
|
+
passed=False,
|
|
19
|
+
check_type="nginx_ready",
|
|
20
|
+
message=f"Nginx check failed: {result.stderr or result.stdout}",
|
|
21
|
+
)
|