diff --git a/app.py b/app.py index 71be63b..4c87493 100644 --- a/app.py +++ b/app.py @@ -108,12 +108,10 @@ def maskToken(token: str | None) -> str: return token[:4] + "*" * (len(token) - 8) + token[-4:] def commandCandidates(name: str): - # 1) konfiguracja ma priorytet sect = f"service:{name}" if cfg.has_section(sect) and cfg.has_option(sect, "command"): return [cfg.get(sect, "command")] - # 2) auto-kandydaci zależnie od dostępności cmds = [] if shutil.which("systemctl"): 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"] if shutil.which("rc-service"): cmds += [f"rc-service {name} reload", f"rc-service {name} restart"] - # 3) ostatnia deska ratunku: HUP if shutil.which("pkill"): 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): for cmd in commandCandidates(name): @@ -208,10 +213,12 @@ def writeHostsAtomic(new_content: str, path: str = "/etc/hosts", backup_dir: str # kopia zapasowa if backup_dir: os.makedirs(backup_dir, exist_ok=True) - ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") - backup_path = os.path.join(backup_dir, f"hosts.{ts}.bak") - shutil.copy2(path, backup_path) - info["backup"] = backup_path + try: + shutil.copy2(path, backup_path) + info["backup"] = backup_path + except Exception as e: + logger.warning(f"Backup nieudany: {e}") + # zapis atomowy dir_name = os.path.dirname(path) or "." diff --git a/config.example.ini b/config.example.ini index d94b652..902811a 100644 --- a/config.example.ini +++ b/config.example.ini @@ -6,5 +6,8 @@ services = dnsmasq # opcjonalnie: globalny timeout (sekundy) 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] command = pkill -HUP dnsmasq \ No newline at end of file