Compare commits

..

1 Commits

Author SHA1 Message Date
amery bd307db0d3 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 15:45:30 +00:00
3 changed files with 19 additions and 17 deletions
+13 -10
View File
@@ -10,18 +10,21 @@ 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 {
m.writeEnvZone(&buf, z)
return false
err = m.writeEnvZone(&buf, z)
return err != nil
})
_, err := buf.WriteTo(w)
if err == nil {
_, err = buf.WriteTo(w)
}
return err
}
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) error {
zoneID := z.ID
// ZONE{zoneID}
@@ -32,14 +35,14 @@ func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
// ZONE{zoneID}_GW
gatewayID := getRingZeroGatewayID(z)
if gatewayID > 0 {
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
if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
}
// ZONE{zoneID}_IP
if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
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) {
+5
View File
@@ -3,10 +3,12 @@ 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,6 +43,9 @@ func (m *Machine) IsGateway() bool {
// 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:
+1 -7
View File
@@ -3,7 +3,6 @@ package zones
import (
"io/fs"
"path/filepath"
"github.com/hack-pad/hackpadfs/os"
@@ -131,12 +130,7 @@ 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) {
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
base, err := os.NewFS().Sub(dir[1:])
base, err := os.NewFS().Sub(dir)
if err != nil {
return nil, err
}