package zones import ( "io/fs" "os" ) var ( _ machineRinger = (*Zone)(nil) _ machineRinger = (*Zones)(nil) ) type machineRinger interface { MachineIterator SyncWireguardConfig(ring int) error PruneWireguardConfig(ring int) error } // SyncWireguardConfig updates all wgN.conf files for the specified // ring func (z *Zone) SyncWireguardConfig(ring int) error { switch ring { case 0: return syncWireguardConfig(z.zones, z.zones, ring) case 1: return syncWireguardConfig(z.zones, z, ring) default: return fs.ErrInvalid } } // PruneWireguardConfig removes wgN.conf files of machines with // the corresponding ring disabled. func (z *Zone) PruneWireguardConfig(ring int) error { return pruneWireguardConfig(z, ring) } // WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone func (z *Zone) WriteWireguardKeys(ring int) error { return writeWireguardKeys(z, ring) } // SyncWireguardConfig updates all wgN.conf files for the specified // ring func (m *Zones) SyncWireguardConfig(ring int) error { switch ring { case 0: return syncWireguardConfig(m, m, ring) case 1: var err error m.ForEachZone(func(z *Zone) bool { err = syncWireguardConfig(m, z, ring) return err != nil }) return err default: return fs.ErrInvalid } } // PruneWireguardConfig removes wgN.conf files of machines with // the corresponding ring disabled on all zones func (m *Zones) PruneWireguardConfig(ring int) error { return pruneWireguardConfig(m, ring) } // WriteWireguardKeys rewrites all wgN.{key,pub} files func (m *Zones) WriteWireguardKeys(ring int) error { return writeWireguardKeys(m, ring) } func syncWireguardConfig(z ZoneIterator, m machineRinger, ring int) error { r, err := NewRing(z, m, ring) if err != nil { return err } m.ForEachMachine(func(p *Machine) bool { if _, ok := p.getRingInfo(ring); ok { err = p.writeWireguardRingConfig(r) } else { err = p.RemoveWireguardConfig(ring) } return err != nil }) return err } func pruneWireguardConfig(m MachineIterator, ring int) error { var err error m.ForEachMachine(func(p *Machine) bool { _, ok := p.getRingInfo(ring) if !ok { err = p.RemoveWireguardConfig(ring) } return err != nil }) return err } func writeWireguardKeys(m MachineIterator, ring int) error { var err error m.ForEachMachine(func(p *Machine) bool { err = p.WriteWireguardKeys(ring) if os.IsNotExist(err) { // ignore err = nil } return err != nil }) return err }