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,68 @@
|
|
|
1
|
+
"""Fabric-based transport implementation.
|
|
2
|
+
|
|
3
|
+
Wraps the existing SSHSession to satisfy the Transport protocol.
|
|
4
|
+
This is the default transport used by the client when no agent is available.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from loft_cli.runtime.ssh import CommandResult, SSHSession
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class FabricTransport:
|
|
15
|
+
"""Transport implementation backed by Fabric SSH.
|
|
16
|
+
|
|
17
|
+
Delegates all operations to the underlying SSHSession. Implements the
|
|
18
|
+
Transport protocol via structural typing (no explicit inheritance needed).
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
host: str,
|
|
24
|
+
user: str,
|
|
25
|
+
port: int = 22,
|
|
26
|
+
password: str | None = None,
|
|
27
|
+
key_path: str | None = None,
|
|
28
|
+
connect_timeout: int = 10,
|
|
29
|
+
) -> None:
|
|
30
|
+
self._session = SSHSession(
|
|
31
|
+
host=host,
|
|
32
|
+
user=user,
|
|
33
|
+
port=port,
|
|
34
|
+
password=password,
|
|
35
|
+
key_path=key_path,
|
|
36
|
+
connect_timeout=connect_timeout,
|
|
37
|
+
)
|
|
38
|
+
self.host = host
|
|
39
|
+
self.user = user
|
|
40
|
+
self.port = port
|
|
41
|
+
|
|
42
|
+
def run(
|
|
43
|
+
self,
|
|
44
|
+
cmd: str,
|
|
45
|
+
sudo: bool = False,
|
|
46
|
+
warn: bool = True,
|
|
47
|
+
hide: bool = True,
|
|
48
|
+
) -> CommandResult:
|
|
49
|
+
return self._session.run(cmd, sudo=sudo, warn=warn, hide=hide)
|
|
50
|
+
|
|
51
|
+
def upload(self, local_path: Path | str, remote_path: str) -> None:
|
|
52
|
+
self._session.upload(local_path, remote_path)
|
|
53
|
+
|
|
54
|
+
def upload_content(self, content: str, remote_path: str, sudo: bool = False) -> CommandResult:
|
|
55
|
+
return self._session.upload_content(content, remote_path, sudo=sudo)
|
|
56
|
+
|
|
57
|
+
def download(self, remote_path: str) -> str:
|
|
58
|
+
"""Download a remote file's content by reading it via SSH."""
|
|
59
|
+
result = self.run(f"cat {remote_path}", sudo=False, warn=True)
|
|
60
|
+
if not result.ok:
|
|
61
|
+
raise RuntimeError(f"Failed to download {remote_path}: {result.stderr}")
|
|
62
|
+
return result.stdout
|
|
63
|
+
|
|
64
|
+
def test_connection(self) -> bool:
|
|
65
|
+
return self._session.test_connection()
|
|
66
|
+
|
|
67
|
+
def close(self) -> None:
|
|
68
|
+
self._session.close()
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Secret rotation: find secret references in specs and rotate them."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import secrets
|
|
7
|
+
import string
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class SecretRef:
|
|
13
|
+
"""A reference to a secret (environment variable) found in a spec."""
|
|
14
|
+
|
|
15
|
+
env_var: str
|
|
16
|
+
kind: str # spec kind where found
|
|
17
|
+
field_path: str # e.g. "users[0].password_env"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class RotationResult:
|
|
22
|
+
"""Result of a secret rotation operation."""
|
|
23
|
+
|
|
24
|
+
secret_name: str
|
|
25
|
+
new_value: str
|
|
26
|
+
refs_found: list[SecretRef] = field(default_factory=list)
|
|
27
|
+
applied: bool = False
|
|
28
|
+
error: str | None = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def find_secret_refs(spec) -> list[SecretRef]:
|
|
32
|
+
"""Scan a parsed spec for password_env references."""
|
|
33
|
+
refs: list[SecretRef] = []
|
|
34
|
+
kind = spec.kind
|
|
35
|
+
|
|
36
|
+
# service spec: postgres.roles[].password_env
|
|
37
|
+
if hasattr(spec, "postgres") and spec.postgres:
|
|
38
|
+
pg = spec.postgres
|
|
39
|
+
if hasattr(pg, "roles"):
|
|
40
|
+
for i, role in enumerate(pg.roles):
|
|
41
|
+
if role.password_env:
|
|
42
|
+
refs.append(
|
|
43
|
+
SecretRef(
|
|
44
|
+
env_var=role.password_env,
|
|
45
|
+
kind=kind,
|
|
46
|
+
field_path=f"postgres.roles[{i}].password_env",
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# postgres_ensure spec: users[].password_env
|
|
51
|
+
if hasattr(spec, "users"):
|
|
52
|
+
for i, user in enumerate(spec.users):
|
|
53
|
+
if hasattr(user, "password_env") and user.password_env:
|
|
54
|
+
refs.append(
|
|
55
|
+
SecretRef(
|
|
56
|
+
env_var=user.password_env,
|
|
57
|
+
kind=kind,
|
|
58
|
+
field_path=f"users[{i}].password_env",
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
return refs
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def generate_password(length: int = 32) -> str:
|
|
66
|
+
"""Generate a cryptographically secure random password."""
|
|
67
|
+
alphabet = string.ascii_letters + string.digits + "!@#$%^&*"
|
|
68
|
+
return "".join(secrets.choice(alphabet) for _ in range(length))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def rotate_secret(
|
|
72
|
+
spec,
|
|
73
|
+
secret_name: str,
|
|
74
|
+
new_value: str | None = None,
|
|
75
|
+
) -> RotationResult:
|
|
76
|
+
"""Rotate a secret by name.
|
|
77
|
+
|
|
78
|
+
Finds all references to the given env var in the spec,
|
|
79
|
+
sets the env var to the new value, and returns a result
|
|
80
|
+
indicating what was found.
|
|
81
|
+
"""
|
|
82
|
+
refs = find_secret_refs(spec)
|
|
83
|
+
matching = [r for r in refs if r.env_var == secret_name]
|
|
84
|
+
|
|
85
|
+
if not matching:
|
|
86
|
+
return RotationResult(
|
|
87
|
+
secret_name=secret_name,
|
|
88
|
+
new_value="",
|
|
89
|
+
error=f"No references to '{secret_name}' found in spec",
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
value = new_value or generate_password()
|
|
93
|
+
os.environ[secret_name] = value
|
|
94
|
+
|
|
95
|
+
return RotationResult(
|
|
96
|
+
secret_name=secret_name,
|
|
97
|
+
new_value=value,
|
|
98
|
+
refs_found=matching,
|
|
99
|
+
)
|
loft_cli/runtime/ssh.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""Fabric SSH session wrapper.
|
|
2
|
+
|
|
3
|
+
Adapted from vm_wizard's run_fabric_process() pattern.
|
|
4
|
+
Fabric is transport only — business logic lives in plan/executor layers.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import contextlib
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import paramiko
|
|
13
|
+
from fabric import Connection
|
|
14
|
+
from pydantic import BaseModel
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class _AutoAddConnection(Connection):
|
|
18
|
+
"""Fabric Connection that auto-accepts unknown host keys.
|
|
19
|
+
|
|
20
|
+
Provisioning tools connect to fresh VMs whose keys are not yet in
|
|
21
|
+
~/.ssh/known_hosts. Paramiko's default RejectPolicy would abort the
|
|
22
|
+
connection; AutoAddPolicy accepts the key and adds it to known_hosts.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def create_client(self) -> paramiko.SSHClient: # type: ignore[override]
|
|
26
|
+
client = super().create_client() # type: ignore[misc]
|
|
27
|
+
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
28
|
+
return client
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class CommandResult(BaseModel):
|
|
32
|
+
ok: bool
|
|
33
|
+
stdout: str
|
|
34
|
+
stderr: str
|
|
35
|
+
return_code: int
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class SSHSession:
|
|
39
|
+
"""Wraps a Fabric Connection with a clean API for loft-cli."""
|
|
40
|
+
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
host: str,
|
|
44
|
+
user: str,
|
|
45
|
+
port: int = 22,
|
|
46
|
+
password: str | None = None,
|
|
47
|
+
key_path: str | None = None,
|
|
48
|
+
connect_timeout: int = 10,
|
|
49
|
+
) -> None:
|
|
50
|
+
connect_kwargs: dict = {}
|
|
51
|
+
if password:
|
|
52
|
+
connect_kwargs["password"] = password
|
|
53
|
+
# When using password auth without a key, tell Paramiko not to
|
|
54
|
+
# try key-based auth. This prevents "allowed types: ['publickey']"
|
|
55
|
+
# errors when the server has both auth methods available.
|
|
56
|
+
if not key_path:
|
|
57
|
+
connect_kwargs["look_for_keys"] = False
|
|
58
|
+
connect_kwargs["allow_agent"] = False
|
|
59
|
+
if key_path:
|
|
60
|
+
connect_kwargs["key_filename"] = str(Path(key_path).expanduser())
|
|
61
|
+
|
|
62
|
+
self._conn = _AutoAddConnection(
|
|
63
|
+
host=host,
|
|
64
|
+
user=user,
|
|
65
|
+
port=port,
|
|
66
|
+
connect_kwargs=connect_kwargs,
|
|
67
|
+
connect_timeout=connect_timeout,
|
|
68
|
+
)
|
|
69
|
+
self.host = host
|
|
70
|
+
self.user = user
|
|
71
|
+
self.port = port
|
|
72
|
+
|
|
73
|
+
def run(
|
|
74
|
+
self,
|
|
75
|
+
cmd: str,
|
|
76
|
+
sudo: bool = False,
|
|
77
|
+
warn: bool = True,
|
|
78
|
+
hide: bool = True,
|
|
79
|
+
) -> CommandResult:
|
|
80
|
+
"""Execute a command on the remote host."""
|
|
81
|
+
try:
|
|
82
|
+
if sudo:
|
|
83
|
+
result = self._conn.sudo(cmd, warn=warn, hide=hide)
|
|
84
|
+
else:
|
|
85
|
+
result = self._conn.run(cmd, warn=warn, hide=hide)
|
|
86
|
+
|
|
87
|
+
return CommandResult(
|
|
88
|
+
ok=result.ok,
|
|
89
|
+
stdout=result.stdout or "",
|
|
90
|
+
stderr=result.stderr or "",
|
|
91
|
+
return_code=result.return_code,
|
|
92
|
+
)
|
|
93
|
+
except Exception as e:
|
|
94
|
+
return CommandResult(
|
|
95
|
+
ok=False,
|
|
96
|
+
stdout="",
|
|
97
|
+
stderr=str(e),
|
|
98
|
+
return_code=1,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
def upload(self, local_path: Path | str, remote_path: str) -> None:
|
|
102
|
+
"""Upload a local file to the remote host."""
|
|
103
|
+
self._conn.put(str(local_path), remote=remote_path)
|
|
104
|
+
|
|
105
|
+
def upload_content(self, content: str, remote_path: str, sudo: bool = False) -> CommandResult:
|
|
106
|
+
"""Write string content to a remote file via /tmp.
|
|
107
|
+
|
|
108
|
+
Returns a CommandResult indicating success or failure of the upload.
|
|
109
|
+
"""
|
|
110
|
+
import os
|
|
111
|
+
import tempfile
|
|
112
|
+
|
|
113
|
+
with tempfile.NamedTemporaryFile(mode="w", suffix=".tmp", delete=False) as f:
|
|
114
|
+
f.write(content)
|
|
115
|
+
tmp_path = f.name
|
|
116
|
+
|
|
117
|
+
try:
|
|
118
|
+
tmp_remote = f"/tmp/{Path(remote_path).name}.loft_cli_tmp"
|
|
119
|
+
self._conn.put(tmp_path, remote=tmp_remote)
|
|
120
|
+
if sudo:
|
|
121
|
+
mv_result = self.run(f"mv {tmp_remote} {remote_path}", sudo=True)
|
|
122
|
+
if not mv_result.ok:
|
|
123
|
+
return mv_result
|
|
124
|
+
chown_result = self.run(f"chown root:root {remote_path}", sudo=True)
|
|
125
|
+
if not chown_result.ok:
|
|
126
|
+
return chown_result
|
|
127
|
+
chmod_result = self.run(f"chmod 600 {remote_path}", sudo=True)
|
|
128
|
+
if not chmod_result.ok:
|
|
129
|
+
return chmod_result
|
|
130
|
+
else:
|
|
131
|
+
mv_result = self.run(f"mv {tmp_remote} {remote_path}")
|
|
132
|
+
if not mv_result.ok:
|
|
133
|
+
return mv_result
|
|
134
|
+
finally:
|
|
135
|
+
os.unlink(tmp_path)
|
|
136
|
+
|
|
137
|
+
return CommandResult(ok=True, stdout="", stderr="", return_code=0)
|
|
138
|
+
|
|
139
|
+
def test_connection(self) -> bool:
|
|
140
|
+
"""Return True if SSH connection succeeds."""
|
|
141
|
+
result = self.run("echo ok", warn=True)
|
|
142
|
+
return result.ok and "ok" in result.stdout
|
|
143
|
+
|
|
144
|
+
def close(self) -> None:
|
|
145
|
+
"""Close the Fabric connection."""
|
|
146
|
+
with contextlib.suppress(Exception):
|
|
147
|
+
self._conn.close()
|
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Shell script generators for kind: backup_job steps.
|
|
2
|
+
|
|
3
|
+
Generates backup scripts for postgres_dump and directory backup types.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def render_backup_script(
|
|
10
|
+
*,
|
|
11
|
+
name: str,
|
|
12
|
+
source_type: str,
|
|
13
|
+
destination_path: str,
|
|
14
|
+
retention_count: int,
|
|
15
|
+
# postgres_dump fields
|
|
16
|
+
database: str | None = None,
|
|
17
|
+
pg_host: str = "localhost",
|
|
18
|
+
pg_port: int = 5432,
|
|
19
|
+
pg_user: str = "postgres",
|
|
20
|
+
docker_exec: str | None = None,
|
|
21
|
+
# directory fields
|
|
22
|
+
source_path: str | None = None,
|
|
23
|
+
) -> str:
|
|
24
|
+
"""Render a backup shell script."""
|
|
25
|
+
lines: list[str] = [
|
|
26
|
+
"#!/bin/bash",
|
|
27
|
+
"set -euo pipefail",
|
|
28
|
+
"",
|
|
29
|
+
f"# Backup script for: {name}",
|
|
30
|
+
"# Generated by loft-cli -- do not edit manually",
|
|
31
|
+
"",
|
|
32
|
+
f'BACKUP_DIR="{destination_path}"',
|
|
33
|
+
'TIMESTAMP="$(date +%Y%m%d-%H%M%S)"',
|
|
34
|
+
"",
|
|
35
|
+
"# Ensure backup directory exists",
|
|
36
|
+
'mkdir -p "$BACKUP_DIR"',
|
|
37
|
+
"",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
if source_type == "postgres_dump":
|
|
41
|
+
backup_file = f'"$BACKUP_DIR/{name}-$TIMESTAMP.sql.gz"'
|
|
42
|
+
if docker_exec:
|
|
43
|
+
lines.append("# Dump PostgreSQL database via docker exec")
|
|
44
|
+
lines.append(
|
|
45
|
+
f"docker exec {docker_exec} pg_dump -U {pg_user} {database} "
|
|
46
|
+
f"| gzip > {backup_file}"
|
|
47
|
+
)
|
|
48
|
+
else:
|
|
49
|
+
lines.append("# Dump PostgreSQL database")
|
|
50
|
+
lines.append(
|
|
51
|
+
f"pg_dump -h {pg_host} -p {pg_port} -U {pg_user} {database} "
|
|
52
|
+
f"| gzip > {backup_file}"
|
|
53
|
+
)
|
|
54
|
+
elif source_type == "directory":
|
|
55
|
+
backup_file = f'"$BACKUP_DIR/{name}-$TIMESTAMP.tar.gz"'
|
|
56
|
+
lines.append("# Archive directory")
|
|
57
|
+
lines.append(
|
|
58
|
+
f'tar czf {backup_file} -C "$(dirname {source_path})" "$(basename {source_path})"'
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
lines.extend(
|
|
62
|
+
[
|
|
63
|
+
"",
|
|
64
|
+
f"# Retention: keep {retention_count} most recent backups",
|
|
65
|
+
'cd "$BACKUP_DIR"',
|
|
66
|
+
f"ls -1t {name}-*.* 2>/dev/null | tail -n +{retention_count + 1} | xargs -r rm -f",
|
|
67
|
+
"",
|
|
68
|
+
f'echo "Backup completed: {name} at $TIMESTAMP"',
|
|
69
|
+
]
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
return "\n".join(lines) + "\n"
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"""Shell command templates for bootstrap steps.
|
|
2
|
+
|
|
3
|
+
Adapted from vm_wizard/fab_infra/tasks/user/bootstrap_admin_user/bootstrap_admin_user.py.
|
|
4
|
+
All functions return shell command strings consumed by the executor.
|
|
5
|
+
|
|
6
|
+
IMPORTANT — Fabric sudo() compatibility:
|
|
7
|
+
Fabric's ``sudo()`` runs ``sudo -S -p '' <raw command>``. Shell
|
|
8
|
+
metacharacters that create a new command context (``&&``, ``||``, ``|``,
|
|
9
|
+
``>``, ``>>``) break out of the sudo elevation — only the first simple
|
|
10
|
+
command runs as root.
|
|
11
|
+
|
|
12
|
+
To work around this, every function that needs shell operators wraps the
|
|
13
|
+
entire command string in ``bash -c '...'`` so that Fabric elevates the
|
|
14
|
+
outer ``bash`` process and all inner commands inherit root privileges.
|
|
15
|
+
|
|
16
|
+
Functions that are *logically separate operations* (e.g. firewall rules)
|
|
17
|
+
are split into individual functions so the planner can emit one step per
|
|
18
|
+
operation — no shell chaining required.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def apt_update() -> str:
|
|
25
|
+
return "bash -c 'apt-get update -y 2>&1 | tail -3'"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def apt_upgrade() -> str:
|
|
29
|
+
return "bash -c 'DEBIAN_FRONTEND=noninteractive apt-get upgrade -y 2>&1 | tail -5'"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def install_packages(packages: list[str]) -> str:
|
|
33
|
+
pkg_list = " ".join(packages)
|
|
34
|
+
return f"DEBIAN_FRONTEND=noninteractive apt-get install -y {pkg_list}"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def delete_non_system_users(admin_username: str) -> str:
|
|
38
|
+
"""Remove non-system users (UID 1000-65533) except the admin user.
|
|
39
|
+
|
|
40
|
+
Awk pattern from vm_wizard bootstrap_admin_user.py lines 111-114.
|
|
41
|
+
Uses double-quoted ``bash -c`` with ``\\$`` to protect awk field
|
|
42
|
+
references from shell expansion.
|
|
43
|
+
"""
|
|
44
|
+
return (
|
|
45
|
+
f'bash -c "awk -F: '
|
|
46
|
+
f'\'\\$3 >= 1000 && \\$3 < 65534 && \\$1 != \\"{admin_username}\\" '
|
|
47
|
+
f"{{print \\$1}}' /etc/passwd "
|
|
48
|
+
f'| while read u; do userdel -r \\$u 2>&1; done"'
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def create_admin_user(username: str, groups: list[str]) -> str:
|
|
53
|
+
"""Create admin user idempotently and add to specified groups."""
|
|
54
|
+
group_str = ",".join(groups) if groups else "sudo"
|
|
55
|
+
return (
|
|
56
|
+
f"bash -c '"
|
|
57
|
+
f"getent group {username} >/dev/null 2>&1 || addgroup {username} && "
|
|
58
|
+
f'id {username} >/dev/null 2>&1 || adduser --disabled-password --gecos "" --ingroup {username} {username} && '
|
|
59
|
+
f"usermod -aG {group_str} {username}"
|
|
60
|
+
f"'"
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def set_admin_password(username: str, password: str) -> str:
|
|
65
|
+
# Use single-quoted password inside double-quoted bash -c to prevent
|
|
66
|
+
# shell expansion of special characters in the password.
|
|
67
|
+
escaped_pw = password.replace("'", "'\\''")
|
|
68
|
+
return f"bash -c \"echo '{username}:{escaped_pw}' | chpasswd\""
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def nopasswd_sudoers(username: str) -> str:
|
|
72
|
+
return f"bash -c 'echo \"{username} ALL=(ALL) NOPASSWD:ALL\" > /etc/sudoers.d/{username}'"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def secure_sudoers(username: str) -> str:
|
|
76
|
+
return f"chmod 440 /etc/sudoers.d/{username}"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def install_authorized_keys(username: str, pubkey_content: str) -> str:
|
|
80
|
+
"""Install SSH authorized_keys for admin user.
|
|
81
|
+
|
|
82
|
+
The entire chain runs inside ``bash -c "..."`` so Fabric's sudo elevates
|
|
83
|
+
everything. We use double quotes for the outer wrapper so single quotes
|
|
84
|
+
in the inner ``printf`` don't conflict.
|
|
85
|
+
"""
|
|
86
|
+
inner = (
|
|
87
|
+
f"mkdir -p /home/{username}/.ssh && "
|
|
88
|
+
f"printf '%s\\n' '{pubkey_content}' >> /home/{username}/.ssh/authorized_keys && "
|
|
89
|
+
f"chmod 700 /home/{username}/.ssh && "
|
|
90
|
+
f"chmod 600 /home/{username}/.ssh/authorized_keys && "
|
|
91
|
+
f"chown -R {username}:{username} /home/{username}/.ssh"
|
|
92
|
+
)
|
|
93
|
+
return f'bash -c "{inner}"'
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def enable_pubkey_auth() -> str:
|
|
97
|
+
"""Ensure PubkeyAuthentication yes is active so admin key login can succeed.
|
|
98
|
+
|
|
99
|
+
Some distro images ship with PubkeyAuthentication no. We must enable it and
|
|
100
|
+
reload sshd before running the admin-login gate, otherwise key auth never works.
|
|
101
|
+
The grep+sed pattern handles commented, uncommented, or missing lines.
|
|
102
|
+
|
|
103
|
+
On socket-activated systems (Ubuntu 24.04+), ``systemctl reload ssh`` may not
|
|
104
|
+
have a running service to signal, so we also try ``restart ssh.socket`` which
|
|
105
|
+
causes new connections to pick up the updated config.
|
|
106
|
+
|
|
107
|
+
Uses double quotes for the outer ``bash -c`` to avoid conflicts with
|
|
108
|
+
the single-quoted sed expressions inside.
|
|
109
|
+
"""
|
|
110
|
+
return (
|
|
111
|
+
'bash -c "'
|
|
112
|
+
"grep -q '^#\\\\?PubkeyAuthentication' /etc/ssh/sshd_config "
|
|
113
|
+
"&& sed -i 's/^#\\\\?PubkeyAuthentication.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config "
|
|
114
|
+
"|| echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_config; "
|
|
115
|
+
"if systemctl is-active ssh.socket >/dev/null 2>&1; then "
|
|
116
|
+
"systemctl daemon-reload && systemctl restart ssh.socket; "
|
|
117
|
+
"else "
|
|
118
|
+
"systemctl reload ssh || systemctl reload sshd; "
|
|
119
|
+
'fi"'
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def write_sshd_config_candidate(port: int) -> str:
|
|
124
|
+
"""Configure SSH port. Root login and password auth are deferred until gate passes.
|
|
125
|
+
|
|
126
|
+
Uses grep+sed+append pattern: if a ``Port`` line exists (commented or not)
|
|
127
|
+
it is replaced; otherwise ``Port <port>`` is appended. This handles
|
|
128
|
+
Ubuntu 24.04 where the default ``sshd_config`` may not contain a ``Port``
|
|
129
|
+
line at all (the default is implicit or set via ``sshd_config.d/``).
|
|
130
|
+
"""
|
|
131
|
+
return (
|
|
132
|
+
f"bash -c '"
|
|
133
|
+
f"cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak && "
|
|
134
|
+
f'(grep -q "^#\\?Port " /etc/ssh/sshd_config '
|
|
135
|
+
f'&& sed -i "s/^#\\?Port .*/Port {port}/" /etc/ssh/sshd_config '
|
|
136
|
+
f'|| echo "Port {port}" >> /etc/ssh/sshd_config)'
|
|
137
|
+
f"'"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def validate_sshd_config() -> str:
|
|
142
|
+
return "sshd -t"
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def reload_sshd() -> str:
|
|
146
|
+
"""Reload sshd, handling both socket-activated and traditional modes.
|
|
147
|
+
|
|
148
|
+
Ubuntu 24.04+ uses systemd socket activation (``ssh.socket``) by default.
|
|
149
|
+
When socket activation is active, the ``Port`` directive in ``sshd_config``
|
|
150
|
+
is ignored for listening — the socket unit controls that. Changing the port
|
|
151
|
+
requires ``systemctl daemon-reload`` (which triggers ``sshd-socket-generator``
|
|
152
|
+
to regenerate ``ListenStream`` from ``sshd_config``) followed by
|
|
153
|
+
``systemctl restart ssh.socket`` to re-bind.
|
|
154
|
+
|
|
155
|
+
On traditional systems (Ubuntu 22.04, Debian, etc.) a simple
|
|
156
|
+
``systemctl reload ssh`` is sufficient.
|
|
157
|
+
"""
|
|
158
|
+
return (
|
|
159
|
+
"bash -c '"
|
|
160
|
+
"if systemctl is-active ssh.socket >/dev/null 2>&1; then "
|
|
161
|
+
"systemctl daemon-reload && systemctl restart ssh.socket; "
|
|
162
|
+
"else "
|
|
163
|
+
"systemctl reload ssh || systemctl reload sshd; "
|
|
164
|
+
"fi'"
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def disable_root_login() -> str:
|
|
169
|
+
"""Disable root SSH login. MUST only run after gate (verify_admin_login_on_new_port)."""
|
|
170
|
+
return r"sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config"
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def disable_password_auth() -> str:
|
|
174
|
+
"""Disable password auth. MUST only run after gate (verify_admin_login_on_new_port)."""
|
|
175
|
+
return (
|
|
176
|
+
r"sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config"
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def open_firewall_port(port: int) -> str:
|
|
181
|
+
return f"ufw allow {port}/tcp"
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# ── Firewall finalization (split into individual steps) ──────────────
|
|
185
|
+
# Previously a single ``&&`` chain; now three separate functions so the
|
|
186
|
+
# planner emits one step per operation — no shell chaining required.
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def ufw_default_deny_incoming() -> str:
|
|
190
|
+
return "ufw default deny incoming"
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def ufw_default_allow_outgoing() -> str:
|
|
194
|
+
return "ufw default allow outgoing"
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def ufw_force_enable() -> str:
|
|
198
|
+
return "ufw --force enable"
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# ── WireGuard SSH restriction (split into individual steps) ──────────
|
|
202
|
+
# Previously a single ``&&`` chain; now two separate functions.
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def allow_ssh_on_wireguard(
|
|
206
|
+
ssh_port: int,
|
|
207
|
+
wg_interface: str,
|
|
208
|
+
peer_ip: str | None = None,
|
|
209
|
+
) -> str:
|
|
210
|
+
"""Add a WireGuard-restricted SSH allow rule.
|
|
211
|
+
|
|
212
|
+
When peer_ip is set (firewall.registered_peers_only=true):
|
|
213
|
+
ufw allow in on {wg_interface} from {peer_ip} to any port {ssh_port} proto tcp
|
|
214
|
+
When peer_ip is None (registered_peers_only=false, default):
|
|
215
|
+
ufw allow in on {wg_interface} to any port {ssh_port} proto tcp
|
|
216
|
+
"""
|
|
217
|
+
from_clause = f"from {peer_ip} " if peer_ip else ""
|
|
218
|
+
return f"ufw allow in on {wg_interface} {from_clause}to any port {ssh_port} proto tcp"
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def delete_open_ssh_rule(ssh_port: int) -> str:
|
|
222
|
+
"""Remove the temporary open-to-all SSH rule.
|
|
223
|
+
|
|
224
|
+
MUST be the last remote SSH step — after this executes, direct SSH to
|
|
225
|
+
spec.host.address stops working. All subsequent steps must be LOCAL.
|
|
226
|
+
"""
|
|
227
|
+
return f"ufw delete allow {ssh_port}/tcp"
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Shell command builders for kind: compose_project steps.
|
|
2
|
+
|
|
3
|
+
IMPORTANT — Fabric sudo() compatibility:
|
|
4
|
+
See bootstrap.py module docstring for details. Functions here follow
|
|
5
|
+
the same pattern: no shell operators in command strings that will be
|
|
6
|
+
executed via Fabric's sudo().
|
|
7
|
+
|
|
8
|
+
Commands that require shell operators (e.g. cd + docker compose) are
|
|
9
|
+
wrapped in bash -c '...' so Fabric elevates the outer bash process.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def mkdir_with_permissions(path: str, mode: str, owner: str, group: str) -> str:
|
|
16
|
+
"""Create a directory with specific mode and ownership.
|
|
17
|
+
|
|
18
|
+
Uses bash -c to chain mkdir + chmod + chown under a single sudo elevation.
|
|
19
|
+
"""
|
|
20
|
+
return f"bash -c 'mkdir -p {path} && chmod {mode} {path} && chown {owner}:{group} {path}'"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def compose_config(directory: str, compose_file: str, project_name: str) -> str:
|
|
24
|
+
"""Validate compose file syntax (docker compose config)."""
|
|
25
|
+
return (
|
|
26
|
+
f"bash -c 'cd {directory} && "
|
|
27
|
+
f"docker compose -f {compose_file} -p {project_name} config --quiet'"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def compose_pull(directory: str, compose_file: str, project_name: str) -> str:
|
|
32
|
+
"""Pull images defined in compose file."""
|
|
33
|
+
return f"bash -c 'cd {directory} && docker compose -f {compose_file} -p {project_name} pull'"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def compose_up(directory: str, compose_file: str, project_name: str) -> str:
|
|
37
|
+
"""Start compose project in detached mode."""
|
|
38
|
+
return f"bash -c 'cd {directory} && docker compose -f {compose_file} -p {project_name} up -d'"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def compose_down(directory: str, compose_file: str, project_name: str) -> str:
|
|
42
|
+
"""Stop and remove compose project containers."""
|
|
43
|
+
return f"bash -c 'cd {directory} && docker compose -f {compose_file} -p {project_name} down'"
|