Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cf09cfa743 | |||
| 00cf3959a2 | |||
| 0db3e18227 | |||
| 0094450ca8 | |||
| a910bba406 | |||
| 5ef6d45ef7 | |||
| 99998dc7e8 | |||
| 892d849740 | |||
| 125a4c0dbe | |||
| 7c811d7813 | |||
| 1580c09746 | |||
| a928ab8880 | |||
| 66fc213f64 | |||
| 944400249f | |||
| 1492061ab8 | |||
| 57e1077a85 | |||
| 5a3d483f98 | |||
| 0ff17abd59 | |||
| a742bad084 | |||
| 629b6ee74f | |||
| 884b11d1f9 | |||
| 5bbe15ef24 | |||
| fd1c57d377 | |||
| 2fd5947f1b | |||
| 14b3d91191 | |||
| abe3005769 | |||
| 727fd67bc6 | |||
| b8e1b321e5 | |||
| 0c5429a681 | |||
| e5639b2f4e | |||
| 543824a54a | |||
| 9ab7594bcc | |||
| 07d4f462a3 | |||
| 142ea00577 | |||
| 052f89152c | |||
| 557f156579 | |||
| e857ff7456 | |||
| 9da49f2d86 | |||
| 356322bc94 | |||
| 7dac96f474 | |||
| 134606207d | |||
| 66178923a3 | |||
| 440dcde50a | |||
| c578990f8c | |||
| b0f4be7047 |
+40
-17
@@ -12,6 +12,14 @@ const (
|
|||||||
// DefaultConfigFile is read if -f/--config-file isn't specified.
|
// DefaultConfigFile is read if -f/--config-file isn't specified.
|
||||||
// If it doesn't exist, m/ will be scanned
|
// If it doesn't exist, m/ will be scanned
|
||||||
DefaultConfigFile = "cloud.yaml"
|
DefaultConfigFile = "cloud.yaml"
|
||||||
|
|
||||||
|
// DefaultClusterDir is the directory we will scan and write
|
||||||
|
// unless something else is indicated
|
||||||
|
DefaultClusterDir = "m"
|
||||||
|
|
||||||
|
// DefaultDomain indicates the domain to use unless
|
||||||
|
// something else is specified
|
||||||
|
DefaultDomain = "jpi.cloud"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config describes the repository
|
// Config describes the repository
|
||||||
@@ -22,27 +30,34 @@ type Config struct {
|
|||||||
ConfigFile string
|
ConfigFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var forceScan bool
|
||||||
|
|
||||||
var cfg = &Config{
|
var cfg = &Config{
|
||||||
Base: "m",
|
Base: DefaultClusterDir,
|
||||||
Domain: "jpi.cloud",
|
Domain: DefaultDomain,
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadZones loads all zones and machines in the config directory
|
// LoadZones loads all zones and machines in the config directory
|
||||||
// or file
|
// or file
|
||||||
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
|
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
|
||||||
// try config file first
|
var zones *cluster.Cluster
|
||||||
zones, err := cluster.NewFromConfig(cfg.ConfigFile,
|
var err error
|
||||||
cluster.ResolvePublicAddresses(resolve),
|
|
||||||
cluster.WithLogger(log),
|
|
||||||
)
|
|
||||||
|
|
||||||
switch {
|
if !forceScan {
|
||||||
case err == nil:
|
// try config file first
|
||||||
// file was good
|
zones, err = cluster.NewFromConfig(cfg.ConfigFile,
|
||||||
return zones, nil
|
cluster.ResolvePublicAddresses(resolve),
|
||||||
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
|
cluster.WithLogger(log),
|
||||||
// file was bad
|
)
|
||||||
return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
|
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
// file was good
|
||||||
|
return zones, nil
|
||||||
|
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
|
||||||
|
// file was bad
|
||||||
|
return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// default file doesn't exist. scan instead.
|
// default file doesn't exist. scan instead.
|
||||||
@@ -53,7 +68,15 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().
|
flags := rootCmd.PersistentFlags()
|
||||||
StringVarP(&cfg.ConfigFile, "config-file", "f",
|
|
||||||
DefaultConfigFile, "config file (JSON or YAML)")
|
flags.StringVarP(&cfg.Base, "scan-dir", "d",
|
||||||
|
DefaultClusterDir, "directory to scan for cluster data")
|
||||||
|
flags.StringVarP(&cfg.Domain, "domain", "D",
|
||||||
|
DefaultDomain, "domain to use for scanned data")
|
||||||
|
flags.StringVarP(&cfg.ConfigFile, "config-file", "f",
|
||||||
|
DefaultConfigFile, "config file (JSON or YAML)")
|
||||||
|
|
||||||
|
flags.BoolVarP(&forceScan, "force-scan", "S",
|
||||||
|
false, "ignore config file and scan the directory instead")
|
||||||
}
|
}
|
||||||
|
|||||||
+81
-20
@@ -2,9 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/cluster"
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
@@ -50,7 +52,7 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
|
|||||||
|
|
||||||
m.ForEachZone(func(z *cluster.Zone) bool {
|
m.ForEachZone(func(z *cluster.Zone) bool {
|
||||||
z.ForEachMachine(func(p *cluster.Machine) bool {
|
z.ForEachMachine(func(p *cluster.Machine) bool {
|
||||||
err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...)
|
err = mgr.AddHost(ctx, z.Name, p.ID, p.IsActive(), p.PublicAddresses...)
|
||||||
return err != nil
|
return err != nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -72,6 +74,29 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// revive:disable:flag-parameter
|
||||||
|
func newDNSManagerCommand(_ *cobra.Command,
|
||||||
|
resolve bool, withCredentials bool) (*dns.Manager, error) {
|
||||||
|
// revive:enable:flag-parameter
|
||||||
|
var cred dns.Provider
|
||||||
|
|
||||||
|
if withCredentials {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
cred, err = dns.DefaultDNSProvider()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := cfg.LoadZones(resolve)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return newDNSManager(m, cred)
|
||||||
|
}
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
var dnsCmd = &cobra.Command{
|
var dnsCmd = &cobra.Command{
|
||||||
Use: "dns",
|
Use: "dns",
|
||||||
@@ -81,13 +106,8 @@ var dnsWriteCmd = &cobra.Command{
|
|||||||
Use: "write",
|
Use: "write",
|
||||||
Short: "dns write generates public DNS records",
|
Short: "dns write generates public DNS records",
|
||||||
PreRun: setVerbosity,
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
m, err := cfg.LoadZones(true)
|
mgr, err := newDNSManagerCommand(cmd, true, false)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
mgr, err := newDNSManager(m, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -101,18 +121,8 @@ var dnsSyncCmd = &cobra.Command{
|
|||||||
Use: "sync",
|
Use: "sync",
|
||||||
Short: "dns sync updates public DNS records",
|
Short: "dns sync updates public DNS records",
|
||||||
PreRun: setVerbosity,
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
cred, err := dns.DefaultDNSProvider()
|
mgr, err := newDNSManagerCommand(cmd, true, true)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
m, err := cfg.LoadZones(true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
mgr, err := newDNSManager(m, cred)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -124,9 +134,60 @@ var dnsSyncCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dnsShowCmd = &cobra.Command{
|
||||||
|
Use: "show [<name>...]",
|
||||||
|
Short: "dns show lists entries on DNS for our domain",
|
||||||
|
PreRun: setVerbosity,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
mgr, err := newDNSManagerCommand(cmd, true, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return mgr.Show(ctx, args...)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var dnsAddCmd = &cobra.Command{
|
||||||
|
Use: "add <name> <address..>",
|
||||||
|
Short: "dns add registers a new machine on the public DNS",
|
||||||
|
Args: cobra.MinimumNArgs(2),
|
||||||
|
PreRun: setVerbosity,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var addrs []netip.Addr
|
||||||
|
|
||||||
|
for _, s := range args[1:] {
|
||||||
|
addr, err := core.ParseAddr(s)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, s)
|
||||||
|
case !addr.IsValid(), addr.IsUnspecified(), addr.IsPrivate(), addr.IsMulticast():
|
||||||
|
return core.Wrap(core.ErrInvalid, s)
|
||||||
|
default:
|
||||||
|
addrs = append(addrs, addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mgr, err := newDNSManagerCommand(cmd, true, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return mgr.Add(ctx, args[0], addrs...)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(dnsCmd)
|
rootCmd.AddCommand(dnsCmd)
|
||||||
|
|
||||||
dnsCmd.AddCommand(dnsWriteCmd)
|
dnsCmd.AddCommand(dnsWriteCmd)
|
||||||
dnsCmd.AddCommand(dnsSyncCmd)
|
dnsCmd.AddCommand(dnsSyncCmd)
|
||||||
|
dnsCmd.AddCommand(dnsShowCmd)
|
||||||
|
dnsCmd.AddCommand(dnsAddCmd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"darvaza.org/sidecar/pkg/logger/zerolog"
|
||||||
"darvaza.org/slog"
|
"darvaza.org/slog"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = zerolog.New(nil, slog.Error)
|
||||||
|
|
||||||
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
|
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
|
||||||
func fatal(err error, msg string, args ...any) {
|
func fatal(err error, msg string, args ...any) {
|
||||||
l := log.Fatal()
|
l := log.Fatal()
|
||||||
@@ -19,3 +23,20 @@ func fatal(err error, msg string, args ...any) {
|
|||||||
|
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var verbosity int
|
||||||
|
|
||||||
|
// setVerbosity replaces the global logger using the
|
||||||
|
// verbosity level specified via -v flags
|
||||||
|
func setVerbosity(_ *cobra.Command, _ []string) {
|
||||||
|
desired := int8(slog.Error) + int8(verbosity)
|
||||||
|
if desired > 6 {
|
||||||
|
desired = 6
|
||||||
|
}
|
||||||
|
log = zerolog.New(nil, slog.LogLevel(desired))
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
|
||||||
|
"increase the verbosity level to Warn, Info or Debug")
|
||||||
|
}
|
||||||
|
|||||||
+1
-18
@@ -2,8 +2,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"darvaza.org/sidecar/pkg/logger/zerolog"
|
|
||||||
"darvaza.org/slog"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,9 +11,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
log = zerolog.New(nil, slog.Error)
|
rootCmd = &cobra.Command{
|
||||||
verbosity int
|
|
||||||
rootCmd = &cobra.Command{
|
|
||||||
Use: CmdName,
|
Use: CmdName,
|
||||||
Short: "control tool for jpi.cloud",
|
Short: "control tool for jpi.cloud",
|
||||||
}
|
}
|
||||||
@@ -26,16 +22,3 @@ func main() {
|
|||||||
fatal(err, "")
|
fatal(err, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
|
|
||||||
"increase the verbosity level to Warn, Info or Debug")
|
|
||||||
}
|
|
||||||
|
|
||||||
func setVerbosity(_ *cobra.Command, _ []string) {
|
|
||||||
desired := int8(slog.Error) + int8(verbosity)
|
|
||||||
if desired > 6 {
|
|
||||||
desired = 6
|
|
||||||
}
|
|
||||||
log = zerolog.New(nil, slog.LogLevel(desired))
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ go 1.19
|
|||||||
require (
|
require (
|
||||||
asciigoat.org/ini v0.2.5
|
asciigoat.org/ini v0.2.5
|
||||||
darvaza.org/core v0.10.0
|
darvaza.org/core v0.10.0
|
||||||
darvaza.org/resolver v0.5.4
|
darvaza.org/resolver v0.5.8
|
||||||
darvaza.org/sidecar v0.0.2
|
darvaza.org/sidecar v0.0.8
|
||||||
darvaza.org/slog v0.5.3
|
darvaza.org/slog v0.5.4
|
||||||
darvaza.org/slog/handlers/discard v0.4.5
|
darvaza.org/slog/handlers/discard v0.4.6
|
||||||
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/cloudflare v0.1.0
|
||||||
@@ -23,8 +23,8 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
asciigoat.org/core v0.3.9 // indirect
|
asciigoat.org/core v0.3.9 // indirect
|
||||||
darvaza.org/slog/handlers/filter v0.4.5 // indirect
|
darvaza.org/slog/handlers/filter v0.4.6 // indirect
|
||||||
darvaza.org/slog/handlers/zerolog v0.4.5 // indirect
|
darvaza.org/slog/handlers/zerolog v0.4.6 // indirect
|
||||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||||
github.com/chavacava/garif v0.1.0 // indirect
|
github.com/chavacava/garif v0.1.0 // indirect
|
||||||
github.com/fatih/color v1.15.0 // indirect
|
github.com/fatih/color v1.15.0 // indirect
|
||||||
|
|||||||
@@ -4,18 +4,18 @@ asciigoat.org/ini v0.2.5 h1:4gRIp9rU+XQt8+HMqZO5R7GavMv9Yl2+N+je6djDIAE=
|
|||||||
asciigoat.org/ini v0.2.5/go.mod h1:gmXzJ9XFqf1NLk5nQkj04USQ4tMtdRJHNQX6vp3DzjU=
|
asciigoat.org/ini v0.2.5/go.mod h1:gmXzJ9XFqf1NLk5nQkj04USQ4tMtdRJHNQX6vp3DzjU=
|
||||||
darvaza.org/core v0.10.0 h1:/nQOSWnMgWW8ZJmv3AEdTgIK+Pg4lkPd+VNejL84q3M=
|
darvaza.org/core v0.10.0 h1:/nQOSWnMgWW8ZJmv3AEdTgIK+Pg4lkPd+VNejL84q3M=
|
||||||
darvaza.org/core v0.10.0/go.mod h1:72iWMVoXjMHjsPSlctDzA7yKzwXsj5dO+se6F9B3ERs=
|
darvaza.org/core v0.10.0/go.mod h1:72iWMVoXjMHjsPSlctDzA7yKzwXsj5dO+se6F9B3ERs=
|
||||||
darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs=
|
darvaza.org/resolver v0.5.8 h1:y410WQ3vRCgE7437eyA55cNMZRP32qYXiokLejkFQeg=
|
||||||
darvaza.org/resolver v0.5.4/go.mod h1:vHMkQUmHjaetFqG2ZLZJiQHsXEMGoTOFGm+NXwfndhE=
|
darvaza.org/resolver v0.5.8/go.mod h1:QnfX+eSZZZbmnE3n+6w4gfqXDH1Gj2MWJVQxhlQDHq8=
|
||||||
darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0=
|
darvaza.org/sidecar v0.0.8 h1:vsWK2SZfBYzU999brmT8gzVeCRKbuNQZOVdG5zxjO6U=
|
||||||
darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo=
|
darvaza.org/sidecar v0.0.8/go.mod h1:G96TMPge2jqpKMpaCWc9zwdfaJTmko7dMMWXwDsdocM=
|
||||||
darvaza.org/slog v0.5.3 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0=
|
darvaza.org/slog v0.5.4 h1:xzlWVzYh4tuZLnj4A9tOHXfn/SAEIkApXPvK3YDiW9g=
|
||||||
darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0=
|
darvaza.org/slog v0.5.4/go.mod h1:QFtY3QoQ7xxww85umlEKPcMCNzqNrHYqnj53KehsmBU=
|
||||||
darvaza.org/slog/handlers/discard v0.4.5 h1:RRykOItNolHyiUav57lG/GFBL33rcljoa0nWTpY+T0g=
|
darvaza.org/slog/handlers/discard v0.4.6 h1:TatHJn34y6eKQzNRHSo6lGZnJg4SLOGaWstlvwwOyrE=
|
||||||
darvaza.org/slog/handlers/discard v0.4.5/go.mod h1:HYHfISQjMqcPbPoPZ92ib/u7s9JcXvF6OaygpPFwdF8=
|
darvaza.org/slog/handlers/discard v0.4.6/go.mod h1:AG8WKr7m11NPPzvHW/b8nCT5RvYR9RZcIT/NWUOoMAo=
|
||||||
darvaza.org/slog/handlers/filter v0.4.5 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc=
|
darvaza.org/slog/handlers/filter v0.4.6 h1:AI5AQDyXS534QeXIV54pAKxplA6AVZNr4H2PEmAXT0k=
|
||||||
darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g=
|
darvaza.org/slog/handlers/filter v0.4.6/go.mod h1:MGTKdlnA/FanOn3GU2mltzwBn41HgSxxNeWUQEKFbl8=
|
||||||
darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4=
|
darvaza.org/slog/handlers/zerolog v0.4.6 h1:Di+FXUD2R2pKUrynaidyXzS0WsrEiwbL11LQlQzwZv4=
|
||||||
darvaza.org/slog/handlers/zerolog v0.4.5/go.mod h1:mCoh/mIl8Nsa6Yu1Um7d7cos6RuEJzgaTXaX5LDRUao=
|
darvaza.org/slog/handlers/zerolog v0.4.6/go.mod h1:r5B9/FQ256R3Wo5vFLOa2YarM2P8WOjVjFn8xHikNjk=
|
||||||
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/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
|
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
|
||||||
@@ -96,7 +96,7 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
|
|||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
|
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
|
||||||
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
fs "github.com/hack-pad/hackpadfs"
|
fs "github.com/hack-pad/hackpadfs"
|
||||||
)
|
)
|
||||||
@@ -40,6 +43,21 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose
|
|||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveFile deletes a file from the cluster's config directory
|
||||||
|
func (m *Cluster) RemoveFile(name string, args ...any) error {
|
||||||
|
if len(args) > 0 {
|
||||||
|
name = fmt.Sprintf(name, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := fs.Remove(m.dir, name)
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ReadFile reads a file from the cluster's config directory
|
// ReadFile reads a file from the cluster's config directory
|
||||||
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@@ -48,3 +66,50 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
|||||||
|
|
||||||
return fs.ReadFile(m.dir, name)
|
return fs.ReadFile(m.dir, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadLines reads a file from the cluster's config directory,
|
||||||
|
// split by lines, trimmed, and accepting `#` to comment lines out.
|
||||||
|
func (m *Cluster) ReadLines(name string, args ...any) ([]string, error) {
|
||||||
|
var out []string
|
||||||
|
|
||||||
|
data, err := m.ReadFile(name, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sc := bufio.NewScanner(bytes.NewReader(data))
|
||||||
|
for sc.Scan() {
|
||||||
|
s := strings.TrimSpace(sc.Text())
|
||||||
|
switch {
|
||||||
|
case s == "", strings.HasPrefix(s, "#"):
|
||||||
|
// ignore
|
||||||
|
default:
|
||||||
|
// accepted
|
||||||
|
out = append(out, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteStringFile writes the given content to a file on the machine's config directory
|
||||||
|
func (m *Cluster) WriteStringFile(value string, name string, args ...any) error {
|
||||||
|
f, err := m.CreateTruncFile(name, args...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
buf := bytes.NewBufferString(value)
|
||||||
|
_, err = buf.WriteTo(f)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MkdirAll creates directories relative to the cluster's config directory
|
||||||
|
func (m *Cluster) MkdirAll(name string, args ...any) error {
|
||||||
|
if len(args) > 0 {
|
||||||
|
name = fmt.Sprintf(name, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs.MkdirAll(m.dir, name, 0755)
|
||||||
|
}
|
||||||
|
|||||||
+91
-26
@@ -2,17 +2,25 @@ package cluster
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ZoneRegionsFileName indicates the file containing
|
||||||
|
// region names as references
|
||||||
|
ZoneRegionsFileName = "regions"
|
||||||
|
)
|
||||||
|
|
||||||
func (m *Cluster) scan(opts *ScanOptions) error {
|
func (m *Cluster) scan(opts *ScanOptions) error {
|
||||||
for _, fn := range []func(*ScanOptions) error{
|
for _, fn := range []func(*ScanOptions) error{
|
||||||
m.scanDirectory,
|
m.scanDirectory,
|
||||||
m.scanMachines,
|
m.scanMachines,
|
||||||
m.scanZoneIDs,
|
m.scanZoneIDs,
|
||||||
m.scanSort,
|
m.scanSort,
|
||||||
|
m.initRegions,
|
||||||
m.scanGateways,
|
m.scanGateways,
|
||||||
m.scanCephMonitors,
|
m.scanCephMonitors,
|
||||||
} {
|
} {
|
||||||
@@ -24,7 +32,7 @@ func (m *Cluster) scan(opts *ScanOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
func (m *Cluster) scanDirectory(opts *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 {
|
||||||
@@ -33,16 +41,14 @@ func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
|||||||
|
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if e.IsDir() {
|
if e.IsDir() {
|
||||||
z, err := m.newZone(e.Name())
|
ok, err := m.scanSubdirectory(opts, e.Name())
|
||||||
switch {
|
switch {
|
||||||
case err != nil:
|
case err != nil:
|
||||||
return core.Wrap(err, e.Name())
|
return core.Wrap(err, e.Name())
|
||||||
case z.Machines.Len() == 0:
|
case !ok:
|
||||||
z.warn(nil).
|
m.warn(nil).
|
||||||
WithField("zone", z.Name).
|
WithField("zone", e.Name()).
|
||||||
Print("empty")
|
Print("empty")
|
||||||
default:
|
|
||||||
m.Zones = append(m.Zones, z)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,6 +56,27 @@ func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) scanSubdirectory(_ *ScanOptions, name string) (bool, error) {
|
||||||
|
z, err := m.newZone(name)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
// somewhere went wrong scanning the subdirectory
|
||||||
|
return false, err
|
||||||
|
case z.Machines.Len() > 0:
|
||||||
|
// zones have machines and the regions they belong
|
||||||
|
m.Zones = append(m.Zones, z)
|
||||||
|
return true, nil
|
||||||
|
case len(z.Regions) > 0:
|
||||||
|
// regions have no machines but can include
|
||||||
|
// other regions
|
||||||
|
m.appendRegionRegions(name, z.Regions...)
|
||||||
|
return true, nil
|
||||||
|
default:
|
||||||
|
// empty
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Cluster) newZone(name string) (*Zone, error) {
|
func (m *Cluster) newZone(name string) (*Zone, error) {
|
||||||
z := &Zone{
|
z := &Zone{
|
||||||
zones: m,
|
zones: m,
|
||||||
@@ -73,6 +100,10 @@ func (m *Cluster) scanMachines(opts *ScanOptions) error {
|
|||||||
err = p.scan(opts)
|
err = p.scan(opts)
|
||||||
return err != nil
|
return err != nil
|
||||||
})
|
})
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
err = p.scanWrapUp(opts)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,34 +181,68 @@ func (z *Zone) scan() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if e.IsDir() {
|
name := e.Name()
|
||||||
m := &Machine{
|
|
||||||
zone: z,
|
|
||||||
logger: z,
|
|
||||||
Name: e.Name(),
|
|
||||||
}
|
|
||||||
|
|
||||||
m.debug().
|
switch {
|
||||||
WithField("node", m.Name).
|
case name == ZoneRegionsFileName:
|
||||||
|
err = z.loadRegions()
|
||||||
|
case e.IsDir():
|
||||||
|
err = z.scanSubdirectory(name)
|
||||||
|
default:
|
||||||
|
z.warn(nil).
|
||||||
WithField("zone", z.Name).
|
WithField("zone", z.Name).
|
||||||
Print("found")
|
WithField("filename", name).
|
||||||
|
Print("unknown")
|
||||||
|
}
|
||||||
|
|
||||||
if err := m.init(); err != nil {
|
if err != nil {
|
||||||
m.error(err).
|
return err
|
||||||
WithField("node", m.Name).
|
|
||||||
WithField("zone", z.Name).
|
|
||||||
Print()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
z.Machines = append(z.Machines, m)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (z *Zone) loadRegions() error {
|
||||||
|
filename := path.Join(z.Name, ZoneRegionsFileName)
|
||||||
|
regions, err := z.zones.ReadLines(filename)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
// parsed
|
||||||
|
err = z.appendRegions(regions...)
|
||||||
|
if err != nil {
|
||||||
|
err = core.Wrap(err, filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *Zone) scanSubdirectory(name string) error {
|
||||||
|
m := &Machine{
|
||||||
|
zone: z,
|
||||||
|
logger: z,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
m.debug().
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
Print("found")
|
||||||
|
|
||||||
|
if err := m.init(); err != nil {
|
||||||
|
m.error(err).
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
Print()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
z.Machines = append(z.Machines, m)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetGateway returns the first gateway found, if none
|
// GetGateway returns the first gateway found, if none
|
||||||
// files will be created to enable the first [Machine] to
|
// files will be created to enable the first [Machine] to
|
||||||
// be one
|
// be one
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type Machine struct {
|
|||||||
ID int
|
ID int
|
||||||
Name string `json:"-" yaml:"-"`
|
Name string `json:"-" yaml:"-"`
|
||||||
|
|
||||||
|
Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"`
|
||||||
CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
|
CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
|
||||||
PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"`
|
PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"`
|
||||||
Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"`
|
Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"`
|
||||||
@@ -43,6 +44,11 @@ func (m *Machine) FullName() string {
|
|||||||
return strings.Join(name, ".")
|
return strings.Join(name, ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsActive indicates the machine is to be included in regions' DNS entries
|
||||||
|
func (m *Machine) IsActive() bool {
|
||||||
|
return !m.Inactive
|
||||||
|
}
|
||||||
|
|
||||||
// IsGateway tells if the Machine is a ring0 gateway
|
// IsGateway tells if the Machine is a ring0 gateway
|
||||||
func (m *Machine) IsGateway() bool {
|
func (m *Machine) IsGateway() bool {
|
||||||
_, ok := m.getRingInfo(0)
|
_, ok := m.getRingInfo(0)
|
||||||
|
|||||||
+20
-21
@@ -1,7 +1,6 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -12,10 +11,9 @@ import (
|
|||||||
|
|
||||||
// OpenFile opens a file on the machine's config directory with the specified flags
|
// OpenFile opens a file on the machine's config directory with the specified flags
|
||||||
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
||||||
base := m.zone.zones.dir
|
|
||||||
fullName := m.getFilename(name, args...)
|
fullName := m.getFilename(name, args...)
|
||||||
|
|
||||||
return fs.OpenFile(base, fullName, flags, 0644)
|
return m.zone.zones.OpenFile(fullName, flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTruncFile creates or truncates a file on the machine's config directory
|
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||||||
@@ -43,37 +41,38 @@ func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteClose
|
|||||||
|
|
||||||
// RemoveFile deletes a file from the machine's config directory
|
// RemoveFile deletes a file from the machine's config directory
|
||||||
func (m *Machine) RemoveFile(name string, args ...any) error {
|
func (m *Machine) RemoveFile(name string, args ...any) error {
|
||||||
base := m.zone.zones.dir
|
|
||||||
fullName := m.getFilename(name, args...)
|
fullName := m.getFilename(name, args...)
|
||||||
err := fs.Remove(base, fullName)
|
|
||||||
|
|
||||||
switch {
|
return m.zone.zones.RemoveFile(fullName)
|
||||||
case os.IsNotExist(err):
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile reads a file from the machine's config directory
|
// ReadFile reads a file from the machine's config directory
|
||||||
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
||||||
base := m.zone.zones.dir
|
|
||||||
fullName := m.getFilename(name, args...)
|
fullName := m.getFilename(name, args...)
|
||||||
|
|
||||||
return fs.ReadFile(base, fullName)
|
return m.zone.zones.ReadFile(fullName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadLines reads a file from the machine's config directory,
|
||||||
|
// split by lines, trimmed, and accepting `#` to comment lines out.
|
||||||
|
func (m *Machine) ReadLines(name string, args ...any) ([]string, error) {
|
||||||
|
fullName := m.getFilename(name, args...)
|
||||||
|
|
||||||
|
return m.zone.zones.ReadLines(fullName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteStringFile writes the given content to a file on the machine's config directory
|
// WriteStringFile writes the given content to a file on the machine's config directory
|
||||||
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
||||||
f, err := m.CreateTruncFile(name, args...)
|
fullName := m.getFilename(name, args...)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
buf := bytes.NewBufferString(value)
|
return m.zone.zones.WriteStringFile(value, fullName)
|
||||||
_, err = buf.WriteTo(f)
|
}
|
||||||
return err
|
|
||||||
|
// MkdirAll creates directories relative to the machine's config directory
|
||||||
|
func (m *Machine) MkdirAll(name string, args ...any) error {
|
||||||
|
fullName := m.getFilename(name, args...)
|
||||||
|
|
||||||
|
return m.zone.zones.MkdirAll(fullName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) getFilename(name string, args ...any) string {
|
func (m *Machine) getFilename(name string, args ...any) string {
|
||||||
|
|||||||
@@ -118,21 +118,31 @@ func (m *Machine) tryApplyWireguardConfig(ring int) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
func (m *Machine) applyWireguardConfigNode(ring int, wg *wireguard.Config) error {
|
||||||
addr := wg.GetAddress()
|
addr := wg.GetAddress()
|
||||||
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
if !core.IsZero(addr) {
|
||||||
if !ok {
|
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
||||||
return fmt.Errorf("%s: invalid address", addr)
|
if !ok {
|
||||||
}
|
return fmt.Errorf("%s: invalid address", addr)
|
||||||
|
}
|
||||||
|
|
||||||
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||||
return core.Wrap(err, "%s: invalid address", addr)
|
return core.Wrap(err, "%s: invalid address", addr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
||||||
return core.Wrap(err, "interface")
|
return core.Wrap(err, "interface")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
||||||
|
if err := m.applyWireguardConfigNode(ring, wg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for _, peer := range wg.Peer {
|
for _, peer := range wg.Peer {
|
||||||
err := m.applyWireguardPeerConfig(ring, peer)
|
err := m.applyWireguardPeerConfig(ring, peer)
|
||||||
switch {
|
switch {
|
||||||
@@ -230,6 +240,23 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Machine) setRingDefaults(ri *RingInfo) error {
|
||||||
|
if ri.Keys.PrivateKey.IsZero() {
|
||||||
|
m.info().
|
||||||
|
WithField("subsystem", "wireguard").
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("ring", ri.Ring).
|
||||||
|
Print("generating key pair")
|
||||||
|
|
||||||
|
kp, err := wireguard.NewKeyPair()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ri.Keys = kp
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
||||||
// config directory.
|
// config directory.
|
||||||
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package cluster
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -68,7 +69,8 @@ func (m *Machine) setID() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) scan(opts *ScanOptions) error {
|
// scan is called once we know about all zones and machine names
|
||||||
|
func (m *Machine) scan(_ *ScanOptions) error {
|
||||||
for i := 0; i < RingsCount; i++ {
|
for i := 0; i < RingsCount; i++ {
|
||||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||||
m.error(err).
|
m.error(err).
|
||||||
@@ -80,6 +82,45 @@ func (m *Machine) scan(opts *ScanOptions) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return m.loadInactive()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) loadInactive() error {
|
||||||
|
data, err := m.ReadLines("region")
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
// no file
|
||||||
|
return nil
|
||||||
|
case err != nil:
|
||||||
|
// read error
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
// look for "none"
|
||||||
|
for _, r := range data {
|
||||||
|
switch r {
|
||||||
|
case "none":
|
||||||
|
m.Inactive = true
|
||||||
|
default:
|
||||||
|
m.Inactive = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// scanWrapUp is called once all machines have been scanned
|
||||||
|
func (m *Machine) scanWrapUp(opts *ScanOptions) error {
|
||||||
|
for _, ri := range m.Rings {
|
||||||
|
if err := m.setRingDefaults(ri); err != nil {
|
||||||
|
m.error(err).
|
||||||
|
WithField("subsystem", "wireguard").
|
||||||
|
WithField("node", m.Name).
|
||||||
|
WithField("ring", ri.Ring).
|
||||||
|
Print()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !opts.DontResolvePublicAddresses {
|
if !opts.DontResolvePublicAddresses {
|
||||||
return m.UpdatePublicAddresses()
|
return m.UpdatePublicAddresses()
|
||||||
}
|
}
|
||||||
|
|||||||
+105
-4
@@ -1,5 +1,10 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ MachineIterator = (*Region)(nil)
|
_ MachineIterator = (*Region)(nil)
|
||||||
_ ZoneIterator = (*Region)(nil)
|
_ ZoneIterator = (*Region)(nil)
|
||||||
@@ -32,7 +37,9 @@ func (r *Region) ForEachMachine(fn func(*Machine) bool) {
|
|||||||
var term bool
|
var term bool
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
term = fn(p)
|
if p.IsActive() {
|
||||||
|
term = fn(p)
|
||||||
|
}
|
||||||
return term
|
return term
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -56,6 +63,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|||||||
|
|
||||||
// first regions defined by zones
|
// first regions defined by zones
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
SortRegions(z.Regions)
|
||||||
for _, region := range z.Regions {
|
for _, region := range z.Regions {
|
||||||
regions[region] = append(regions[region], z)
|
regions[region] = append(regions[region], z)
|
||||||
}
|
}
|
||||||
@@ -65,7 +73,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|||||||
|
|
||||||
// bind first level regions and their zones
|
// bind first level regions and their zones
|
||||||
for name, zones := range regions {
|
for name, zones := range regions {
|
||||||
m.syncRegions(name, zones...)
|
m.setRegionZones(name, zones...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// and combine zones to produce larger regions
|
// and combine zones to produce larger regions
|
||||||
@@ -74,11 +82,14 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|||||||
m.finishRegion(r)
|
m.finishRegion(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.sortRegions()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Cluster) syncRegions(name string, zones ...*Zone) {
|
func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
|
||||||
for _, r := range m.Regions {
|
for i := range m.Regions {
|
||||||
|
r := &m.Regions[i]
|
||||||
|
|
||||||
if r.Name == name {
|
if r.Name == name {
|
||||||
// found
|
// found
|
||||||
r.m = m
|
r.m = m
|
||||||
@@ -95,6 +106,38 @@ func (m *Cluster) syncRegions(name string, zones ...*Zone) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) appendRegionRegions(name string, subs ...string) {
|
||||||
|
for i := range m.Regions {
|
||||||
|
r := &m.Regions[i]
|
||||||
|
|
||||||
|
if name == r.Name {
|
||||||
|
// found
|
||||||
|
r.Regions = append(r.Regions, subs...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// new
|
||||||
|
m.Regions = append(m.Regions, Region{
|
||||||
|
Name: name,
|
||||||
|
Regions: subs,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *Zone) appendRegions(regions ...string) error {
|
||||||
|
for _, s := range regions {
|
||||||
|
// TODO: validate
|
||||||
|
z.debug().
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
WithField("region", s).
|
||||||
|
Print("attached")
|
||||||
|
|
||||||
|
z.Regions = append(z.Regions, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Cluster) finishRegion(r *Region) {
|
func (m *Cluster) finishRegion(r *Region) {
|
||||||
if r.m != nil {
|
if r.m != nil {
|
||||||
// ready
|
// ready
|
||||||
@@ -128,3 +171,61 @@ func (m *Cluster) getRegion(name string) (*Region, bool) {
|
|||||||
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SyncRegions writes to the file system the regions this [Zone]
|
||||||
|
// belongs to.
|
||||||
|
func (z *Zone) SyncRegions() error {
|
||||||
|
err := z.syncZoneRegions()
|
||||||
|
if err == nil {
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.IsActive() {
|
||||||
|
err = p.RemoveFile("region")
|
||||||
|
} else {
|
||||||
|
err = p.WriteStringFile("none\n", "region")
|
||||||
|
}
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *Zone) syncZoneRegions() error {
|
||||||
|
name := filepath.Join(z.Name, "regions")
|
||||||
|
|
||||||
|
if len(z.Regions) > 0 {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
for _, s := range z.Regions {
|
||||||
|
_, _ = buf.WriteString(s)
|
||||||
|
_, _ = buf.WriteRune('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
return z.zones.WriteStringFile(buf.String(), name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return z.zones.RemoveFile(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncRegions writes to the file system the regions covered
|
||||||
|
// by this meta-region
|
||||||
|
func (r *Region) SyncRegions() error {
|
||||||
|
name := filepath.Join(r.Name, "regions")
|
||||||
|
|
||||||
|
if len(r.Regions) > 0 {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
for _, s := range r.Regions {
|
||||||
|
_, _ = buf.WriteString(s)
|
||||||
|
_, _ = buf.WriteRune('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.m.MkdirAll(r.Name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.m.WriteStringFile(buf.String(), name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.m.RemoveFile(name)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
// SortRegions sorts regions. first by length those 3-character
|
||||||
|
// or shorter, and then by length. It's mostly aimed at
|
||||||
|
// supporting ISO-3166 order
|
||||||
|
func SortRegions(regions []string) []string {
|
||||||
|
sort.Slice(regions, func(i, j int) bool {
|
||||||
|
r1, r2 := regions[i], regions[j]
|
||||||
|
return regionLess(r1, r2)
|
||||||
|
})
|
||||||
|
|
||||||
|
return regions
|
||||||
|
}
|
||||||
|
|
||||||
|
func regionLess(r1, r2 string) bool {
|
||||||
|
switch {
|
||||||
|
case len(r1) < 4:
|
||||||
|
switch {
|
||||||
|
case len(r1) < len(r2):
|
||||||
|
return true
|
||||||
|
case len(r1) > len(r2):
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return r1 < r2
|
||||||
|
}
|
||||||
|
case len(r2) < 4:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return r1 < r2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) sortRegions() {
|
||||||
|
sort.Slice(m.Regions, func(i, j int) bool {
|
||||||
|
r1 := m.Regions[i].Name
|
||||||
|
r2 := m.Regions[j].Name
|
||||||
|
return regionLess(r1, r2)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -41,7 +41,7 @@ func (ri *RingInfo) Merge(alter *RingInfo) error {
|
|||||||
// can't disable via Merge
|
// can't disable via Merge
|
||||||
return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled)
|
return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled)
|
||||||
case !canMergeKeyPairs(ri.Keys, alter.Keys):
|
case !canMergeKeyPairs(ri.Keys, alter.Keys):
|
||||||
// incompatible keypairs
|
// incompatible key pairs
|
||||||
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package cluster
|
|||||||
// SyncAll updates all config files
|
// SyncAll updates all config files
|
||||||
func (m *Cluster) SyncAll() error {
|
func (m *Cluster) SyncAll() error {
|
||||||
for _, fn := range []func() error{
|
for _, fn := range []func() error{
|
||||||
|
m.SyncMkdirAll,
|
||||||
m.SyncAllWireguard,
|
m.SyncAllWireguard,
|
||||||
m.SyncAllCeph,
|
m.SyncAllCeph,
|
||||||
|
m.SyncAllRegions,
|
||||||
m.WriteHosts,
|
m.WriteHosts,
|
||||||
} {
|
} {
|
||||||
if err := fn(); err != nil {
|
if err := fn(); err != nil {
|
||||||
@@ -15,6 +17,20 @@ func (m *Cluster) SyncAll() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SyncMkdirAll creates the directories needed to store files
|
||||||
|
// required to represent the cluster.
|
||||||
|
func (m *Cluster) SyncMkdirAll() error {
|
||||||
|
err := m.MkdirAll(".")
|
||||||
|
if err == nil {
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
err = p.MkdirAll(".")
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// SyncAllWireguard updates all wireguard config files
|
// SyncAllWireguard updates all wireguard config files
|
||||||
func (m *Cluster) SyncAllWireguard() error {
|
func (m *Cluster) SyncAllWireguard() error {
|
||||||
var err error
|
var err error
|
||||||
@@ -43,3 +59,24 @@ func (m *Cluster) SyncAllCeph() error {
|
|||||||
|
|
||||||
return m.WriteCephConfig(cfg)
|
return m.WriteCephConfig(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SyncAllRegions rewrites all region data
|
||||||
|
func (m *Cluster) SyncAllRegions() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err := z.SyncRegions()
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.ForEachRegion(func(r *Region) bool {
|
||||||
|
err = r.SyncRegions()
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
"github.com/libdns/libdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add adds a machine to the DNS records
|
||||||
|
func (mgr *Manager) Add(ctx context.Context, name string, addrs ...netip.Addr) error {
|
||||||
|
// TODO: validate name
|
||||||
|
|
||||||
|
cur, err := mgr.GetRecords(ctx, name)
|
||||||
|
if err != nil {
|
||||||
|
return core.Wrap(err, "GetRecords")
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge []SyncAddr for name
|
||||||
|
s := mgr.asSyncRecordsMap(cur)[name+mgr.suffix]
|
||||||
|
for _, addr := range addrs {
|
||||||
|
s = AppendSyncAddr(s, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mgr.addSyncAddr(ctx, name, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) addSyncAddr(ctx context.Context, name string, s []SyncAddr) error {
|
||||||
|
var recs []libdns.Record
|
||||||
|
|
||||||
|
for _, a := range s {
|
||||||
|
recs = append(recs, libdns.Record{
|
||||||
|
ID: a.ID,
|
||||||
|
Name: name + mgr.suffix,
|
||||||
|
Type: core.IIf(a.Addr.Is6(), "AAAA", "A"),
|
||||||
|
TTL: time.Second,
|
||||||
|
Value: a.Addr.String(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
SortRecords(recs)
|
||||||
|
err := writeRecords(recs, os.Stdout)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = mgr.p.SetRecords(ctx, mgr.domain, recs)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendSyncAddr appends a [netip.Addr] to a [SyncAddr] slice
|
||||||
|
// if the address is new.
|
||||||
|
func AppendSyncAddr(s []SyncAddr, addr netip.Addr) []SyncAddr {
|
||||||
|
for _, se := range s {
|
||||||
|
if se.Addr.Compare(addr) == 0 {
|
||||||
|
// found
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s = append(s, SyncAddr{
|
||||||
|
Addr: addr,
|
||||||
|
TTL: time.Second,
|
||||||
|
})
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrNoDNSProvider indicates a [libdns.Provider] wasn't assigned
|
||||||
|
// to the [Manager]
|
||||||
|
ErrNoDNSProvider = errors.New("dns provider not specified")
|
||||||
|
|
||||||
|
// ErrNoDomain indicates a domain wasn't specified
|
||||||
|
ErrNoDomain = errors.New("domain not specified")
|
||||||
|
)
|
||||||
+60
-3
@@ -2,15 +2,16 @@ package dns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
"darvaza.org/slog"
|
"darvaza.org/slog"
|
||||||
"git.jpi.io/amery/jpictl/pkg/cluster"
|
"github.com/libdns/libdns"
|
||||||
"golang.org/x/net/publicsuffix"
|
"golang.org/x/net/publicsuffix"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Manager is a DNS Manager instance
|
// Manager is a DNS Manager instance
|
||||||
@@ -71,7 +72,7 @@ func (mgr *Manager) setDefaults() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if mgr.domain == "" || mgr.suffix == "" {
|
if mgr.domain == "" || mgr.suffix == "" {
|
||||||
return errors.New("domain not specified")
|
return ErrNoDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -120,6 +121,62 @@ func NewManager(opts ...ManagerOption) (*Manager, error) {
|
|||||||
return mgr, nil
|
return mgr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRecords pulls all the address records on DNS for our domain,
|
||||||
|
// optionally only those matching the given names.
|
||||||
|
func (mgr *Manager) GetRecords(ctx context.Context, names ...string) ([]libdns.Record, error) {
|
||||||
|
if mgr.p == nil {
|
||||||
|
return nil, ErrNoDNSProvider
|
||||||
|
}
|
||||||
|
|
||||||
|
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
// failed
|
||||||
|
return nil, err
|
||||||
|
case len(recs) == 0:
|
||||||
|
// empty
|
||||||
|
return []libdns.Record{}, nil
|
||||||
|
case mgr.suffix == "" && len(names) == 0:
|
||||||
|
// unfiltered
|
||||||
|
return recs, nil
|
||||||
|
default:
|
||||||
|
// filtered
|
||||||
|
recs = mgr.filterRecords(recs, names...)
|
||||||
|
return recs, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) filterRecords(recs []libdns.Record, names ...string) []libdns.Record {
|
||||||
|
out := make([]libdns.Record, 0, len(recs))
|
||||||
|
for _, rr := range recs {
|
||||||
|
name, ok := mgr.matchSuffix(rr)
|
||||||
|
switch {
|
||||||
|
case !ok:
|
||||||
|
// skip, wrong subdomain
|
||||||
|
continue
|
||||||
|
case len(names) == 0:
|
||||||
|
// unfiltered, take it
|
||||||
|
case !core.SliceContains(names, name):
|
||||||
|
// skip, not one of the requested names
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
out = append(out, rr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) matchSuffix(rr libdns.Record) (string, bool) {
|
||||||
|
if mgr.suffix == "" {
|
||||||
|
// no suffix
|
||||||
|
return rr.Name, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove suffix
|
||||||
|
return strings.CutSuffix(rr.Name, mgr.suffix)
|
||||||
|
}
|
||||||
|
|
||||||
// AddHost registers a host
|
// AddHost registers a host
|
||||||
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
|
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
|
||||||
active bool, addrs ...netip.Addr) error {
|
active bool, addrs ...netip.Addr) error {
|
||||||
|
|||||||
+54
-20
@@ -6,10 +6,13 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
"github.com/libdns/libdns"
|
"github.com/libdns/libdns"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (mgr *Manager) fqdn(name string) string {
|
func (mgr *Manager) fqdn(name string) string {
|
||||||
@@ -38,30 +41,51 @@ func SortAddrRecords(s []AddrRecord) []AddrRecord {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// SortRegions sorts regions. first by length those 3-character
|
// SortRecords sorts a slice of [libdns.Record], by Name, Type and Value
|
||||||
// or shorter, and then by length. It's mostly aimed at
|
func SortRecords(s []libdns.Record) []libdns.Record {
|
||||||
// supporting ISO-3166 order
|
sort.Slice(s, func(i, j int) bool {
|
||||||
func SortRegions(regions []string) []string {
|
return lessRecord(s[i], s[j])
|
||||||
sort.Slice(regions, func(i, j int) bool {
|
})
|
||||||
r1, r2 := regions[i], regions[j]
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func lessRecord(a, b libdns.Record) bool {
|
||||||
|
aName := strings.ToLower(a.Name)
|
||||||
|
bName := strings.ToLower(b.Name)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case aName < bName:
|
||||||
|
return true
|
||||||
|
case aName > bName:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
aType := strings.ToUpper(a.Type)
|
||||||
|
bType := strings.ToUpper(b.Type)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case aType < bType:
|
||||||
|
return true
|
||||||
|
case aType > bType:
|
||||||
|
return false
|
||||||
|
case aType == "A", aType == "AAAA":
|
||||||
|
// IP Addresses
|
||||||
|
var aa, ba netip.Addr
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case len(r1) < 4:
|
case aa.UnmarshalText([]byte(a.Value)) != nil:
|
||||||
switch {
|
// bad address on a
|
||||||
case len(r1) < len(r2):
|
return true
|
||||||
return true
|
case ba.UnmarshalText([]byte(b.Value)) != nil:
|
||||||
case len(r1) > len(r2):
|
// bad address on b
|
||||||
return false
|
|
||||||
default:
|
|
||||||
return r1 < r2
|
|
||||||
}
|
|
||||||
case len(r2) < 4:
|
|
||||||
return false
|
return false
|
||||||
default:
|
default:
|
||||||
return r1 < r2
|
return aa.Less(ba)
|
||||||
}
|
}
|
||||||
})
|
default:
|
||||||
return regions
|
// text
|
||||||
|
return a.Value < b.Value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddrRecord represents an A or AAAA record
|
// AddrRecord represents an A or AAAA record
|
||||||
@@ -124,7 +148,17 @@ func (mgr *Manager) genRegionsSorted() []string {
|
|||||||
regions = append(regions, name)
|
regions = append(regions, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return SortRegions(regions)
|
return cluster.SortRegions(regions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) genZonesSorted() []string {
|
||||||
|
zones := make([]string, 0, len(mgr.zones))
|
||||||
|
for name := range mgr.zones {
|
||||||
|
zones = append(zones, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(zones)
|
||||||
|
return zones
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
|
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
"github.com/libdns/libdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Show shows current DNS entries
|
||||||
|
func (mgr *Manager) Show(ctx context.Context, names ...string) error {
|
||||||
|
recs, err := mgr.GetRecords(ctx, names...)
|
||||||
|
if err != nil {
|
||||||
|
return core.Wrap(err, "GetRecords")
|
||||||
|
}
|
||||||
|
|
||||||
|
SortRecords(recs)
|
||||||
|
return writeRecords(recs, os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeRecords(recs []libdns.Record, w io.Writer) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
for _, rr := range recs {
|
||||||
|
_ = fmtRecord(&buf, rr)
|
||||||
|
_, _ = buf.WriteRune('\n')
|
||||||
|
}
|
||||||
|
_, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs))
|
||||||
|
|
||||||
|
_, err := buf.WriteTo(w)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func fmtRecord(w io.Writer, rr libdns.Record) error {
|
||||||
|
ttl := int(rr.TTL / time.Second)
|
||||||
|
if ttl < 1 {
|
||||||
|
ttl = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := fmt.Fprintf(w, "%s\t%v\tIN\t%s\t%s",
|
||||||
|
rr.Name,
|
||||||
|
ttl,
|
||||||
|
rr.Type,
|
||||||
|
rr.Value)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
if rr.ID != "" {
|
||||||
|
_, err = fmt.Fprintf(w, "\t; %s", rr.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
+13
-13
@@ -2,7 +2,6 @@ package dns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -48,18 +47,14 @@ func SortSyncAddrSlice(s []SyncAddr) []SyncAddr {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRecords pulls all the address records on DNS for our domain
|
// GetSyncRecords pulls all the address records on DNS for our domain
|
||||||
func (mgr *Manager) GetRecords(ctx context.Context) ([]SyncAddrRecord, error) {
|
func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error) {
|
||||||
if mgr.p == nil {
|
recs, err := mgr.GetRecords(ctx)
|
||||||
return nil, errors.New("dns provider not specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return mgr.filteredRecords(recs)
|
return mgr.asSyncRecords(recs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
|
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
|
||||||
@@ -94,9 +89,9 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
|
|||||||
return out, true, nil
|
return out, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
func (mgr *Manager) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr {
|
||||||
// filter and convert
|
// filter and convert
|
||||||
cache := make(map[string][]SyncAddr)
|
out := make(map[string][]SyncAddr)
|
||||||
for _, rr := range recs {
|
for _, rr := range recs {
|
||||||
addr, ok, err := mgr.AsSyncAddr(rr)
|
addr, ok, err := mgr.AsSyncAddr(rr)
|
||||||
switch {
|
switch {
|
||||||
@@ -111,9 +106,14 @@ func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, err
|
|||||||
Print()
|
Print()
|
||||||
case ok:
|
case ok:
|
||||||
// store
|
// store
|
||||||
cache[rr.Name] = append(cache[rr.Name], addr)
|
out[rr.Name] = append(out[rr.Name], addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mgr *Manager) asSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
||||||
|
cache := mgr.asSyncRecordsMap(recs)
|
||||||
|
|
||||||
// prepare records
|
// prepare records
|
||||||
out := make([]SyncAddrRecord, len(cache))
|
out := make([]SyncAddrRecord, len(cache))
|
||||||
@@ -137,7 +137,7 @@ func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, err
|
|||||||
|
|
||||||
// Sync updates all the address records on DNS for our domain
|
// Sync updates all the address records on DNS for our domain
|
||||||
func (mgr *Manager) Sync(ctx context.Context) error {
|
func (mgr *Manager) Sync(ctx context.Context) error {
|
||||||
current, err := mgr.GetRecords(ctx)
|
current, err := mgr.GetSyncRecords(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return core.Wrap(err, "GetRecords")
|
return core.Wrap(err, "GetRecords")
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -14,12 +14,13 @@ func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
|
|||||||
cache := make(map[string][]netip.Addr)
|
cache := make(map[string][]netip.Addr)
|
||||||
|
|
||||||
// zones
|
// zones
|
||||||
for _, z := range mgr.zones {
|
for _, zoneName := range mgr.genZonesSorted() {
|
||||||
|
z := mgr.zones[zoneName]
|
||||||
|
|
||||||
mgr.writeZoneHosts(&buf, z)
|
mgr.writeZoneHosts(&buf, z)
|
||||||
|
|
||||||
// zone alias
|
// zone alias
|
||||||
addrs := mgr.genZoneAddresses(z)
|
addrs := mgr.genZoneAddresses(z)
|
||||||
zoneName := z.Name
|
|
||||||
|
|
||||||
rr := AddrRecord{
|
rr := AddrRecord{
|
||||||
Name: mgr.fqdn(zoneName + mgr.suffix),
|
Name: mgr.fqdn(zoneName + mgr.suffix),
|
||||||
|
|||||||
+11
-4
@@ -107,6 +107,11 @@ func (ep EndpointAddress) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalText loads an endpoint address from text data
|
||||||
|
func (ep *EndpointAddress) UnmarshalText(b []byte) error {
|
||||||
|
return ep.FromString(string(b))
|
||||||
|
}
|
||||||
|
|
||||||
// FromString sets the EndpointAddress from a given "[host]:port"
|
// FromString sets the EndpointAddress from a given "[host]:port"
|
||||||
func (ep *EndpointAddress) FromString(s string) error {
|
func (ep *EndpointAddress) FromString(s string) error {
|
||||||
host, port, err := core.SplitHostPort(s)
|
host, port, err := core.SplitHostPort(s)
|
||||||
@@ -175,10 +180,12 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
|||||||
ListenPort: p.ListenPort,
|
ListenPort: p.ListenPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
if p.PrivateKey != "" {
|
||||||
if err != nil {
|
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
||||||
err = core.Wrap(err, "PrivateKey")
|
if err != nil {
|
||||||
return InterfaceConfig{}, err
|
err = core.Wrap(err, "PrivateKey")
|
||||||
|
return InterfaceConfig{}, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
|
|||||||
+25
-9
@@ -51,6 +51,28 @@ func (pub PublicKey) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalText loads the value from base64
|
||||||
|
func (key *PrivateKey) UnmarshalText(b []byte) error {
|
||||||
|
v, err := PrivateKeyFromBase64(string(b))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*key = v
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText loads the value from base64
|
||||||
|
func (pub *PublicKey) UnmarshalText(b []byte) error {
|
||||||
|
v, err := PublicKeyFromBase64(string(b))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*pub = v
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omitting empty.
|
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||||
return encodeKeyJSON(key.String())
|
return encodeKeyJSON(key.String())
|
||||||
@@ -183,20 +205,14 @@ type KeyPair struct {
|
|||||||
// Validate checks the PublicKey matches the PrivateKey,
|
// Validate checks the PublicKey matches the PrivateKey,
|
||||||
// and sets the PublicKey if missing
|
// and sets the PublicKey if missing
|
||||||
func (kp *KeyPair) Validate() error {
|
func (kp *KeyPair) Validate() error {
|
||||||
keyLen := len(kp.PrivateKey)
|
|
||||||
pubLen := len(kp.PublicKey)
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case keyLen != PrivateKeySize:
|
case kp.PrivateKey.IsZero():
|
||||||
// bad private key
|
// no private key
|
||||||
return ErrInvalidPrivateKey
|
return ErrInvalidPrivateKey
|
||||||
case pubLen == 0:
|
case kp.PublicKey.IsZero():
|
||||||
// no public key, set it
|
// no public key, set it
|
||||||
kp.PublicKey = kp.PrivateKey.Public()
|
kp.PublicKey = kp.PrivateKey.Public()
|
||||||
return nil
|
return nil
|
||||||
case pubLen != PublicKeySize:
|
|
||||||
// bad public key
|
|
||||||
return ErrInvalidPublicKey
|
|
||||||
case !kp.PrivateKey.Public().Equal(kp.PublicKey):
|
case !kp.PrivateKey.Public().Equal(kp.PublicKey):
|
||||||
// wrong public key
|
// wrong public key
|
||||||
return ErrInvalidPublicKey
|
return ErrInvalidPublicKey
|
||||||
|
|||||||
Reference in New Issue
Block a user