sofia-deploy 1.0.0__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.
- sofia/__init__.py +7 -0
- sofia/__main__.py +4 -0
- sofia/build.py +59 -0
- sofia/cli.py +191 -0
- sofia/config.py +107 -0
- sofia/console.py +78 -0
- sofia/deploy.py +101 -0
- sofia/detect.py +141 -0
- sofia/nginx_setup.py +69 -0
- sofia/node_runtime.py +82 -0
- sofia/packages.py +72 -0
- sofia/ssl_setup.py +61 -0
- sofia/system.py +135 -0
- sofia/systemd_setup.py +52 -0
- sofia/templates/__init__.py +1 -0
- sofia/templates/app.service.tpl +20 -0
- sofia/templates/nginx-proxy-http.conf.tpl +24 -0
- sofia/templates/nginx-proxy-ssl.conf.tpl +45 -0
- sofia/templates/nginx-static-http.conf.tpl +18 -0
- sofia/templates/nginx-static-ssl.conf.tpl +36 -0
- sofia/templates/options-ssl-nginx.conf +6 -0
- sofia/templates_util.py +20 -0
- sofia_deploy-1.0.0.dist-info/METADATA +107 -0
- sofia_deploy-1.0.0.dist-info/RECORD +28 -0
- sofia_deploy-1.0.0.dist-info/WHEEL +5 -0
- sofia_deploy-1.0.0.dist-info/entry_points.txt +2 -0
- sofia_deploy-1.0.0.dist-info/licenses/LICENSE +21 -0
- sofia_deploy-1.0.0.dist-info/top_level.txt +1 -0
sofia/nginx_setup.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from . import console
|
|
6
|
+
from .config import DeployConfig
|
|
7
|
+
from .packages import ensure_letsencrypt_ssl_snippets
|
|
8
|
+
from .system import run
|
|
9
|
+
from .templates_util import write_rendered
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def nginx_conf_path(cfg: DeployConfig) -> Path:
|
|
13
|
+
if Path("/etc/nginx/conf.d").is_dir():
|
|
14
|
+
return Path(f"/etc/nginx/conf.d/{cfg.nginx_site}.conf")
|
|
15
|
+
if Path("/etc/nginx/sites-available").is_dir():
|
|
16
|
+
return Path(f"/etc/nginx/sites-available/{cfg.nginx_site}")
|
|
17
|
+
console.die("Répertoire de conf nginx introuvable")
|
|
18
|
+
raise AssertionError
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def enable_nginx_site(cfg: DeployConfig, conf: Path) -> None:
|
|
22
|
+
sites_enabled = Path("/etc/nginx/sites-enabled")
|
|
23
|
+
if Path("/etc/nginx/sites-available").is_dir() and sites_enabled.is_dir():
|
|
24
|
+
link = sites_enabled / cfg.nginx_site
|
|
25
|
+
if link.exists() or link.is_symlink():
|
|
26
|
+
link.unlink()
|
|
27
|
+
link.symlink_to(conf)
|
|
28
|
+
default = sites_enabled / "default"
|
|
29
|
+
if default.exists() or default.is_symlink():
|
|
30
|
+
default.unlink()
|
|
31
|
+
default_conf = Path("/etc/nginx/conf.d/default.conf")
|
|
32
|
+
if default_conf.exists():
|
|
33
|
+
default_conf.rename(default_conf.with_suffix(".conf.disabled"))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _mapping(cfg: DeployConfig) -> dict[str, str]:
|
|
37
|
+
return {
|
|
38
|
+
"DOMAIN": cfg.domain,
|
|
39
|
+
"WWW_DOMAIN": cfg.www_domain,
|
|
40
|
+
"PORT": str(cfg.port),
|
|
41
|
+
"STATIC_ROOT": str(cfg.static_path),
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _http_template(cfg: DeployConfig) -> str:
|
|
46
|
+
return "nginx-static-http.conf.tpl" if cfg.serve_mode == "static" else "nginx-proxy-http.conf.tpl"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _ssl_template(cfg: DeployConfig) -> str:
|
|
50
|
+
return "nginx-static-ssl.conf.tpl" if cfg.serve_mode == "static" else "nginx-proxy-ssl.conf.tpl"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def install_nginx_http(cfg: DeployConfig) -> None:
|
|
54
|
+
Path("/var/www/certbot").mkdir(parents=True, exist_ok=True)
|
|
55
|
+
conf = nginx_conf_path(cfg)
|
|
56
|
+
write_rendered(_http_template(cfg), conf, _mapping(cfg))
|
|
57
|
+
enable_nginx_site(cfg, conf)
|
|
58
|
+
run(["nginx", "-t"])
|
|
59
|
+
run(["systemctl", "enable", "nginx"])
|
|
60
|
+
run(["systemctl", "restart", "nginx"])
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def install_nginx_ssl(cfg: DeployConfig) -> None:
|
|
64
|
+
ensure_letsencrypt_ssl_snippets()
|
|
65
|
+
conf = nginx_conf_path(cfg)
|
|
66
|
+
write_rendered(_ssl_template(cfg), conf, _mapping(cfg))
|
|
67
|
+
enable_nginx_site(cfg, conf)
|
|
68
|
+
run(["nginx", "-t"])
|
|
69
|
+
run(["systemctl", "reload", "nginx"])
|
sofia/node_runtime.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
import tarfile
|
|
5
|
+
import tempfile
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from . import console
|
|
9
|
+
from .config import DeployConfig
|
|
10
|
+
from .system import HostOS, download, machine_arch, pkg_install, run
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _node_major() -> int | None:
|
|
14
|
+
if not shutil.which("node"):
|
|
15
|
+
return None
|
|
16
|
+
out = run(["node", "-v"], capture=True, check=False)
|
|
17
|
+
if out.returncode != 0:
|
|
18
|
+
return None
|
|
19
|
+
try:
|
|
20
|
+
return int(out.stdout.strip().lstrip("v").split(".", 1)[0])
|
|
21
|
+
except ValueError:
|
|
22
|
+
return None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def node_ok() -> bool:
|
|
26
|
+
major = _node_major()
|
|
27
|
+
return major is not None and major >= 18
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def install_node_from_tarball(cfg: DeployConfig) -> str:
|
|
31
|
+
arch = machine_arch()
|
|
32
|
+
ver = cfg.node_version
|
|
33
|
+
name = f"node-v{ver}-linux-{arch}"
|
|
34
|
+
url = f"https://nodejs.org/dist/v{ver}/{name}.tar.xz"
|
|
35
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
36
|
+
tmp_path = Path(tmp)
|
|
37
|
+
archive = tmp_path / "node.tar.xz"
|
|
38
|
+
download(url, archive)
|
|
39
|
+
with tarfile.open(archive, "r:xz") as tar:
|
|
40
|
+
tar.extractall(tmp_path)
|
|
41
|
+
target_root = Path("/usr/local/lib/nodejs")
|
|
42
|
+
if target_root.exists():
|
|
43
|
+
shutil.rmtree(target_root)
|
|
44
|
+
target_root.mkdir(parents=True)
|
|
45
|
+
shutil.move(str(tmp_path / name), str(target_root / "node"))
|
|
46
|
+
for binary in ("node", "npm", "npx"):
|
|
47
|
+
src = Path("/usr/local/lib/nodejs/node/bin") / binary
|
|
48
|
+
dest = Path("/usr/local/bin") / binary
|
|
49
|
+
if dest.exists() or dest.is_symlink():
|
|
50
|
+
dest.unlink()
|
|
51
|
+
dest.symlink_to(src)
|
|
52
|
+
return "/usr/local/bin/node"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def ensure_node(cfg: DeployConfig, host: HostOS) -> str:
|
|
56
|
+
if node_ok():
|
|
57
|
+
console.log(f"Node.js déjà installé : {run(['node', '-v'], capture=True).stdout.strip()}")
|
|
58
|
+
return "/usr/local/bin/node" if Path("/usr/local/bin/node").exists() else shutil.which("node") # type: ignore[return-value]
|
|
59
|
+
|
|
60
|
+
if _node_major() is not None:
|
|
61
|
+
console.warn(f"Node.js trop ancien — mise à jour vers {cfg.node_version}")
|
|
62
|
+
|
|
63
|
+
console.log(f"Installation de Node.js {cfg.node_version}…")
|
|
64
|
+
if host.pkg == "apt":
|
|
65
|
+
setup = Path("/tmp/nodesource_setup.sh")
|
|
66
|
+
try:
|
|
67
|
+
download("https://deb.nodesource.com/setup_20.x", setup)
|
|
68
|
+
run(["bash", str(setup)], check=False)
|
|
69
|
+
pkg_install(host, "nodejs", check=False)
|
|
70
|
+
except Exception as exc: # noqa: BLE001
|
|
71
|
+
console.warn(f"NodeSource a échoué ({exc})")
|
|
72
|
+
if not node_ok():
|
|
73
|
+
return install_node_from_tarball(cfg)
|
|
74
|
+
else:
|
|
75
|
+
pkg_install(host, "nodejs", "npm", check=False)
|
|
76
|
+
if not node_ok():
|
|
77
|
+
console.warn("Fallback binaire officiel nodejs.org")
|
|
78
|
+
return install_node_from_tarball(cfg)
|
|
79
|
+
|
|
80
|
+
if not node_ok():
|
|
81
|
+
console.die("Échec installation Node.js (>= 18 requis)")
|
|
82
|
+
return "/usr/local/bin/node" if Path("/usr/local/bin/node").exists() else shutil.which("node") # type: ignore[return-value]
|
sofia/packages.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from . import console
|
|
7
|
+
from .system import HostOS, pkg_install, pkg_update, run
|
|
8
|
+
from .templates_util import template_text
|
|
9
|
+
|
|
10
|
+
OPTIONS_SSL = """ssl_session_cache shared:le_nginx_SSL:10m;
|
|
11
|
+
ssl_session_timeout 1440m;
|
|
12
|
+
ssl_session_tickets off;
|
|
13
|
+
ssl_protocols TLSv1.2 TLSv1.3;
|
|
14
|
+
ssl_prefer_server_ciphers off;
|
|
15
|
+
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def ensure_letsencrypt_ssl_snippets() -> None:
|
|
20
|
+
le = Path("/etc/letsencrypt")
|
|
21
|
+
le.mkdir(parents=True, exist_ok=True)
|
|
22
|
+
options = le / "options-ssl-nginx.conf"
|
|
23
|
+
if not options.exists():
|
|
24
|
+
try:
|
|
25
|
+
options.write_text(template_text("options-ssl-nginx.conf"), encoding="utf-8")
|
|
26
|
+
except Exception: # noqa: BLE001
|
|
27
|
+
options.write_text(OPTIONS_SSL, encoding="utf-8")
|
|
28
|
+
console.log("Créé /etc/letsencrypt/options-ssl-nginx.conf")
|
|
29
|
+
dh = le / "ssl-dhparams.pem"
|
|
30
|
+
if not dh.exists():
|
|
31
|
+
console.log("Génération de ssl-dhparams.pem (1–2 min)…")
|
|
32
|
+
run(["openssl", "dhparam", "-out", str(dh), "2048"])
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def install_base_packages(host: HostOS) -> None:
|
|
36
|
+
console.log("Installation des paquets système…")
|
|
37
|
+
pkg_update(host)
|
|
38
|
+
|
|
39
|
+
if host.pkg == "apt":
|
|
40
|
+
pkg_install(
|
|
41
|
+
host,
|
|
42
|
+
"curl",
|
|
43
|
+
"ca-certificates",
|
|
44
|
+
"gnupg",
|
|
45
|
+
"openssl",
|
|
46
|
+
"git",
|
|
47
|
+
"nginx",
|
|
48
|
+
"certbot",
|
|
49
|
+
"python3-certbot-nginx",
|
|
50
|
+
)
|
|
51
|
+
ensure_letsencrypt_ssl_snippets()
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
if not shutil.which("curl"):
|
|
55
|
+
pkg_install(host, "curl", check=False)
|
|
56
|
+
pkg_install(host, "ca-certificates", "openssl", "git", "tar", "gzip", check=False)
|
|
57
|
+
|
|
58
|
+
if not shutil.which("nginx"):
|
|
59
|
+
if shutil.which("amazon-linux-extras"):
|
|
60
|
+
run(["amazon-linux-extras", "enable", "nginx1"], check=False)
|
|
61
|
+
if not pkg_install(host, "nginx", check=False):
|
|
62
|
+
console.die("Échec installation nginx")
|
|
63
|
+
|
|
64
|
+
if not shutil.which("certbot"):
|
|
65
|
+
if not pkg_install(host, "certbot", "python3-certbot-nginx", check=False):
|
|
66
|
+
console.warn("certbot introuvable — tentative pip…")
|
|
67
|
+
if pkg_install(host, "python3-pip", check=False):
|
|
68
|
+
run(["pip3", "install", "--upgrade", "certbot", "certbot-nginx"], check=False)
|
|
69
|
+
else:
|
|
70
|
+
console.warn("certbot non installé — SSL pourra échouer")
|
|
71
|
+
|
|
72
|
+
ensure_letsencrypt_ssl_snippets()
|
sofia/ssl_setup.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from . import console
|
|
7
|
+
from .config import DeployConfig
|
|
8
|
+
from .nginx_setup import install_nginx_ssl
|
|
9
|
+
from .system import run
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def obtain_ssl(cfg: DeployConfig) -> None:
|
|
13
|
+
if not cfg.enable_ssl:
|
|
14
|
+
console.warn(f"SSL désactivé — HTTP seulement")
|
|
15
|
+
return
|
|
16
|
+
|
|
17
|
+
live = Path(f"/etc/letsencrypt/live/{cfg.domain}/fullchain.pem")
|
|
18
|
+
if live.exists():
|
|
19
|
+
console.log(f"Certificat déjà présent pour {cfg.domain}")
|
|
20
|
+
else:
|
|
21
|
+
if not shutil.which("certbot"):
|
|
22
|
+
console.die("certbot introuvable")
|
|
23
|
+
console.log(f"Let's Encrypt : {cfg.domain} + {cfg.www_domain}")
|
|
24
|
+
webroot = run(
|
|
25
|
+
[
|
|
26
|
+
"certbot", "certonly", "--webroot", "-w", "/var/www/certbot",
|
|
27
|
+
"-d", cfg.domain, "-d", cfg.www_domain,
|
|
28
|
+
"--email", cfg.email, "--agree-tos", "--non-interactive", "--keep-until-expiring",
|
|
29
|
+
],
|
|
30
|
+
check=False,
|
|
31
|
+
)
|
|
32
|
+
if webroot.returncode != 0:
|
|
33
|
+
console.warn("Webroot échoué — tentative standalone…")
|
|
34
|
+
run(["systemctl", "stop", "nginx"], check=False)
|
|
35
|
+
try:
|
|
36
|
+
run(
|
|
37
|
+
[
|
|
38
|
+
"certbot", "certonly", "--standalone",
|
|
39
|
+
"-d", cfg.domain, "-d", cfg.www_domain,
|
|
40
|
+
"--email", cfg.email, "--agree-tos", "--non-interactive",
|
|
41
|
+
"--preferred-challenges", "http",
|
|
42
|
+
]
|
|
43
|
+
)
|
|
44
|
+
finally:
|
|
45
|
+
run(["systemctl", "start", "nginx"], check=False)
|
|
46
|
+
|
|
47
|
+
units = run(["systemctl", "list-unit-files"], capture=True, check=False)
|
|
48
|
+
if units.returncode == 0 and "certbot.timer" in (units.stdout or ""):
|
|
49
|
+
run(["systemctl", "enable", "--now", "certbot.timer"], check=False)
|
|
50
|
+
else:
|
|
51
|
+
cron = run(["crontab", "-l"], capture=True, check=False)
|
|
52
|
+
existing = cron.stdout if cron.returncode == 0 else ""
|
|
53
|
+
if "certbot renew" not in existing:
|
|
54
|
+
run(
|
|
55
|
+
"(crontab -l 2>/dev/null; echo \"0 3 * * * certbot renew --quiet --deploy-hook 'systemctl reload nginx'\") | crontab -",
|
|
56
|
+
check=False,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
if not live.exists():
|
|
60
|
+
console.die("Certificat SSL introuvable. Vérifiez DNS + ports 80/443, puis relancez.")
|
|
61
|
+
install_nginx_ssl(cfg)
|
sofia/system.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import platform
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess
|
|
7
|
+
import urllib.request
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from . import console
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class HostOS:
|
|
14
|
+
def __init__(self) -> None:
|
|
15
|
+
self.id = "unknown"
|
|
16
|
+
self.version = ""
|
|
17
|
+
self.pkg = "unknown"
|
|
18
|
+
self._detect()
|
|
19
|
+
|
|
20
|
+
def _detect(self) -> None:
|
|
21
|
+
data: dict[str, str] = {}
|
|
22
|
+
os_release = Path("/etc/os-release")
|
|
23
|
+
if os_release.exists():
|
|
24
|
+
for line in os_release.read_text().splitlines():
|
|
25
|
+
if "=" in line and not line.startswith("#"):
|
|
26
|
+
k, v = line.split("=", 1)
|
|
27
|
+
data[k] = v.strip().strip('"')
|
|
28
|
+
self.id = data.get("ID", "unknown")
|
|
29
|
+
self.version = data.get("VERSION_ID", "")
|
|
30
|
+
if self.id in {"amzn", "amazon", "rhel", "centos", "rocky", "almalinux", "fedora"}:
|
|
31
|
+
self.pkg = "dnf" if shutil.which("dnf") else "yum"
|
|
32
|
+
elif self.id in {"ubuntu", "debian"}:
|
|
33
|
+
self.pkg = "apt"
|
|
34
|
+
else:
|
|
35
|
+
console.die(
|
|
36
|
+
f"OS non supporté ({self.id}). Supportés : Amazon Linux, RHEL/CentOS/Rocky, Ubuntu/Debian."
|
|
37
|
+
)
|
|
38
|
+
console.log(f"OS détecté : {self.id} {self.version} ({self.pkg})")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def require_root() -> None:
|
|
42
|
+
if os.geteuid() != 0:
|
|
43
|
+
console.die("Relancez avec sudo : sudo sofia")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def run(
|
|
47
|
+
cmd: list[str] | str,
|
|
48
|
+
*,
|
|
49
|
+
check: bool = True,
|
|
50
|
+
capture: bool = False,
|
|
51
|
+
env: dict[str, str] | None = None,
|
|
52
|
+
user: str | None = None,
|
|
53
|
+
) -> subprocess.CompletedProcess:
|
|
54
|
+
if isinstance(cmd, str):
|
|
55
|
+
display = cmd
|
|
56
|
+
if user and user != "root":
|
|
57
|
+
args: list[str] | str = ["sudo", "-u", user, "-H", "bash", "-lc", cmd]
|
|
58
|
+
shell = False
|
|
59
|
+
else:
|
|
60
|
+
args = cmd
|
|
61
|
+
shell = True
|
|
62
|
+
else:
|
|
63
|
+
display = " ".join(cmd)
|
|
64
|
+
if user and user != "root":
|
|
65
|
+
args = ["sudo", "-u", user, "-H", *cmd]
|
|
66
|
+
shell = False
|
|
67
|
+
else:
|
|
68
|
+
args = cmd
|
|
69
|
+
shell = False
|
|
70
|
+
console.log("$ " + display)
|
|
71
|
+
return subprocess.run(
|
|
72
|
+
args,
|
|
73
|
+
shell=shell,
|
|
74
|
+
check=check,
|
|
75
|
+
text=True,
|
|
76
|
+
capture_output=capture,
|
|
77
|
+
env={**os.environ, **(env or {})},
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def pkg_update(host: HostOS) -> None:
|
|
82
|
+
if host.pkg == "apt":
|
|
83
|
+
run(["apt-get", "update", "-y"], check=False)
|
|
84
|
+
elif host.pkg == "dnf":
|
|
85
|
+
run(["dnf", "-y", "makecache"], check=False)
|
|
86
|
+
else:
|
|
87
|
+
run(["yum", "-y", "makecache"], check=False)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def pkg_install(host: HostOS, *packages: str, check: bool = True) -> bool:
|
|
91
|
+
if not packages:
|
|
92
|
+
return True
|
|
93
|
+
try:
|
|
94
|
+
if host.pkg == "apt":
|
|
95
|
+
run(["apt-get", "install", "-y", *packages], env={"DEBIAN_FRONTEND": "noninteractive"}, check=check)
|
|
96
|
+
elif host.pkg == "dnf":
|
|
97
|
+
run(["dnf", "install", "-y", "--allowerasing", *packages], check=check)
|
|
98
|
+
else:
|
|
99
|
+
run(["yum", "install", "-y", *packages], check=check)
|
|
100
|
+
return True
|
|
101
|
+
except subprocess.CalledProcessError:
|
|
102
|
+
if check:
|
|
103
|
+
raise
|
|
104
|
+
return False
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def download(url: str, dest: Path) -> None:
|
|
108
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
109
|
+
console.log(f"Téléchargement {url}")
|
|
110
|
+
urllib.request.urlretrieve(url, dest) # noqa: S310
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def open_firewall() -> None:
|
|
114
|
+
console.log("Ouverture des ports 80/443…")
|
|
115
|
+
if shutil.which("firewall-cmd"):
|
|
116
|
+
if run(["systemctl", "is-active", "--quiet", "firewalld"], check=False).returncode == 0:
|
|
117
|
+
run(["firewall-cmd", "--permanent", "--add-service=http"], check=False)
|
|
118
|
+
run(["firewall-cmd", "--permanent", "--add-service=https"], check=False)
|
|
119
|
+
run(["firewall-cmd", "--reload"], check=False)
|
|
120
|
+
return
|
|
121
|
+
if shutil.which("ufw"):
|
|
122
|
+
run(["ufw", "allow", "80/tcp"], check=False)
|
|
123
|
+
run(["ufw", "allow", "443/tcp"], check=False)
|
|
124
|
+
return
|
|
125
|
+
console.warn("Pas de firewalld/ufw — vérifiez le security group AWS (80, 443).")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def machine_arch() -> str:
|
|
129
|
+
m = platform.machine()
|
|
130
|
+
if m in {"x86_64", "amd64"}:
|
|
131
|
+
return "x64"
|
|
132
|
+
if m in {"aarch64", "arm64"}:
|
|
133
|
+
return "arm64"
|
|
134
|
+
console.die(f"Architecture non supportée : {m}")
|
|
135
|
+
raise AssertionError
|
sofia/systemd_setup.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shlex
|
|
4
|
+
import time
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from . import console
|
|
8
|
+
from .config import DeployConfig
|
|
9
|
+
from .system import run
|
|
10
|
+
from .templates_util import write_rendered
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _start_exec(cfg: DeployConfig) -> str:
|
|
14
|
+
"""Build ExecStart line. Prefer absolute node when command starts with node."""
|
|
15
|
+
cmd = cfg.start_command.strip()
|
|
16
|
+
if cmd.startswith("node "):
|
|
17
|
+
script = cmd[5:].strip()
|
|
18
|
+
return f"{cfg.node_bin} {script}"
|
|
19
|
+
if cmd in {"npm start", "npm run start"}:
|
|
20
|
+
npm = str(Path(cfg.node_bin).parent / "npm")
|
|
21
|
+
if not Path(npm).exists():
|
|
22
|
+
npm = "npm"
|
|
23
|
+
return f"{npm} start"
|
|
24
|
+
# Generic: run via bash -lc so npm/pnpm/yarn work
|
|
25
|
+
return f"/bin/bash -lc {shlex.quote(cmd)}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def install_systemd(cfg: DeployConfig) -> None:
|
|
29
|
+
if cfg.serve_mode != "node":
|
|
30
|
+
console.log("Mode static — pas de service systemd applicatif")
|
|
31
|
+
# Disable previous service if any
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
console.log(f"Installation du service systemd {cfg.service_name}…")
|
|
35
|
+
unit = Path(f"/etc/systemd/system/{cfg.service_name}.service")
|
|
36
|
+
write_rendered(
|
|
37
|
+
"app.service.tpl",
|
|
38
|
+
unit,
|
|
39
|
+
{
|
|
40
|
+
"APP_NAME": cfg.app_name,
|
|
41
|
+
"APP_USER": cfg.app_user,
|
|
42
|
+
"APP_GROUP": cfg.app_group,
|
|
43
|
+
"APP_DIR": str(cfg.app_dir),
|
|
44
|
+
"ENV_FILE": str(cfg.env_system),
|
|
45
|
+
"START_EXEC": _start_exec(cfg),
|
|
46
|
+
},
|
|
47
|
+
)
|
|
48
|
+
run(["systemctl", "daemon-reload"])
|
|
49
|
+
run(["systemctl", "enable", cfg.service_name])
|
|
50
|
+
run(["systemctl", "restart", cfg.service_name])
|
|
51
|
+
time.sleep(1)
|
|
52
|
+
run(["systemctl", "--no-pager", "--full", "status", cfg.service_name], check=False)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# nginx / systemd templates
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[Unit]
|
|
2
|
+
Description=Sofia-managed app (__APP_NAME__)
|
|
3
|
+
After=network.target
|
|
4
|
+
Wants=network-online.target
|
|
5
|
+
|
|
6
|
+
[Service]
|
|
7
|
+
Type=simple
|
|
8
|
+
User=__APP_USER__
|
|
9
|
+
Group=__APP_GROUP__
|
|
10
|
+
WorkingDirectory=__APP_DIR__
|
|
11
|
+
EnvironmentFile=__ENV_FILE__
|
|
12
|
+
ExecStart=__START_EXEC__
|
|
13
|
+
Restart=always
|
|
14
|
+
RestartSec=5
|
|
15
|
+
TimeoutStopSec=20
|
|
16
|
+
NoNewPrivileges=true
|
|
17
|
+
PrivateTmp=true
|
|
18
|
+
|
|
19
|
+
[Install]
|
|
20
|
+
WantedBy=multi-user.target
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Generated by Sofia — HTTP (pre-SSL or SSL disabled)
|
|
2
|
+
|
|
3
|
+
server {
|
|
4
|
+
listen 80;
|
|
5
|
+
listen [::]:80;
|
|
6
|
+
server_name __DOMAIN__ __WWW_DOMAIN__;
|
|
7
|
+
|
|
8
|
+
location /.well-known/acme-challenge/ {
|
|
9
|
+
root /var/www/certbot;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
client_max_body_size 50m;
|
|
13
|
+
|
|
14
|
+
location / {
|
|
15
|
+
proxy_pass http://127.0.0.1:__PORT__;
|
|
16
|
+
proxy_http_version 1.1;
|
|
17
|
+
proxy_set_header Host $host;
|
|
18
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
19
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
20
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
|
21
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
22
|
+
proxy_set_header Connection "upgrade";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Generated by Sofia — HTTPS reverse proxy to Node
|
|
2
|
+
|
|
3
|
+
server {
|
|
4
|
+
listen 80;
|
|
5
|
+
listen [::]:80;
|
|
6
|
+
server_name __DOMAIN__ __WWW_DOMAIN__;
|
|
7
|
+
|
|
8
|
+
location /.well-known/acme-challenge/ {
|
|
9
|
+
root /var/www/certbot;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
location / {
|
|
13
|
+
return 301 https://__DOMAIN__$request_uri;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
server {
|
|
18
|
+
listen 443 ssl;
|
|
19
|
+
listen [::]:443 ssl;
|
|
20
|
+
server_name __DOMAIN__ __WWW_DOMAIN__;
|
|
21
|
+
|
|
22
|
+
ssl_certificate /etc/letsencrypt/live/__DOMAIN__/fullchain.pem;
|
|
23
|
+
ssl_certificate_key /etc/letsencrypt/live/__DOMAIN__/privkey.pem;
|
|
24
|
+
include /etc/letsencrypt/options-ssl-nginx.conf;
|
|
25
|
+
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
|
|
26
|
+
|
|
27
|
+
add_header X-Frame-Options "SAMEORIGIN" always;
|
|
28
|
+
add_header X-Content-Type-Options "nosniff" always;
|
|
29
|
+
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
|
30
|
+
|
|
31
|
+
client_max_body_size 50m;
|
|
32
|
+
|
|
33
|
+
location / {
|
|
34
|
+
proxy_pass http://127.0.0.1:__PORT__;
|
|
35
|
+
proxy_http_version 1.1;
|
|
36
|
+
proxy_set_header Host $host;
|
|
37
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
38
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
39
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
|
40
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
41
|
+
proxy_set_header Connection "upgrade";
|
|
42
|
+
proxy_read_timeout 60s;
|
|
43
|
+
proxy_send_timeout 60s;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Generated by Sofia — static site (HTTP)
|
|
2
|
+
|
|
3
|
+
server {
|
|
4
|
+
listen 80;
|
|
5
|
+
listen [::]:80;
|
|
6
|
+
server_name __DOMAIN__ __WWW_DOMAIN__;
|
|
7
|
+
|
|
8
|
+
root __STATIC_ROOT__;
|
|
9
|
+
index index.html;
|
|
10
|
+
|
|
11
|
+
location /.well-known/acme-challenge/ {
|
|
12
|
+
root /var/www/certbot;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
location / {
|
|
16
|
+
try_files $uri $uri/ /index.html;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Generated by Sofia — static site (HTTPS)
|
|
2
|
+
|
|
3
|
+
server {
|
|
4
|
+
listen 80;
|
|
5
|
+
listen [::]:80;
|
|
6
|
+
server_name __DOMAIN__ __WWW_DOMAIN__;
|
|
7
|
+
|
|
8
|
+
location /.well-known/acme-challenge/ {
|
|
9
|
+
root /var/www/certbot;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
location / {
|
|
13
|
+
return 301 https://__DOMAIN__$request_uri;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
server {
|
|
18
|
+
listen 443 ssl;
|
|
19
|
+
listen [::]:443 ssl;
|
|
20
|
+
server_name __DOMAIN__ __WWW_DOMAIN__;
|
|
21
|
+
|
|
22
|
+
ssl_certificate /etc/letsencrypt/live/__DOMAIN__/fullchain.pem;
|
|
23
|
+
ssl_certificate_key /etc/letsencrypt/live/__DOMAIN__/privkey.pem;
|
|
24
|
+
include /etc/letsencrypt/options-ssl-nginx.conf;
|
|
25
|
+
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
|
|
26
|
+
|
|
27
|
+
root __STATIC_ROOT__;
|
|
28
|
+
index index.html;
|
|
29
|
+
|
|
30
|
+
add_header X-Frame-Options "SAMEORIGIN" always;
|
|
31
|
+
add_header X-Content-Type-Options "nosniff" always;
|
|
32
|
+
|
|
33
|
+
location / {
|
|
34
|
+
try_files $uri $uri/ /index.html;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
ssl_session_cache shared:le_nginx_SSL:10m;
|
|
2
|
+
ssl_session_timeout 1440m;
|
|
3
|
+
ssl_session_tickets off;
|
|
4
|
+
ssl_protocols TLSv1.2 TLSv1.3;
|
|
5
|
+
ssl_prefer_server_ciphers off;
|
|
6
|
+
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
|
sofia/templates_util.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from importlib import resources
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def template_text(name: str) -> str:
|
|
8
|
+
return resources.files("sofia").joinpath("templates", name).read_text(encoding="utf-8")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def render_template(name: str, mapping: dict[str, str]) -> str:
|
|
12
|
+
text = template_text(name)
|
|
13
|
+
for key, value in mapping.items():
|
|
14
|
+
text = text.replace(f"__{key}__", value)
|
|
15
|
+
return text
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def write_rendered(name: str, dest: Path, mapping: dict[str, str]) -> None:
|
|
19
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
20
|
+
dest.write_text(render_template(name, mapping), encoding="utf-8")
|