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,40 @@
|
|
|
1
|
+
"""Docker container management commands."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from loft_cli_core.specs.service_schema import ContainerBlock
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def pull_image(image: str) -> str:
|
|
9
|
+
return f"docker pull {image}"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def stop_container(name: str) -> str:
|
|
13
|
+
return f"bash -c 'docker stop {name} 2>/dev/null || true'"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def remove_container(name: str) -> str:
|
|
17
|
+
return f"bash -c 'docker rm {name} 2>/dev/null || true'"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def run_container(container: ContainerBlock) -> str:
|
|
21
|
+
"""Build docker run command from ContainerBlock."""
|
|
22
|
+
parts = ["docker run -d"]
|
|
23
|
+
parts.append(f"--name {container.name}")
|
|
24
|
+
parts.append(f"--restart {container.restart}")
|
|
25
|
+
|
|
26
|
+
for port_mapping in container.ports:
|
|
27
|
+
parts.append(f"-p {port_mapping}")
|
|
28
|
+
|
|
29
|
+
for k, v in container.env.items():
|
|
30
|
+
parts.append(f"-e {k}={v!r}")
|
|
31
|
+
|
|
32
|
+
if container.env_file:
|
|
33
|
+
parts.append(f"--env-file {container.env_file}")
|
|
34
|
+
|
|
35
|
+
parts.append(container.image)
|
|
36
|
+
return " \\\n ".join(parts)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def container_running_check(name: str) -> str:
|
|
40
|
+
return f"docker inspect --format='{{{{.State.Running}}}}' {name}"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Docker installation commands."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def install_docker() -> str:
|
|
7
|
+
return "bash -c 'curl -fsSL https://get.docker.com | sh'"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def enable_docker() -> str:
|
|
11
|
+
return "systemctl enable --now docker"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def docker_version_check() -> str:
|
|
15
|
+
return "docker --version"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Shell command builders for kind: file_template 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
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def mkdir_for_file(dest: str) -> str:
|
|
13
|
+
"""Create the parent directory for a target file."""
|
|
14
|
+
parent = dest.rsplit("/", 1)[0]
|
|
15
|
+
return f"mkdir -p {parent}"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def chmod_file(dest: str, mode: str) -> str:
|
|
19
|
+
"""Set file permissions."""
|
|
20
|
+
return f"chmod {mode} {dest}"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def chown_file(dest: str, owner: str, group: str) -> str:
|
|
24
|
+
"""Set file ownership."""
|
|
25
|
+
return f"chown {owner}:{group} {dest}"
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""Nginx installation and configuration commands.
|
|
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
|
|
6
|
+
strings that will be executed via Fabric's ``sudo()``.
|
|
7
|
+
|
|
8
|
+
Site configuration files are written via ``SSH_UPLOAD`` steps (which use
|
|
9
|
+
the ``upload_content()`` method that stages through /tmp) instead of
|
|
10
|
+
shell redirects. Config validation and reload are separate steps so
|
|
11
|
+
the plan clearly shows each operation.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from typing import TYPE_CHECKING
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from loft_cli_core.specs.service_schema import NginxSiteBlock
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def install_nginx() -> str:
|
|
23
|
+
return "DEBIAN_FRONTEND=noninteractive apt-get install -y nginx"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def enable_nginx() -> str:
|
|
27
|
+
return "systemctl enable --now nginx"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def validate_nginx_config() -> str:
|
|
31
|
+
"""Validate nginx configuration (nginx -t)."""
|
|
32
|
+
return "nginx -t"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def reload_nginx_service() -> str:
|
|
36
|
+
"""Reload nginx to apply configuration changes."""
|
|
37
|
+
return "systemctl reload nginx"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def remove_default_site() -> str:
|
|
41
|
+
return "rm -f /etc/nginx/sites-enabled/default"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def site_config_path(site: NginxSiteBlock) -> str:
|
|
45
|
+
"""Return the target path for a site's nginx configuration file.
|
|
46
|
+
|
|
47
|
+
Uses the raw domain name as the filename — standard nginx convention
|
|
48
|
+
(e.g. ``/etc/nginx/sites-available/app.example.com``).
|
|
49
|
+
"""
|
|
50
|
+
return f"/etc/nginx/sites-available/{site.domain}"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def site_config_content(site: NginxSiteBlock) -> str:
|
|
54
|
+
"""Return the rendered nginx site config content (for plan display and SSH_UPLOAD)."""
|
|
55
|
+
return _render_site_conf(site)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def enable_site(site: NginxSiteBlock) -> str:
|
|
59
|
+
"""Create the symlink from sites-enabled to sites-available.
|
|
60
|
+
|
|
61
|
+
Uses the raw domain name as the filename — standard nginx convention.
|
|
62
|
+
"""
|
|
63
|
+
return f"ln -sf /etc/nginx/sites-available/{site.domain} /etc/nginx/sites-enabled/{site.domain}"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _render_site_conf(site: NginxSiteBlock) -> str:
|
|
67
|
+
"""Render an nginx virtual host configuration."""
|
|
68
|
+
upstream = site.upstream or "127.0.0.1"
|
|
69
|
+
upstream_addr = f"{upstream}:{site.upstream_port}"
|
|
70
|
+
|
|
71
|
+
lines: list[str] = []
|
|
72
|
+
|
|
73
|
+
if site.ssl and site.listen_port == 80:
|
|
74
|
+
# Add HTTP -> HTTPS redirect block
|
|
75
|
+
lines.extend(
|
|
76
|
+
[
|
|
77
|
+
"server {",
|
|
78
|
+
" listen 80;",
|
|
79
|
+
" listen [::]:80;",
|
|
80
|
+
f" server_name {site.domain};",
|
|
81
|
+
" return 301 https://$host$request_uri;",
|
|
82
|
+
"}",
|
|
83
|
+
"",
|
|
84
|
+
]
|
|
85
|
+
)
|
|
86
|
+
listen_port = 443
|
|
87
|
+
else:
|
|
88
|
+
listen_port = site.listen_port
|
|
89
|
+
|
|
90
|
+
lines.append("server {")
|
|
91
|
+
|
|
92
|
+
if site.ssl:
|
|
93
|
+
lines.append(f" listen {listen_port} ssl;")
|
|
94
|
+
lines.append(f" listen [::]:{listen_port} ssl;")
|
|
95
|
+
else:
|
|
96
|
+
lines.append(f" listen {listen_port};")
|
|
97
|
+
lines.append(f" listen [::]:{listen_port};")
|
|
98
|
+
|
|
99
|
+
lines.append(f" server_name {site.domain};")
|
|
100
|
+
lines.append("")
|
|
101
|
+
|
|
102
|
+
if site.ssl:
|
|
103
|
+
lines.append(f" ssl_certificate {site.ssl_certificate};")
|
|
104
|
+
lines.append(f" ssl_certificate_key {site.ssl_certificate_key};")
|
|
105
|
+
lines.append(" ssl_protocols TLSv1.2 TLSv1.3;")
|
|
106
|
+
lines.append(" ssl_ciphers HIGH:!aNULL:!MD5;")
|
|
107
|
+
lines.append("")
|
|
108
|
+
|
|
109
|
+
lines.extend(
|
|
110
|
+
[
|
|
111
|
+
" location / {",
|
|
112
|
+
f" proxy_pass http://{upstream_addr};",
|
|
113
|
+
" proxy_set_header Host $host;",
|
|
114
|
+
" proxy_set_header X-Real-IP $remote_addr;",
|
|
115
|
+
" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;",
|
|
116
|
+
" proxy_set_header X-Forwarded-Proto $scheme;",
|
|
117
|
+
" }",
|
|
118
|
+
"}",
|
|
119
|
+
]
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
return "\n".join(lines) + "\n"
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def nginx_ready_check() -> str:
|
|
126
|
+
return "nginx -t"
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""PostgreSQL installation and configuration commands.
|
|
2
|
+
|
|
3
|
+
IMPORTANT — Fabric sudo() compatibility:
|
|
4
|
+
See bootstrap.py module docstring for details. Functions here follow
|
|
5
|
+
the same pattern: compound commands are wrapped in ``bash -c '...'``
|
|
6
|
+
so Fabric's ``sudo()`` elevates the entire operation.
|
|
7
|
+
|
|
8
|
+
PGDG Repository:
|
|
9
|
+
Ubuntu/Debian ship a single PostgreSQL version per release (e.g. PG 14
|
|
10
|
+
on Ubuntu 22.04). To install any other version we add the official
|
|
11
|
+
PostgreSQL Global Development Group (PGDG) apt repository first. The
|
|
12
|
+
planner emits these steps unconditionally — adding the repo is
|
|
13
|
+
idempotent and ensures the exact requested version is always available.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
# ── PGDG Repository setup ───────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def install_pgdg_prerequisites() -> str:
|
|
22
|
+
"""Install packages required to add the PGDG apt repository."""
|
|
23
|
+
return "DEBIAN_FRONTEND=noninteractive apt-get install -y curl ca-certificates gnupg"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def add_pgdg_signing_key() -> str:
|
|
27
|
+
"""Import the PGDG apt repository signing key.
|
|
28
|
+
|
|
29
|
+
Downloads the key and stores it in ``/usr/share/keyrings/`` for use
|
|
30
|
+
with signed-by apt sources (the modern approach replacing apt-key).
|
|
31
|
+
Wrapped in ``bash -c`` because of the pipe.
|
|
32
|
+
"""
|
|
33
|
+
return (
|
|
34
|
+
"bash -c '"
|
|
35
|
+
"install -d /usr/share/postgresql-common/pgdg && "
|
|
36
|
+
"curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc "
|
|
37
|
+
"--fail https://www.postgresql.org/media/keys/ACCC4CF8.asc"
|
|
38
|
+
"'"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def add_pgdg_source_list() -> str:
|
|
43
|
+
"""Add the PGDG apt repository source list.
|
|
44
|
+
|
|
45
|
+
Uses the signed-by mechanism with the key installed by
|
|
46
|
+
``add_pgdg_signing_key()``. Wrapped in ``bash -c`` because of the
|
|
47
|
+
redirect (``>``).
|
|
48
|
+
"""
|
|
49
|
+
return (
|
|
50
|
+
"bash -c '"
|
|
51
|
+
'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] '
|
|
52
|
+
'https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" '
|
|
53
|
+
"> /etc/apt/sources.list.d/pgdg.list"
|
|
54
|
+
"'"
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# ── PostgreSQL installation and management ───────────────────────────
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def install_postgres(version: str) -> str:
|
|
62
|
+
return f"DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql-{version}"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def configure_listen(addresses: list[str]) -> str:
|
|
66
|
+
addr_value = ", ".join(addresses)
|
|
67
|
+
return (
|
|
68
|
+
f"sed -i \"s/^#\\?listen_addresses.*/listen_addresses = '{addr_value}'/\" "
|
|
69
|
+
f"/etc/postgresql/*/main/postgresql.conf"
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def enable_postgres() -> str:
|
|
74
|
+
return "systemctl enable --now postgresql"
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def create_role(name: str, password: str) -> str:
|
|
78
|
+
if password:
|
|
79
|
+
sql = f"CREATE ROLE {name} WITH LOGIN PASSWORD '{password}';"
|
|
80
|
+
else:
|
|
81
|
+
sql = f"CREATE ROLE {name} WITH LOGIN;"
|
|
82
|
+
# Use double quotes for bash -c so SQL single quotes (PASSWORD '...')
|
|
83
|
+
# pass through safely. Inner double quotes escaped with backslash.
|
|
84
|
+
return (
|
|
85
|
+
f'bash -c "sudo -u postgres psql -c \\"{sql}\\" 2>/dev/null || echo Role_may_already_exist"'
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def create_database(name: str, owner: str) -> str:
|
|
90
|
+
if owner:
|
|
91
|
+
sql = f"CREATE DATABASE {name} OWNER {owner};"
|
|
92
|
+
else:
|
|
93
|
+
sql = f"CREATE DATABASE {name};"
|
|
94
|
+
return (
|
|
95
|
+
f'bash -c "sudo -u postgres psql -c \\"{sql}\\" 2>/dev/null '
|
|
96
|
+
f'|| echo Database_may_already_exist"'
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def postgres_ready_check() -> str:
|
|
101
|
+
return "pg_isready"
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""SQL command generators for kind: postgres_ensure steps.
|
|
2
|
+
|
|
3
|
+
Generates idempotent psql commands for ensuring PostgreSQL resources exist.
|
|
4
|
+
All commands are wrapped for execution via shell (directly or docker exec).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _psql_wrap(
|
|
11
|
+
sql: str,
|
|
12
|
+
*,
|
|
13
|
+
conn_host: str,
|
|
14
|
+
conn_port: int,
|
|
15
|
+
admin_user: str,
|
|
16
|
+
docker_exec: str | None = None,
|
|
17
|
+
database: str = "postgres",
|
|
18
|
+
) -> str:
|
|
19
|
+
"""Wrap a SQL statement in a psql command, optionally via docker exec."""
|
|
20
|
+
escaped_sql = sql.replace("'", "'\\''")
|
|
21
|
+
if docker_exec:
|
|
22
|
+
return (
|
|
23
|
+
f"docker exec {docker_exec} psql -U {admin_user} -d {database} " f"-c '{escaped_sql}'"
|
|
24
|
+
)
|
|
25
|
+
# Use Unix socket via sudo to avoid TCP password auth (scram-sha-256).
|
|
26
|
+
# The SSH admin user has NOPASSWD sudo (loft-cli bootstrap invariant).
|
|
27
|
+
return f"sudo -u {admin_user} psql -d {database} -c '{escaped_sql}'"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def ensure_user_cmd(
|
|
31
|
+
name: str,
|
|
32
|
+
password: str | None,
|
|
33
|
+
*,
|
|
34
|
+
conn_host: str,
|
|
35
|
+
conn_port: int,
|
|
36
|
+
admin_user: str,
|
|
37
|
+
docker_exec: str | None = None,
|
|
38
|
+
) -> str:
|
|
39
|
+
"""Generate command to ensure a PostgreSQL user exists."""
|
|
40
|
+
if password:
|
|
41
|
+
sql = (
|
|
42
|
+
f"DO $$ BEGIN "
|
|
43
|
+
f"IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname='{name}') THEN "
|
|
44
|
+
f"CREATE ROLE {name} LOGIN PASSWORD '{password}'; "
|
|
45
|
+
f"ELSE ALTER ROLE {name} PASSWORD '{password}'; "
|
|
46
|
+
f"END IF; END $$;"
|
|
47
|
+
)
|
|
48
|
+
else:
|
|
49
|
+
sql = (
|
|
50
|
+
f"DO $$ BEGIN "
|
|
51
|
+
f"IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname='{name}') THEN "
|
|
52
|
+
f"CREATE ROLE {name} LOGIN; "
|
|
53
|
+
f"END IF; END $$;"
|
|
54
|
+
)
|
|
55
|
+
return _psql_wrap(
|
|
56
|
+
sql,
|
|
57
|
+
conn_host=conn_host,
|
|
58
|
+
conn_port=conn_port,
|
|
59
|
+
admin_user=admin_user,
|
|
60
|
+
docker_exec=docker_exec,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def ensure_database_cmd(
|
|
65
|
+
name: str,
|
|
66
|
+
owner: str,
|
|
67
|
+
*,
|
|
68
|
+
conn_host: str,
|
|
69
|
+
conn_port: int,
|
|
70
|
+
admin_user: str,
|
|
71
|
+
docker_exec: str | None = None,
|
|
72
|
+
) -> str:
|
|
73
|
+
"""Generate command to ensure a PostgreSQL database exists."""
|
|
74
|
+
# createdb is idempotent-ish: check existence first
|
|
75
|
+
check = f"SELECT 1 FROM pg_database WHERE datname='{name}'"
|
|
76
|
+
if docker_exec:
|
|
77
|
+
return (
|
|
78
|
+
f"docker exec {docker_exec} bash -c "
|
|
79
|
+
f'"psql -U {admin_user} -tc \\"{check}\\" | grep -q 1 '
|
|
80
|
+
f'|| createdb -U {admin_user} -O {owner} {name}"'
|
|
81
|
+
)
|
|
82
|
+
return (
|
|
83
|
+
f"sudo -u {admin_user} bash -c "
|
|
84
|
+
f'"psql -tc \\"{check}\\" | grep -q 1 '
|
|
85
|
+
f'|| createdb -O {owner} {name}"'
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def ensure_extension_cmd(
|
|
90
|
+
name: str,
|
|
91
|
+
database: str,
|
|
92
|
+
*,
|
|
93
|
+
conn_host: str,
|
|
94
|
+
conn_port: int,
|
|
95
|
+
admin_user: str,
|
|
96
|
+
docker_exec: str | None = None,
|
|
97
|
+
) -> str:
|
|
98
|
+
"""Generate command to ensure a PostgreSQL extension is installed."""
|
|
99
|
+
sql = f'CREATE EXTENSION IF NOT EXISTS "{name}";'
|
|
100
|
+
return _psql_wrap(
|
|
101
|
+
sql,
|
|
102
|
+
conn_host=conn_host,
|
|
103
|
+
conn_port=conn_port,
|
|
104
|
+
admin_user=admin_user,
|
|
105
|
+
docker_exec=docker_exec,
|
|
106
|
+
database=database,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def ensure_grant_cmd(
|
|
111
|
+
privilege: str,
|
|
112
|
+
on_database: str,
|
|
113
|
+
to_user: str,
|
|
114
|
+
*,
|
|
115
|
+
conn_host: str,
|
|
116
|
+
conn_port: int,
|
|
117
|
+
admin_user: str,
|
|
118
|
+
docker_exec: str | None = None,
|
|
119
|
+
) -> str:
|
|
120
|
+
"""Generate command to grant a privilege."""
|
|
121
|
+
sql = f"GRANT {privilege} ON DATABASE {on_database} TO {to_user};"
|
|
122
|
+
return _psql_wrap(
|
|
123
|
+
sql,
|
|
124
|
+
conn_host=conn_host,
|
|
125
|
+
conn_port=conn_port,
|
|
126
|
+
admin_user=admin_user,
|
|
127
|
+
docker_exec=docker_exec,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def pg_isready_cmd(
|
|
132
|
+
*,
|
|
133
|
+
conn_host: str,
|
|
134
|
+
conn_port: int,
|
|
135
|
+
admin_user: str,
|
|
136
|
+
docker_exec: str | None = None,
|
|
137
|
+
) -> str:
|
|
138
|
+
"""Generate command to check PostgreSQL readiness."""
|
|
139
|
+
if docker_exec:
|
|
140
|
+
return f"docker exec {docker_exec} pg_isready -U {admin_user}"
|
|
141
|
+
return f"sudo -u {admin_user} pg_isready"
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""Shell command builders and unit file renderers for systemd kinds.
|
|
2
|
+
|
|
3
|
+
Used by both systemd_unit and systemd_timer planners.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def render_service_unit(
|
|
10
|
+
*,
|
|
11
|
+
description: str,
|
|
12
|
+
exec_start: str,
|
|
13
|
+
exec_stop: str | None = None,
|
|
14
|
+
working_directory: str | None = None,
|
|
15
|
+
user: str = "root",
|
|
16
|
+
group: str = "root",
|
|
17
|
+
restart: str = "on-failure",
|
|
18
|
+
restart_sec: int = 5,
|
|
19
|
+
after: list[str] | None = None,
|
|
20
|
+
environment: dict[str, str] | None = None,
|
|
21
|
+
environment_file: str | None = None,
|
|
22
|
+
service_type: str = "simple",
|
|
23
|
+
wanted_by: str = "multi-user.target",
|
|
24
|
+
) -> str:
|
|
25
|
+
"""Render a systemd .service unit file from structured parameters."""
|
|
26
|
+
lines: list[str] = ["[Unit]"]
|
|
27
|
+
lines.append(f"Description={description}")
|
|
28
|
+
if after:
|
|
29
|
+
lines.append(f"After={' '.join(after)}")
|
|
30
|
+
lines.append("")
|
|
31
|
+
|
|
32
|
+
lines.append("[Service]")
|
|
33
|
+
lines.append(f"Type={service_type}")
|
|
34
|
+
lines.append(f"ExecStart={exec_start}")
|
|
35
|
+
if exec_stop:
|
|
36
|
+
lines.append(f"ExecStop={exec_stop}")
|
|
37
|
+
if working_directory:
|
|
38
|
+
lines.append(f"WorkingDirectory={working_directory}")
|
|
39
|
+
lines.append(f"User={user}")
|
|
40
|
+
lines.append(f"Group={group}")
|
|
41
|
+
lines.append(f"Restart={restart}")
|
|
42
|
+
lines.append(f"RestartSec={restart_sec}")
|
|
43
|
+
if environment:
|
|
44
|
+
for key, value in sorted(environment.items()):
|
|
45
|
+
lines.append(f"Environment={key}={value}")
|
|
46
|
+
if environment_file:
|
|
47
|
+
lines.append(f"EnvironmentFile={environment_file}")
|
|
48
|
+
lines.append("")
|
|
49
|
+
|
|
50
|
+
lines.append("[Install]")
|
|
51
|
+
lines.append(f"WantedBy={wanted_by}")
|
|
52
|
+
lines.append("")
|
|
53
|
+
|
|
54
|
+
return "\n".join(lines)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def render_timer_unit(
|
|
58
|
+
*,
|
|
59
|
+
description: str,
|
|
60
|
+
on_calendar: str,
|
|
61
|
+
persistent: bool = True,
|
|
62
|
+
accuracy_sec: str = "1min",
|
|
63
|
+
unit: str | None = None,
|
|
64
|
+
wanted_by: str = "timers.target",
|
|
65
|
+
) -> str:
|
|
66
|
+
"""Render a systemd .timer unit file from structured parameters."""
|
|
67
|
+
lines: list[str] = ["[Unit]"]
|
|
68
|
+
lines.append(f"Description={description}")
|
|
69
|
+
lines.append("")
|
|
70
|
+
|
|
71
|
+
lines.append("[Timer]")
|
|
72
|
+
lines.append(f"OnCalendar={on_calendar}")
|
|
73
|
+
if persistent:
|
|
74
|
+
lines.append("Persistent=true")
|
|
75
|
+
lines.append(f"AccuracySec={accuracy_sec}")
|
|
76
|
+
if unit:
|
|
77
|
+
lines.append(f"Unit={unit}")
|
|
78
|
+
lines.append("")
|
|
79
|
+
|
|
80
|
+
lines.append("[Install]")
|
|
81
|
+
lines.append(f"WantedBy={wanted_by}")
|
|
82
|
+
lines.append("")
|
|
83
|
+
|
|
84
|
+
return "\n".join(lines)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def render_logrotate_config(
|
|
88
|
+
*,
|
|
89
|
+
name: str,
|
|
90
|
+
path: str,
|
|
91
|
+
rotate: int = 7,
|
|
92
|
+
frequency: str = "daily",
|
|
93
|
+
compress: bool = True,
|
|
94
|
+
max_size: str = "",
|
|
95
|
+
) -> str:
|
|
96
|
+
"""Render a logrotate configuration file."""
|
|
97
|
+
lines: list[str] = [f"{path} {{"]
|
|
98
|
+
lines.append(f" {frequency}")
|
|
99
|
+
lines.append(f" rotate {rotate}")
|
|
100
|
+
lines.append(" missingok")
|
|
101
|
+
lines.append(" notifempty")
|
|
102
|
+
if compress:
|
|
103
|
+
lines.append(" compress")
|
|
104
|
+
lines.append(" delaycompress")
|
|
105
|
+
if max_size:
|
|
106
|
+
lines.append(f" maxsize {max_size}")
|
|
107
|
+
lines.append(" copytruncate")
|
|
108
|
+
lines.append("}")
|
|
109
|
+
lines.append("")
|
|
110
|
+
|
|
111
|
+
return "\n".join(lines)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def daemon_reload() -> str:
|
|
115
|
+
return "systemctl daemon-reload"
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def enable_unit(unit_name: str) -> str:
|
|
119
|
+
return f"systemctl enable {unit_name}"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def enable_now_unit(unit_name: str) -> str:
|
|
123
|
+
return f"systemctl enable --now {unit_name}"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def restart_unit(unit_name: str) -> str:
|
|
127
|
+
return f"systemctl restart {unit_name}"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def is_active(unit_name: str) -> str:
|
|
131
|
+
return f"systemctl is-active {unit_name}"
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""WireGuard installation and configuration commands."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def install_wireguard() -> str:
|
|
7
|
+
return "DEBIAN_FRONTEND=noninteractive apt-get install -y wireguard"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def generate_server_config(
|
|
11
|
+
interface: str,
|
|
12
|
+
address: str,
|
|
13
|
+
private_key: str,
|
|
14
|
+
listen_port: int,
|
|
15
|
+
client_public_key: str,
|
|
16
|
+
peer_address: str,
|
|
17
|
+
) -> str:
|
|
18
|
+
"""Generate the server-side /etc/wireguard/{interface}.conf.
|
|
19
|
+
|
|
20
|
+
The server listens on ``listen_port`` and allows traffic from the peer
|
|
21
|
+
whose public key is ``client_public_key`` at VPN address ``peer_address``.
|
|
22
|
+
"""
|
|
23
|
+
return f"""\
|
|
24
|
+
[Interface]
|
|
25
|
+
Address = {address}
|
|
26
|
+
ListenPort = {listen_port}
|
|
27
|
+
PrivateKey = {private_key}
|
|
28
|
+
|
|
29
|
+
[Peer]
|
|
30
|
+
PublicKey = {client_public_key}
|
|
31
|
+
AllowedIPs = {peer_address}
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def generate_client_config(
|
|
36
|
+
client_private_key: str,
|
|
37
|
+
peer_address: str,
|
|
38
|
+
server_public_key: str,
|
|
39
|
+
endpoint: str,
|
|
40
|
+
vpn_subnet: str,
|
|
41
|
+
persistent_keepalive: int = 25,
|
|
42
|
+
) -> str:
|
|
43
|
+
"""Generate the local client wg-quick config saved to ~/.wg/loft-cli/{host}/client.conf.
|
|
44
|
+
|
|
45
|
+
``vpn_subnet`` is the full VPN network CIDR (e.g. ``10.10.0.0/24``) derived
|
|
46
|
+
from the server's interface address — traffic to that subnet is routed
|
|
47
|
+
through the tunnel.
|
|
48
|
+
``server_public_key`` is derived from the server's private key via PyNaCl.
|
|
49
|
+
"""
|
|
50
|
+
return f"""\
|
|
51
|
+
[Interface]
|
|
52
|
+
PrivateKey = {client_private_key}
|
|
53
|
+
Address = {peer_address}
|
|
54
|
+
|
|
55
|
+
[Peer]
|
|
56
|
+
PublicKey = {server_public_key}
|
|
57
|
+
Endpoint = {endpoint}
|
|
58
|
+
AllowedIPs = {vpn_subnet}
|
|
59
|
+
PersistentKeepalive = {persistent_keepalive}
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def load_wireguard_module() -> str:
|
|
64
|
+
return "modprobe wireguard"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def enable_wireguard(interface: str) -> str:
|
|
68
|
+
return f"systemctl enable wg-quick@{interface} && wg-quick up {interface}"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def verify_wireguard(interface: str) -> str:
|
|
72
|
+
return f"wg show {interface}"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def set_wireguard_dir_permissions() -> str:
|
|
76
|
+
return "chmod 700 /etc/wireguard"
|