Compare commits
1 Commits
bd307db0d3
..
v0.4.3
| Author | SHA1 | Date | |
|---|---|---|---|
| 2207e4a4a4 |
+17
-14
@@ -10,21 +10,18 @@ import (
|
|||||||
// WriteEnv generates environment variables for shell scripts
|
// WriteEnv generates environment variables for shell scripts
|
||||||
func (m *Zones) WriteEnv(w io.Writer) error {
|
func (m *Zones) WriteEnv(w io.Writer) error {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var err error
|
|
||||||
|
|
||||||
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
err = m.writeEnvZone(&buf, z)
|
m.writeEnvZone(&buf, z)
|
||||||
return err != nil
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
if err == nil {
|
_, err := buf.WriteTo(w)
|
||||||
_, err = buf.WriteTo(w)
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) error {
|
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
|
||||||
zoneID := z.ID
|
zoneID := z.ID
|
||||||
|
|
||||||
// ZONE{zoneID}
|
// ZONE{zoneID}
|
||||||
@@ -38,11 +35,8 @@ func (m *Zones) writeEnvZone(w io.Writer, z *Zone) error {
|
|||||||
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
||||||
|
|
||||||
// ZONE{zoneID}_IP
|
// ZONE{zoneID}_IP
|
||||||
if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
|
ip, _ := RingZeroAddress(zoneID, gatewayID)
|
||||||
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
|
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
|
func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
|
||||||
@@ -85,9 +79,13 @@ func genEnvZoneNodes(z *Zone) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getRingZeroGatewayID(z *Zone) int {
|
func getRingZeroGatewayID(z *Zone) int {
|
||||||
var gatewayID int
|
var firstNodeID, gatewayID int
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if firstNodeID == 0 {
|
||||||
|
firstNodeID = p.ID
|
||||||
|
}
|
||||||
|
|
||||||
if p.IsGateway() {
|
if p.IsGateway() {
|
||||||
gatewayID = p.ID
|
gatewayID = p.ID
|
||||||
}
|
}
|
||||||
@@ -95,5 +93,10 @@ func getRingZeroGatewayID(z *Zone) int {
|
|||||||
return gatewayID != 0
|
return gatewayID != 0
|
||||||
})
|
})
|
||||||
|
|
||||||
return gatewayID
|
switch {
|
||||||
|
case gatewayID == 0:
|
||||||
|
return firstNodeID
|
||||||
|
default:
|
||||||
|
return gatewayID
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,10 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Machine is a machine on a Zone
|
// A Machine is a machine on a Zone
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
mu sync.Mutex
|
|
||||||
zone *Zone
|
zone *Zone
|
||||||
ID int
|
ID int
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
@@ -41,27 +39,6 @@ func (m *Machine) IsGateway() bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetGateway enables/disables a Machine ring0 integration
|
|
||||||
func (m *Machine) SetGateway(enabled bool) error {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
|
|
||||||
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) {
|
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
||||||
return m.zone.zones.GetMachineByName(name)
|
return m.zone.zones.GetMachineByName(name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -261,57 +261,3 @@ func (m *Machine) RemoveWireguardConfig(ring int) error {
|
|||||||
|
|
||||||
return err
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
+7
-26
@@ -3,6 +3,7 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/hack-pad/hackpadfs/os"
|
"github.com/hack-pad/hackpadfs/os"
|
||||||
|
|
||||||
@@ -33,31 +34,6 @@ 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
|
// Zones represents all zones in a cluster
|
||||||
type Zones struct {
|
type Zones struct {
|
||||||
dir fs.FS
|
dir fs.FS
|
||||||
@@ -130,7 +106,12 @@ func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
|||||||
|
|
||||||
// New builds a [Zones] tree using the given directory
|
// New builds a [Zones] tree using the given directory
|
||||||
func New(dir, domain string) (*Zones, error) {
|
func New(dir, domain string) (*Zones, error) {
|
||||||
base, err := os.NewFS().Sub(dir)
|
dir, err := filepath.Abs(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
base, err := os.NewFS().Sub(dir[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user