Refactor temporary file handling in rotate.py to ensure compatibility with Docker bind mounts. Replace tempfile usage with a direct path for the WireGuard config, improving reliability during rotation.
Build and Push Docker Images / build-and-push (push) Successful in 19s

This commit is contained in:
2026-08-14 22:26:37 +02:00
parent f1e4a91bd8
commit f5b9a7fa21
@@ -10,7 +10,6 @@ import re
import socket import socket
import subprocess import subprocess
import sys import sys
import tempfile
import time import time
from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime from datetime import datetime
@@ -283,8 +282,11 @@ def rotate_once() -> None:
server, mode, latency_results = pick_server(state_path) server, mode, latency_results = pick_server(state_path)
log(f"Selected endpoint: {server.region} / {server.cn} / {server.ip} (mode={mode})") log(f"Selected endpoint: {server.region} / {server.cn} / {server.ip} (mode={mode})")
with tempfile.TemporaryDirectory(prefix="pia-rotate-") as tmp: # Write temp file on the same filesystem as the destination so os.replace works
tmp_conf = Path(tmp) / "wg0.conf" # across Docker bind mounts (/tmp is often a different device than /config).
wg_path.parent.mkdir(parents=True, exist_ok=True)
tmp_conf = wg_path.with_name(wg_path.name + ".tmp")
try:
log("Generating WireGuard config via native PIA client") log("Generating WireGuard config via native PIA client")
pia.generate_wg_config( pia.generate_wg_config(
require_env("PIA_USER"), require_env("PIA_USER"),
@@ -297,10 +299,11 @@ def rotate_once() -> None:
if not re.search(r"^\[Interface\]", text, flags=re.MULTILINE): if not re.search(r"^\[Interface\]", text, flags=re.MULTILINE):
raise SystemExit("Generated config missing [Interface] section") raise SystemExit("Generated config missing [Interface] section")
wg_path.parent.mkdir(parents=True, exist_ok=True)
os.replace(tmp_conf, wg_path) os.replace(tmp_conf, wg_path)
wg_path.chmod(0o600) wg_path.chmod(0o600)
log(f"Wrote {wg_path}") log(f"Wrote {wg_path}")
finally:
tmp_conf.unlink(missing_ok=True)
restarted = parse_restart_containers() restarted = parse_restart_containers()
restart_containers(restarted) restart_containers(restarted)