Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a03404a07 | |||
| d2f0a0744b | |||
| 71a1d1a7c2 | |||
| de45fa6c30 | |||
| 6e46d23b45 | |||
| 94daf5ad59 | |||
| 0989dec5e8 | |||
| 216bf5aa29 | |||
| 9af88f6593 | |||
| af2d836000 | |||
| 1655ce85bc | |||
| f63ce6c4e7 | |||
| 1885c76198 | |||
| 2224e70638 |
@@ -14,6 +14,8 @@ var cfg = &Config{
|
||||
}
|
||||
|
||||
// LoadZones loads all zones and machines in the config directory
|
||||
func (cfg *Config) LoadZones() (*zones.Zones, error) {
|
||||
return zones.New(cfg.Base, cfg.Domain)
|
||||
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
|
||||
return zones.New(cfg.Base, cfg.Domain,
|
||||
zones.ResolvePublicAddresses(resolve),
|
||||
)
|
||||
}
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ var dumpCmd = &cobra.Command{
|
||||
var buf bytes.Buffer
|
||||
var enc Encoder
|
||||
|
||||
m, err := cfg.LoadZones()
|
||||
m, err := cfg.LoadZones(true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ var envCmd = &cobra.Command{
|
||||
Use: "env",
|
||||
Short: "generates environment variables for shell scripts",
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := cfg.LoadZones()
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ var writeCmd = &cobra.Command{
|
||||
Use: "write",
|
||||
Short: "rewrites all config files",
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := cfg.LoadZones()
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+39
-44
@@ -22,11 +22,23 @@ func (m *Zones) Env(export bool) *Env {
|
||||
}
|
||||
}
|
||||
|
||||
// Zones returns the list of Zone IDs
|
||||
func (m *Env) Zones() []int {
|
||||
var zones []int
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
zones = append(zones, z.ID)
|
||||
return false
|
||||
})
|
||||
|
||||
return zones
|
||||
}
|
||||
|
||||
// WriteTo generates environment variables for shell scripts
|
||||
func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
||||
m.writeEnvVarInts(&buf, m.Zones(), "ZONES")
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
m.writeEnvZone(&buf, z)
|
||||
return false
|
||||
@@ -45,25 +57,27 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
||||
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
|
||||
|
||||
// ZONE{zoneID}_GW
|
||||
gatewayID := getRingZeroGatewayID(z)
|
||||
if gatewayID > 0 {
|
||||
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")
|
||||
}
|
||||
}
|
||||
gateways, _ := z.GatewayIDs()
|
||||
m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW")
|
||||
}
|
||||
|
||||
func (m *Env) writeEnvVarFn(w io.Writer, fn func(*Env) string, name string, args ...any) {
|
||||
var value string
|
||||
func (m *Env) writeEnvVarInts(w io.Writer, value []int, name string, args ...any) {
|
||||
var s string
|
||||
|
||||
if fn != nil {
|
||||
value = fn(m)
|
||||
if n := len(value); n > 0 {
|
||||
var buf bytes.Buffer
|
||||
|
||||
for i, v := range value {
|
||||
if i != 0 {
|
||||
_, _ = fmt.Fprint(&buf, " ")
|
||||
}
|
||||
_, _ = fmt.Fprintf(&buf, "%v", v)
|
||||
}
|
||||
|
||||
s = buf.String()
|
||||
}
|
||||
|
||||
m.writeEnvVar(w, value, name, args...)
|
||||
m.writeEnvVar(w, s, name, args...)
|
||||
}
|
||||
|
||||
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
||||
@@ -84,35 +98,16 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
||||
}
|
||||
}
|
||||
|
||||
func genEnvZones(m *Env) string {
|
||||
var s []string
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
s = append(s, fmt.Sprintf("%v", z.ID))
|
||||
return false
|
||||
})
|
||||
|
||||
return strings.Join(s, " ")
|
||||
}
|
||||
|
||||
func genEnvZoneNodes(z *Zone) string {
|
||||
s := make([]string, 0, len(z.Machines))
|
||||
for _, p := range z.Machines {
|
||||
s = append(s, p.Name)
|
||||
if n := z.Len(); n > 0 {
|
||||
s := make([]string, 0, n)
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
s = append(s, p.Name)
|
||||
return false
|
||||
})
|
||||
|
||||
return strings.Join(s, " ")
|
||||
}
|
||||
return strings.Join(s, " ")
|
||||
}
|
||||
|
||||
func getRingZeroGatewayID(z *Zone) int {
|
||||
var gatewayID int
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
if p.IsGateway() {
|
||||
gatewayID = p.ID
|
||||
}
|
||||
|
||||
return gatewayID != 0
|
||||
})
|
||||
|
||||
return gatewayID
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
|
||||
timeout := 2 * time.Second
|
||||
// LookupNetIP uses the DNS Resolver to get the public addresses associated
|
||||
// to a Machine
|
||||
func (m *Machine) LookupNetIP(timeout time.Duration) ([]netip.Addr, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
|
||||
defer cancel()
|
||||
@@ -16,8 +17,9 @@ func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
|
||||
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
|
||||
}
|
||||
|
||||
func (m *Machine) updatePublicAddresses() error {
|
||||
addrs, err := m.lookupNetIP()
|
||||
// UpdatePublicAddresses uses the DNS Resolver to set Machine.PublicAddresses
|
||||
func (m *Machine) UpdatePublicAddresses() error {
|
||||
addrs, err := m.LookupNetIP(2 * time.Second)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -52,12 +54,16 @@ func (m *Machine) setID() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) scan() error {
|
||||
func (m *Machine) scan(opts *ScanOptions) error {
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return m.updatePublicAddresses()
|
||||
if !opts.DontResolvePublicAddresses {
|
||||
return m.UpdatePublicAddresses()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
)
|
||||
|
||||
// A ScanOption preconfigures the Zones before scanning
|
||||
type ScanOption func(*Zones, *ScanOptions) error
|
||||
|
||||
// ScanOptions contains flags used by the initial scan
|
||||
type ScanOptions struct {
|
||||
// DontResolvePublicAddresses indicates we shouldn't
|
||||
// pre-populate Machine.PublicAddresses during the
|
||||
// initial scan
|
||||
DontResolvePublicAddresses bool
|
||||
}
|
||||
|
||||
// ResolvePublicAddresses instructs the scanner to use
|
||||
// the DNS resolver to get PublicAddresses of nodes.
|
||||
// Default is true
|
||||
func ResolvePublicAddresses(resolve bool) ScanOption {
|
||||
return func(m *Zones, opt *ScanOptions) error {
|
||||
opt.DontResolvePublicAddresses = !resolve
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithLookuper specifies what resolver.Lookuper to use to
|
||||
// find public addresses
|
||||
func WithLookuper(h resolver.Lookuper) ScanOption {
|
||||
return func(m *Zones, opt *ScanOptions) error {
|
||||
if h == nil {
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
m.resolver = resolver.NewResolver(h)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithResolver specifies what resolver to use to find
|
||||
// public addresses. if nil is passed, the [net.Resolver] will be used.
|
||||
// The default is using Cloudflare's 1.1.1.1.
|
||||
func WithResolver(h resolver.Resolver) ScanOption {
|
||||
return func(m *Zones, opt *ScanOptions) error {
|
||||
if h == nil {
|
||||
h = resolver.SystemResolver(true)
|
||||
}
|
||||
|
||||
m.resolver = h
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Zones) setDefaults(opt *ScanOptions) error {
|
||||
if m.resolver == nil {
|
||||
h := resolver.NewCloudflareLookuper()
|
||||
|
||||
if err := WithLookuper(h)(m, opt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewFS builds a [Zones] tree using the given directory
|
||||
func NewFS(dir fs.FS, domain string, opts ...ScanOption) (*Zones, error) {
|
||||
var scanOptions ScanOptions
|
||||
|
||||
z := &Zones{
|
||||
dir: dir,
|
||||
domain: domain,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if err := opt(z, &scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := z.setDefaults(&scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := z.scan(&scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return z, nil
|
||||
}
|
||||
|
||||
// New builds a [Zones] tree using the given directory
|
||||
func New(dir, domain string, opts ...ScanOption) (*Zones, error) {
|
||||
dir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base, err := os.NewFS().Sub(dir[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewFS(base, domain, opts...)
|
||||
}
|
||||
+10
-15
@@ -5,15 +5,15 @@ import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func (m *Zones) scan() error {
|
||||
for _, fn := range []func() error{
|
||||
func (m *Zones) scan(opts *ScanOptions) error {
|
||||
for _, fn := range []func(*ScanOptions) error{
|
||||
m.scanDirectory,
|
||||
m.scanMachines,
|
||||
m.scanZoneIDs,
|
||||
m.scanSort,
|
||||
m.scanGateways,
|
||||
} {
|
||||
if err := fn(); err != nil {
|
||||
if err := fn(opts); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ func (m *Zones) scan() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanDirectory() error {
|
||||
func (m *Zones) scanDirectory(_ *ScanOptions) error {
|
||||
// each directory is a zone
|
||||
entries, err := fs.ReadDir(m.dir, ".")
|
||||
if err != nil {
|
||||
@@ -46,16 +46,16 @@ func (m *Zones) scanDirectory() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanMachines() error {
|
||||
func (m *Zones) scanMachines(opts *ScanOptions) error {
|
||||
var err error
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.scan()
|
||||
err = p.scan(opts)
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Zones) scanZoneIDs() error {
|
||||
func (m *Zones) scanZoneIDs(_ *ScanOptions) error {
|
||||
var hasMissing bool
|
||||
var lastZoneID int
|
||||
|
||||
@@ -85,7 +85,7 @@ func (m *Zones) scanZoneIDs() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanSort() error {
|
||||
func (m *Zones) scanSort(_ *ScanOptions) error {
|
||||
sort.SliceStable(m.Zones, func(i, j int) bool {
|
||||
id1 := m.Zones[i].ID
|
||||
id2 := m.Zones[j].ID
|
||||
@@ -93,12 +93,7 @@ func (m *Zones) scanSort() error {
|
||||
})
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
sort.SliceStable(z.Machines, func(i, j int) bool {
|
||||
id1 := z.Machines[i].ID
|
||||
id2 := z.Machines[j].ID
|
||||
return id1 < id2
|
||||
})
|
||||
|
||||
sort.Sort(z)
|
||||
return false
|
||||
})
|
||||
|
||||
@@ -116,7 +111,7 @@ func (m *Zones) scanSort() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanGateways() error {
|
||||
func (m *Zones) scanGateways(_ *ScanOptions) error {
|
||||
var err error
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
|
||||
+74
-46
@@ -3,14 +3,15 @@ package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
"sort"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
)
|
||||
|
||||
var (
|
||||
_ MachineIterator = Machines(nil)
|
||||
_ sort.Interface = Machines(nil)
|
||||
|
||||
_ MachineIterator = (*Zone)(nil)
|
||||
_ MachineIterator = (*Zones)(nil)
|
||||
_ ZoneIterator = (*Zones)(nil)
|
||||
@@ -26,6 +27,62 @@ type ZoneIterator interface {
|
||||
ForEachZone(func(*Zone) bool)
|
||||
}
|
||||
|
||||
// Machines is a list of Machine objects
|
||||
type Machines []*Machine
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the list
|
||||
// until instructed to terminate the loop
|
||||
func (m Machines) ForEachMachine(fn func(*Machine) bool) {
|
||||
for _, p := range m {
|
||||
if fn(p) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Len returns the number of machines in the list
|
||||
func (m Machines) Len() int {
|
||||
return len(m)
|
||||
}
|
||||
|
||||
// Less implements sort.Interface to sort the list
|
||||
func (m Machines) Less(i, j int) bool {
|
||||
a, b := m[i], m[j]
|
||||
za, zb := a.Zone(), b.Zone()
|
||||
|
||||
switch {
|
||||
case za == zb:
|
||||
return a.ID < b.ID
|
||||
default:
|
||||
return za < zb
|
||||
}
|
||||
}
|
||||
|
||||
// Swap implements sort.Interface to sort the list
|
||||
func (m Machines) Swap(i, j int) {
|
||||
m[i], m[j] = m[j], m[i]
|
||||
}
|
||||
|
||||
// FilterMachines produces a subset of the machines offered by the given
|
||||
// iterator fulfilling a condition
|
||||
func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) {
|
||||
var out []*Machine
|
||||
|
||||
if cond == nil {
|
||||
// unconditional
|
||||
cond = func(*Machine) bool { return true }
|
||||
}
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
if cond(p) {
|
||||
out = append(out, p)
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return out, len(out)
|
||||
}
|
||||
|
||||
// Zone represents one zone in a cluster
|
||||
type Zone struct {
|
||||
zones *Zones
|
||||
@@ -33,23 +90,13 @@ type Zone struct {
|
||||
ID int `toml:"id"`
|
||||
Name string `toml:"name"`
|
||||
|
||||
Machines []*Machine `toml:"machines"`
|
||||
Machines `toml:"machines"`
|
||||
}
|
||||
|
||||
func (z *Zone) String() string {
|
||||
return z.Name
|
||||
}
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the zone
|
||||
// until instructed to terminate the loop
|
||||
func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
|
||||
for _, p := range z.Machines {
|
||||
if fn(p) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||
var err error
|
||||
@@ -75,6 +122,19 @@ func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
// GatewayIDs returns the list of IDs of machines that act as ring0 gateways
|
||||
func (z *Zone) GatewayIDs() ([]int, int) {
|
||||
var out []int
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
if p.IsGateway() {
|
||||
out = append(out, p.ID)
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return out, len(out)
|
||||
}
|
||||
|
||||
// Zones represents all zones in a cluster
|
||||
type Zones struct {
|
||||
dir fs.FS
|
||||
@@ -127,35 +187,3 @@ func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
|
||||
|
||||
return out, out != nil
|
||||
}
|
||||
|
||||
// NewFS builds a [Zones] tree using the given directory
|
||||
func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
||||
lockuper := resolver.NewCloudflareLookuper()
|
||||
|
||||
z := &Zones{
|
||||
dir: dir,
|
||||
resolver: resolver.NewResolver(lockuper),
|
||||
domain: domain,
|
||||
}
|
||||
|
||||
if err := z.scan(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return z, nil
|
||||
}
|
||||
|
||||
// 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:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewFS(base, domain)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user