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
loft_cli/logs/reader.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Read and display apply logs.
|
|
2
|
+
|
|
3
|
+
The default log directory is determined by ``get_local_paths().log_dir``
|
|
4
|
+
which respects the ``LOFT_CLI_STATE_DIR`` environment variable.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _default_log_dir() -> Path:
|
|
14
|
+
from loft_cli_core.registry.local_paths import get_local_paths
|
|
15
|
+
|
|
16
|
+
return get_local_paths().log_dir
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def list_logs(log_dir: Path | None = None) -> list[dict]:
|
|
20
|
+
"""List all apply logs with summary info, newest first."""
|
|
21
|
+
d = (log_dir or _default_log_dir()).expanduser()
|
|
22
|
+
if not d.exists():
|
|
23
|
+
return []
|
|
24
|
+
logs = []
|
|
25
|
+
for f in sorted(d.glob("*.json"), reverse=True):
|
|
26
|
+
try:
|
|
27
|
+
data = json.loads(f.read_text(encoding="utf-8"))
|
|
28
|
+
logs.append(
|
|
29
|
+
{
|
|
30
|
+
"file": str(f),
|
|
31
|
+
"run_id": data.get("run_id"),
|
|
32
|
+
"spec_name": data.get("spec_name"),
|
|
33
|
+
"target_host": data.get("target_host"),
|
|
34
|
+
"status": data.get("status"),
|
|
35
|
+
"started_at": data.get("started_at"),
|
|
36
|
+
}
|
|
37
|
+
)
|
|
38
|
+
except Exception:
|
|
39
|
+
pass
|
|
40
|
+
return logs
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def read_log(log_path: Path) -> dict:
|
|
44
|
+
"""Read a single apply log file."""
|
|
45
|
+
return json.loads(log_path.read_text(encoding="utf-8"))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def find_log(run_id: str, log_dir: Path | None = None) -> Path | None:
|
|
49
|
+
"""Find a log file by run_id prefix."""
|
|
50
|
+
d = (log_dir or _default_log_dir()).expanduser()
|
|
51
|
+
if not d.exists():
|
|
52
|
+
return None
|
|
53
|
+
for f in d.glob(f"{run_id}*.json"):
|
|
54
|
+
return f
|
|
55
|
+
return None
|
loft_cli/logs/writer.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Write structured apply logs.
|
|
2
|
+
|
|
3
|
+
The default log directory is determined by ``get_local_paths().log_dir``
|
|
4
|
+
which respects the ``LOFT_CLI_STATE_DIR`` environment variable.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from loft_cli.runtime.executor import ApplyResult
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _default_log_dir() -> Path:
|
|
18
|
+
from loft_cli_core.registry.local_paths import get_local_paths
|
|
19
|
+
|
|
20
|
+
return get_local_paths().log_dir
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def write_log(result: ApplyResult, log_dir: Path | None = None) -> Path:
|
|
24
|
+
"""Write JSON log of apply result. Returns path to the log file."""
|
|
25
|
+
d = (log_dir or _default_log_dir()).expanduser()
|
|
26
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
27
|
+
|
|
28
|
+
ts = result.started_at.replace(":", "-").replace("+", "Z")[:19]
|
|
29
|
+
spec_name = result.plan.spec_name.replace(" ", "_").lower()
|
|
30
|
+
log_path = d / f"{ts}_{spec_name}.json"
|
|
31
|
+
|
|
32
|
+
data = {
|
|
33
|
+
"run_id": ts,
|
|
34
|
+
"spec_name": result.plan.spec_name,
|
|
35
|
+
"spec_kind": result.plan.spec_kind,
|
|
36
|
+
"target_host": result.plan.target_host,
|
|
37
|
+
"spec_hash": result.plan.spec_hash,
|
|
38
|
+
"plan_hash": result.plan.plan_hash,
|
|
39
|
+
"status": result.status,
|
|
40
|
+
"started_at": result.started_at,
|
|
41
|
+
"finished_at": result.finished_at,
|
|
42
|
+
"aborted_at_step": result.aborted_at,
|
|
43
|
+
"steps": [
|
|
44
|
+
{
|
|
45
|
+
"index": r.step_index,
|
|
46
|
+
"id": r.step_id,
|
|
47
|
+
"scope": r.scope,
|
|
48
|
+
"status": r.status,
|
|
49
|
+
"duration_seconds": r.duration_seconds,
|
|
50
|
+
"output": r.output[:500] if r.output else "",
|
|
51
|
+
"error": r.error[:500] if r.error else "",
|
|
52
|
+
}
|
|
53
|
+
for r in result.step_results
|
|
54
|
+
],
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
log_path.write_text(json.dumps(data, indent=2), encoding="utf-8")
|
|
58
|
+
return log_path
|
|
File without changes
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""Agent-based transport implementation.
|
|
2
|
+
|
|
3
|
+
Turns the client into a thin transporter: uploads the plan to the target
|
|
4
|
+
server, invokes the loft-cli-agent, and retrieves the results. Falls back
|
|
5
|
+
to direct SSH for bootstrap steps before the agent is installed.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from loft_cli.runtime.fabric_transport import FabricTransport
|
|
14
|
+
from loft_cli.runtime.ssh import CommandResult
|
|
15
|
+
from loft_cli_core.agent_models import AgentApplyResult
|
|
16
|
+
from loft_cli_core.agent_paths import AGENT_BINARY_PATH, AGENT_DESIRED_DIR
|
|
17
|
+
from loft_cli_core.plan.models import Plan
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class AgentTransport:
|
|
24
|
+
"""Transport that delegates execution to the server-side agent.
|
|
25
|
+
|
|
26
|
+
Uses an underlying FabricTransport for SSH communication (uploading
|
|
27
|
+
plan, invoking agent, downloading results). The agent executes the
|
|
28
|
+
plan locally on the managed server.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
host: str,
|
|
34
|
+
user: str,
|
|
35
|
+
port: int = 22,
|
|
36
|
+
password: str | None = None,
|
|
37
|
+
key_path: str | None = None,
|
|
38
|
+
connect_timeout: int = 10,
|
|
39
|
+
) -> None:
|
|
40
|
+
self._ssh = FabricTransport(
|
|
41
|
+
host=host,
|
|
42
|
+
user=user,
|
|
43
|
+
port=port,
|
|
44
|
+
password=password,
|
|
45
|
+
key_path=key_path,
|
|
46
|
+
connect_timeout=connect_timeout,
|
|
47
|
+
)
|
|
48
|
+
self.host = host
|
|
49
|
+
self.user = user
|
|
50
|
+
self.port = port
|
|
51
|
+
|
|
52
|
+
def apply_plan(self, plan: Plan) -> AgentApplyResult:
|
|
53
|
+
"""Upload plan to target and invoke the agent to execute it.
|
|
54
|
+
|
|
55
|
+
Returns the agent's apply result.
|
|
56
|
+
"""
|
|
57
|
+
# 1. Ensure desired state directory exists
|
|
58
|
+
self._ssh.run(f"mkdir -p {AGENT_DESIRED_DIR}", sudo=True, warn=True)
|
|
59
|
+
|
|
60
|
+
# 2. Upload plan as JSON
|
|
61
|
+
plan_json = plan.model_dump_json(indent=2)
|
|
62
|
+
plan_remote_path = f"{AGENT_DESIRED_DIR}/plan.json"
|
|
63
|
+
self._ssh.upload_content(plan_json, plan_remote_path, sudo=True)
|
|
64
|
+
|
|
65
|
+
# 3. Invoke the agent
|
|
66
|
+
result = self._ssh.run(
|
|
67
|
+
f"{AGENT_BINARY_PATH} apply {plan_remote_path}",
|
|
68
|
+
sudo=True,
|
|
69
|
+
warn=True,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# 4. Retrieve the result file
|
|
73
|
+
result_content = self._ssh.download("/var/lib/loft-cli/last-result.json")
|
|
74
|
+
|
|
75
|
+
try:
|
|
76
|
+
return AgentApplyResult.model_validate_json(result_content)
|
|
77
|
+
except Exception:
|
|
78
|
+
# If result file parsing fails, construct from the SSH output
|
|
79
|
+
return AgentApplyResult(
|
|
80
|
+
plan_hash=plan.plan_hash,
|
|
81
|
+
spec_hash=plan.spec_hash,
|
|
82
|
+
step_results=[],
|
|
83
|
+
status="failed" if not result.ok else "success",
|
|
84
|
+
started_at="",
|
|
85
|
+
finished_at="",
|
|
86
|
+
unchanged_count=0,
|
|
87
|
+
applied_count=0,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# Delegate standard transport methods to the inner SSH transport
|
|
91
|
+
# (used during bootstrap before agent is installed)
|
|
92
|
+
|
|
93
|
+
def run(
|
|
94
|
+
self,
|
|
95
|
+
cmd: str,
|
|
96
|
+
sudo: bool = False,
|
|
97
|
+
warn: bool = True,
|
|
98
|
+
hide: bool = True,
|
|
99
|
+
) -> CommandResult:
|
|
100
|
+
return self._ssh.run(cmd, sudo=sudo, warn=warn, hide=hide)
|
|
101
|
+
|
|
102
|
+
def upload(self, local_path: Path | str, remote_path: str) -> None:
|
|
103
|
+
self._ssh.upload(local_path, remote_path)
|
|
104
|
+
|
|
105
|
+
def upload_content(self, content: str, remote_path: str, sudo: bool = False) -> CommandResult:
|
|
106
|
+
return self._ssh.upload_content(content, remote_path, sudo=sudo)
|
|
107
|
+
|
|
108
|
+
def download(self, remote_path: str) -> str:
|
|
109
|
+
return self._ssh.download(remote_path)
|
|
110
|
+
|
|
111
|
+
def test_connection(self) -> bool:
|
|
112
|
+
return self._ssh.test_connection()
|
|
113
|
+
|
|
114
|
+
def close(self) -> None:
|
|
115
|
+
self._ssh.close()
|