Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix certbot process getting stuck in dstack-proxy #2143

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/dstack/_internal/proxy/gateway/services/nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from dstack._internal.utils.common import run_async
from dstack._internal.utils.logging import get_logger

CERTBOT_TIMEOUT = 30
CERTBOT_TIMEOUT = 40
CERTBOT_2ND_TIMEOUT = 5
CONFIGS_DIR = Path("/etc/nginx/sites-enabled")
logger = get_logger(__name__)

Expand Down Expand Up @@ -108,7 +109,8 @@ def write_conf(self, conf: str, conf_name: str) -> None:
def run_certbot(domain: str, acme: ACMESettings) -> None:
logger.info("Running certbot for %s", domain)

cmd = ["sudo", "certbot", "certonly"]
cmd = ["sudo", "timeout", "--kill-after", str(CERTBOT_2ND_TIMEOUT), str(CERTBOT_TIMEOUT)]
cmd += ["certbot", "certonly"]
cmd += ["--non-interactive", "--agree-tos", "--register-unsafely-without-email"]
cmd += ["--keep", "--nginx", "--domain", domain]

Expand All @@ -119,13 +121,16 @@ def run_certbot(domain: str, acme: ACMESettings) -> None:
cmd += ["--eab-kid", acme.eab_kid]
cmd += ["--eab-hmac-key", acme.eab_hmac_key]

try:
r = subprocess.run(cmd, capture_output=True, timeout=CERTBOT_TIMEOUT)
except subprocess.TimeoutExpired as e:
r = subprocess.run(
cmd,
capture_output=True,
timeout=CERTBOT_TIMEOUT + CERTBOT_2ND_TIMEOUT + 1, # shouldn't happen
)
if r.returncode == 124:
raise ProxyError(
f"Could not obtain {domain} TLS certificate in {CERTBOT_TIMEOUT}s."
" Make sure DNS records are configured for this gateway."
) from e
)
if r.returncode != 0:
raise ProxyError(f"Error obtaining {domain} TLS certificate:\n{r.stderr.decode()}")

Expand Down
Loading