Compare commits

..

3 Commits

Author SHA1 Message Date
amery 6a2cbdffd0 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:04:01 +00:00
amery 81aafbc083 zones: change WriteEnv() to not fake gateways
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:03:25 +00:00
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
3 changed files with 17 additions and 19 deletions
+10 -13
View File
@@ -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}
@@ -35,14 +32,14 @@ func (m *Zones) writeEnvZone(w io.Writer, z *Zone) error {
// ZONE{zoneID}_GW // ZONE{zoneID}_GW
gatewayID := getRingZeroGatewayID(z) gatewayID := getRingZeroGatewayID(z)
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW") if gatewayID > 0 {
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 { if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
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) {
-5
View File
@@ -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"`
@@ -43,9 +41,6 @@ func (m *Machine) IsGateway() bool {
// SetGateway enables/disables a Machine ring0 integration // SetGateway enables/disables a Machine ring0 integration
func (m *Machine) SetGateway(enabled bool) error { func (m *Machine) SetGateway(enabled bool) error {
m.mu.Lock()
defer m.mu.Unlock()
ri, found := m.getRingInfo(0) ri, found := m.getRingInfo(0)
switch { switch {
case !found && !enabled: case !found && !enabled:
+7 -1
View File
@@ -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"
@@ -130,7 +131,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
} }