|
|
@ -3,6 +3,8 @@ package cluster |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"io/fs" |
|
|
|
"io/fs" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"git.jpi.io/amery/jpictl/pkg/rings" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
@ -26,22 +28,22 @@ var ( |
|
|
|
// A WireguardConfigPruner deletes wgN.conf on all machines under
|
|
|
|
// A WireguardConfigPruner deletes wgN.conf on all machines under
|
|
|
|
// its scope with the specified ring disabled
|
|
|
|
// its scope with the specified ring disabled
|
|
|
|
type WireguardConfigPruner interface { |
|
|
|
type WireguardConfigPruner interface { |
|
|
|
PruneWireguardConfig(ring int) error |
|
|
|
PruneWireguardConfig(ring rings.RingID) error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
|
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
|
|
// the corresponding ring disabled on all zones
|
|
|
|
// the corresponding ring disabled on all zones
|
|
|
|
func (m *Cluster) PruneWireguardConfig(ring int) error { |
|
|
|
func (m *Cluster) PruneWireguardConfig(ring rings.RingID) error { |
|
|
|
return pruneWireguardConfig(m, ring) |
|
|
|
return pruneWireguardConfig(m, ring) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
|
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
|
|
// the corresponding ring disabled.
|
|
|
|
// the corresponding ring disabled.
|
|
|
|
func (z *Zone) PruneWireguardConfig(ring int) error { |
|
|
|
func (z *Zone) PruneWireguardConfig(ring rings.RingID) error { |
|
|
|
return pruneWireguardConfig(z, ring) |
|
|
|
return pruneWireguardConfig(z, ring) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func pruneWireguardConfig(m MachineIterator, ring int) error { |
|
|
|
func pruneWireguardConfig(m MachineIterator, ring rings.RingID) error { |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
m.ForEachMachine(func(p *Machine) bool { |
|
|
|
m.ForEachMachine(func(p *Machine) bool { |
|
|
@ -59,7 +61,7 @@ func pruneWireguardConfig(m MachineIterator, ring int) error { |
|
|
|
|
|
|
|
|
|
|
|
// PruneWireguardConfig deletes the wgN.conf file if its
|
|
|
|
// PruneWireguardConfig deletes the wgN.conf file if its
|
|
|
|
// presence on the ring is disabled
|
|
|
|
// presence on the ring is disabled
|
|
|
|
func (m *Machine) PruneWireguardConfig(ring int) error { |
|
|
|
func (m *Machine) PruneWireguardConfig(ring rings.RingID) error { |
|
|
|
_, ok := m.getRingInfo(ring) |
|
|
|
_, ok := m.getRingInfo(ring) |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
return m.RemoveWireguardConfig(ring) |
|
|
|
return m.RemoveWireguardConfig(ring) |
|
|
@ -71,16 +73,16 @@ func (m *Machine) PruneWireguardConfig(ring int) error { |
|
|
|
// A WireguardConfigWriter rewrites all wgN.conf on all machines under
|
|
|
|
// A WireguardConfigWriter rewrites all wgN.conf on all machines under
|
|
|
|
// its scope attached to that ring
|
|
|
|
// its scope attached to that ring
|
|
|
|
type WireguardConfigWriter interface { |
|
|
|
type WireguardConfigWriter interface { |
|
|
|
WriteWireguardConfig(ring int) error |
|
|
|
WriteWireguardConfig(ring rings.RingID) error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
|
|
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
|
|
|
// attached to that ring
|
|
|
|
// attached to that ring
|
|
|
|
func (m *Cluster) WriteWireguardConfig(ring int) error { |
|
|
|
func (m *Cluster) WriteWireguardConfig(ring rings.RingID) error { |
|
|
|
switch ring { |
|
|
|
switch ring { |
|
|
|
case 0: |
|
|
|
case rings.RingZeroID: |
|
|
|
return writeWireguardConfig(m, m, ring) |
|
|
|
return writeWireguardConfig(m, m, ring) |
|
|
|
case 1: |
|
|
|
case rings.RingOneID: |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
err = writeWireguardConfig(m, z, ring) |
|
|
|
err = writeWireguardConfig(m, z, ring) |
|
|
@ -88,24 +90,24 @@ func (m *Cluster) WriteWireguardConfig(ring int) error { |
|
|
|
}) |
|
|
|
}) |
|
|
|
return err |
|
|
|
return err |
|
|
|
default: |
|
|
|
default: |
|
|
|
return fs.ErrInvalid |
|
|
|
return ErrInvalidRing(ring) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
|
|
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
|
|
|
// on the Zone attached to that ring
|
|
|
|
// on the Zone attached to that ring
|
|
|
|
func (z *Zone) WriteWireguardConfig(ring int) error { |
|
|
|
func (z *Zone) WriteWireguardConfig(ring rings.RingID) error { |
|
|
|
switch ring { |
|
|
|
switch ring { |
|
|
|
case 0: |
|
|
|
case rings.RingZeroID: |
|
|
|
return writeWireguardConfig(z.zones, z.zones, ring) |
|
|
|
return writeWireguardConfig(z.zones, z.zones, ring) |
|
|
|
case 1: |
|
|
|
case rings.RingOneID: |
|
|
|
return writeWireguardConfig(z.zones, z, ring) |
|
|
|
return writeWireguardConfig(z.zones, z, ring) |
|
|
|
default: |
|
|
|
default: |
|
|
|
return fs.ErrInvalid |
|
|
|
return ErrInvalidRing(ring) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error { |
|
|
|
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring rings.RingID) error { |
|
|
|
r, err := NewRing(z, m, ring) |
|
|
|
r, err := NewRing(z, m, ring) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
@ -121,7 +123,7 @@ func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error { |
|
|
|
|
|
|
|
|
|
|
|
// WriteWireguardConfig rewrites the wgN.conf file of this Machine
|
|
|
|
// WriteWireguardConfig rewrites the wgN.conf file of this Machine
|
|
|
|
// if enabled
|
|
|
|
// if enabled
|
|
|
|
func (m *Machine) WriteWireguardConfig(ring int) error { |
|
|
|
func (m *Machine) WriteWireguardConfig(ring rings.RingID) error { |
|
|
|
r, err := NewRing(m.zone.zones, m.zone, ring) |
|
|
|
r, err := NewRing(m.zone.zones, m.zone, ring) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
@ -131,12 +133,17 @@ func (m *Machine) WriteWireguardConfig(ring int) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (m *Machine) writeWireguardRingConfig(r *Ring) error { |
|
|
|
func (m *Machine) writeWireguardRingConfig(r *Ring) error { |
|
|
|
|
|
|
|
ring, err := AsWireguardInterfaceID(r.ID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
wg, err := r.ExportConfig(m) |
|
|
|
wg, err := r.ExportConfig(m) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
f, err := m.CreateTruncFile("wg%v.conf", r.ID) |
|
|
|
f, err := m.CreateTruncFile(ring.ConfFile()) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -149,16 +156,16 @@ func (m *Machine) writeWireguardRingConfig(r *Ring) error { |
|
|
|
// A WireguardConfigSyncer updates all wgN.conf on all machines under
|
|
|
|
// A WireguardConfigSyncer updates all wgN.conf on all machines under
|
|
|
|
// its scope reflecting the state of the ring
|
|
|
|
// its scope reflecting the state of the ring
|
|
|
|
type WireguardConfigSyncer interface { |
|
|
|
type WireguardConfigSyncer interface { |
|
|
|
SyncWireguardConfig(ring int) error |
|
|
|
SyncWireguardConfig(ring rings.RingID) error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
|
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
|
|
// ring
|
|
|
|
// ring
|
|
|
|
func (m *Cluster) SyncWireguardConfig(ring int) error { |
|
|
|
func (m *Cluster) SyncWireguardConfig(ring rings.RingID) error { |
|
|
|
switch ring { |
|
|
|
switch ring { |
|
|
|
case 0: |
|
|
|
case rings.RingZeroID: |
|
|
|
return syncWireguardConfig(m, m, ring) |
|
|
|
return syncWireguardConfig(m, m, ring) |
|
|
|
case 1: |
|
|
|
case rings.RingOneID: |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
err = syncWireguardConfig(m, z, ring) |
|
|
|
err = syncWireguardConfig(m, z, ring) |
|
|
@ -166,24 +173,24 @@ func (m *Cluster) SyncWireguardConfig(ring int) error { |
|
|
|
}) |
|
|
|
}) |
|
|
|
return err |
|
|
|
return err |
|
|
|
default: |
|
|
|
default: |
|
|
|
return fs.ErrInvalid |
|
|
|
return ErrInvalidRing(ring) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
|
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
|
|
// ring
|
|
|
|
// ring
|
|
|
|
func (z *Zone) SyncWireguardConfig(ring int) error { |
|
|
|
func (z *Zone) SyncWireguardConfig(ring rings.RingID) error { |
|
|
|
switch ring { |
|
|
|
switch ring { |
|
|
|
case 0: |
|
|
|
case rings.RingZeroID: |
|
|
|
return syncWireguardConfig(z.zones, z.zones, ring) |
|
|
|
return syncWireguardConfig(z.zones, z.zones, ring) |
|
|
|
case 1: |
|
|
|
case rings.RingOneID: |
|
|
|
return syncWireguardConfig(z.zones, z, ring) |
|
|
|
return syncWireguardConfig(z.zones, z, ring) |
|
|
|
default: |
|
|
|
default: |
|
|
|
return fs.ErrInvalid |
|
|
|
return ErrInvalidRing(ring) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error { |
|
|
|
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring rings.RingID) error { |
|
|
|
r, err := NewRing(z, m, ring) |
|
|
|
r, err := NewRing(z, m, ring) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
@ -203,27 +210,27 @@ func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error { |
|
|
|
|
|
|
|
|
|
|
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
|
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
|
|
// ring
|
|
|
|
// ring
|
|
|
|
func (m *Machine) SyncWireguardConfig(ring int) error { |
|
|
|
func (m *Machine) SyncWireguardConfig(ring rings.RingID) error { |
|
|
|
return m.zone.SyncWireguardConfig(ring) |
|
|
|
return m.zone.SyncWireguardConfig(ring) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// A WireguardKeysWriter writes the Wireguard Keys for all machines
|
|
|
|
// A WireguardKeysWriter writes the Wireguard Keys for all machines
|
|
|
|
// under its scope for the specified ring
|
|
|
|
// under its scope for the specified ring
|
|
|
|
type WireguardKeysWriter interface { |
|
|
|
type WireguardKeysWriter interface { |
|
|
|
WriteWireguardKeys(ring int) error |
|
|
|
WriteWireguardKeys(ring rings.RingID) error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
|
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
|
|
func (m *Cluster) WriteWireguardKeys(ring int) error { |
|
|
|
func (m *Cluster) WriteWireguardKeys(ring rings.RingID) error { |
|
|
|
return writeWireguardKeys(m, ring) |
|
|
|
return writeWireguardKeys(m, ring) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
|
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
|
|
func (z *Zone) WriteWireguardKeys(ring int) error { |
|
|
|
func (z *Zone) WriteWireguardKeys(ring rings.RingID) error { |
|
|
|
return writeWireguardKeys(z, ring) |
|
|
|
return writeWireguardKeys(z, ring) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func writeWireguardKeys(m MachineIterator, ring int) error { |
|
|
|
func writeWireguardKeys(m MachineIterator, ring rings.RingID) error { |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
m.ForEachMachine(func(p *Machine) bool { |
|
|
|
m.ForEachMachine(func(p *Machine) bool { |
|
|
@ -240,12 +247,12 @@ func writeWireguardKeys(m MachineIterator, ring int) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
|
|
|
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
|
|
|
func (m *Machine) WriteWireguardKeys(ring int) error { |
|
|
|
func (m *Machine) WriteWireguardKeys(ringID rings.RingID) error { |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
var key, pub string |
|
|
|
var key, pub string |
|
|
|
var ri *RingInfo |
|
|
|
var ri *RingInfo |
|
|
|
|
|
|
|
|
|
|
|
ri, _ = m.getRingInfo(ring) |
|
|
|
ri, _ = m.getRingInfo(ringID) |
|
|
|
if ri != nil { |
|
|
|
if ri != nil { |
|
|
|
key = ri.Keys.PrivateKey.String() |
|
|
|
key = ri.Keys.PrivateKey.String() |
|
|
|
pub = ri.Keys.PublicKey.String() |
|
|
|
pub = ri.Keys.PublicKey.String() |
|
|
@ -258,12 +265,13 @@ func (m *Machine) WriteWireguardKeys(ring int) error { |
|
|
|
pub = ri.Keys.PrivateKey.Public().String() |
|
|
|
pub = ri.Keys.PrivateKey.Public().String() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = m.WriteStringFile(key+"\n", "wg%v.key", ring) |
|
|
|
keyFile, pubFile, _ := ri.Ring.Files() |
|
|
|
|
|
|
|
err = m.WriteStringFile(key+"\n", keyFile) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring) |
|
|
|
err = m.WriteStringFile(pub+"\n", pubFile) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|