Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fb82a7f358 | |||
| 9da2f8711f |
+6
-1
@@ -93,7 +93,12 @@ func (m *Zones) scanSort() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
sort.Sort(z)
|
sort.SliceStable(z.Machines, func(i, j int) bool {
|
||||||
|
id1 := z.Machines[i].ID
|
||||||
|
id2 := z.Machines[j].ID
|
||||||
|
return id1 < id2
|
||||||
|
})
|
||||||
|
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func pruneWireguardConfig(m MachineIterator, ring int) error {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
err = p.zone.PruneWireguardConfig(ring)
|
err = p.PruneWireguardConfig(ring)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
// ignore
|
// ignore
|
||||||
err = nil
|
err = nil
|
||||||
|
|||||||
+11
-74
@@ -4,7 +4,6 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
|
||||||
|
|
||||||
"github.com/hack-pad/hackpadfs/os"
|
"github.com/hack-pad/hackpadfs/os"
|
||||||
|
|
||||||
@@ -12,9 +11,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ MachineIterator = Machines(nil)
|
|
||||||
_ sort.Interface = Machines(nil)
|
|
||||||
|
|
||||||
_ MachineIterator = (*Zone)(nil)
|
_ MachineIterator = (*Zone)(nil)
|
||||||
_ MachineIterator = (*Zones)(nil)
|
_ MachineIterator = (*Zones)(nil)
|
||||||
_ ZoneIterator = (*Zones)(nil)
|
_ ZoneIterator = (*Zones)(nil)
|
||||||
@@ -30,62 +26,6 @@ type ZoneIterator interface {
|
|||||||
ForEachZone(func(*Zone) bool)
|
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
|
// Zone represents one zone in a cluster
|
||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Zones
|
zones *Zones
|
||||||
@@ -93,13 +33,23 @@ type Zone struct {
|
|||||||
ID int `toml:"id"`
|
ID int `toml:"id"`
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
|
|
||||||
Machines `toml:"machines"`
|
Machines []*Machine `toml:"machines"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zone) String() string {
|
func (z *Zone) String() string {
|
||||||
return z.Name
|
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
|
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||||
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||||
var err error
|
var err error
|
||||||
@@ -125,19 +75,6 @@ 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
|
// Zones represents all zones in a cluster
|
||||||
type Zones struct {
|
type Zones struct {
|
||||||
dir fs.FS
|
dir fs.FS
|
||||||
|
|||||||
Reference in New Issue
Block a user