Compare commits

..

2 Commits

Author SHA1 Message Date
amery 634941a1ef wireguard: implement EndpointAddress.UnmarshalText
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-28 15:55:09 +00:00
amery 1da505fd3d wireguard: implement UnmarshalText for PrivateKey and PublicKey
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-28 15:54:33 +00:00
18 changed files with 139 additions and 535 deletions
+17 -40
View File
@@ -12,14 +12,6 @@ const (
// DefaultConfigFile is read if -f/--config-file isn't specified. // DefaultConfigFile is read if -f/--config-file isn't specified.
// If it doesn't exist, m/ will be scanned // If it doesn't exist, m/ will be scanned
DefaultConfigFile = "cloud.yaml" DefaultConfigFile = "cloud.yaml"
// DefaultClusterDir is the directory we will scan and write
// unless something else is indicated
DefaultClusterDir = "m"
// DefaultDomain indicates the domain to use unless
// something else is specified
DefaultDomain = "jpi.cloud"
) )
// Config describes the repository // Config describes the repository
@@ -30,34 +22,27 @@ type Config struct {
ConfigFile string ConfigFile string
} }
var forceScan bool
var cfg = &Config{ var cfg = &Config{
Base: DefaultClusterDir, Base: "m",
Domain: DefaultDomain, Domain: "jpi.cloud",
} }
// LoadZones loads all zones and machines in the config directory // LoadZones loads all zones and machines in the config directory
// or file // or file
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) { func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
var zones *cluster.Cluster // try config file first
var err error zones, err := cluster.NewFromConfig(cfg.ConfigFile,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
if !forceScan { switch {
// try config file first case err == nil:
zones, err = cluster.NewFromConfig(cfg.ConfigFile, // file was good
cluster.ResolvePublicAddresses(resolve), return zones, nil
cluster.WithLogger(log), case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
) // file was bad
return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
switch {
case err == nil:
// file was good
return zones, nil
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
// file was bad
return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
}
} }
// default file doesn't exist. scan instead. // default file doesn't exist. scan instead.
@@ -68,15 +53,7 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
} }
func init() { func init() {
flags := rootCmd.PersistentFlags() rootCmd.PersistentFlags().
StringVarP(&cfg.ConfigFile, "config-file", "f",
flags.StringVarP(&cfg.Base, "scan-dir", "d", DefaultConfigFile, "config file (JSON or YAML)")
DefaultClusterDir, "directory to scan for cluster data")
flags.StringVarP(&cfg.Domain, "domain", "D",
DefaultDomain, "domain to use for scanned data")
flags.StringVarP(&cfg.ConfigFile, "config-file", "f",
DefaultConfigFile, "config file (JSON or YAML)")
flags.BoolVarP(&forceScan, "force-scan", "S",
false, "ignore config file and scan the directory instead")
} }
+1 -1
View File
@@ -52,7 +52,7 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
m.ForEachZone(func(z *cluster.Zone) bool { m.ForEachZone(func(z *cluster.Zone) bool {
z.ForEachMachine(func(p *cluster.Machine) bool { z.ForEachMachine(func(p *cluster.Machine) bool {
err = mgr.AddHost(ctx, z.Name, p.ID, p.IsActive(), p.PublicAddresses...) err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...)
return err != nil return err != nil
}) })
-21
View File
@@ -3,13 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog" "darvaza.org/slog"
"github.com/spf13/cobra"
) )
var log = zerolog.New(nil, slog.Error)
// fatal is a convenience wrapper for slog.Logger.Fatal().Print() // fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) { func fatal(err error, msg string, args ...any) {
l := log.Fatal() l := log.Fatal()
@@ -23,20 +19,3 @@ func fatal(err error, msg string, args ...any) {
panic("unreachable") panic("unreachable")
} }
var verbosity int
// setVerbosity replaces the global logger using the
// verbosity level specified via -v flags
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
+18 -1
View File
@@ -2,6 +2,8 @@
package main package main
import ( import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -11,7 +13,9 @@ const (
) )
var ( var (
rootCmd = &cobra.Command{ log = zerolog.New(nil, slog.Error)
verbosity int
rootCmd = &cobra.Command{
Use: CmdName, Use: CmdName,
Short: "control tool for jpi.cloud", Short: "control tool for jpi.cloud",
} }
@@ -22,3 +26,16 @@ func main() {
fatal(err, "") fatal(err, "")
} }
} }
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
-65
View File
@@ -1,12 +1,9 @@
package cluster package cluster
import ( import (
"bufio"
"bytes"
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
fs "github.com/hack-pad/hackpadfs" fs "github.com/hack-pad/hackpadfs"
) )
@@ -43,21 +40,6 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose
panic("unreachable") panic("unreachable")
} }
// RemoveFile deletes a file from the cluster's config directory
func (m *Cluster) RemoveFile(name string, args ...any) error {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
err := fs.Remove(m.dir, name)
switch {
case os.IsNotExist(err):
return nil
default:
return err
}
}
// ReadFile reads a file from the cluster's config directory // ReadFile reads a file from the cluster's config directory
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) { func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
if len(args) > 0 { if len(args) > 0 {
@@ -66,50 +48,3 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
return fs.ReadFile(m.dir, name) return fs.ReadFile(m.dir, name)
} }
// ReadLines reads a file from the cluster's config directory,
// split by lines, trimmed, and accepting `#` to comment lines out.
func (m *Cluster) ReadLines(name string, args ...any) ([]string, error) {
var out []string
data, err := m.ReadFile(name, args...)
if err != nil {
return nil, err
}
sc := bufio.NewScanner(bytes.NewReader(data))
for sc.Scan() {
s := strings.TrimSpace(sc.Text())
switch {
case s == "", strings.HasPrefix(s, "#"):
// ignore
default:
// accepted
out = append(out, s)
}
}
return out, nil
}
// WriteStringFile writes the given content to a file on the machine's config directory
func (m *Cluster) WriteStringFile(value string, name string, args ...any) error {
f, err := m.CreateTruncFile(name, args...)
if err != nil {
return err
}
defer f.Close()
buf := bytes.NewBufferString(value)
_, err = buf.WriteTo(f)
return err
}
// MkdirAll creates directories relative to the cluster's config directory
func (m *Cluster) MkdirAll(name string, args ...any) error {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.MkdirAll(m.dir, name, 0755)
}
+26 -91
View File
@@ -2,25 +2,17 @@ package cluster
import ( import (
"io/fs" "io/fs"
"path"
"sort" "sort"
"darvaza.org/core" "darvaza.org/core"
) )
const (
// ZoneRegionsFileName indicates the file containing
// region names as references
ZoneRegionsFileName = "regions"
)
func (m *Cluster) scan(opts *ScanOptions) error { func (m *Cluster) scan(opts *ScanOptions) error {
for _, fn := range []func(*ScanOptions) error{ for _, fn := range []func(*ScanOptions) error{
m.scanDirectory, m.scanDirectory,
m.scanMachines, m.scanMachines,
m.scanZoneIDs, m.scanZoneIDs,
m.scanSort, m.scanSort,
m.initRegions,
m.scanGateways, m.scanGateways,
m.scanCephMonitors, m.scanCephMonitors,
} { } {
@@ -32,7 +24,7 @@ func (m *Cluster) scan(opts *ScanOptions) error {
return nil return nil
} }
func (m *Cluster) scanDirectory(opts *ScanOptions) error { func (m *Cluster) scanDirectory(_ *ScanOptions) 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 {
@@ -41,14 +33,16 @@ func (m *Cluster) scanDirectory(opts *ScanOptions) error {
for _, e := range entries { for _, e := range entries {
if e.IsDir() { if e.IsDir() {
ok, err := m.scanSubdirectory(opts, e.Name()) z, err := m.newZone(e.Name())
switch { switch {
case err != nil: case err != nil:
return core.Wrap(err, e.Name()) return core.Wrap(err, e.Name())
case !ok: case z.Machines.Len() == 0:
m.warn(nil). z.warn(nil).
WithField("zone", e.Name()). WithField("zone", z.Name).
Print("empty") Print("empty")
default:
m.Zones = append(m.Zones, z)
} }
} }
} }
@@ -56,27 +50,6 @@ func (m *Cluster) scanDirectory(opts *ScanOptions) error {
return nil return nil
} }
func (m *Cluster) scanSubdirectory(_ *ScanOptions, name string) (bool, error) {
z, err := m.newZone(name)
switch {
case err != nil:
// somewhere went wrong scanning the subdirectory
return false, err
case z.Machines.Len() > 0:
// zones have machines and the regions they belong
m.Zones = append(m.Zones, z)
return true, nil
case len(z.Regions) > 0:
// regions have no machines but can include
// other regions
m.appendRegionRegions(name, z.Regions...)
return true, nil
default:
// empty
return false, nil
}
}
func (m *Cluster) newZone(name string) (*Zone, error) { func (m *Cluster) newZone(name string) (*Zone, error) {
z := &Zone{ z := &Zone{
zones: m, zones: m,
@@ -100,10 +73,6 @@ func (m *Cluster) scanMachines(opts *ScanOptions) error {
err = p.scan(opts) err = p.scan(opts)
return err != nil return err != nil
}) })
m.ForEachMachine(func(p *Machine) bool {
err = p.scanWrapUp(opts)
return err != nil
})
return err return err
} }
@@ -181,68 +150,34 @@ func (z *Zone) scan() error {
} }
for _, e := range entries { for _, e := range entries {
name := e.Name() if e.IsDir() {
m := &Machine{
zone: z,
logger: z,
Name: e.Name(),
}
switch { m.debug().
case name == ZoneRegionsFileName: WithField("node", m.Name).
err = z.loadRegions()
case e.IsDir():
err = z.scanSubdirectory(name)
default:
z.warn(nil).
WithField("zone", z.Name). WithField("zone", z.Name).
WithField("filename", name). Print("found")
Print("unknown")
}
if err != nil { if err := m.init(); err != nil {
return err m.error(err).
WithField("node", m.Name).
WithField("zone", z.Name).
Print()
return err
}
z.Machines = append(z.Machines, m)
} }
} }
return nil return nil
} }
func (z *Zone) loadRegions() error {
filename := path.Join(z.Name, ZoneRegionsFileName)
regions, err := z.zones.ReadLines(filename)
if err == nil {
// parsed
err = z.appendRegions(regions...)
if err != nil {
err = core.Wrap(err, filename)
}
}
return err
}
func (z *Zone) scanSubdirectory(name string) error {
m := &Machine{
zone: z,
logger: z,
Name: name,
}
m.debug().
WithField("node", m.Name).
WithField("zone", z.Name).
Print("found")
if err := m.init(); err != nil {
m.error(err).
WithField("node", m.Name).
WithField("zone", z.Name).
Print()
return err
}
z.Machines = append(z.Machines, m)
return nil
}
// GetGateway returns the first gateway found, if none // GetGateway returns the first gateway found, if none
// files will be created to enable the first [Machine] to // files will be created to enable the first [Machine] to
// be one // be one
-6
View File
@@ -15,7 +15,6 @@ type Machine struct {
ID int ID int
Name string `json:"-" yaml:"-"` Name string `json:"-" yaml:"-"`
Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"`
CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"` CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"` PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"`
Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"` Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"`
@@ -44,11 +43,6 @@ func (m *Machine) FullName() string {
return strings.Join(name, ".") return strings.Join(name, ".")
} }
// IsActive indicates the machine is to be included in regions' DNS entries
func (m *Machine) IsActive() bool {
return !m.Inactive
}
// IsGateway tells if the Machine is a ring0 gateway // IsGateway tells if the Machine is a ring0 gateway
func (m *Machine) IsGateway() bool { func (m *Machine) IsGateway() bool {
_, ok := m.getRingInfo(0) _, ok := m.getRingInfo(0)
+21 -20
View File
@@ -1,6 +1,7 @@
package cluster package cluster
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"os" "os"
@@ -11,9 +12,10 @@ import (
// OpenFile opens a file on the machine's config directory with the specified flags // OpenFile opens a file on the machine's config directory with the specified flags
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) { func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...) fullName := m.getFilename(name, args...)
return m.zone.zones.OpenFile(fullName, flags) return fs.OpenFile(base, fullName, flags, 0644)
} }
// CreateTruncFile creates or truncates a file on the machine's config directory // CreateTruncFile creates or truncates a file on the machine's config directory
@@ -41,38 +43,37 @@ func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteClose
// RemoveFile deletes a file from the machine's config directory // RemoveFile deletes a file from the machine's config directory
func (m *Machine) RemoveFile(name string, args ...any) error { func (m *Machine) RemoveFile(name string, args ...any) error {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...) fullName := m.getFilename(name, args...)
err := fs.Remove(base, fullName)
return m.zone.zones.RemoveFile(fullName) switch {
case os.IsNotExist(err):
return nil
default:
return err
}
} }
// ReadFile reads a file from the machine's config directory // ReadFile reads a file from the machine's config directory
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) { func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...) fullName := m.getFilename(name, args...)
return m.zone.zones.ReadFile(fullName) return fs.ReadFile(base, fullName)
}
// ReadLines reads a file from the machine's config directory,
// split by lines, trimmed, and accepting `#` to comment lines out.
func (m *Machine) ReadLines(name string, args ...any) ([]string, error) {
fullName := m.getFilename(name, args...)
return m.zone.zones.ReadLines(fullName)
} }
// WriteStringFile writes the given content to a file on the machine's config directory // WriteStringFile writes the given content to a file on the machine's config directory
func (m *Machine) WriteStringFile(value string, name string, args ...any) error { func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
fullName := m.getFilename(name, args...) f, err := m.CreateTruncFile(name, args...)
if err != nil {
return err
}
defer f.Close()
return m.zone.zones.WriteStringFile(value, fullName) buf := bytes.NewBufferString(value)
} _, err = buf.WriteTo(f)
return err
// MkdirAll creates directories relative to the machine's config directory
func (m *Machine) MkdirAll(name string, args ...any) error {
fullName := m.getFilename(name, args...)
return m.zone.zones.MkdirAll(fullName)
} }
func (m *Machine) getFilename(name string, args ...any) string { func (m *Machine) getFilename(name string, args ...any) string {
+7 -34
View File
@@ -118,31 +118,21 @@ func (m *Machine) tryApplyWireguardConfig(ring int) error {
} }
} }
func (m *Machine) applyWireguardConfigNode(ring int, wg *wireguard.Config) error { func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
addr := wg.GetAddress() addr := wg.GetAddress()
if !core.IsZero(addr) { zoneID, nodeID, ok := Rings[ring].Decode(addr)
zoneID, nodeID, ok := Rings[ring].Decode(addr) if !ok {
if !ok { return fmt.Errorf("%s: invalid address", addr)
return fmt.Errorf("%s: invalid address", addr) }
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil { if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
return core.Wrap(err, "%s: invalid address", addr) return core.Wrap(err, "%s: invalid address", addr)
}
} }
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil { if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
return core.Wrap(err, "interface") return core.Wrap(err, "interface")
} }
return nil
}
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
if err := m.applyWireguardConfigNode(ring, wg); err != nil {
return err
}
for _, peer := range wg.Peer { for _, peer := range wg.Peer {
err := m.applyWireguardPeerConfig(ring, peer) err := m.applyWireguardPeerConfig(ring, peer)
switch { switch {
@@ -240,23 +230,6 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
return nil return nil
} }
func (m *Machine) setRingDefaults(ri *RingInfo) error {
if ri.Keys.PrivateKey.IsZero() {
m.info().
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", ri.Ring).
Print("generating key pair")
kp, err := wireguard.NewKeyPair()
if err != nil {
return err
}
ri.Keys = kp
}
return nil
}
// RemoveWireguardConfig deletes wgN.conf from the machine's // RemoveWireguardConfig deletes wgN.conf from the machine's
// config directory. // config directory.
func (m *Machine) RemoveWireguardConfig(ring int) error { func (m *Machine) RemoveWireguardConfig(ring int) error {
+1 -42
View File
@@ -3,7 +3,6 @@ package cluster
import ( import (
"context" "context"
"net/netip" "net/netip"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -69,8 +68,7 @@ func (m *Machine) setID() error {
return nil return nil
} }
// scan is called once we know about all zones and machine names func (m *Machine) scan(opts *ScanOptions) error {
func (m *Machine) scan(_ *ScanOptions) error {
for i := 0; i < RingsCount; i++ { for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil { if err := m.tryApplyWireguardConfig(i); err != nil {
m.error(err). m.error(err).
@@ -82,45 +80,6 @@ func (m *Machine) scan(_ *ScanOptions) error {
} }
} }
return m.loadInactive()
}
func (m *Machine) loadInactive() error {
data, err := m.ReadLines("region")
switch {
case os.IsNotExist(err):
// no file
return nil
case err != nil:
// read error
return err
default:
// look for "none"
for _, r := range data {
switch r {
case "none":
m.Inactive = true
default:
m.Inactive = false
}
}
return nil
}
}
// scanWrapUp is called once all machines have been scanned
func (m *Machine) scanWrapUp(opts *ScanOptions) error {
for _, ri := range m.Rings {
if err := m.setRingDefaults(ri); err != nil {
m.error(err).
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", ri.Ring).
Print()
return err
}
}
if !opts.DontResolvePublicAddresses { if !opts.DontResolvePublicAddresses {
return m.UpdatePublicAddresses() return m.UpdatePublicAddresses()
} }
+4 -105
View File
@@ -1,10 +1,5 @@
package cluster package cluster
import (
"bytes"
"path/filepath"
)
var ( var (
_ MachineIterator = (*Region)(nil) _ MachineIterator = (*Region)(nil)
_ ZoneIterator = (*Region)(nil) _ ZoneIterator = (*Region)(nil)
@@ -37,9 +32,7 @@ func (r *Region) ForEachMachine(fn func(*Machine) bool) {
var term bool var term bool
z.ForEachMachine(func(p *Machine) bool { z.ForEachMachine(func(p *Machine) bool {
if p.IsActive() { term = fn(p)
term = fn(p)
}
return term return term
}) })
@@ -63,7 +56,6 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
// first regions defined by zones // first regions defined by zones
m.ForEachZone(func(z *Zone) bool { m.ForEachZone(func(z *Zone) bool {
SortRegions(z.Regions)
for _, region := range z.Regions { for _, region := range z.Regions {
regions[region] = append(regions[region], z) regions[region] = append(regions[region], z)
} }
@@ -73,7 +65,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
// bind first level regions and their zones // bind first level regions and their zones
for name, zones := range regions { for name, zones := range regions {
m.setRegionZones(name, zones...) m.syncRegions(name, zones...)
} }
// and combine zones to produce larger regions // and combine zones to produce larger regions
@@ -82,14 +74,11 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
m.finishRegion(r) m.finishRegion(r)
} }
m.sortRegions()
return nil return nil
} }
func (m *Cluster) setRegionZones(name string, zones ...*Zone) { func (m *Cluster) syncRegions(name string, zones ...*Zone) {
for i := range m.Regions { for _, r := range m.Regions {
r := &m.Regions[i]
if r.Name == name { if r.Name == name {
// found // found
r.m = m r.m = m
@@ -106,38 +95,6 @@ func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
}) })
} }
func (m *Cluster) appendRegionRegions(name string, subs ...string) {
for i := range m.Regions {
r := &m.Regions[i]
if name == r.Name {
// found
r.Regions = append(r.Regions, subs...)
return
}
}
// new
m.Regions = append(m.Regions, Region{
Name: name,
Regions: subs,
})
}
func (z *Zone) appendRegions(regions ...string) error {
for _, s := range regions {
// TODO: validate
z.debug().
WithField("zone", z.Name).
WithField("region", s).
Print("attached")
z.Regions = append(z.Regions, s)
}
return nil
}
func (m *Cluster) finishRegion(r *Region) { func (m *Cluster) finishRegion(r *Region) {
if r.m != nil { if r.m != nil {
// ready // ready
@@ -171,61 +128,3 @@ func (m *Cluster) getRegion(name string) (*Region, bool) {
return nil, false return nil, false
} }
// SyncRegions writes to the file system the regions this [Zone]
// belongs to.
func (z *Zone) SyncRegions() error {
err := z.syncZoneRegions()
if err == nil {
z.ForEachMachine(func(p *Machine) bool {
if p.IsActive() {
err = p.RemoveFile("region")
} else {
err = p.WriteStringFile("none\n", "region")
}
return err != nil
})
}
return err
}
func (z *Zone) syncZoneRegions() error {
name := filepath.Join(z.Name, "regions")
if len(z.Regions) > 0 {
var buf bytes.Buffer
for _, s := range z.Regions {
_, _ = buf.WriteString(s)
_, _ = buf.WriteRune('\n')
}
return z.zones.WriteStringFile(buf.String(), name)
}
return z.zones.RemoveFile(name)
}
// SyncRegions writes to the file system the regions covered
// by this meta-region
func (r *Region) SyncRegions() error {
name := filepath.Join(r.Name, "regions")
if len(r.Regions) > 0 {
var buf bytes.Buffer
for _, s := range r.Regions {
_, _ = buf.WriteString(s)
_, _ = buf.WriteRune('\n')
}
if err := r.m.MkdirAll(r.Name); err != nil {
return err
}
return r.m.WriteStringFile(buf.String(), name)
}
return r.m.RemoveFile(name)
}
-41
View File
@@ -1,41 +0,0 @@
package cluster
import "sort"
// SortRegions sorts regions. first by length those 3-character
// or shorter, and then by length. It's mostly aimed at
// supporting ISO-3166 order
func SortRegions(regions []string) []string {
sort.Slice(regions, func(i, j int) bool {
r1, r2 := regions[i], regions[j]
return regionLess(r1, r2)
})
return regions
}
func regionLess(r1, r2 string) bool {
switch {
case len(r1) < 4:
switch {
case len(r1) < len(r2):
return true
case len(r1) > len(r2):
return false
default:
return r1 < r2
}
case len(r2) < 4:
return false
default:
return r1 < r2
}
}
func (m *Cluster) sortRegions() {
sort.Slice(m.Regions, func(i, j int) bool {
r1 := m.Regions[i].Name
r2 := m.Regions[j].Name
return regionLess(r1, r2)
})
}
+1 -1
View File
@@ -41,7 +41,7 @@ func (ri *RingInfo) Merge(alter *RingInfo) error {
// can't disable via Merge // can't disable via Merge
return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled) return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled)
case !canMergeKeyPairs(ri.Keys, alter.Keys): case !canMergeKeyPairs(ri.Keys, alter.Keys):
// incompatible key pairs // incompatible keypairs
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys) return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
} }
-37
View File
@@ -3,10 +3,8 @@ package cluster
// SyncAll updates all config files // SyncAll updates all config files
func (m *Cluster) SyncAll() error { func (m *Cluster) SyncAll() error {
for _, fn := range []func() error{ for _, fn := range []func() error{
m.SyncMkdirAll,
m.SyncAllWireguard, m.SyncAllWireguard,
m.SyncAllCeph, m.SyncAllCeph,
m.SyncAllRegions,
m.WriteHosts, m.WriteHosts,
} { } {
if err := fn(); err != nil { if err := fn(); err != nil {
@@ -17,20 +15,6 @@ func (m *Cluster) SyncAll() error {
return nil return nil
} }
// SyncMkdirAll creates the directories needed to store files
// required to represent the cluster.
func (m *Cluster) SyncMkdirAll() error {
err := m.MkdirAll(".")
if err == nil {
m.ForEachMachine(func(p *Machine) bool {
err = p.MkdirAll(".")
return err != nil
})
}
return err
}
// SyncAllWireguard updates all wireguard config files // SyncAllWireguard updates all wireguard config files
func (m *Cluster) SyncAllWireguard() error { func (m *Cluster) SyncAllWireguard() error {
var err error var err error
@@ -59,24 +43,3 @@ func (m *Cluster) SyncAllCeph() error {
return m.WriteCephConfig(cfg) return m.WriteCephConfig(cfg)
} }
// SyncAllRegions rewrites all region data
func (m *Cluster) SyncAllRegions() error {
var err error
m.ForEachZone(func(z *Zone) bool {
err := z.SyncRegions()
return err != nil
})
if err != nil {
return err
}
m.ForEachRegion(func(r *Region) bool {
err = r.SyncRegions()
return err != nil
})
return err
}
+27 -13
View File
@@ -11,8 +11,6 @@ import (
"darvaza.org/core" "darvaza.org/core"
"github.com/libdns/libdns" "github.com/libdns/libdns"
"git.jpi.io/amery/jpictl/pkg/cluster"
) )
func (mgr *Manager) fqdn(name string) string { func (mgr *Manager) fqdn(name string) string {
@@ -88,6 +86,32 @@ func lessRecord(a, b libdns.Record) bool {
} }
} }
// SortRegions sorts regions. first by length those 3-character
// or shorter, and then by length. It's mostly aimed at
// supporting ISO-3166 order
func SortRegions(regions []string) []string {
sort.Slice(regions, func(i, j int) bool {
r1, r2 := regions[i], regions[j]
switch {
case len(r1) < 4:
switch {
case len(r1) < len(r2):
return true
case len(r1) > len(r2):
return false
default:
return r1 < r2
}
case len(r2) < 4:
return false
default:
return r1 < r2
}
})
return regions
}
// AddrRecord represents an A or AAAA record // AddrRecord represents an A or AAAA record
type AddrRecord struct { type AddrRecord struct {
Name string Name string
@@ -148,17 +172,7 @@ func (mgr *Manager) genRegionsSorted() []string {
regions = append(regions, name) regions = append(regions, name)
} }
return cluster.SortRegions(regions) return SortRegions(regions)
}
func (mgr *Manager) genZonesSorted() []string {
zones := make([]string, 0, len(mgr.zones))
for name := range mgr.zones {
zones = append(zones, name)
}
sort.Strings(zones)
return zones
} }
func (mgr *Manager) genAllAddrRecords() []AddrRecord { func (mgr *Manager) genAllAddrRecords() []AddrRecord {
+2 -3
View File
@@ -14,13 +14,12 @@ func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
cache := make(map[string][]netip.Addr) cache := make(map[string][]netip.Addr)
// zones // zones
for _, zoneName := range mgr.genZonesSorted() { for _, z := range mgr.zones {
z := mgr.zones[zoneName]
mgr.writeZoneHosts(&buf, z) mgr.writeZoneHosts(&buf, z)
// zone alias // zone alias
addrs := mgr.genZoneAddresses(z) addrs := mgr.genZoneAddresses(z)
zoneName := z.Name
rr := AddrRecord{ rr := AddrRecord{
Name: mgr.fqdn(zoneName + mgr.suffix), Name: mgr.fqdn(zoneName + mgr.suffix),
+4 -6
View File
@@ -180,12 +180,10 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
ListenPort: p.ListenPort, ListenPort: p.ListenPort,
} }
if p.PrivateKey != "" { out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey) if err != nil {
if err != nil { err = core.Wrap(err, "PrivateKey")
err = core.Wrap(err, "PrivateKey") return InterfaceConfig{}, err
return InterfaceConfig{}, err
}
} }
return out, nil return out, nil
+10 -8
View File
@@ -54,23 +54,25 @@ func (pub PublicKey) String() string {
// UnmarshalText loads the value from base64 // UnmarshalText loads the value from base64
func (key *PrivateKey) UnmarshalText(b []byte) error { func (key *PrivateKey) UnmarshalText(b []byte) error {
v, err := PrivateKeyFromBase64(string(b)) v, err := PrivateKeyFromBase64(string(b))
if err != nil { switch {
case err != nil:
return err return err
default:
*key = v
return nil
} }
*key = v
return nil
} }
// UnmarshalText loads the value from base64 // UnmarshalText loads the value from base64
func (pub *PublicKey) UnmarshalText(b []byte) error { func (pub *PublicKey) UnmarshalText(b []byte) error {
v, err := PublicKeyFromBase64(string(b)) v, err := PublicKeyFromBase64(string(b))
if err != nil { switch {
case err != nil:
return err return err
default:
*pub = v
return nil
} }
*pub = v
return nil
} }
// MarshalJSON encodes the key for JSON, omitting empty. // MarshalJSON encodes the key for JSON, omitting empty.