Compare commits
50 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 76b40e63c7 | |||
| 5d82de5535 | |||
| c33d0dab16 | |||
| dd585b0fa2 | |||
| 172752ab90 | |||
| 4e2693b12c | |||
| eba0340e32 | |||
| 1a47985bd7 | |||
| f5ea72740c | |||
| 357c85dc1a | |||
| 00aec477a4 | |||
| e0d8592dc1 | |||
| c397ca29ac | |||
| 066788b9be | |||
| 4402555f04 | |||
| 6e7f24f491 | |||
| 54b302c6d5 | |||
| f62a47003d | |||
| 5abaed9047 | |||
| c702d649e0 | |||
| e9f9d474dc | |||
| e2941cf2c0 | |||
| ea755113a8 | |||
| 1c199ed923 | |||
| 5dc5c95aa1 | |||
| a0cc698a39 | |||
| 70008e0ead | |||
| ec2b30c1e7 | |||
| 3de7fcb605 | |||
| d0b0698c10 | |||
| 2a2e6c121e | |||
| 24059dc9ee | |||
| bedf62977f | |||
| 5abb4c2f92 | |||
| 046c9a508b | |||
|
f6766547f9
|
|||
| 6aec17d079 | |||
| ab8ffdd507 | |||
| 422e119f88 | |||
| 204f3a49a1 | |||
|
d1198328f6
|
|||
| 7795610caf | |||
| 32046fc1ec | |||
| 2016b27707 | |||
| c038ad4431 | |||
| 159ccf59ac | |||
| 6a071ba5f0 | |||
| 3e90e3ab1e | |||
| 90dd0c1239 | |||
| 033ca2f20e |
Vendored
+6
@@ -1,9 +1,15 @@
|
|||||||
{
|
{
|
||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
|
"asciigoat",
|
||||||
"ceph",
|
"ceph",
|
||||||
|
"cyclomatic",
|
||||||
"darvaza",
|
"darvaza",
|
||||||
"gofrs",
|
"gofrs",
|
||||||
"jpictl",
|
"jpictl",
|
||||||
|
"libdns",
|
||||||
|
"Lookuper",
|
||||||
|
"publicsuffix",
|
||||||
|
"Wrapf",
|
||||||
"zerolog"
|
"zerolog"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-7
@@ -1,22 +1,59 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "git.jpi.io/amery/jpictl/pkg/zones"
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DefaultConfigFile is read if -f/--config-file isn't specified.
|
||||||
|
// If it doesn't exist, m/ will be scanned
|
||||||
|
DefaultConfigFile = "cloud.yaml"
|
||||||
|
)
|
||||||
|
|
||||||
// Config describes the repository
|
// Config describes the repository
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Base string
|
Base string
|
||||||
Domain string
|
Domain string
|
||||||
|
|
||||||
|
ConfigFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
var cfg = &Config{
|
var cfg = &Config{
|
||||||
Base: "./m",
|
Base: "m",
|
||||||
Domain: "m.jpi.cloud",
|
Domain: "jpi.cloud",
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadZones loads all zones and machines in the config directory
|
// LoadZones loads all zones and machines in the config directory
|
||||||
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
|
// or file
|
||||||
return zones.New(cfg.Base, cfg.Domain,
|
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
|
||||||
zones.ResolvePublicAddresses(resolve),
|
// try config file first
|
||||||
zones.WithLogger(log),
|
zones, err := cluster.NewFromConfig(cfg.ConfigFile,
|
||||||
|
cluster.ResolvePublicAddresses(resolve),
|
||||||
|
cluster.WithLogger(log),
|
||||||
|
)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
// file was good
|
||||||
|
return zones, nil
|
||||||
|
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
|
||||||
|
// file was bad
|
||||||
|
return nil, core.Wrapf(err, "NewFromConfig(%q)", cfg.ConfigFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
// default file doesn't exist. scan instead.
|
||||||
|
return cluster.NewFromDirectory(cfg.Base, cfg.Domain,
|
||||||
|
cluster.ResolvePublicAddresses(resolve),
|
||||||
|
cluster.WithLogger(log),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.PersistentFlags().
|
||||||
|
StringVarP(&cfg.ConfigFile, "config-file", "f",
|
||||||
|
DefaultConfigFile, "config file (JSON or YAML)")
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,132 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/dns"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DNSSyncTimeout specifies how long are we willing to wait for a DNS
|
||||||
|
// synchronization
|
||||||
|
DNSSyncTimeout = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
func newDNSManager(m *cluster.Cluster, provider dns.Provider) (*dns.Manager, error) {
|
||||||
|
domain := m.Domain
|
||||||
|
if m.Name != "" {
|
||||||
|
domain = m.Name + "." + domain
|
||||||
|
}
|
||||||
|
|
||||||
|
mgr, err := dns.NewManager(dns.WithDomain(domain), dns.WithLogger(log))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if provider != nil {
|
||||||
|
// set provider only if specified
|
||||||
|
err = dns.WithProvider(provider)(mgr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := populateDNSManager(mgr, m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mgr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *cluster.Zone) bool {
|
||||||
|
z.ForEachMachine(func(p *cluster.Machine) bool {
|
||||||
|
err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.ForEachRegion(func(r *cluster.Region) bool {
|
||||||
|
r.ForEachZone(func(z *cluster.Zone) bool {
|
||||||
|
err = mgr.AddRegion(ctx, r.Name, z.Name)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command
|
||||||
|
var dnsCmd = &cobra.Command{
|
||||||
|
Use: "dns",
|
||||||
|
}
|
||||||
|
|
||||||
|
var dnsWriteCmd = &cobra.Command{
|
||||||
|
Use: "write",
|
||||||
|
Short: "dns write generates public DNS records",
|
||||||
|
PreRun: setVerbosity,
|
||||||
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
|
m, err := cfg.LoadZones(true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
mgr, err := newDNSManager(m, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = mgr.WriteTo(os.Stdout)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var dnsSyncCmd = &cobra.Command{
|
||||||
|
Use: "sync",
|
||||||
|
Short: "dns sync updates public DNS records",
|
||||||
|
PreRun: setVerbosity,
|
||||||
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
|
cred, err := dns.DefaultDNSProvider()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := cfg.LoadZones(true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
mgr, err := newDNSManager(m, cred)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return mgr.Sync(ctx)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(dnsCmd)
|
||||||
|
|
||||||
|
dnsCmd.AddCommand(dnsWriteCmd)
|
||||||
|
dnsCmd.AddCommand(dnsSyncCmd)
|
||||||
|
}
|
||||||
+6
-14
@@ -6,7 +6,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/burntSushi/toml"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@@ -20,8 +19,8 @@ type Encoder interface {
|
|||||||
type Encoding int
|
type Encoding int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// TOMLEncoding represents TOML encoding
|
// UndefinedEncoding implies the default encoding
|
||||||
TOMLEncoding Encoding = iota
|
UndefinedEncoding Encoding = iota
|
||||||
// JSONEncoding represents JSON encoding
|
// JSONEncoding represents JSON encoding
|
||||||
JSONEncoding
|
JSONEncoding
|
||||||
// YAMLEncoding represents YAML encoding
|
// YAMLEncoding represents YAML encoding
|
||||||
@@ -42,18 +41,13 @@ func NewYAMLEncoder(w io.Writer) Encoder {
|
|||||||
return enc
|
return enc
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTOMLEncoder returns a TOML [Encoder] to work on the given [io.Writer]
|
|
||||||
func NewTOMLEncoder(w io.Writer) Encoder {
|
|
||||||
enc := toml.NewEncoder(w)
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
const encoding = YAMLEncoding
|
const encoding = YAMLEncoding
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
var dumpCmd = &cobra.Command{
|
var dumpCmd = &cobra.Command{
|
||||||
Use: "dump",
|
Use: "dump",
|
||||||
Short: "generates a text representation of the config",
|
Short: "generates a text representation of the config",
|
||||||
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var enc Encoder
|
var enc Encoder
|
||||||
@@ -66,10 +60,8 @@ var dumpCmd = &cobra.Command{
|
|||||||
switch encoding {
|
switch encoding {
|
||||||
case JSONEncoding:
|
case JSONEncoding:
|
||||||
enc = NewJSONEncoder(&buf)
|
enc = NewJSONEncoder(&buf)
|
||||||
case YAMLEncoding:
|
|
||||||
enc = NewYAMLEncoder(&buf)
|
|
||||||
default:
|
default:
|
||||||
enc = NewTOMLEncoder(&buf)
|
enc = NewYAMLEncoder(&buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = enc.Encode(m); err != nil {
|
if err = enc.Encode(m); err != nil {
|
||||||
|
|||||||
+3
-2
@@ -8,8 +8,9 @@ import (
|
|||||||
|
|
||||||
// Command
|
// Command
|
||||||
var envCmd = &cobra.Command{
|
var envCmd = &cobra.Command{
|
||||||
Use: "env",
|
Use: "env",
|
||||||
Short: "generates environment variables for shell scripts",
|
Short: "generates environment variables for shell scripts",
|
||||||
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
m, err := cfg.LoadZones(false)
|
m, err := cfg.LoadZones(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+13
-11
@@ -7,8 +7,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
@@ -19,8 +20,9 @@ var gatewayCmd = &cobra.Command{
|
|||||||
|
|
||||||
// gateway set
|
// gateway set
|
||||||
var gatewaySetCmd = &cobra.Command{
|
var gatewaySetCmd = &cobra.Command{
|
||||||
Use: "set",
|
Use: "set",
|
||||||
Short: "gateway set sets machines as gateways",
|
Short: "gateway set sets machines as gateways",
|
||||||
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(_ *cobra.Command, args []string) error {
|
||||||
m, err := cfg.LoadZones(false)
|
m, err := cfg.LoadZones(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -37,9 +39,9 @@ var gatewaySetCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewaySet(zi zones.ZoneIterator, gw string) error {
|
func gatewaySet(zi cluster.ZoneIterator, gw string) error {
|
||||||
var err error
|
var err error
|
||||||
zi.ForEachZone(func(z *zones.Zone) bool {
|
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||||
for _, m := range z.Machines {
|
for _, m := range z.Machines {
|
||||||
if m.Name == gw {
|
if m.Name == gw {
|
||||||
z.SetGateway(m.ID, true)
|
z.SetGateway(m.ID, true)
|
||||||
@@ -73,9 +75,9 @@ var gatewayUnsetCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewayUnset(zi zones.ZoneIterator, ngw string) error {
|
func gatewayUnset(zi cluster.ZoneIterator, ngw string) error {
|
||||||
var err error
|
var err error
|
||||||
zi.ForEachZone(func(z *zones.Zone) bool {
|
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||||
for _, m := range z.Machines {
|
for _, m := range z.Machines {
|
||||||
if m.Name == ngw && m.IsGateway() {
|
if m.Name == ngw && m.IsGateway() {
|
||||||
z.SetGateway(m.ID, false)
|
z.SetGateway(m.ID, false)
|
||||||
@@ -114,10 +116,10 @@ var gatewayListCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewayListAll(zi zones.ZoneIterator) error {
|
func gatewayListAll(zi cluster.ZoneIterator) error {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
var err error
|
var err error
|
||||||
zi.ForEachZone(func(z *zones.Zone) bool {
|
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||||
b.WriteString(z.Name + ":")
|
b.WriteString(z.Name + ":")
|
||||||
var sIDs []string
|
var sIDs []string
|
||||||
ids, num := z.GatewayIDs()
|
ids, num := z.GatewayIDs()
|
||||||
@@ -136,10 +138,10 @@ func gatewayListAll(zi zones.ZoneIterator) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewayList(zi zones.ZoneIterator, m string) error {
|
func gatewayList(zi cluster.ZoneIterator, m string) error {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
var err error
|
var err error
|
||||||
zi.ForEachZone(func(z *zones.Zone) bool {
|
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||||
if z.Name == m {
|
if z.Name == m {
|
||||||
b.WriteString(z.Name + ":")
|
b.WriteString(z.Name + ":")
|
||||||
ids, num := z.GatewayIDs()
|
ids, num := z.GatewayIDs()
|
||||||
|
|||||||
@@ -3,15 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"darvaza.org/sidecar/pkg/logger/zerolog"
|
|
||||||
"darvaza.org/slog"
|
"darvaza.org/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: make log level configurable via flags
|
|
||||||
var (
|
|
||||||
log = zerolog.New(nil, slog.Debug)
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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
-4
@@ -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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,13 +12,30 @@ const (
|
|||||||
CmdName = "jpictl"
|
CmdName = "jpictl"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var (
|
||||||
Use: CmdName,
|
log = zerolog.New(nil, slog.Error)
|
||||||
Short: "control tool for jpi.cloud",
|
verbosity int
|
||||||
}
|
rootCmd = &cobra.Command{
|
||||||
|
Use: CmdName,
|
||||||
|
Short: "control tool for jpi.cloud",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
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))
|
||||||
|
}
|
||||||
|
|||||||
+3
-2
@@ -6,8 +6,9 @@ import (
|
|||||||
|
|
||||||
// Command
|
// Command
|
||||||
var writeCmd = &cobra.Command{
|
var writeCmd = &cobra.Command{
|
||||||
Use: "write",
|
Use: "write",
|
||||||
Short: "rewrites all config files",
|
Short: "rewrites all config files",
|
||||||
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
m, err := cfg.LoadZones(false)
|
m, err := cfg.LoadZones(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ require (
|
|||||||
darvaza.org/sidecar v0.0.2
|
darvaza.org/sidecar v0.0.2
|
||||||
darvaza.org/slog v0.5.3
|
darvaza.org/slog v0.5.3
|
||||||
darvaza.org/slog/handlers/discard v0.4.5
|
darvaza.org/slog/handlers/discard v0.4.5
|
||||||
github.com/burntSushi/toml v0.3.1
|
|
||||||
github.com/gofrs/uuid/v5 v5.0.0
|
github.com/gofrs/uuid/v5 v5.0.0
|
||||||
github.com/hack-pad/hackpadfs v0.2.1
|
github.com/hack-pad/hackpadfs v0.2.1
|
||||||
|
github.com/libdns/cloudflare v0.1.0
|
||||||
|
github.com/libdns/libdns v0.2.1
|
||||||
github.com/mgechev/revive v1.3.3
|
github.com/mgechev/revive v1.3.3
|
||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
golang.org/x/crypto v0.12.0
|
golang.org/x/crypto v0.12.0
|
||||||
|
golang.org/x/net v0.14.0
|
||||||
gopkg.in/gcfg.v1 v1.2.3
|
gopkg.in/gcfg.v1 v1.2.3
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
@@ -40,7 +42,6 @@ require (
|
|||||||
github.com/rs/zerolog v1.30.0 // indirect
|
github.com/rs/zerolog v1.30.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
golang.org/x/mod v0.12.0 // indirect
|
golang.org/x/mod v0.12.0 // indirect
|
||||||
golang.org/x/net v0.14.0 // indirect
|
|
||||||
golang.org/x/sys v0.12.0 // indirect
|
golang.org/x/sys v0.12.0 // indirect
|
||||||
golang.org/x/text v0.13.0 // indirect
|
golang.org/x/text v0.13.0 // indirect
|
||||||
golang.org/x/tools v0.12.0 // indirect
|
golang.org/x/tools v0.12.0 // indirect
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPS
|
|||||||
darvaza.org/slog/handlers/zerolog v0.4.5/go.mod h1:mCoh/mIl8Nsa6Yu1Um7d7cos6RuEJzgaTXaX5LDRUao=
|
darvaza.org/slog/handlers/zerolog v0.4.5/go.mod h1:mCoh/mIl8Nsa6Yu1Um7d7cos6RuEJzgaTXaX5LDRUao=
|
||||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/burntSushi/toml v0.3.1 h1:Hu1cOEC2qtKULZJCzym5tyA35bZr3HREuolgiAzMlhY=
|
|
||||||
github.com/burntSushi/toml v0.3.1/go.mod h1:sGTquCpRYr9McuHdv0m6YKIhx8DJGJa4t04/Y9pfSio=
|
|
||||||
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
|
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
|
||||||
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
|
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
|
||||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
@@ -38,6 +36,11 @@ github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMI
|
|||||||
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
|
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/libdns/cloudflare v0.1.0 h1:93WkJaGaiXCe353LHEP36kAWCUw0YjFqwhkBkU2/iic=
|
||||||
|
github.com/libdns/cloudflare v0.1.0/go.mod h1:a44IP6J1YH6nvcNl1PverfJviADgXUnsozR3a7vBKN8=
|
||||||
|
github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
|
||||||
|
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
|
||||||
|
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
|
||||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GetCephFSID returns our Ceph's FSID
|
// GetCephFSID returns our Ceph's FSID
|
||||||
func (m *Zones) GetCephFSID() (uuid.UUID, error) {
|
func (m *Cluster) GetCephFSID() (uuid.UUID, error) {
|
||||||
if core.IsZero(m.CephFSID) {
|
if core.IsZero(m.CephFSID) {
|
||||||
// generate one
|
// generate one
|
||||||
v, err := uuid.NewV4()
|
v, err := uuid.NewV4()
|
||||||
@@ -25,7 +25,7 @@ func (m *Zones) GetCephFSID() (uuid.UUID, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetCephConfig reads the ceph.conf file
|
// GetCephConfig reads the ceph.conf file
|
||||||
func (m *Zones) GetCephConfig() (*ceph.Config, error) {
|
func (m *Cluster) GetCephConfig() (*ceph.Config, error) {
|
||||||
data, err := m.ReadFile("ceph.conf")
|
data, err := m.ReadFile("ceph.conf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -36,7 +36,7 @@ func (m *Zones) GetCephConfig() (*ceph.Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteCephConfig writes the ceph.conf file
|
// WriteCephConfig writes the ceph.conf file
|
||||||
func (m *Zones) WriteCephConfig(cfg *ceph.Config) error {
|
func (m *Cluster) WriteCephConfig(cfg *ceph.Config) error {
|
||||||
f, err := m.CreateTruncFile("ceph.conf")
|
f, err := m.CreateTruncFile("ceph.conf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -48,7 +48,7 @@ func (m *Zones) WriteCephConfig(cfg *ceph.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GenCephConfig prepares a ceph.Config using the cluster information
|
// GenCephConfig prepares a ceph.Config using the cluster information
|
||||||
func (m *Zones) GenCephConfig() (*ceph.Config, error) {
|
func (m *Cluster) GenCephConfig() (*ceph.Config, error) {
|
||||||
fsid, err := m.GetCephFSID()
|
fsid, err := m.GetCephFSID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"darvaza.org/slog"
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/ceph"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cephScanTODO struct {
|
||||||
|
names map[string]bool
|
||||||
|
addrs map[string]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (todo *cephScanTODO) checkMachine(p *Machine) bool {
|
||||||
|
// on ceph all addresses are ring1
|
||||||
|
ring1, _ := RingOneAddress(p.Zone(), p.ID)
|
||||||
|
addr := ring1.String()
|
||||||
|
|
||||||
|
if _, found := todo.names[p.Name]; found {
|
||||||
|
// found on the TODO by name
|
||||||
|
todo.names[p.Name] = true
|
||||||
|
todo.addrs[addr] = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, found := todo.addrs[addr]; found {
|
||||||
|
// found on the TODO by address
|
||||||
|
todo.names[p.Name] = true
|
||||||
|
todo.addrs[addr] = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (todo *cephScanTODO) LogMissing(log slog.Logger) {
|
||||||
|
for name, found := range todo.names {
|
||||||
|
if !found {
|
||||||
|
log.Warn().
|
||||||
|
WithField("subsystem", "ceph").
|
||||||
|
WithField("monitor", name).
|
||||||
|
Print("unknown monitor")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for addr, found := range todo.addrs {
|
||||||
|
if !found {
|
||||||
|
log.Warn().
|
||||||
|
WithField("subsystem", "ceph").
|
||||||
|
WithField("monitor", addr).
|
||||||
|
Print("unknown monitor")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
|
||||||
|
todo := &cephScanTODO{
|
||||||
|
names: make(map[string]bool),
|
||||||
|
addrs: make(map[string]bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range cfg.Global.Monitors {
|
||||||
|
todo.names[name] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range cfg.Global.MonitorsAddr {
|
||||||
|
todo.addrs[addr.String()] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return todo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) scanCephMonitors(opts *ScanOptions) error {
|
||||||
|
cfg, err := m.GetCephConfig()
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
err = nil
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg != nil {
|
||||||
|
// store FSID
|
||||||
|
m.CephFSID = cfg.Global.FSID
|
||||||
|
|
||||||
|
// flag monitors based on config
|
||||||
|
todo := newCephScanTODO(cfg)
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
p.CephMonitor = todo.checkMachine(p)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
todo.LogMissing(m.log)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.initCephMonitors(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) initCephMonitors(_ *ScanOptions) error {
|
||||||
|
// make sure every zone has one
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
_ = z.GetCephMonitors()
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
// Package cluster contains information about the cluster
|
||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
|
||||||
|
"darvaza.org/resolver"
|
||||||
|
"darvaza.org/slog"
|
||||||
|
"github.com/gofrs/uuid/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = (*Cluster)(nil)
|
||||||
|
_ ZoneIterator = (*Cluster)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// revive:disable:line-length-limit
|
||||||
|
|
||||||
|
// Cluster represents all zones in a cluster
|
||||||
|
type Cluster struct {
|
||||||
|
dir fs.FS
|
||||||
|
log slog.Logger
|
||||||
|
resolver resolver.Resolver
|
||||||
|
|
||||||
|
BaseDir string `json:"dir,omitempty" yaml:"dir,omitempty"`
|
||||||
|
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||||
|
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
|
||||||
|
|
||||||
|
CephFSID uuid.UUID `json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"`
|
||||||
|
Regions []Region `json:",omitempty" yaml:",omitempty"`
|
||||||
|
Zones []*Zone `json:",omitempty" yaml:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// revive:enable:line-length-limit
|
||||||
|
|
||||||
|
// ForEachMachine calls a function for each Machine in the cluster
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (m *Cluster) ForEachMachine(fn func(*Machine) bool) {
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
var term bool
|
||||||
|
|
||||||
|
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 *Cluster) ForEachZone(fn func(*Zone) bool) {
|
||||||
|
for _, p := range m.Zones {
|
||||||
|
if fn(p) {
|
||||||
|
// terminate
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMachineByName looks for a machine with the specified
|
||||||
|
// name on any zone
|
||||||
|
func (m *Cluster) GetMachineByName(name string) (*Machine, bool) {
|
||||||
|
var out *Machine
|
||||||
|
|
||||||
|
if name != "" {
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.Name == name {
|
||||||
|
out = p
|
||||||
|
}
|
||||||
|
|
||||||
|
return out != nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, out != nil
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// OpenFile opens a file on the cluster's config directory with the specified flags
|
// OpenFile opens a file on the cluster's config directory with the specified flags
|
||||||
func (m *Zones) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
func (m *Cluster) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
name = fmt.Sprintf(name, args...)
|
name = fmt.Sprintf(name, args...)
|
||||||
}
|
}
|
||||||
@@ -18,16 +18,16 @@ func (m *Zones) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateTruncFile creates or truncates a file on the cluster's config directory
|
// CreateTruncFile creates or truncates a file on the cluster's config directory
|
||||||
func (m *Zones) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
func (m *Cluster) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
||||||
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateFile creates a file on the cluster's config directory
|
// CreateFile creates a file on the cluster's config directory
|
||||||
func (m *Zones) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
func (m *Cluster) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||||||
return m.openWriter(name, os.O_CREATE, args...)
|
return m.openWriter(name, os.O_CREATE, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||||||
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -37,7 +37,7 @@ func (m *Zones) openWriter(name string, flags int, args ...any) (io.WriteCloser,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile reads a file from the cluster's config directory
|
// ReadFile reads a file from the cluster's config directory
|
||||||
func (m *Zones) 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 {
|
||||||
name = fmt.Sprintf(name, args...)
|
name = fmt.Sprintf(name, args...)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/hack-pad/hackpadfs/os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DirFS returns a file system (an [fs.FS]) for the tree
|
||||||
|
// of files rooted at the directory dir.
|
||||||
|
func DirFS(dir string) (fs.FS, error) {
|
||||||
|
dir = filepath.Clean(dir)
|
||||||
|
fullPath, err := filepath.Abs(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sub, err := os.NewFS().Sub(fullPath[1:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return sub, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m *Cluster) init(opts *ScanOptions) error {
|
||||||
|
for _, fn := range []func(*ScanOptions) error{
|
||||||
|
m.initZones,
|
||||||
|
m.scanZoneIDs,
|
||||||
|
m.scanSort,
|
||||||
|
m.scanGateways,
|
||||||
|
m.initCephMonitors,
|
||||||
|
m.initRegions,
|
||||||
|
} {
|
||||||
|
if err := fn(opts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) initZones(opts *ScanOptions) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
sub, err := DirFS(m.BaseDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.dir = sub
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = m.initZone(z, opts)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error {
|
||||||
|
var hasMissing bool
|
||||||
|
var lastMachineID int
|
||||||
|
|
||||||
|
z.zones = m
|
||||||
|
z.logger = m
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
p.zone = z
|
||||||
|
p.logger = z
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case p.ID == 0:
|
||||||
|
hasMissing = true
|
||||||
|
case p.ID > lastMachineID:
|
||||||
|
lastMachineID = z.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if hasMissing {
|
||||||
|
next := lastMachineID + 1
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.ID == 0 {
|
||||||
|
p.ID, next = next, next+1
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
p.Name = fmt.Sprintf("%s-%v", z.Name, p.ID)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeConfigData(data []byte) (out *Cluster, err error) {
|
||||||
|
// try JSON first
|
||||||
|
out = new(Cluster)
|
||||||
|
err = json.Unmarshal(data, out)
|
||||||
|
if err == nil {
|
||||||
|
// good json
|
||||||
|
return out, nil
|
||||||
|
} else if _, ok := err.(*json.SyntaxError); !ok {
|
||||||
|
// bad json
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
out = new(Cluster)
|
||||||
|
err = yaml.Unmarshal(data, out)
|
||||||
|
if err != nil {
|
||||||
|
// bad yaml too
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// good yaml
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFromConfig loads the cluster data from the given file
|
||||||
|
func NewFromConfig(filename string, opts ...ScanOption) (*Cluster, error) {
|
||||||
|
var scanOptions ScanOptions
|
||||||
|
|
||||||
|
data, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := decodeConfigData(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if err = opt(m, &scanOptions); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = m.setScanDefaults(&scanOptions); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := m.init(&scanOptions); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *Zones) 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,
|
||||||
@@ -22,7 +24,7 @@ func (m *Zones) scan(opts *ScanOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanDirectory(_ *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 {
|
||||||
@@ -31,24 +33,41 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
|
|||||||
|
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if e.IsDir() {
|
if e.IsDir() {
|
||||||
z := &Zone{
|
z, err := m.newZone(e.Name())
|
||||||
zones: m,
|
switch {
|
||||||
logger: m,
|
case err != nil:
|
||||||
Name: e.Name(),
|
return core.Wrap(err, e.Name())
|
||||||
|
case z.Machines.Len() == 0:
|
||||||
|
z.warn(nil).
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
Print("empty")
|
||||||
|
default:
|
||||||
|
m.Zones = append(m.Zones, z)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := z.scan(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Zones = append(m.Zones, z)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanMachines(opts *ScanOptions) error {
|
func (m *Cluster) newZone(name string) (*Zone, error) {
|
||||||
|
z := &Zone{
|
||||||
|
zones: m,
|
||||||
|
logger: m,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
z.debug().
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
Print("found")
|
||||||
|
|
||||||
|
if err := z.scan(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return z, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) scanMachines(opts *ScanOptions) error {
|
||||||
var err error
|
var err error
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
err = p.scan(opts)
|
err = p.scan(opts)
|
||||||
@@ -57,7 +76,7 @@ func (m *Zones) scanMachines(opts *ScanOptions) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanZoneIDs(_ *ScanOptions) error {
|
func (m *Cluster) scanZoneIDs(_ *ScanOptions) error {
|
||||||
var hasMissing bool
|
var hasMissing bool
|
||||||
var lastZoneID int
|
var lastZoneID int
|
||||||
|
|
||||||
@@ -87,7 +106,7 @@ func (m *Zones) scanZoneIDs(_ *ScanOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanSort(_ *ScanOptions) error {
|
func (m *Cluster) scanSort(_ *ScanOptions) error {
|
||||||
sort.SliceStable(m.Zones, func(i, j int) bool {
|
sort.SliceStable(m.Zones, func(i, j int) bool {
|
||||||
id1 := m.Zones[i].ID
|
id1 := m.Zones[i].ID
|
||||||
id2 := m.Zones[j].ID
|
id2 := m.Zones[j].ID
|
||||||
@@ -113,7 +132,7 @@ func (m *Zones) scanSort(_ *ScanOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanGateways(_ *ScanOptions) error {
|
func (m *Cluster) scanGateways(_ *ScanOptions) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
@@ -138,7 +157,17 @@ func (z *Zone) scan() error {
|
|||||||
Name: e.Name(),
|
Name: e.Name(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.debug().
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
Print("found")
|
||||||
|
|
||||||
if err := m.init(); err != nil {
|
if err := m.init(); err != nil {
|
||||||
|
m.error(err).
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
Print()
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -6,12 +6,10 @@ import (
|
|||||||
|
|
||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
"darvaza.org/slog"
|
"darvaza.org/slog"
|
||||||
"darvaza.org/slog/handlers/discard"
|
|
||||||
"github.com/hack-pad/hackpadfs/os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A ScanOption pre-configures the Zones before scanning
|
// A ScanOption pre-configures the Zones before scanning
|
||||||
type ScanOption func(*Zones, *ScanOptions) error
|
type ScanOption func(*Cluster, *ScanOptions) error
|
||||||
|
|
||||||
// ScanOptions contains flags used by the initial scan
|
// ScanOptions contains flags used by the initial scan
|
||||||
type ScanOptions struct {
|
type ScanOptions struct {
|
||||||
@@ -29,7 +27,7 @@ type ScanOptions struct {
|
|||||||
// the DNS resolver to get PublicAddresses of nodes.
|
// the DNS resolver to get PublicAddresses of nodes.
|
||||||
// Default is true
|
// Default is true
|
||||||
func ResolvePublicAddresses(resolve bool) ScanOption {
|
func ResolvePublicAddresses(resolve bool) ScanOption {
|
||||||
return func(m *Zones, opt *ScanOptions) error {
|
return func(m *Cluster, opt *ScanOptions) error {
|
||||||
opt.DontResolvePublicAddresses = !resolve
|
opt.DontResolvePublicAddresses = !resolve
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -38,7 +36,7 @@ func ResolvePublicAddresses(resolve bool) ScanOption {
|
|||||||
// WithLookuper specifies what resolver.Lookuper to use to
|
// WithLookuper specifies what resolver.Lookuper to use to
|
||||||
// find public addresses
|
// find public addresses
|
||||||
func WithLookuper(h resolver.Lookuper) ScanOption {
|
func WithLookuper(h resolver.Lookuper) ScanOption {
|
||||||
return func(m *Zones, opt *ScanOptions) error {
|
return func(m *Cluster, opt *ScanOptions) error {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
return fs.ErrInvalid
|
return fs.ErrInvalid
|
||||||
}
|
}
|
||||||
@@ -51,7 +49,7 @@ func WithLookuper(h resolver.Lookuper) ScanOption {
|
|||||||
// public addresses. if nil is passed, the [net.Resolver] will be used.
|
// public addresses. if nil is passed, the [net.Resolver] will be used.
|
||||||
// The default is using Cloudflare's 1.1.1.1.
|
// The default is using Cloudflare's 1.1.1.1.
|
||||||
func WithResolver(h resolver.Resolver) ScanOption {
|
func WithResolver(h resolver.Resolver) ScanOption {
|
||||||
return func(m *Zones, opt *ScanOptions) error {
|
return func(m *Cluster, opt *ScanOptions) error {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
h = resolver.SystemResolver(true)
|
h = resolver.SystemResolver(true)
|
||||||
}
|
}
|
||||||
@@ -63,9 +61,9 @@ func WithResolver(h resolver.Resolver) ScanOption {
|
|||||||
|
|
||||||
// WithLogger specifies what to use for logging
|
// WithLogger specifies what to use for logging
|
||||||
func WithLogger(log slog.Logger) ScanOption {
|
func WithLogger(log slog.Logger) ScanOption {
|
||||||
return func(m *Zones, opt *ScanOptions) error {
|
return func(m *Cluster, opt *ScanOptions) error {
|
||||||
if log == nil {
|
if log == nil {
|
||||||
log = discard.New()
|
log = DefaultLogger()
|
||||||
}
|
}
|
||||||
|
|
||||||
opt.Logger = log
|
opt.Logger = log
|
||||||
@@ -74,9 +72,9 @@ func WithLogger(log slog.Logger) ScanOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) setDefaults(opt *ScanOptions) error {
|
func (m *Cluster) setScanDefaults(opt *ScanOptions) error {
|
||||||
if m.resolver == nil {
|
if m.resolver == nil {
|
||||||
h := resolver.NewCloudflareLookuper()
|
h := DefaultLookuper()
|
||||||
|
|
||||||
if err := WithLookuper(h)(m, opt); err != nil {
|
if err := WithLookuper(h)(m, opt); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -92,43 +90,41 @@ func (m *Zones) setDefaults(opt *ScanOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFS builds a [Zones] tree using the given directory
|
// NewFromDirectory builds a [Cluster] tree using the given directory
|
||||||
func NewFS(dir fs.FS, domain string, opts ...ScanOption) (*Zones, error) {
|
func NewFromDirectory(dir, domain string, opts ...ScanOption) (*Cluster, error) {
|
||||||
var scanOptions ScanOptions
|
var scanOptions ScanOptions
|
||||||
|
|
||||||
z := &Zones{
|
dir = filepath.Clean(dir)
|
||||||
dir: dir,
|
fullPath, err := filepath.Abs(dir)
|
||||||
domain: domain,
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sub, err := DirFS(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
m := &Cluster{
|
||||||
|
dir: sub,
|
||||||
|
BaseDir: dir,
|
||||||
|
Name: filepath.Base(fullPath),
|
||||||
|
Domain: domain,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
if err := opt(z, &scanOptions); err != nil {
|
if err := opt(m, &scanOptions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := z.setDefaults(&scanOptions); err != nil {
|
if err := m.setScanDefaults(&scanOptions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := z.scan(&scanOptions); err != nil {
|
if err := m.scan(&scanOptions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return z, nil
|
return m, 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...)
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"darvaza.org/resolver"
|
||||||
|
"darvaza.org/slog"
|
||||||
|
"darvaza.org/slog/handlers/discard"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultLogger returns a logger that doesn't log anything
|
||||||
|
func DefaultLogger() slog.Logger {
|
||||||
|
return discard.New()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultLookuper returns a [resolver.Lookuper] using Cloudflare's 1.1.1.1
|
||||||
|
func DefaultLookuper() resolver.Lookuper {
|
||||||
|
return resolver.NewCloudflareLookuper()
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -16,7 +16,7 @@ type Env struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Env returns a shell environment factory
|
// Env returns a shell environment factory
|
||||||
func (m *Zones) Env(export bool) (*Env, error) {
|
func (m *Cluster) Env(export bool) (*Env, error) {
|
||||||
fsid, err := m.GetCephFSID()
|
fsid, err := m.GetCephFSID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrInvalidName indicates the name isn't valid
|
||||||
|
ErrInvalidName = errors.New("invalid name")
|
||||||
|
|
||||||
|
// ErrUnknownNode indicates there is a reference to a node
|
||||||
|
// we don't have on the tree
|
||||||
|
ErrUnknownNode = errors.New("node does not exist")
|
||||||
|
|
||||||
|
// ErrInvalidNode indicates the nodes can't be used for
|
||||||
|
// the intended purpose
|
||||||
|
ErrInvalidNode = errors.New("invalid node")
|
||||||
|
)
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
type hostsFile struct {
|
||||||
|
Ring0 []hostsEntry
|
||||||
|
Ring1 []hostsEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
type hostsEntry struct {
|
||||||
|
Addr string
|
||||||
|
Names []string
|
||||||
|
}
|
||||||
|
|
||||||
|
var hostsTemplate = template.Must(template.New("hosts").Funcs(template.FuncMap{
|
||||||
|
"StringsJoin": strings.Join,
|
||||||
|
}).Parse(`127.0.0.1 localhost
|
||||||
|
|
||||||
|
# The following lines are desirable for IPv6 capable hosts
|
||||||
|
::1 ip6-localhost ip6-loopback
|
||||||
|
fe00::0 ip6-localnet
|
||||||
|
ff00::0 ip6-mcastprefix
|
||||||
|
ff02::1 ip6-allnodes
|
||||||
|
ff02::2 ip6-allrouters
|
||||||
|
ff02::3 ip6-allhosts
|
||||||
|
|
||||||
|
{{range .Ring1 -}}
|
||||||
|
{{.Addr}} {{StringsJoin .Names " "}}
|
||||||
|
{{end}}
|
||||||
|
{{range .Ring0 -}}
|
||||||
|
{{.Addr}} {{StringsJoin .Names " "}}
|
||||||
|
{{end -}}
|
||||||
|
`))
|
||||||
|
|
||||||
|
// WriteHosts rewrites all hosts files on the tree
|
||||||
|
func (m *Cluster) WriteHosts() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = z.WriteHosts()
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteHosts rewrites all hosts files in the zone
|
||||||
|
func (z *Zone) WriteHosts() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
s := z.Hosts()
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
err = p.WriteStringFile(s, "hosts")
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteHosts rewrites the hosts file
|
||||||
|
func (p *Machine) WriteHosts() error {
|
||||||
|
s := p.zone.Hosts()
|
||||||
|
return p.WriteStringFile(s, "hosts")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *Zone) genHosts(out *hostsFile, p *Machine) {
|
||||||
|
var names []string
|
||||||
|
|
||||||
|
ip, _ := RingOneAddress(p.zone.ID, p.ID)
|
||||||
|
names = append(names, p.Name)
|
||||||
|
|
||||||
|
if p.CephMonitor {
|
||||||
|
names = append(names, fmt.Sprintf("%s-%s", p.zone.Name, "ceph"))
|
||||||
|
names = append(names, fmt.Sprintf("%s-%s", p.zone.Name, "k3s"))
|
||||||
|
|
||||||
|
if z.ID == p.zone.ID {
|
||||||
|
names = append(names, "ceph")
|
||||||
|
names = append(names, "k3s")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := hostsEntry{
|
||||||
|
Addr: ip.String(),
|
||||||
|
Names: names,
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Ring1 = append(out.Ring1, entry)
|
||||||
|
|
||||||
|
if p.IsGateway() {
|
||||||
|
var s string
|
||||||
|
|
||||||
|
ip, _ = RingZeroAddress(p.zone.ID, p.ID)
|
||||||
|
s = fmt.Sprintf("%s-%v", p.Name, 0)
|
||||||
|
|
||||||
|
entry = hostsEntry{
|
||||||
|
Addr: ip.String(),
|
||||||
|
Names: []string{s},
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Ring0 = append(out.Ring0, entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hosts renders the /etc/hosts to be used on this zone
|
||||||
|
func (z *Zone) Hosts() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var out hostsFile
|
||||||
|
|
||||||
|
z.zones.ForEachZone(func(z2 *Zone) bool {
|
||||||
|
z2.ForEachMachine(func(p *Machine) bool {
|
||||||
|
z.genHosts(&out, p)
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := hostsTemplate.Execute(&buf, &out); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import "darvaza.org/slog"
|
import "darvaza.org/slog"
|
||||||
|
|
||||||
@@ -13,26 +13,26 @@ type logger interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ logger = (*Zones)(nil)
|
_ logger = (*Cluster)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
func (z *Zones) withDebug() (slog.Logger, bool) {
|
func (z *Cluster) withDebug() (slog.Logger, bool) {
|
||||||
return z.debug().WithEnabled()
|
return z.debug().WithEnabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zones) withInfo() (slog.Logger, bool) {
|
func (z *Cluster) withInfo() (slog.Logger, bool) {
|
||||||
return z.debug().WithEnabled()
|
return z.debug().WithEnabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zones) debug() slog.Logger {
|
func (z *Cluster) debug() slog.Logger {
|
||||||
return z.log.Debug()
|
return z.log.Debug()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zones) info() slog.Logger {
|
func (z *Cluster) info() slog.Logger {
|
||||||
return z.log.Info()
|
return z.log.Info()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zones) warn(err error) slog.Logger {
|
func (z *Cluster) warn(err error) slog.Logger {
|
||||||
l := z.log.Warn()
|
l := z.log.Warn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l = l.WithField(slog.ErrorFieldName, err)
|
l = l.WithField(slog.ErrorFieldName, err)
|
||||||
@@ -40,7 +40,7 @@ func (z *Zones) warn(err error) slog.Logger {
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zones) error(err error) slog.Logger {
|
func (z *Cluster) error(err error) slog.Logger {
|
||||||
l := z.log.Error()
|
l := z.log.Error()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l = l.WithField(slog.ErrorFieldName, err)
|
l = l.WithField(slog.ErrorFieldName, err)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -9,16 +9,15 @@ import (
|
|||||||
|
|
||||||
// A Machine is a machine on a Zone
|
// A Machine is a machine on a Zone
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
zone *Zone
|
zone *Zone
|
||||||
logger
|
logger `json:"-" yaml:"-"`
|
||||||
|
|
||||||
ID int `toml:"id"`
|
ID int
|
||||||
Name string `toml:"-" json:"-" yaml:"-"`
|
Name string `json:"-" yaml:"-"`
|
||||||
|
|
||||||
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
|
||||||
Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,omitempty"`
|
PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"`
|
||||||
|
Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"`
|
||||||
CephMonitor bool `toml:"ceph_monitor,omitempty" json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// revive:enable:line-length-limit
|
// revive:enable:line-length-limit
|
||||||
@@ -29,16 +28,19 @@ func (m *Machine) String() string {
|
|||||||
|
|
||||||
// FullName returns the Name of the machine including domain name
|
// FullName returns the Name of the machine including domain name
|
||||||
func (m *Machine) FullName() string {
|
func (m *Machine) FullName() string {
|
||||||
if domain := m.zone.zones.domain; domain != "" {
|
var name []string
|
||||||
var s = []string{
|
|
||||||
m.Name,
|
|
||||||
domain,
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(s, ".")
|
for _, s := range []string{
|
||||||
|
m.Name,
|
||||||
|
m.zone.zones.Name,
|
||||||
|
m.zone.zones.Domain,
|
||||||
|
} {
|
||||||
|
if s != "" {
|
||||||
|
name = append(name, s)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.Name
|
return strings.Join(name, ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsGateway tells if the Machine is a ring0 gateway
|
// IsGateway tells if the Machine is a ring0 gateway
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -121,23 +122,30 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
|||||||
addr := wg.GetAddress()
|
addr := wg.GetAddress()
|
||||||
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%s: invalid wg%v address: %s", m.Name, ring, addr)
|
return fmt.Errorf("%s: invalid address", addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||||
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
return core.Wrapf(err, "%s: invalid address", addr)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
||||||
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
return core.Wrap(err, "interface")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, peer := range wg.Peer {
|
for _, peer := range wg.Peer {
|
||||||
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
|
err := m.applyWireguardPeerConfig(ring, peer)
|
||||||
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
switch {
|
||||||
return err
|
case errors.Is(err, ErrUnknownNode):
|
||||||
|
// ignore unknown peers
|
||||||
|
m.warn(nil).
|
||||||
|
WithField("subsystem", "wireguard").
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("peer", peer.Endpoint.Host).
|
||||||
|
WithField("ring", ring).
|
||||||
|
Print("ignoring unknown endpoint")
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, "peer")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,6 +166,10 @@ func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
|||||||
cur, _ := m.getRingInfo(ring)
|
cur, _ := m.getRingInfo(ring)
|
||||||
if cur == nil {
|
if cur == nil {
|
||||||
// first, append
|
// first, append
|
||||||
|
m.debug().
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("ring", ring).
|
||||||
|
Print("found")
|
||||||
m.Rings = append(m.Rings, new)
|
m.Rings = append(m.Rings, new)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -183,8 +195,10 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
|
|||||||
switch {
|
switch {
|
||||||
case !found:
|
case !found:
|
||||||
// unknown
|
// unknown
|
||||||
|
return core.Wrap(ErrUnknownNode, pc.Endpoint.Host)
|
||||||
case ring == 1 && m.zone != peer.zone:
|
case ring == 1 && m.zone != peer.zone:
|
||||||
// invalid zone
|
// invalid zone
|
||||||
|
return core.Wrap(ErrInvalidNode, peer.Name)
|
||||||
default:
|
default:
|
||||||
// apply RingInfo
|
// apply RingInfo
|
||||||
ri := &RingInfo{
|
ri := &RingInfo{
|
||||||
@@ -197,8 +211,6 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
|
|||||||
|
|
||||||
return peer.applyRingInfo(ring, ri)
|
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 {
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LookupNetIP uses the DNS Resolver to get the public addresses associated
|
// LookupNetIP uses the DNS Resolver to get the public addresses associated
|
||||||
@@ -30,21 +33,32 @@ func (m *Machine) UpdatePublicAddresses() error {
|
|||||||
|
|
||||||
func (m *Machine) init() error {
|
func (m *Machine) init() error {
|
||||||
if err := m.setID(); err != nil {
|
if err := m.setID(); err != nil {
|
||||||
return err
|
return core.Wrap(err, m.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < RingsCount; i++ {
|
for i := 0; i < RingsCount; i++ {
|
||||||
if err := m.tryReadWireguardKeys(i); err != nil {
|
if err := m.tryReadWireguardKeys(i); err != nil {
|
||||||
return err
|
return core.Wrap(err, m.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) setID() error {
|
func (m *Machine) setID() error {
|
||||||
zoneName := m.zone.Name
|
zoneName := m.zone.Name
|
||||||
suffix := m.Name[len(zoneName)+1:]
|
|
||||||
|
|
||||||
|
l := len(zoneName)
|
||||||
|
switch {
|
||||||
|
case len(m.Name) < l+2:
|
||||||
|
return ErrInvalidName
|
||||||
|
case !strings.HasPrefix(m.Name, zoneName):
|
||||||
|
return ErrInvalidName
|
||||||
|
case m.Name[l] != '-':
|
||||||
|
return ErrInvalidName
|
||||||
|
}
|
||||||
|
|
||||||
|
suffix := m.Name[l+1:]
|
||||||
id, err := strconv.ParseInt(suffix, 10, 8)
|
id, err := strconv.ParseInt(suffix, 10, 8)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -57,6 +71,11 @@ func (m *Machine) setID() error {
|
|||||||
func (m *Machine) scan(opts *ScanOptions) error {
|
func (m *Machine) scan(opts *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).
|
||||||
|
WithField("subsystem", "wireguard").
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("ring", i).
|
||||||
|
Print()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = Machines(nil)
|
||||||
|
_ sort.Interface = Machines(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// A MachineIterator is a set of Machines we can iterate on
|
||||||
|
type MachineIterator interface {
|
||||||
|
ForEachMachine(func(*Machine) 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)
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = (*Region)(nil)
|
||||||
|
_ ZoneIterator = (*Region)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Region represents a group of zones geographically related
|
||||||
|
type Region struct {
|
||||||
|
m *Cluster
|
||||||
|
zones []*Zone
|
||||||
|
|
||||||
|
Name string
|
||||||
|
Regions []string `json:",omitempty" yaml:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachRegion calls a function for each Region of the cluster
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (m *Cluster) ForEachRegion(fn func(r *Region) bool) {
|
||||||
|
for i := range m.Regions {
|
||||||
|
r := &m.Regions[i]
|
||||||
|
if fn(r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachMachine calls a function for each Machine in the region
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (r *Region) ForEachMachine(fn func(*Machine) bool) {
|
||||||
|
r.ForEachZone(func(z *Zone) bool {
|
||||||
|
var term bool
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
term = fn(p)
|
||||||
|
return term
|
||||||
|
})
|
||||||
|
|
||||||
|
return term
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachZone calls a function for each Zone in the region
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (r *Region) ForEachZone(fn func(*Zone) bool) {
|
||||||
|
for _, p := range r.zones {
|
||||||
|
if fn(p) {
|
||||||
|
// terminate
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) initRegions(_ *ScanOptions) error {
|
||||||
|
regions := make(map[string][]*Zone)
|
||||||
|
|
||||||
|
// first regions defined by zones
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
for _, region := range z.Regions {
|
||||||
|
regions[region] = append(regions[region], z)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
// bind first level regions and their zones
|
||||||
|
for name, zones := range regions {
|
||||||
|
m.syncRegions(name, zones...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// and combine zones to produce larger regions
|
||||||
|
for i := range m.Regions {
|
||||||
|
r := &m.Regions[i]
|
||||||
|
m.finishRegion(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) syncRegions(name string, zones ...*Zone) {
|
||||||
|
for _, r := range m.Regions {
|
||||||
|
if r.Name == name {
|
||||||
|
// found
|
||||||
|
r.m = m
|
||||||
|
r.zones = zones
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// new
|
||||||
|
m.Regions = append(m.Regions, Region{
|
||||||
|
m: m,
|
||||||
|
zones: zones,
|
||||||
|
Name: name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) finishRegion(r *Region) {
|
||||||
|
if r.m != nil {
|
||||||
|
// ready
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.m = m
|
||||||
|
sub := []string{}
|
||||||
|
for _, name := range r.Regions {
|
||||||
|
r2, ok := m.getRegion(name)
|
||||||
|
if !ok {
|
||||||
|
m.warn(nil).WithField("region", name).Print("unknown region")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
sub = append(sub, r2.Name)
|
||||||
|
r.zones = append(r.zones, r2.zones...)
|
||||||
|
}
|
||||||
|
r.Regions = sub
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) getRegion(name string) (*Region, bool) {
|
||||||
|
for i := range m.Regions {
|
||||||
|
r := &m.Regions[i]
|
||||||
|
|
||||||
|
if name == r.Name {
|
||||||
|
m.finishRegion(r)
|
||||||
|
return r, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -24,9 +24,9 @@ const (
|
|||||||
// RingInfo contains represents the Wireguard endpoint details
|
// RingInfo contains represents the Wireguard endpoint details
|
||||||
// for a Machine on a particular ring
|
// for a Machine on a particular ring
|
||||||
type RingInfo struct {
|
type RingInfo struct {
|
||||||
Ring int `toml:"ring"`
|
Ring int
|
||||||
Enabled bool `toml:"enabled,omitempty"`
|
Enabled bool
|
||||||
Keys wireguard.KeyPair `toml:"keys,omitempty"`
|
Keys wireguard.KeyPair
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge attempts to combine two RingInfo structs
|
// Merge attempts to combine two RingInfo structs
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
// SyncAll updates all config files
|
// SyncAll updates all config files
|
||||||
func (m *Zones) SyncAll() error {
|
func (m *Cluster) SyncAll() error {
|
||||||
for _, fn := range []func() error{
|
for _, fn := range []func() error{
|
||||||
m.SyncAllWireguard,
|
m.SyncAllWireguard,
|
||||||
m.SyncAllCeph,
|
m.SyncAllCeph,
|
||||||
|
m.WriteHosts,
|
||||||
} {
|
} {
|
||||||
if err := fn(); err != nil {
|
if err := fn(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -15,7 +16,7 @@ func (m *Zones) SyncAll() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SyncAllWireguard updates all wireguard config files
|
// SyncAllWireguard updates all wireguard config files
|
||||||
func (m *Zones) SyncAllWireguard() error {
|
func (m *Cluster) SyncAllWireguard() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
for ring := 0; ring < RingsCount; ring++ {
|
for ring := 0; ring < RingsCount; ring++ {
|
||||||
@@ -34,7 +35,7 @@ func (m *Zones) SyncAllWireguard() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SyncAllCeph updates the ceph.conf file
|
// SyncAllCeph updates the ceph.conf file
|
||||||
func (m *Zones) SyncAllCeph() error {
|
func (m *Cluster) SyncAllCeph() error {
|
||||||
cfg, err := m.GenCephConfig()
|
cfg, err := m.GenCephConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package zones
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -6,19 +6,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ WireguardConfigPruner = (*Zones)(nil)
|
_ WireguardConfigPruner = (*Cluster)(nil)
|
||||||
_ WireguardConfigPruner = (*Zone)(nil)
|
_ WireguardConfigPruner = (*Zone)(nil)
|
||||||
_ WireguardConfigPruner = (*Machine)(nil)
|
_ WireguardConfigPruner = (*Machine)(nil)
|
||||||
|
|
||||||
_ WireguardConfigWriter = (*Zones)(nil)
|
_ WireguardConfigWriter = (*Cluster)(nil)
|
||||||
_ WireguardConfigWriter = (*Zone)(nil)
|
_ WireguardConfigWriter = (*Zone)(nil)
|
||||||
_ WireguardConfigWriter = (*Machine)(nil)
|
_ WireguardConfigWriter = (*Machine)(nil)
|
||||||
|
|
||||||
_ WireguardConfigSyncer = (*Zones)(nil)
|
_ WireguardConfigSyncer = (*Cluster)(nil)
|
||||||
_ WireguardConfigSyncer = (*Zone)(nil)
|
_ WireguardConfigSyncer = (*Zone)(nil)
|
||||||
_ WireguardConfigSyncer = (*Machine)(nil)
|
_ WireguardConfigSyncer = (*Machine)(nil)
|
||||||
|
|
||||||
_ WireguardKeysWriter = (*Zones)(nil)
|
_ WireguardKeysWriter = (*Cluster)(nil)
|
||||||
_ WireguardKeysWriter = (*Zone)(nil)
|
_ WireguardKeysWriter = (*Zone)(nil)
|
||||||
_ WireguardKeysWriter = (*Machine)(nil)
|
_ WireguardKeysWriter = (*Machine)(nil)
|
||||||
)
|
)
|
||||||
@@ -31,7 +31,7 @@ type WireguardConfigPruner interface {
|
|||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||||
// the corresponding ring disabled on all zones
|
// the corresponding ring disabled on all zones
|
||||||
func (m *Zones) PruneWireguardConfig(ring int) error {
|
func (m *Cluster) PruneWireguardConfig(ring int) error {
|
||||||
return pruneWireguardConfig(m, ring)
|
return pruneWireguardConfig(m, ring)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ type WireguardConfigWriter interface {
|
|||||||
|
|
||||||
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
||||||
// attached to that ring
|
// attached to that ring
|
||||||
func (m *Zones) WriteWireguardConfig(ring int) error {
|
func (m *Cluster) WriteWireguardConfig(ring int) error {
|
||||||
switch ring {
|
switch ring {
|
||||||
case 0:
|
case 0:
|
||||||
return writeWireguardConfig(m, m, ring)
|
return writeWireguardConfig(m, m, ring)
|
||||||
@@ -154,7 +154,7 @@ type WireguardConfigSyncer interface {
|
|||||||
|
|
||||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
// ring
|
// ring
|
||||||
func (m *Zones) SyncWireguardConfig(ring int) error {
|
func (m *Cluster) SyncWireguardConfig(ring int) error {
|
||||||
switch ring {
|
switch ring {
|
||||||
case 0:
|
case 0:
|
||||||
return syncWireguardConfig(m, m, ring)
|
return syncWireguardConfig(m, m, ring)
|
||||||
@@ -214,7 +214,7 @@ type WireguardKeysWriter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
||||||
func (m *Zones) WriteWireguardKeys(ring int) error {
|
func (m *Cluster) WriteWireguardKeys(ring int) error {
|
||||||
return writeWireguardKeys(m, ring)
|
return writeWireguardKeys(m, ring)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = (*Zone)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// A ZoneIterator is a set of Zones we can iterate on
|
||||||
|
type ZoneIterator interface {
|
||||||
|
ForEachZone(func(*Zone) bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Zone is a set of machines in close proximity and strong
|
||||||
|
// affinity.
|
||||||
|
type Zone struct {
|
||||||
|
zones *Cluster
|
||||||
|
logger `json:"-" yaml:"-"`
|
||||||
|
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
Regions []string `json:",omitempty" yaml:",omitempty"`
|
||||||
|
|
||||||
|
Machines
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *Zone) String() string {
|
||||||
|
return z.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||||
|
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||||
|
var err error
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.ID == gatewayID {
|
||||||
|
found = true
|
||||||
|
err = p.SetGateway(enabled)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
case !found:
|
||||||
|
return fs.ErrNotExist
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
// Package dns manages DNS entries for the cluster
|
||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Zone represents a set of hosts with high affinity
|
||||||
|
type Zone struct {
|
||||||
|
Name string
|
||||||
|
|
||||||
|
Hosts map[int]*Host
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *Zone) String() string {
|
||||||
|
if z == nil {
|
||||||
|
return "undetermined"
|
||||||
|
}
|
||||||
|
return z.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host represents a member of the cluster
|
||||||
|
type Host struct {
|
||||||
|
zone *Zone
|
||||||
|
|
||||||
|
ID int
|
||||||
|
Active bool
|
||||||
|
Addrs []netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Host) String() string {
|
||||||
|
if p == nil {
|
||||||
|
return "undetermined"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s-%v", p.zone, p.ID)
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
|
"net/netip"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
"darvaza.org/slog"
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
|
"golang.org/x/net/publicsuffix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Manager is a DNS Manager instance
|
||||||
|
type Manager struct {
|
||||||
|
domain string
|
||||||
|
suffix string
|
||||||
|
zones map[string]*Zone
|
||||||
|
regions map[string][]string
|
||||||
|
|
||||||
|
p Provider
|
||||||
|
l slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// ManagerOption configures a Manager
|
||||||
|
type ManagerOption func(*Manager) error
|
||||||
|
|
||||||
|
func newErrorManagerOption(err error, hint string) ManagerOption {
|
||||||
|
return func(*Manager) error {
|
||||||
|
return core.Wrap(err, hint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithProvider attaches a libdns Provider to the Manager
|
||||||
|
func WithProvider(p Provider) ManagerOption {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if p == nil {
|
||||||
|
p, err = DefaultDNSProvider()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return newErrorManagerOption(err, "WithProvider")
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(mgr *Manager) error {
|
||||||
|
mgr.p = p
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithLogger attaches a logger to the Manager
|
||||||
|
func WithLogger(log slog.Logger) ManagerOption {
|
||||||
|
if log == nil {
|
||||||
|
log = cluster.DefaultLogger()
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(mgr *Manager) error {
|
||||||
|
mgr.l = log
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) setDefaults() error {
|
||||||
|
var opts []ManagerOption
|
||||||
|
|
||||||
|
if mgr.l == nil {
|
||||||
|
opts = append(opts, WithLogger(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
if mgr.domain == "" || mgr.suffix == "" {
|
||||||
|
return errors.New("domain not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if err := opt(mgr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDomain specifies where the manager operates
|
||||||
|
func WithDomain(domain string) ManagerOption {
|
||||||
|
base, err := publicsuffix.EffectiveTLDPlusOne(domain)
|
||||||
|
if err != nil {
|
||||||
|
return newErrorManagerOption(err, "publicsuffix")
|
||||||
|
}
|
||||||
|
|
||||||
|
suffix := strings.TrimSuffix(domain, base)
|
||||||
|
if suffix != "" {
|
||||||
|
suffix = "." + suffix[:len(suffix)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(mgr *Manager) error {
|
||||||
|
mgr.domain = base
|
||||||
|
mgr.suffix = suffix
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewManager creates a DNS manager with the
|
||||||
|
func NewManager(opts ...ManagerOption) (*Manager, error) {
|
||||||
|
mgr := &Manager{
|
||||||
|
zones: make(map[string]*Zone),
|
||||||
|
regions: make(map[string][]string),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if err := opt(mgr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mgr.setDefaults(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return mgr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddHost registers a host
|
||||||
|
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
|
||||||
|
active bool, addrs ...netip.Addr) error {
|
||||||
|
//
|
||||||
|
if zone == "" || id <= 0 {
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
z, ok := mgr.zones[zone]
|
||||||
|
if !ok {
|
||||||
|
z = &Zone{
|
||||||
|
Name: zone,
|
||||||
|
Hosts: make(map[int]*Host),
|
||||||
|
}
|
||||||
|
|
||||||
|
mgr.zones[zone] = z
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &Host{
|
||||||
|
zone: z,
|
||||||
|
ID: id,
|
||||||
|
Active: active,
|
||||||
|
Addrs: SortAddrSlice(addrs),
|
||||||
|
}
|
||||||
|
z.Hosts[id] = p
|
||||||
|
|
||||||
|
if log, ok := mgr.l.Debug().WithEnabled(); ok {
|
||||||
|
log.WithField("subsystem", "dns").
|
||||||
|
WithField("zone", zone).
|
||||||
|
WithField("host", p.String()).
|
||||||
|
WithField("active", active).
|
||||||
|
Print()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddRegion specifies a new region and the zones it contains
|
||||||
|
func (mgr *Manager) AddRegion(_ context.Context, region string, zones ...string) error {
|
||||||
|
mgr.regions[region] = append(mgr.regions[region], zones...)
|
||||||
|
|
||||||
|
if log, ok := mgr.l.Debug().WithEnabled(); ok {
|
||||||
|
for _, zoneName := range zones {
|
||||||
|
log.WithField("subsystem", "dns").
|
||||||
|
WithField("region", region).
|
||||||
|
WithField("zone", zoneName).Print()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/libdns/cloudflare"
|
||||||
|
"github.com/libdns/libdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// CloudflareAPIToken is the environment variable
|
||||||
|
// containing the API Token
|
||||||
|
CloudflareAPIToken = "CLOUDFLARE_DNS_API_TOKEN"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Provider manages DNS entries
|
||||||
|
type Provider interface {
|
||||||
|
libdns.RecordGetter
|
||||||
|
libdns.RecordDeleter
|
||||||
|
libdns.RecordSetter
|
||||||
|
libdns.RecordAppender
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultDNSProvider returns a cloudflare DNS provider
|
||||||
|
// using an API Token from env [CloudflareAPIToken]
|
||||||
|
func DefaultDNSProvider() (*cloudflare.Provider, error) {
|
||||||
|
s := os.Getenv(CloudflareAPIToken)
|
||||||
|
if s == "" {
|
||||||
|
return nil, fmt.Errorf("%q: %s", CloudflareAPIToken, "not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &cloudflare.Provider{
|
||||||
|
APIToken: s,
|
||||||
|
}
|
||||||
|
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/netip"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
"github.com/libdns/libdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (mgr *Manager) fqdn(name string) string {
|
||||||
|
return fmt.Sprintf("%s.%s.", name, mgr.domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortAddrSlice sorts a slice of [netip.Addr]
|
||||||
|
func SortAddrSlice(s []netip.Addr) []netip.Addr {
|
||||||
|
sort.Slice(s, func(i, j int) bool {
|
||||||
|
return s[i].Less(s[j])
|
||||||
|
})
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortAddrRecords sorts a slice of [AddrRecord]
|
||||||
|
// by Name and Address
|
||||||
|
func SortAddrRecords(s []AddrRecord) []AddrRecord {
|
||||||
|
sort.Slice(s, func(i, j int) bool {
|
||||||
|
return s[i].Name < s[j].Name
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, p := range s {
|
||||||
|
SortAddrSlice(p.Addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
type AddrRecord struct {
|
||||||
|
Name string
|
||||||
|
Addr []netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort sorts the addresses of the record
|
||||||
|
func (rr *AddrRecord) Sort() {
|
||||||
|
SortAddrSlice(rr.Addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export converts the record into libdns.Record
|
||||||
|
func (rr *AddrRecord) Export() []libdns.Record {
|
||||||
|
out := make([]libdns.Record, len(rr.Addr))
|
||||||
|
for i, addr := range rr.Addr {
|
||||||
|
out[i] = libdns.Record{
|
||||||
|
Name: rr.Name,
|
||||||
|
TTL: time.Second * 1,
|
||||||
|
Type: core.IIf(addr.Is6(), "AAAA", "A"),
|
||||||
|
Value: addr.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes the record in BIND notation
|
||||||
|
func (rr *AddrRecord) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
var total int
|
||||||
|
for _, addr := range rr.Addr {
|
||||||
|
n, err := fmt.Fprint(w,
|
||||||
|
rr.Name, "\t",
|
||||||
|
1, "\t",
|
||||||
|
core.IIf(addr.Is6(), "AAAA", "A"), "\t",
|
||||||
|
addr.String(), "\n")
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return 0, err
|
||||||
|
case n > 0:
|
||||||
|
total += n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(total), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String converts the record into BIND entries
|
||||||
|
func (rr *AddrRecord) String() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, _ = rr.WriteTo(&buf)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) genRegionsSorted() []string {
|
||||||
|
regions := make([]string, 0, len(mgr.regions))
|
||||||
|
for name := range mgr.regions {
|
||||||
|
regions = append(regions, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return SortRegions(regions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
|
||||||
|
var out []AddrRecord
|
||||||
|
|
||||||
|
cache := make(map[string][]netip.Addr)
|
||||||
|
|
||||||
|
// zones
|
||||||
|
for _, z := range mgr.zones {
|
||||||
|
// hosts
|
||||||
|
s := mgr.genZoneHostRecords(z)
|
||||||
|
out = append(out, s...)
|
||||||
|
|
||||||
|
// zone alias
|
||||||
|
addrs := mgr.genZoneAddresses(z)
|
||||||
|
name := z.Name
|
||||||
|
|
||||||
|
out = append(out, AddrRecord{
|
||||||
|
Name: name + mgr.suffix,
|
||||||
|
Addr: addrs,
|
||||||
|
})
|
||||||
|
|
||||||
|
// and cache for regions
|
||||||
|
cache[name] = addrs
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range mgr.genRegionsSorted() {
|
||||||
|
var addrs []netip.Addr
|
||||||
|
|
||||||
|
for _, z := range mgr.regions[name] {
|
||||||
|
addrs = append(addrs, cache[z]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
rec := AddrRecord{
|
||||||
|
Name: name + mgr.suffix,
|
||||||
|
Addr: addrs,
|
||||||
|
}
|
||||||
|
rec.Sort()
|
||||||
|
|
||||||
|
out = append(out, rec)
|
||||||
|
}
|
||||||
|
|
||||||
|
SortAddrRecords(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Manager) genZoneAddresses(z *Zone) []netip.Addr {
|
||||||
|
var out []netip.Addr
|
||||||
|
|
||||||
|
for _, p := range z.Hosts {
|
||||||
|
if p.Active {
|
||||||
|
out = append(out, p.Addrs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SortAddrSlice(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) genZoneHostRecords(z *Zone) []AddrRecord {
|
||||||
|
out := make([]AddrRecord, 0, len(z.Hosts))
|
||||||
|
|
||||||
|
for _, p := range z.Hosts {
|
||||||
|
rec := AddrRecord{
|
||||||
|
Name: p.String() + mgr.suffix,
|
||||||
|
Addr: p.Addrs,
|
||||||
|
}
|
||||||
|
out = append(out, rec)
|
||||||
|
}
|
||||||
|
|
||||||
|
SortAddrRecords(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) genRegionAddressesCached(name string,
|
||||||
|
zones map[string][]netip.Addr) []netip.Addr {
|
||||||
|
//
|
||||||
|
var addrs []netip.Addr
|
||||||
|
|
||||||
|
for _, zoneName := range mgr.regions[name] {
|
||||||
|
addrs = append(addrs, zones[zoneName]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
SortAddrSlice(addrs)
|
||||||
|
return addrs
|
||||||
|
}
|
||||||
+347
@@ -0,0 +1,347 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/netip"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
"darvaza.org/slog"
|
||||||
|
"github.com/libdns/libdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SyncAddrRecord is similar to AddrRecord but include libdns.Record details
|
||||||
|
// fetched from the Provider
|
||||||
|
type SyncAddrRecord struct {
|
||||||
|
Name string
|
||||||
|
Addrs []SyncAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncAddr extends netip.Addr with ID and TTL fetched from the Provider
|
||||||
|
type SyncAddr struct {
|
||||||
|
ID string
|
||||||
|
Addr netip.Addr
|
||||||
|
TTL time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export assembles a libdns.Record
|
||||||
|
func (rec *SyncAddr) Export(name string) libdns.Record {
|
||||||
|
return libdns.Record{
|
||||||
|
ID: rec.ID,
|
||||||
|
Name: name,
|
||||||
|
Type: core.IIf(rec.Addr.Is6(), "AAAA", "A"),
|
||||||
|
TTL: time.Second,
|
||||||
|
Value: rec.Addr.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortSyncAddrSlice sorts a slice of [SyncAddr] by its address
|
||||||
|
func SortSyncAddrSlice(s []SyncAddr) []SyncAddr {
|
||||||
|
sort.Slice(s, func(i, j int) bool {
|
||||||
|
a1 := s[i].Addr
|
||||||
|
a2 := s[j].Addr
|
||||||
|
return a1.Less(a2)
|
||||||
|
})
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRecords pulls all the address records on DNS for our domain
|
||||||
|
func (mgr *Manager) GetRecords(ctx context.Context) ([]SyncAddrRecord, error) {
|
||||||
|
if mgr.p == nil {
|
||||||
|
return nil, errors.New("dns provider not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mgr.filteredRecords(recs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
|
||||||
|
func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
|
||||||
|
var out SyncAddr
|
||||||
|
var addr netip.Addr
|
||||||
|
|
||||||
|
// skip non-address types
|
||||||
|
if rr.Type != "A" && rr.Type != "AAAA" {
|
||||||
|
return out, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip entries not containing our suffix
|
||||||
|
if mgr.suffix != "" {
|
||||||
|
if !strings.HasSuffix(rr.Name, mgr.suffix) {
|
||||||
|
return out, false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := addr.UnmarshalText([]byte(rr.Value))
|
||||||
|
if err != nil {
|
||||||
|
// invalid address on A or AAAA record
|
||||||
|
return out, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
out = SyncAddr{
|
||||||
|
ID: rr.ID,
|
||||||
|
TTL: rr.TTL,
|
||||||
|
Addr: addr,
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
||||||
|
// filter and convert
|
||||||
|
cache := make(map[string][]SyncAddr)
|
||||||
|
for _, rr := range recs {
|
||||||
|
addr, ok, err := mgr.AsSyncAddr(rr)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
// skip invalid addresses
|
||||||
|
mgr.l.Error().
|
||||||
|
WithField("subsystem", "dns").
|
||||||
|
WithField(slog.ErrorFieldName, err).
|
||||||
|
WithField("name", rr.Name).
|
||||||
|
WithField("type", rr.Type).
|
||||||
|
WithField("addr", rr.Value).
|
||||||
|
Print()
|
||||||
|
case ok:
|
||||||
|
// store
|
||||||
|
cache[rr.Name] = append(cache[rr.Name], addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare records
|
||||||
|
out := make([]SyncAddrRecord, len(cache))
|
||||||
|
names := make([]string, 0, len(cache))
|
||||||
|
for name := range cache {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
|
||||||
|
for i, name := range names {
|
||||||
|
addrs := cache[name]
|
||||||
|
|
||||||
|
out[i] = SyncAddrRecord{
|
||||||
|
Name: name,
|
||||||
|
Addrs: SortSyncAddrSlice(addrs),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sync updates all the address records on DNS for our domain
|
||||||
|
func (mgr *Manager) Sync(ctx context.Context) error {
|
||||||
|
current, err := mgr.GetRecords(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return core.Wrap(err, "GetRecords")
|
||||||
|
}
|
||||||
|
|
||||||
|
goal := mgr.genAllAddrRecords()
|
||||||
|
for _, p := range makeSyncMap(current, goal) {
|
||||||
|
err := mgr.doSync(ctx, p.Name, p.Before, p.After)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) doSync(ctx context.Context, name string,
|
||||||
|
before []SyncAddr, after []netip.Addr) error {
|
||||||
|
//
|
||||||
|
var err error
|
||||||
|
|
||||||
|
for _, a := range after {
|
||||||
|
before, err = mgr.doSyncUpdateOrInsert(ctx, name, a, before)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, b := range before {
|
||||||
|
err = mgr.doSyncRemove(ctx, name, b)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) doSyncUpdateOrInsert(ctx context.Context, name string,
|
||||||
|
addr netip.Addr, addrs []SyncAddr) ([]SyncAddr, error) {
|
||||||
|
//
|
||||||
|
var err error
|
||||||
|
|
||||||
|
i, ok := findSyncAddrSorted(addr, addrs)
|
||||||
|
if ok {
|
||||||
|
rec := addrs[i]
|
||||||
|
|
||||||
|
addrs = append(addrs[:i], addrs[i+1:]...)
|
||||||
|
err = mgr.doSyncUpdate(ctx, name, addr, rec)
|
||||||
|
} else {
|
||||||
|
err = mgr.doSyncInsert(ctx, name, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return addrs, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) doSyncUpdate(ctx context.Context, name string,
|
||||||
|
addr netip.Addr, rec SyncAddr) error {
|
||||||
|
//
|
||||||
|
var log slog.Logger
|
||||||
|
var msg string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if rec.TTL != time.Second {
|
||||||
|
// amend TTL
|
||||||
|
|
||||||
|
// TODO: batch updates
|
||||||
|
_, err = mgr.p.SetRecords(ctx, mgr.domain, []libdns.Record{
|
||||||
|
rec.Export(name),
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
log = mgr.l.Info()
|
||||||
|
msg = "Updated"
|
||||||
|
} else {
|
||||||
|
log = mgr.l.Error().
|
||||||
|
WithField(slog.ErrorFieldName, err)
|
||||||
|
msg = "Failed"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log = mgr.l.Info()
|
||||||
|
msg = "OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
log.
|
||||||
|
WithField("subsystem", "dns").
|
||||||
|
WithField("name", name).
|
||||||
|
WithField("addr", addr).
|
||||||
|
Print(msg)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) doSyncInsert(ctx context.Context, name string,
|
||||||
|
addr netip.Addr) error {
|
||||||
|
//
|
||||||
|
var log slog.Logger
|
||||||
|
var msg string
|
||||||
|
|
||||||
|
rec := libdns.Record{
|
||||||
|
Name: name,
|
||||||
|
Type: core.IIf(addr.Is6(), "AAAA", "A"),
|
||||||
|
TTL: time.Second,
|
||||||
|
Value: addr.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := mgr.p.AppendRecords(ctx, mgr.domain, []libdns.Record{
|
||||||
|
rec,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log = mgr.l.Error().
|
||||||
|
WithField(slog.ErrorFieldName, err)
|
||||||
|
msg = "Failed to Add"
|
||||||
|
} else {
|
||||||
|
log = mgr.l.Info()
|
||||||
|
msg = "Added"
|
||||||
|
}
|
||||||
|
|
||||||
|
log.
|
||||||
|
WithField("subsystem", "dns").
|
||||||
|
WithField("name", name).
|
||||||
|
WithField("addr", addr).
|
||||||
|
Print(msg)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) doSyncRemove(ctx context.Context, name string,
|
||||||
|
rec SyncAddr) error {
|
||||||
|
//
|
||||||
|
var log slog.Logger
|
||||||
|
var msg string
|
||||||
|
|
||||||
|
// TODO: batch deletes
|
||||||
|
_, err := mgr.p.DeleteRecords(ctx, mgr.domain, []libdns.Record{
|
||||||
|
rec.Export(name),
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log = mgr.l.Error().
|
||||||
|
WithField(slog.ErrorFieldName, err)
|
||||||
|
msg = "Failed to Delete"
|
||||||
|
} else {
|
||||||
|
log = mgr.l.Warn()
|
||||||
|
msg = "Deleted"
|
||||||
|
}
|
||||||
|
|
||||||
|
log.
|
||||||
|
WithField("subsystem", "dns").
|
||||||
|
WithField("name", name).
|
||||||
|
WithField("addr", rec.Addr).
|
||||||
|
Print(msg)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func findSyncAddrSorted(target netip.Addr, addrs []SyncAddr) (int, bool) {
|
||||||
|
for i, a := range addrs {
|
||||||
|
switch target.Compare(a.Addr) {
|
||||||
|
case 0:
|
||||||
|
// match
|
||||||
|
return i, true
|
||||||
|
case -1:
|
||||||
|
// miss
|
||||||
|
return -1, false
|
||||||
|
default:
|
||||||
|
// next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1, false
|
||||||
|
}
|
||||||
|
|
||||||
|
type syncMapEntry struct {
|
||||||
|
Name string
|
||||||
|
Before []SyncAddr
|
||||||
|
After []netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeSyncMap(current []SyncAddrRecord,
|
||||||
|
goal []AddrRecord) map[string]syncMapEntry {
|
||||||
|
//
|
||||||
|
data := make(map[string]syncMapEntry)
|
||||||
|
|
||||||
|
for _, cur := range current {
|
||||||
|
me, ok := data[cur.Name]
|
||||||
|
if !ok {
|
||||||
|
me = syncMapEntry{
|
||||||
|
Name: cur.Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
me.Before = append(me.Before, cur.Addrs...)
|
||||||
|
data[cur.Name] = me
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rr := range goal {
|
||||||
|
me, ok := data[rr.Name]
|
||||||
|
if !ok {
|
||||||
|
me = syncMapEntry{
|
||||||
|
Name: rr.Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
me.After = append(me.After, rr.Addr...)
|
||||||
|
data[rr.Name] = me
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/netip"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WriteTo writes the DNS data for the cluster
|
||||||
|
func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
cache := make(map[string][]netip.Addr)
|
||||||
|
|
||||||
|
// zones
|
||||||
|
for _, z := range mgr.zones {
|
||||||
|
mgr.writeZoneHosts(&buf, z)
|
||||||
|
|
||||||
|
// zone alias
|
||||||
|
addrs := mgr.genZoneAddresses(z)
|
||||||
|
zoneName := z.Name
|
||||||
|
|
||||||
|
rr := AddrRecord{
|
||||||
|
Name: mgr.fqdn(zoneName + mgr.suffix),
|
||||||
|
Addr: addrs,
|
||||||
|
}
|
||||||
|
_, _ = rr.WriteTo(&buf)
|
||||||
|
|
||||||
|
// and cache for regions
|
||||||
|
cache[zoneName] = addrs
|
||||||
|
}
|
||||||
|
|
||||||
|
// regions, sorted
|
||||||
|
for _, name := range mgr.genRegionsSorted() {
|
||||||
|
addrs := mgr.genRegionAddressesCached(name, cache)
|
||||||
|
|
||||||
|
mgr.writeRegionAddresses(&buf, name, addrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.WriteTo(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) writeZoneHosts(w io.Writer, z *Zone) {
|
||||||
|
_, _ = fmt.Fprintf(w, ";\n; %s\n;\n", z.Name)
|
||||||
|
|
||||||
|
for _, rr := range mgr.genZoneHostRecords(z) {
|
||||||
|
rr.Name = mgr.fqdn(rr.Name)
|
||||||
|
_, _ = rr.WriteTo(w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) writeRegionAddresses(w io.Writer, name string, addrs []netip.Addr) {
|
||||||
|
_, _ = fmt.Fprintf(w, "; %s\n", name)
|
||||||
|
|
||||||
|
rr := AddrRecord{
|
||||||
|
Name: mgr.fqdn(name + mgr.suffix),
|
||||||
|
Addr: addrs,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = rr.WriteTo(w)
|
||||||
|
}
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
package zones
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"darvaza.org/core"
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/ceph"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CephMissingMonitorError is an error that contains ceph
|
|
||||||
// monitors present in ceph.conf but not found on the cluster
|
|
||||||
type CephMissingMonitorError struct {
|
|
||||||
Names []string
|
|
||||||
Addrs []netip.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (err *CephMissingMonitorError) appendName(name string) {
|
|
||||||
err.Names = append(err.Names, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (err *CephMissingMonitorError) appendAddr(addr netip.Addr) {
|
|
||||||
err.Addrs = append(err.Addrs, addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// OK tells if this instance actual shouldn't be treated as an error
|
|
||||||
func (err CephMissingMonitorError) OK() bool {
|
|
||||||
switch {
|
|
||||||
case len(err.Names) > 0:
|
|
||||||
return false
|
|
||||||
case len(err.Addrs) > 0:
|
|
||||||
return false
|
|
||||||
default:
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (err CephMissingMonitorError) Error() string {
|
|
||||||
if !err.OK() {
|
|
||||||
var buf strings.Builder
|
|
||||||
|
|
||||||
_, _ = buf.WriteString("missing:")
|
|
||||||
err.writeNames(&buf)
|
|
||||||
err.writeAddrs(&buf)
|
|
||||||
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// no error
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (err *CephMissingMonitorError) writeNames(w *strings.Builder) {
|
|
||||||
if len(err.Names) > 0 {
|
|
||||||
_, _ = w.WriteString(" mon_initial_members:")
|
|
||||||
for i, name := range err.Names {
|
|
||||||
if i != 0 {
|
|
||||||
_, _ = w.WriteRune(',')
|
|
||||||
}
|
|
||||||
_, _ = w.WriteString(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (err *CephMissingMonitorError) writeAddrs(w *strings.Builder) {
|
|
||||||
if len(err.Addrs) > 0 {
|
|
||||||
_, _ = w.WriteString(" mon_host:")
|
|
||||||
for i, addr := range err.Addrs {
|
|
||||||
if i != 0 {
|
|
||||||
_, _ = w.WriteRune(',')
|
|
||||||
}
|
|
||||||
_, _ = w.WriteString(addr.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AsError returns nil if the instance is actually OK
|
|
||||||
func (err *CephMissingMonitorError) AsError() error {
|
|
||||||
if err == nil || err.OK() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
type cephScanTODO struct {
|
|
||||||
names map[string]bool
|
|
||||||
addrs map[string]bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (todo *cephScanTODO) checkMachine(p *Machine) bool {
|
|
||||||
// on ceph all addresses are ring1
|
|
||||||
ring1, _ := RingOneAddress(p.Zone(), p.ID)
|
|
||||||
addr := ring1.String()
|
|
||||||
|
|
||||||
if _, found := todo.names[p.Name]; found {
|
|
||||||
// found on the TODO by name
|
|
||||||
todo.names[p.Name] = true
|
|
||||||
todo.addrs[addr] = true
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, found := todo.addrs[addr]; found {
|
|
||||||
// found on the TODO by address
|
|
||||||
todo.names[p.Name] = true
|
|
||||||
todo.addrs[addr] = true
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (todo *cephScanTODO) Missing() error {
|
|
||||||
var check CephMissingMonitorError
|
|
||||||
|
|
||||||
for name, found := range todo.names {
|
|
||||||
if !found {
|
|
||||||
check.appendName(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for addr, found := range todo.addrs {
|
|
||||||
if !found {
|
|
||||||
var a netip.Addr
|
|
||||||
// it wouldn't be on the map if it wasn't valid
|
|
||||||
_ = a.UnmarshalText([]byte(addr))
|
|
||||||
|
|
||||||
check.appendAddr(a)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return check.AsError()
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
|
|
||||||
todo := &cephScanTODO{
|
|
||||||
names: make(map[string]bool),
|
|
||||||
addrs: make(map[string]bool),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, name := range cfg.Global.Monitors {
|
|
||||||
todo.names[name] = false
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, addr := range cfg.Global.MonitorsAddr {
|
|
||||||
todo.addrs[addr.String()] = false
|
|
||||||
}
|
|
||||||
|
|
||||||
return todo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Zones) scanCephMonitors(_ *ScanOptions) error {
|
|
||||||
cfg, err := m.GetCephConfig()
|
|
||||||
switch {
|
|
||||||
case os.IsNotExist(err):
|
|
||||||
err = nil
|
|
||||||
case err != nil:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg != nil {
|
|
||||||
// store FSID
|
|
||||||
m.CephFSID = cfg.Global.FSID
|
|
||||||
|
|
||||||
// flag monitors based on config
|
|
||||||
todo := newCephScanTODO(cfg)
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
p.CephMonitor = todo.checkMachine(p)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
if err := todo.Missing(); err != nil {
|
|
||||||
return core.Wrap(err, "ceph")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure every zone has one
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
_ = z.GetCephMonitors()
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
// Package zones contains information about the cluster
|
|
||||||
package zones
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/fs"
|
|
||||||
"sort"
|
|
||||||
|
|
||||||
"darvaza.org/resolver"
|
|
||||||
"darvaza.org/slog"
|
|
||||||
"github.com/gofrs/uuid/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ MachineIterator = Machines(nil)
|
|
||||||
_ sort.Interface = Machines(nil)
|
|
||||||
|
|
||||||
_ MachineIterator = (*Zone)(nil)
|
|
||||||
_ MachineIterator = (*Zones)(nil)
|
|
||||||
_ ZoneIterator = (*Zones)(nil)
|
|
||||||
)
|
|
||||||
|
|
||||||
// A MachineIterator is a set of Machines we can iterate on
|
|
||||||
type MachineIterator interface {
|
|
||||||
ForEachMachine(func(*Machine) bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A ZoneIterator is a set of Zones we can iterate on
|
|
||||||
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
|
|
||||||
logger
|
|
||||||
|
|
||||||
ID int `toml:"id"`
|
|
||||||
Name string `toml:"name"`
|
|
||||||
|
|
||||||
Machines `toml:"machines"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (z *Zone) String() string {
|
|
||||||
return z.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetGateway configures a machine to be the zone's ring0 gateway
|
|
||||||
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
|
||||||
var err error
|
|
||||||
var found bool
|
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
|
||||||
if p.ID == gatewayID {
|
|
||||||
found = true
|
|
||||||
err = p.SetGateway(enabled)
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case err != nil:
|
|
||||||
return err
|
|
||||||
case !found:
|
|
||||||
return fs.ErrNotExist
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// revive:disable:line-length-limit
|
|
||||||
|
|
||||||
// Zones represents all zones in a cluster
|
|
||||||
type Zones struct {
|
|
||||||
dir fs.FS
|
|
||||||
log slog.Logger
|
|
||||||
resolver resolver.Resolver
|
|
||||||
domain string
|
|
||||||
|
|
||||||
CephFSID uuid.UUID `toml:"ceph_fsid,omitempty" json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"`
|
|
||||||
|
|
||||||
Zones []*Zone `toml:"zones"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// revive:enable:line-length-limit
|
|
||||||
|
|
||||||
// ForEachMachine calls a function for each Machine in the cluster
|
|
||||||
// until instructed to terminate the loop
|
|
||||||
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
var term bool
|
|
||||||
|
|
||||||
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
|
|
||||||
// name on any zone
|
|
||||||
func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
|
|
||||||
var out *Machine
|
|
||||||
|
|
||||||
if name != "" {
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
if p.Name == name {
|
|
||||||
out = p
|
|
||||||
}
|
|
||||||
|
|
||||||
return out != nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, out != nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user