From 6a2cbdffd0576418edf64fc30816fb85d0bd48cb Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 25 Aug 2023 16:04:01 +0000 Subject: [PATCH] WIP Signed-off-by: Alejandro Mery --- pkg/zones/machine.go | 18 +++++++++++++ pkg/zones/machine_rings.go | 54 ++++++++++++++++++++++++++++++++++++++ pkg/zones/zones.go | 25 ++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/pkg/zones/machine.go b/pkg/zones/machine.go index f9ceefe..47aca68 100644 --- a/pkg/zones/machine.go +++ b/pkg/zones/machine.go @@ -39,6 +39,24 @@ func (m *Machine) IsGateway() bool { return ok } +// SetGateway enables/disables a Machine ring0 integration +func (m *Machine) SetGateway(enabled bool) error { + ri, found := m.getRingInfo(0) + switch { + case !found && !enabled: + return nil + case !found: + var err error + + if ri, err = m.createRingInfo(0, false); err != nil { + return err + } + } + + ri.Enabled = enabled + return m.syncRingConfig(0) +} + func (m *Machine) getPeerByName(name string) (*Machine, bool) { return m.zone.zones.GetMachineByName(name) } diff --git a/pkg/zones/machine_rings.go b/pkg/zones/machine_rings.go index 1e950e8..75ab2bf 100644 --- a/pkg/zones/machine_rings.go +++ b/pkg/zones/machine_rings.go @@ -261,3 +261,57 @@ func (m *Machine) RemoveWireguardConfig(ring int) error { return err } + +func (*Machine) syncRingConfig(_ int) error { + // _, err := m.getRingNodes(ring) + return nil +} + +func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) { + keys, err := wireguard.NewKeyPair() + if err != nil { + return nil, err + } + + ri := &RingInfo{ + Ring: ring, + Enabled: enabled, + Keys: keys, + } + + err = m.applyRingInfo(ring, ri) + if err != nil { + return nil, err + } + + return ri, nil +} + +func (m *Machine) writeRingInfo(ri *RingInfo) error { + var err error + + if m == nil || ri == nil { + return fs.ErrInvalid + } + + err = m.WriteWireguardKeys(ri.Ring) + if err != nil { + return err + } + + if !ri.Enabled { + return m.RemoveWireguardConfig(ri.Ring) + } + + return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey) +} + +func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error { + f, err := m.CreateTruncFile("wg%v.conf", ring) + if err != nil { + return err + } + defer f.Close() + + return nil +} diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index 92489c4..d300d59 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -34,6 +34,31 @@ func (z *Zone) ForEachMachine(fn func(*Machine) bool) { } } +// SetGateway configures a machine to be the zone's ring0 gateway +func (z *Zone) SetGateway(gatewayID int, enabled bool) error { + var err error + var found bool + + z.ForEachMachine(func(p *Machine) bool { + if p.ID == gatewayID { + found = true + err = p.SetGateway(enabled) + + return true + } + return false + }) + + switch { + case err != nil: + return err + case !found: + return fs.ErrNotExist + default: + return nil + } +} + // Zones represents all zones in a cluster type Zones struct { dir fs.FS