fix: systemd path unit for reload — plugin touches trigger file, systemd reloads as root
This commit is contained in:
parent
3c0b210d04
commit
a8edcfe823
2 changed files with 27 additions and 8 deletions
21
install.sh
21
install.sh
|
|
@ -354,8 +354,29 @@ EOF
|
||||||
[Service]
|
[Service]
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
RestartSec=5
|
RestartSec=5
|
||||||
|
EOF
|
||||||
|
# Add path unit: watches for reload trigger, runs systemctl reload dnsmasq as root
|
||||||
|
cat > /etc/systemd/system/dnsmasq-reload.path << 'EOF'
|
||||||
|
[Unit]
|
||||||
|
Description=Watch dnsmasq config for reload trigger
|
||||||
|
|
||||||
|
[Path]
|
||||||
|
PathChanged=/opt/zoraxy/conf/dhcp/dnsmasq.conf.reload
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
cat > /etc/systemd/system/dnsmasq-reload.service << 'EOF'
|
||||||
|
[Unit]
|
||||||
|
Description=Reload dnsmasq on config change
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
ExecStart=/usr/bin/systemctl reload dnsmasq
|
||||||
EOF
|
EOF
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
|
systemctl enable dnsmasq-reload.path 2>/dev/null || true
|
||||||
|
systemctl start dnsmasq-reload.path 2>/dev/null || true
|
||||||
systemctl enable dnsmasq 2>/dev/null || true
|
systemctl enable dnsmasq 2>/dev/null || true
|
||||||
systemctl restart dnsmasq 2>/dev/null || service dnsmasq restart 2>/dev/null || true
|
systemctl restart dnsmasq 2>/dev/null || service dnsmasq restart 2>/dev/null || true
|
||||||
log "dnsmasq restarted"
|
log "dnsmasq restarted"
|
||||||
|
|
|
||||||
14
server.go
14
server.go
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -24,8 +23,6 @@ var (
|
||||||
// Override via environment: LEASE_FILE, CONF_FILE, RELOAD_CMD, RELOAD_ARGS
|
// Override via environment: LEASE_FILE, CONF_FILE, RELOAD_CMD, RELOAD_ARGS
|
||||||
leasesFile = envOrDefault("LEASE_FILE", "/opt/zoraxy/conf/dhcp/dnsmasq.leases")
|
leasesFile = envOrDefault("LEASE_FILE", "/opt/zoraxy/conf/dhcp/dnsmasq.leases")
|
||||||
confFile = envOrDefault("CONF_FILE", "/opt/zoraxy/conf/dhcp/dnsmasq.conf")
|
confFile = envOrDefault("CONF_FILE", "/opt/zoraxy/conf/dhcp/dnsmasq.conf")
|
||||||
reloadCmd = envOrDefault("RELOAD_CMD", "/usr/bin/sudo")
|
|
||||||
reloadArgs = strings.Fields(envOrDefault("RELOAD_ARGS", "pkill -HUP dnsmasq"))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func envOrDefault(key, fallback string) string {
|
func envOrDefault(key, fallback string) string {
|
||||||
|
|
@ -242,16 +239,17 @@ func unpinLease(mac string) error {
|
||||||
|
|
||||||
// reloadDnsmasq runs the reload command (e.g. sudo systemctl reload dnsmasq).
|
// reloadDnsmasq runs the reload command (e.g. sudo systemctl reload dnsmasq).
|
||||||
func reloadDnsmasq() error {
|
func reloadDnsmasq() error {
|
||||||
// Scrub expired leases before reloading so the UI stays clean.
|
// Scrub expired leases before reloading.
|
||||||
if err := cleanLeaseFile(); err != nil {
|
if err := cleanLeaseFile(); err != nil {
|
||||||
log.Printf("WARNING: failed to clean lease file: %v", err)
|
log.Printf("WARNING: failed to clean lease file: %v", err)
|
||||||
// Continue — reload is still useful even if cleaning fails.
|
|
||||||
}
|
}
|
||||||
cmd := exec.Command(reloadCmd, reloadArgs...)
|
// Touch a trigger file — systemd.path unit watches this and reloads dnsmasq.
|
||||||
output, err := cmd.CombinedOutput()
|
triggerFile := confFile + ".reload"
|
||||||
|
f, err := os.Create(triggerFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("reload failed: %w — %s", err, string(output))
|
return fmt.Errorf("create trigger file: %w", err)
|
||||||
}
|
}
|
||||||
|
f.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue