Removes more Wild Cloud cruft.
This commit is contained in:
@@ -100,19 +100,6 @@ log-dhcp
|
||||
)
|
||||
}
|
||||
|
||||
// WriteConfig writes the dnsmasq configuration to the specified path
|
||||
func (g *ConfigGenerator) WriteConfig(cfg *config.State, entries []DNSEntry, configPath string) error {
|
||||
configContent := g.Generate(cfg, entries)
|
||||
|
||||
slog.Info("writing dnsmasq config", "component", "dnsmasq", "path", configPath)
|
||||
|
||||
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
|
||||
return fmt.Errorf("writing dnsmasq config: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RestartService reloads the dnsmasq service (SIGHUP — no downtime).
|
||||
// Falls back to restart if reload fails.
|
||||
func (g *ConfigGenerator) RestartService() error {
|
||||
@@ -223,6 +210,95 @@ func (g *ConfigGenerator) UpdateConfig(cfg *config.State, entries []DNSEntry, re
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateMainConfig creates the main dnsmasq configuration with global settings.
|
||||
// Used by the DnsmasqGenerate handler to preview/apply the base config independently
|
||||
// of domain-specific DNS entries (which are handled by Generate + UpdateConfig).
|
||||
func (g *ConfigGenerator) GenerateMainConfig(cfg *config.State) string {
|
||||
dnsIP, err := network.GetWildCentralIP()
|
||||
if err != nil {
|
||||
slog.Error("failed to detect Wild Central IP", "component", "dnsmasq", "error", err)
|
||||
dnsIP = ""
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
fmt.Fprintf(&sb, `# Wild Cloud DNS Configuration (Main)
|
||||
# This file contains global settings. Domain-specific DNS entries are
|
||||
# generated by reconciliation into the monolithic config.
|
||||
|
||||
# Basic Settings
|
||||
listen-address=%s
|
||||
bind-interfaces
|
||||
domain-needed
|
||||
bogus-priv
|
||||
no-resolv
|
||||
|
||||
# Upstream DNS servers
|
||||
server=1.1.1.1
|
||||
server=8.8.8.8
|
||||
|
||||
# Logging
|
||||
log-queries
|
||||
log-dhcp
|
||||
`, dnsIP)
|
||||
|
||||
// DHCP section (only when explicitly enabled)
|
||||
if cfg != nil && cfg.Cloud.Dnsmasq.DHCP.Enabled {
|
||||
dhcp := cfg.Cloud.Dnsmasq.DHCP
|
||||
sb.WriteString("\n# DHCP Configuration\n")
|
||||
|
||||
leaseTime := dhcp.LeaseTime
|
||||
if leaseTime == "" {
|
||||
leaseTime = "24h"
|
||||
}
|
||||
fmt.Fprintf(&sb, "dhcp-range=%s,%s,%s\n", dhcp.RangeStart, dhcp.RangeEnd, leaseTime)
|
||||
|
||||
gateway := dhcp.Gateway
|
||||
if gateway != "" {
|
||||
fmt.Fprintf(&sb, "dhcp-option=3,%s\n", gateway)
|
||||
}
|
||||
if dnsIP != "" {
|
||||
fmt.Fprintf(&sb, "dhcp-option=6,%s\n", dnsIP)
|
||||
}
|
||||
|
||||
for _, lease := range dhcp.StaticLeases {
|
||||
if lease.Hostname != "" {
|
||||
fmt.Fprintf(&sb, "dhcp-host=%s,%s,%s\n", lease.MAC, lease.IP, lease.Hostname)
|
||||
} else {
|
||||
fmt.Fprintf(&sb, "dhcp-host=%s,%s\n", lease.MAC, lease.IP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// ValidateConfig tests a dnsmasq configuration file for syntax errors
|
||||
func (g *ConfigGenerator) ValidateConfig(configPath string) error {
|
||||
cmd := exec.Command("dnsmasq", "--test", "-C", configPath)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("config validation failed: %w (output: %s)", err, string(output))
|
||||
}
|
||||
if !strings.Contains(string(output), "syntax check OK") {
|
||||
return fmt.Errorf("config validation did not report OK: %s", string(output))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReloadService sends a SIGHUP to dnsmasq to reload configuration.
|
||||
// Lighter weight than a full restart. Falls back to RestartService on failure.
|
||||
func (g *ConfigGenerator) ReloadService() error {
|
||||
cmd := exec.Command("systemctl", "reload", "dnsmasq.service")
|
||||
_, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
slog.Error("reload failed, attempting restart", "component", "dnsmasq", "error", err)
|
||||
return g.RestartService()
|
||||
}
|
||||
slog.Info("dnsmasq service reloaded", "component", "dnsmasq")
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigureSystemDNS configures systemd-resolved to use the local dnsmasq server
|
||||
// This should only be called on first start of dnsmasq
|
||||
func (g *ConfigGenerator) ConfigureSystemDNS() error {
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
package dnsmasq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/config"
|
||||
"github.com/wild-cloud/wild-central/internal/network"
|
||||
)
|
||||
|
||||
// GenerateMainConfig creates the main dnsmasq configuration with global settings
|
||||
// and a conf-dir directive to include per-domain configs
|
||||
func (g *ConfigGenerator) GenerateMainConfig(cfg *config.State) string {
|
||||
// Get the Wild Central IP address
|
||||
dnsIP, err := network.GetWildCentralIP()
|
||||
if err != nil {
|
||||
slog.Error("failed to detect Wild Central IP", "component", "dnsmasq", "error", err)
|
||||
dnsIP = ""
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
fmt.Fprintf(&sb, `# Wild Cloud DNS Configuration (Main)
|
||||
# This file contains global settings. Domain-specific DNS entries are
|
||||
# generated by reconciliation into the monolithic config.
|
||||
|
||||
# Basic Settings
|
||||
listen-address=%s
|
||||
bind-interfaces
|
||||
domain-needed
|
||||
bogus-priv
|
||||
no-resolv
|
||||
|
||||
# Upstream DNS servers
|
||||
server=1.1.1.1
|
||||
server=8.8.8.8
|
||||
|
||||
# Logging
|
||||
log-queries
|
||||
log-dhcp
|
||||
`, dnsIP)
|
||||
|
||||
// DHCP section (only when explicitly enabled)
|
||||
if cfg != nil && cfg.Cloud.Dnsmasq.DHCP.Enabled {
|
||||
dhcp := cfg.Cloud.Dnsmasq.DHCP
|
||||
sb.WriteString("\n# DHCP Configuration\n")
|
||||
|
||||
leaseTime := dhcp.LeaseTime
|
||||
if leaseTime == "" {
|
||||
leaseTime = "24h"
|
||||
}
|
||||
fmt.Fprintf(&sb, "dhcp-range=%s,%s,%s\n", dhcp.RangeStart, dhcp.RangeEnd, leaseTime)
|
||||
|
||||
gateway := dhcp.Gateway
|
||||
if gateway != "" {
|
||||
fmt.Fprintf(&sb, "dhcp-option=3,%s\n", gateway) // default gateway
|
||||
}
|
||||
if dnsIP != "" {
|
||||
fmt.Fprintf(&sb, "dhcp-option=6,%s\n", dnsIP) // DNS server
|
||||
}
|
||||
|
||||
for _, lease := range dhcp.StaticLeases {
|
||||
if lease.Hostname != "" {
|
||||
fmt.Fprintf(&sb, "dhcp-host=%s,%s,%s\n", lease.MAC, lease.IP, lease.Hostname)
|
||||
} else {
|
||||
fmt.Fprintf(&sb, "dhcp-host=%s,%s\n", lease.MAC, lease.IP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// ValidateConfig tests a dnsmasq configuration file for syntax errors
|
||||
func (g *ConfigGenerator) ValidateConfig(configPath string) error {
|
||||
// Use dnsmasq --test to validate the configuration
|
||||
cmd := exec.Command("dnsmasq", "--test", "-C", configPath)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("config validation failed: %w (output: %s)", err, string(output))
|
||||
}
|
||||
|
||||
// Check if output contains "syntax check OK"
|
||||
if !strings.Contains(string(output), "syntax check OK") {
|
||||
return fmt.Errorf("config validation did not report OK: %s", string(output))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReloadService sends a SIGHUP to dnsmasq to reload configuration
|
||||
// This is lighter weight than a full restart
|
||||
func (g *ConfigGenerator) ReloadService() error {
|
||||
// Use systemctl reload which sends SIGHUP
|
||||
cmd := exec.Command("systemctl", "reload", "dnsmasq.service")
|
||||
_, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
// If reload fails, try restart as fallback
|
||||
slog.Error("reload failed, attempting restart", "component", "dnsmasq", "error", err)
|
||||
return g.RestartService()
|
||||
}
|
||||
slog.Info("dnsmasq service reloaded", "component", "dnsmasq")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user