Compare commits

..

1 Commits

Author SHA1 Message Date
amery 2207e4a4a4 zones: fix New() to handle relative paths on hackpadfs
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:02:00 +00:00
4 changed files with 24 additions and 117 deletions
+17 -14
View File
@@ -10,21 +10,18 @@ import (
// WriteEnv generates environment variables for shell scripts
func (m *Zones) WriteEnv(w io.Writer) error {
var buf bytes.Buffer
var err error
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
m.ForEachZone(func(z *Zone) bool {
err = m.writeEnvZone(&buf, z)
return err != nil
m.writeEnvZone(&buf, z)
return false
})
if err == nil {
_, err = buf.WriteTo(w)
}
_, err := buf.WriteTo(w)
return err
}
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) error {
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
zoneID := z.ID
// 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")
// ZONE{zoneID}_IP
if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
}
return nil
ip, _ := RingZeroAddress(zoneID, gatewayID)
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
}
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 {
var gatewayID int
var firstNodeID, gatewayID int
z.ForEachMachine(func(p *Machine) bool {
if firstNodeID == 0 {
firstNodeID = p.ID
}
if p.IsGateway() {
gatewayID = p.ID
}
@@ -95,5 +93,10 @@ func getRingZeroGatewayID(z *Zone) int {
return gatewayID != 0
})
return gatewayID
switch {
case gatewayID == 0:
return firstNodeID
default:
return gatewayID
}
}
-23
View File
@@ -3,12 +3,10 @@ package zones
import (
"net/netip"
"strings"
"sync"
)
// A Machine is a machine on a Zone
type Machine struct {
mu sync.Mutex
zone *Zone
ID int
Name string `toml:"name"`
@@ -41,27 +39,6 @@ func (m *Machine) IsGateway() bool {
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) {
return m.zone.zones.GetMachineByName(name)
}
-54
View File
@@ -261,57 +261,3 @@ 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
}
+7 -26
View File
@@ -3,6 +3,7 @@ package zones
import (
"io/fs"
"path/filepath"
"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
type Zones struct {
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
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 {
return nil, err
}