dodatkowe poprawki i funkcje

This commit is contained in:
Mateusz Gruszczyński
2025-09-05 09:37:13 +02:00
parent 05d00cbd14
commit deb6b85284
2 changed files with 18 additions and 8 deletions

23
app.py
View File

@@ -108,12 +108,10 @@ def maskToken(token: str | None) -> str:
return token[:4] + "*" * (len(token) - 8) + token[-4:] return token[:4] + "*" * (len(token) - 8) + token[-4:]
def commandCandidates(name: str): def commandCandidates(name: str):
# 1) konfiguracja ma priorytet
sect = f"service:{name}" sect = f"service:{name}"
if cfg.has_section(sect) and cfg.has_option(sect, "command"): if cfg.has_section(sect) and cfg.has_option(sect, "command"):
return [cfg.get(sect, "command")] return [cfg.get(sect, "command")]
# 2) auto-kandydaci zależnie od dostępności
cmds = [] cmds = []
if shutil.which("systemctl"): if shutil.which("systemctl"):
cmds += [f"systemctl reload {name}", f"systemctl restart {name}"] cmds += [f"systemctl reload {name}", f"systemctl restart {name}"]
@@ -121,10 +119,17 @@ def commandCandidates(name: str):
cmds += [f"service {name} reload", f"service {name} restart"] cmds += [f"service {name} reload", f"service {name} restart"]
if shutil.which("rc-service"): if shutil.which("rc-service"):
cmds += [f"rc-service {name} reload", f"rc-service {name} restart"] cmds += [f"rc-service {name} reload", f"rc-service {name} restart"]
# 3) ostatnia deska ratunku: HUP
if shutil.which("pkill"): if shutil.which("pkill"):
cmds += [f"pkill -HUP {name}"] cmds += [f"pkill -HUP {name}"]
return cmds or [f"pkill -HUP {name}"]
pid_file = cfg.get(sect, "pid_file", fallback=None)
if pid_file and os.path.isfile(pid_file) and shutil.which("kill"):
cmds += [f"kill -HUP $(cat {pid_file})"]
if shutil.which("pgrep") and shutil.which("kill"):
cmds += [f"kill -HUP $(pgrep -o {name})"]
return cmds or []
def reloadService(name: str): def reloadService(name: str):
for cmd in commandCandidates(name): for cmd in commandCandidates(name):
@@ -208,10 +213,12 @@ def writeHostsAtomic(new_content: str, path: str = "/etc/hosts", backup_dir: str
# kopia zapasowa # kopia zapasowa
if backup_dir: if backup_dir:
os.makedirs(backup_dir, exist_ok=True) os.makedirs(backup_dir, exist_ok=True)
ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") try:
backup_path = os.path.join(backup_dir, f"hosts.{ts}.bak") shutil.copy2(path, backup_path)
shutil.copy2(path, backup_path) info["backup"] = backup_path
info["backup"] = backup_path except Exception as e:
logger.warning(f"Backup nieudany: {e}")
# zapis atomowy # zapis atomowy
dir_name = os.path.dirname(path) or "." dir_name = os.path.dirname(path) or "."

View File

@@ -6,5 +6,8 @@ services = dnsmasq
# opcjonalnie: globalny timeout (sekundy) # opcjonalnie: globalny timeout (sekundy)
reload_timeout = 5 reload_timeout = 5
# jeżeli nie podasz command, kod spróbuje kolejno: systemctl/service/rc-service, pkill, kill -HUP z pidfile, pgrep
pid_file = /run/dnsmasq/dnsmasq.pid
[service:dnsmasq] [service:dnsmasq]
command = pkill -HUP dnsmasq command = pkill -HUP dnsmasq