Add resiliency primitives: atomic writes, reconcile mutex, state backup
- Add storage.WriteFileAtomic (temp + rename) and storage.CopyFile helpers - Convert all 8 production config writers to atomic writes: config/state.yaml, dnsmasq, nftables, domains, wireguard (config + secrets + peers + wg0.conf), tunnel/cloudflared - Add sync.Mutex to Reconciler to serialize concurrent Reconcile() calls triggered by domain registration goroutines - Add state.yaml backup (.bak) before every write; LoadState falls back to backup if primary is corrupted - Reconciler refuses to use empty config on corruption (only on first run when no state file exists yet)
This commit is contained in:
@@ -19,6 +19,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/storage"
|
||||
)
|
||||
|
||||
// Config holds tunnel configuration.
|
||||
@@ -143,12 +145,7 @@ func (m *Manager) WriteConfig(cfg Config, services []PublicDomain) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
dir := filepath.Dir(m.configPath())
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("creating tunnel config dir: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(m.configPath(), []byte(content), 0644); err != nil {
|
||||
if err := storage.WriteFileAtomic(m.configPath(), []byte(content), 0644); err != nil {
|
||||
return fmt.Errorf("writing tunnel config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user