nginx: add nginx_deployer to reload nginx only once

This commit is contained in:
missytake 2024-05-13 16:54:00 +02:00
parent 84a7774368
commit ebadcb9704
2 changed files with 100 additions and 91 deletions
pyinfra_nginx

View file

@ -1 +1 @@
from .nginx import deploy_nginx, add_nginx_domain from .nginx import deploy_nginx, nginx_deployer, NGINX

View file

@ -1,9 +1,8 @@
from io import StringIO import contextlib
import importlib.resources import importlib.resources
from pyinfra import host from pyinfra import host, logger
from pyinfra.api.deploy import deploy from pyinfra.operations import files, apt, systemd
from pyinfra.operations import files, server, apt, systemd
from pyinfra.facts.deb import DebPackages from pyinfra.facts.deb import DebPackages
from pyinfra_acmetool import deploy_acmetool from pyinfra_acmetool import deploy_acmetool
@ -20,7 +19,25 @@ def deploy_nginx():
) )
@contextlib.contextmanager
def nginx_deployer(reload_nginx: bool = False):
nginx = NGINX(reload_nginx)
yield nginx
systemd.service(
name="enable and start NGINX service",
service="nginx.service",
running=True,
enabled=True,
reloaded=nginx.reload,
)
class NGINX:
def __init__(self, reload):
self.reload = reload
def add_nginx_domain( def add_nginx_domain(
self,
domain: str, domain: str,
config_path: str = None, config_path: str = None,
webroot: str = None, webroot: str = None,
@ -28,8 +45,7 @@ def add_nginx_domain(
redirect: str = None, redirect: str = None,
enabled=True, enabled=True,
acmetool=True, acmetool=True,
skip_restart=False, ) -> bool:
):
"""Let a domain be handled by nginx, create a Let's Encrypt certificate for it, and deploy the config. """Let a domain be handled by nginx, create a Let's Encrypt certificate for it, and deploy the config.
This method supports 3 template configs for configuring your site: This method supports 3 template configs for configuring your site:
@ -46,17 +62,12 @@ def add_nginx_domain(
:param redirect: where to 301 redirect to, e.g. https://i.delta.chat$request_uri :param redirect: where to 301 redirect to, e.g. https://i.delta.chat$request_uri
:param enabled: whether the site should be enabled at /etc/nginx/sites-enabled :param enabled: whether the site should be enabled at /etc/nginx/sites-enabled
:param acmetool: whether acmetool should fetch TLS certs for the domain :param acmetool: whether acmetool should fetch TLS certs for the domain
:param skip_restart: set True if the nginx restart is done later anyway :return whether the nginx config was changed and needs a reload
""" """
default_config_link = files.link( default_config_link = files.link(
path="/etc/nginx/sites-enabled/default", present=False path="/etc/nginx/sites-enabled/default", present=False
) )
if default_config_link.changed: self.reload |= default_config_link.changed
systemd.service(
name="reload nginx",
service="nginx.service",
reloaded=True,
)
if acmetool: if acmetool:
deploy_acmetool(nginx_hook=True, domains=[domain]) deploy_acmetool(nginx_hook=True, domains=[domain])
@ -101,6 +112,12 @@ def add_nginx_domain(
domain=domain, domain=domain,
redirect=redirect, redirect=redirect,
) )
try:
self.reload |= config.changed
except AttributeError:
logger.error("please pass either webroot, proxy_port, redirect, or config_path to add_nginx_domain")
raise
config_link = files.link( config_link = files.link(
path=f"/etc/nginx/sites-enabled/{domain}", path=f"/etc/nginx/sites-enabled/{domain}",
target=f"/etc/nginx/sites-available/{domain}", target=f"/etc/nginx/sites-available/{domain}",
@ -108,12 +125,4 @@ def add_nginx_domain(
group="root", group="root",
present=enabled, present=enabled,
) )
if not skip_restart: self.reload |= config_link.changed
if config.changed or config_link.changed:
systemd.service(
name="NGINX should be enabled and running",
service="nginx.service",
running=True,
enabled=True,
restarted=True,
)