Compare commits
2 Commits
v0.2.0
..
29dfde5e46
| Author | SHA1 | Date | |
|---|---|---|---|
| 29dfde5e46 | |||
| 93aac4c04c |
@@ -31,13 +31,13 @@ func (f *Config) Peers() int {
|
|||||||
// InterfaceConfig represents the [Interface] section
|
// InterfaceConfig represents the [Interface] section
|
||||||
type InterfaceConfig struct {
|
type InterfaceConfig struct {
|
||||||
Address netip.Addr
|
Address netip.Addr
|
||||||
PrivateKey PrivateKey
|
PrivateKey BinaryKey
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// PeerConfig represents a [Peer] section
|
// PeerConfig represents a [Peer] section
|
||||||
type PeerConfig struct {
|
type PeerConfig struct {
|
||||||
PublicKey PublicKey
|
PublicKey BinaryKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix
|
AllowedIPs []netip.Prefix
|
||||||
}
|
}
|
||||||
@@ -135,7 +135,7 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
|||||||
ListenPort: p.ListenPort,
|
ListenPort: p.ListenPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PrivateKey")
|
err = core.Wrap(err, "PrivateKey")
|
||||||
return InterfaceConfig{}, err
|
return InterfaceConfig{}, err
|
||||||
@@ -162,7 +162,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PublicKey
|
// PublicKey
|
||||||
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i])
|
out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PublicKey")
|
err = core.Wrap(err, "PublicKey")
|
||||||
return out, err
|
return out, err
|
||||||
|
|||||||
+15
-75
@@ -3,94 +3,34 @@ package wireguard
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// BinaryKey is a binary blob
|
||||||
// PrivateKeySize is the length in bytes of a Wireguard Private Key
|
type BinaryKey []byte
|
||||||
PrivateKeySize = 32
|
|
||||||
// PublicKeySize is the length in bytes of a Wireguard Public Key
|
|
||||||
PublicKeySize = 32
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
func (k BinaryKey) String() string {
|
||||||
// ErrInvalidKeySize indicates the key size is wrong
|
return base64.StdEncoding.EncodeToString(k)
|
||||||
ErrInvalidKeySize = errors.New("invalid key size")
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
// PrivateKey is a binary Wireguard Private Key
|
|
||||||
PrivateKey []byte
|
|
||||||
// PublicKey is a binary Wireguard Public Key
|
|
||||||
PublicKey []byte
|
|
||||||
)
|
|
||||||
|
|
||||||
func (key PrivateKey) String() string {
|
|
||||||
return encodeKey(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pub PublicKey) String() string {
|
|
||||||
return encodeKey(pub)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (key PrivateKey) IsZero() bool {
|
func (k BinaryKey) IsZero() bool {
|
||||||
return len(key) == 0
|
return len(k) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// Equal checks if two keys are identical
|
||||||
func (pub PublicKey) IsZero() bool {
|
func (k BinaryKey) Equal(alter BinaryKey) bool {
|
||||||
return len(pub) == 0
|
return bytes.Equal(k, alter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equal checks if two private keys are identical
|
// BinaryKeyFromBase64 decodes a base64-based string into
|
||||||
func (key PrivateKey) Equal(alter PrivateKey) bool {
|
// a [BinaryKey]
|
||||||
return bytes.Equal(key, alter)
|
func BinaryKeyFromBase64(data string) (BinaryKey, error) {
|
||||||
}
|
|
||||||
|
|
||||||
// Equal checks if two public keys are identical
|
|
||||||
func (pub PublicKey) Equal(alter PublicKey) bool {
|
|
||||||
return bytes.Equal(pub, alter)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrivateKeyFromBase64 decodes a base64-based string into
|
|
||||||
// a [PrivateKey]
|
|
||||||
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
|
|
||||||
b, err := decodeKey(data, PrivateKeySize)
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// PublicKeyFromBase64 decodes a base64-based string into
|
|
||||||
// a [PublicKey]
|
|
||||||
func PublicKeyFromBase64(data string) (PublicKey, error) {
|
|
||||||
b, err := decodeKey(data, PublicKeySize)
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeKey(b []byte) string {
|
|
||||||
switch {
|
|
||||||
case len(b) == 0:
|
|
||||||
return ""
|
|
||||||
default:
|
|
||||||
return base64.StdEncoding.EncodeToString(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeKey(data string, size int) ([]byte, error) {
|
|
||||||
b, err := base64.StdEncoding.DecodeString(data)
|
b, err := base64.StdEncoding.DecodeString(data)
|
||||||
switch {
|
return BinaryKey(b), err
|
||||||
case err != nil:
|
|
||||||
return []byte{}, err
|
|
||||||
case len(b) != size:
|
|
||||||
err = ErrInvalidKeySize
|
|
||||||
return []byte{}, err
|
|
||||||
default:
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyPair holds a Key pair
|
// KeyPair holds a Key pair
|
||||||
type KeyPair struct {
|
type KeyPair struct {
|
||||||
PrivateKey PrivateKey
|
PrivateKey BinaryKey
|
||||||
PublicKey PublicKey
|
PublicKey BinaryKey
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-99
@@ -1,104 +1,9 @@
|
|||||||
|
// Package zones abstracts the cluster zones
|
||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import "io"
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// WriteEnv generates environment variables for shell scripts
|
// WriteEnv generates environment variables for shell scripts
|
||||||
func (m *Zones) WriteEnv(w io.Writer) error {
|
func (*Zones) WriteEnv(io.Writer) error {
|
||||||
var buf bytes.Buffer
|
return nil
|
||||||
|
|
||||||
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
m.writeEnvZone(&buf, z)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
_, err := buf.WriteTo(w)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
|
|
||||||
zoneID := z.ID
|
|
||||||
|
|
||||||
// ZONE{zoneID}
|
|
||||||
m.writeEnvVar(w, genEnvZoneNodes(z), "ZONE%v", zoneID)
|
|
||||||
|
|
||||||
// ZONE{zoneID}_NAME
|
|
||||||
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
|
|
||||||
|
|
||||||
// ZONE{zoneID}_GW
|
|
||||||
gatewayID := getRingZeroGatewayID(z)
|
|
||||||
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
|
||||||
|
|
||||||
// ZONE{zoneID}_IP
|
|
||||||
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) {
|
|
||||||
var value string
|
|
||||||
|
|
||||||
if fn != nil {
|
|
||||||
value = fn(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.writeEnvVar(w, value, name, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*Zones) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
|
||||||
if len(args) > 0 {
|
|
||||||
name = fmt.Sprintf(name, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if name != "" {
|
|
||||||
value = strings.TrimSpace(value)
|
|
||||||
|
|
||||||
_, _ = fmt.Fprintf(w, "%s=%q\n", name, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func genEnvZones(m *Zones) string {
|
|
||||||
s := make([]string, 0, len(m.Zones))
|
|
||||||
for _, z := range m.Zones {
|
|
||||||
s = append(s, fmt.Sprintf("%v", z.ID))
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
return strings.Join(s, " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRingZeroGatewayID(z *Zone) int {
|
|
||||||
var firstNodeID, gatewayID int
|
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
|
||||||
nodeID := p.ID()
|
|
||||||
|
|
||||||
if firstNodeID == 0 {
|
|
||||||
firstNodeID = nodeID
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, found := p.getRingInfo(0); found {
|
|
||||||
gatewayID = nodeID
|
|
||||||
}
|
|
||||||
|
|
||||||
return gatewayID != 0
|
|
||||||
})
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case gatewayID == 0:
|
|
||||||
return firstNodeID
|
|
||||||
default:
|
|
||||||
return gatewayID
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,7 +82,3 @@ func (m *Machine) getFilename(name string, args ...any) string {
|
|||||||
|
|
||||||
return filepath.Join(s...)
|
return filepath.Join(s...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
|
||||||
return m.zone.zones.GetMachineByName(name)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -45,79 +45,9 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
|
||||||
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, peer := range wg.Peer {
|
|
||||||
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
|
|
||||||
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
|
||||||
for _, ri := range m.RingAddresses {
|
|
||||||
if ri.Ring == ring {
|
|
||||||
return ri, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
|
||||||
cur, found := m.getRingInfo(ring)
|
|
||||||
if !found {
|
|
||||||
// first, append
|
|
||||||
m.RingAddresses = append(m.RingAddresses, new)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// extra, merge
|
|
||||||
return cur.Merge(new)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.InterfaceConfig) error {
|
|
||||||
ri := &RingInfo{
|
|
||||||
Ring: ring,
|
|
||||||
Enabled: true,
|
|
||||||
Address: data.Address,
|
|
||||||
Keys: &wireguard.KeyPair{
|
|
||||||
PrivateKey: data.PrivateKey,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return m.applyRingInfo(ring, ri)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) error {
|
|
||||||
peer, found := m.getPeerByName(pc.Endpoint.Name())
|
|
||||||
switch {
|
|
||||||
case !found:
|
|
||||||
// unknown
|
|
||||||
case ring == 1 && m.zone != peer.zone:
|
|
||||||
// invalid zone
|
|
||||||
default:
|
|
||||||
// apply RingInfo
|
|
||||||
ri := &RingInfo{
|
|
||||||
Ring: ring,
|
|
||||||
Enabled: true,
|
|
||||||
Keys: &wireguard.KeyPair{
|
|
||||||
PublicKey: pc.PublicKey,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return peer.applyRingInfo(ring, ri)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("%q: invalid peer endpoint", pc.Endpoint.Host)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||||
switch {
|
switch {
|
||||||
case zoneID == 0:
|
case zoneID == 0:
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
@@ -25,73 +24,6 @@ type RingInfo struct {
|
|||||||
Address netip.Addr `toml:"address,omitempty"`
|
Address netip.Addr `toml:"address,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge attempts to combine two RingInfo structs
|
|
||||||
func (ri *RingInfo) Merge(alter *RingInfo) error {
|
|
||||||
switch {
|
|
||||||
case ri.Ring != alter.Ring:
|
|
||||||
// different ring
|
|
||||||
return fmt.Errorf("invalid %s: %v ≠ %v", "ring", ri.Ring, alter.Ring)
|
|
||||||
case ri.Enabled != alter.Enabled:
|
|
||||||
// different state
|
|
||||||
return fmt.Errorf("invalid %s: %v ≠ %v", "enabled", ri.Enabled, alter.Enabled)
|
|
||||||
case !canMergeAddress(ri.Address, alter.Address):
|
|
||||||
// different address
|
|
||||||
return fmt.Errorf("invalid %s: %v ≠ %v", "address", ri.Address, alter.Address)
|
|
||||||
case !canMergeKeyPairs(ri.Keys, alter.Keys):
|
|
||||||
// incompatible keypairs
|
|
||||||
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case ri.Keys == nil:
|
|
||||||
// assign keypair
|
|
||||||
ri.Keys = alter.Keys
|
|
||||||
case alter.Keys != nil:
|
|
||||||
// fill the gaps on our keypair
|
|
||||||
if ri.Keys.PrivateKey.IsZero() {
|
|
||||||
ri.Keys.PrivateKey = alter.Keys.PrivateKey
|
|
||||||
}
|
|
||||||
if ri.Keys.PublicKey.IsZero() {
|
|
||||||
ri.Keys.PublicKey = alter.Keys.PublicKey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if addressEqual(ri.Address, netip.Addr{}) {
|
|
||||||
// assign address
|
|
||||||
ri.Address = alter.Address
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func canMergeAddress(ip1, ip2 netip.Addr) bool {
|
|
||||||
var zero netip.Addr
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case addressEqual(ip1, zero) || addressEqual(ip2, zero) || addressEqual(ip1, ip2):
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func addressEqual(ip1, ip2 netip.Addr) bool {
|
|
||||||
return ip1.Compare(ip2) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func canMergeKeyPairs(p1, p2 *wireguard.KeyPair) bool {
|
|
||||||
switch {
|
|
||||||
case p1 == nil || p2 == nil:
|
|
||||||
return true
|
|
||||||
case !p1.PrivateKey.IsZero() && !p2.PrivateKey.IsZero() && !p1.PrivateKey.Equal(p2.PrivateKey):
|
|
||||||
return false
|
|
||||||
case !p1.PublicKey.IsZero() && !p2.PublicKey.IsZero() && !p1.PublicKey.Equal(p2.PublicKey):
|
|
||||||
return false
|
|
||||||
default:
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RingAddressEncoder provides encoder/decoder access for a particular
|
// RingAddressEncoder provides encoder/decoder access for a particular
|
||||||
// Wireguard ring
|
// Wireguard ring
|
||||||
type RingAddressEncoder struct {
|
type RingAddressEncoder struct {
|
||||||
|
|||||||
+5
-81
@@ -2,25 +2,9 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"sort"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *Zones) scan() error {
|
func (m *Zones) scan() error {
|
||||||
for _, fn := range []func() error{
|
|
||||||
m.scanDirectory,
|
|
||||||
m.scanMachines,
|
|
||||||
m.scanZoneIDs,
|
|
||||||
m.scanSort,
|
|
||||||
} {
|
|
||||||
if err := fn(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Zones) scanDirectory() error {
|
|
||||||
// each directory is a zone
|
// each directory is a zone
|
||||||
entries, err := fs.ReadDir(m.dir, ".")
|
entries, err := fs.ReadDir(m.dir, ".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -42,79 +26,19 @@ func (m *Zones) scanDirectory() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return m.scanMachines()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanMachines() error {
|
func (m *Zones) scanMachines() error {
|
||||||
var err error
|
var err error
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
m.ForEachMachine(func(p *Machine) {
|
||||||
err = p.scan()
|
if err == nil {
|
||||||
return err != nil
|
err = p.scan()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanZoneIDs() error {
|
|
||||||
var hasMissing bool
|
|
||||||
var lastZoneID int
|
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
switch {
|
|
||||||
case z.ID == 0:
|
|
||||||
hasMissing = true
|
|
||||||
case z.ID > lastZoneID:
|
|
||||||
lastZoneID = z.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if hasMissing {
|
|
||||||
next := lastZoneID + 1
|
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
if z.ID == 0 {
|
|
||||||
z.ID, next = next, next+1
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Zones) scanSort() error {
|
|
||||||
sort.SliceStable(m.Zones, func(i, j int) bool {
|
|
||||||
id1 := m.Zones[i].ID
|
|
||||||
id2 := m.Zones[j].ID
|
|
||||||
return id1 < id2
|
|
||||||
})
|
|
||||||
|
|
||||||
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
|
|
||||||
})
|
|
||||||
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
sort.SliceStable(p.RingAddresses, func(i, j int) bool {
|
|
||||||
ri1 := p.RingAddresses[i]
|
|
||||||
ri2 := p.RingAddresses[j]
|
|
||||||
|
|
||||||
return ri1.Ring < ri2.Ring
|
|
||||||
})
|
|
||||||
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (z *Zone) scan() error {
|
func (z *Zone) scan() error {
|
||||||
// each directory is a machine
|
// each directory is a machine
|
||||||
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
||||||
|
|||||||
+8
-46
@@ -22,16 +22,6 @@ 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
@@ -42,47 +32,19 @@ type Zones struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ForEachMachine calls a function for each Machine in the cluster
|
// ForEachMachine calls a function for each Machine in the cluster
|
||||||
// until instructed to terminate the loop
|
func (m *Zones) ForEachMachine(fn func(*Machine)) {
|
||||||
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
for _, z := range m.Zones {
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
for _, p := range z.Machines {
|
||||||
var term bool
|
fn(p)
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
|
||||||
term = fn(p)
|
|
||||||
return term
|
|
||||||
})
|
|
||||||
|
|
||||||
return term
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ForEachZone calls a function for each Zone in the cluster
|
|
||||||
// until instructed to terminate the loop
|
|
||||||
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
|
|
||||||
for _, p := range m.Zones {
|
|
||||||
if fn(p) {
|
|
||||||
// terminate
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMachineByName looks for a machine with the specified
|
// ForEachZone calls a function for each Zone in the cluster
|
||||||
// name on any zone
|
func (m *Zones) ForEachZone(fn func(*Zone)) {
|
||||||
func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
|
for _, p := range m.Zones {
|
||||||
var out *Machine
|
fn(p)
|
||||||
|
|
||||||
if name != "" {
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
if p.Name == name {
|
|
||||||
out = p
|
|
||||||
}
|
|
||||||
|
|
||||||
return out != nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return out, out != nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFS builds a [Zones] tree using the given directory
|
// NewFS builds a [Zones] tree using the given directory
|
||||||
|
|||||||
Reference in New Issue
Block a user