Compare commits
49 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f5ac151c8 | |||
| 28bcaa2838 | |||
| 3cafb1a4e2 | |||
| 09bec11506 | |||
| 14686ff5a8 | |||
| 3c24e24d71 | |||
| 45dc2291bf | |||
| b019d303d4 | |||
| f64f4e08fe | |||
| 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 |
@@ -1 +1,2 @@
|
||||
.tmp
|
||||
.version
|
||||
|
||||
+40
-17
@@ -12,6 +12,14 @@ const (
|
||||
// DefaultConfigFile is read if -f/--config-file isn't specified.
|
||||
// If it doesn't exist, m/ will be scanned
|
||||
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
|
||||
@@ -22,27 +30,34 @@ type Config struct {
|
||||
ConfigFile string
|
||||
}
|
||||
|
||||
var forceScan bool
|
||||
|
||||
var cfg = &Config{
|
||||
Base: "m",
|
||||
Domain: "jpi.cloud",
|
||||
Base: DefaultClusterDir,
|
||||
Domain: DefaultDomain,
|
||||
}
|
||||
|
||||
// LoadZones loads all zones and machines in the config directory
|
||||
// or file
|
||||
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
|
||||
// try config file first
|
||||
zones, err := cluster.NewFromConfig(cfg.ConfigFile,
|
||||
cluster.ResolvePublicAddresses(resolve),
|
||||
cluster.WithLogger(log),
|
||||
)
|
||||
var zones *cluster.Cluster
|
||||
var err error
|
||||
|
||||
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)
|
||||
if !forceScan {
|
||||
// try config file first
|
||||
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.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
|
||||
}
|
||||
}
|
||||
|
||||
// default file doesn't exist. scan instead.
|
||||
@@ -53,7 +68,15 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().
|
||||
StringVarP(&cfg.ConfigFile, "config-file", "f",
|
||||
DefaultConfigFile, "config file (JSON or YAML)")
|
||||
flags := rootCmd.PersistentFlags()
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
+36
-1
@@ -2,9 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"darvaza.org/core"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"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 {
|
||||
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
|
||||
})
|
||||
|
||||
@@ -149,10 +151,43 @@ var dnsShowCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
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() {
|
||||
rootCmd.AddCommand(dnsCmd)
|
||||
|
||||
dnsCmd.AddCommand(dnsWriteCmd)
|
||||
dnsCmd.AddCommand(dnsSyncCmd)
|
||||
dnsCmd.AddCommand(dnsShowCmd)
|
||||
dnsCmd.AddCommand(dnsAddCmd)
|
||||
}
|
||||
|
||||
@@ -3,9 +3,13 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"darvaza.org/sidecar/pkg/logger/zerolog"
|
||||
"darvaza.org/slog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var log = zerolog.New(nil, slog.Error)
|
||||
|
||||
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
|
||||
func fatal(err error, msg string, args ...any) {
|
||||
l := log.Fatal()
|
||||
@@ -19,3 +23,20 @@ func fatal(err error, msg string, args ...any) {
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
+6
-20
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"darvaza.org/sidecar/pkg/logger/zerolog"
|
||||
"darvaza.org/slog"
|
||||
_ "embed"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -13,11 +13,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
log = zerolog.New(nil, slog.Error)
|
||||
verbosity int
|
||||
rootCmd = &cobra.Command{
|
||||
Use: CmdName,
|
||||
Short: "control tool for jpi.cloud",
|
||||
rootCmd = &cobra.Command{
|
||||
Use: CmdName,
|
||||
Short: "control tool for jpi.cloud",
|
||||
Version: version,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -26,16 +25,3 @@ func main() {
|
||||
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))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
//go:generate sh -c "git describe | tr -d '\r\n' > .version"
|
||||
//go:embed .version
|
||||
var version string
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Returns jpictl's version",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
_, _ = fmt.Fprintf(os.Stdout, "%s\n", version)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
if version == "" {
|
||||
version = "undetermined"
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
}
|
||||
@@ -4,30 +4,30 @@ go 1.19
|
||||
|
||||
require (
|
||||
asciigoat.org/ini v0.2.5
|
||||
darvaza.org/core v0.10.0
|
||||
darvaza.org/resolver v0.5.8
|
||||
darvaza.org/sidecar v0.0.8
|
||||
darvaza.org/slog v0.5.4
|
||||
darvaza.org/slog/handlers/discard v0.4.6
|
||||
darvaza.org/core v0.12.0
|
||||
darvaza.org/resolver v0.9.2
|
||||
darvaza.org/sidecar v0.4.0
|
||||
darvaza.org/slog v0.5.7
|
||||
darvaza.org/slog/handlers/discard v0.4.11
|
||||
github.com/gofrs/uuid/v5 v5.0.0
|
||||
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.4
|
||||
github.com/spf13/cobra v1.7.0
|
||||
golang.org/x/crypto v0.14.0
|
||||
golang.org/x/net v0.17.0
|
||||
gopkg.in/gcfg.v1 v1.2.3
|
||||
github.com/mgechev/revive v1.3.7
|
||||
github.com/spf13/cobra v1.8.0
|
||||
golang.org/x/crypto v0.20.0
|
||||
golang.org/x/net v0.21.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
asciigoat.org/core v0.3.9 // indirect
|
||||
darvaza.org/slog/handlers/filter v0.4.6 // indirect
|
||||
darvaza.org/slog/handlers/zerolog v0.4.6 // indirect
|
||||
darvaza.org/cache/x/simplelru v0.1.8 // indirect
|
||||
darvaza.org/slog/handlers/filter v0.4.9 // indirect
|
||||
darvaza.org/slog/handlers/zerolog v0.4.9 // indirect
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/chavacava/garif v0.1.0 // indirect
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fatih/structtag v1.2.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
@@ -35,18 +35,18 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
|
||||
github.com/miekg/dns v1.1.56 // indirect
|
||||
github.com/miekg/dns v1.1.58 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97 // indirect
|
||||
github.com/rs/zerolog v1.31.0 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/rogpeppe/go-internal v1.11.0 // indirect
|
||||
github.com/rs/zerolog v1.32.0 // indirect
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/mod v0.13.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
golang.org/x/tools v0.14.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
golang.org/x/mod v0.15.0 // indirect
|
||||
golang.org/x/sync v0.6.0 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/tools v0.18.0 // indirect
|
||||
)
|
||||
|
||||
@@ -2,32 +2,34 @@ asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
|
||||
asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI=
|
||||
asciigoat.org/ini v0.2.5 h1:4gRIp9rU+XQt8+HMqZO5R7GavMv9Yl2+N+je6djDIAE=
|
||||
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/go.mod h1:72iWMVoXjMHjsPSlctDzA7yKzwXsj5dO+se6F9B3ERs=
|
||||
darvaza.org/resolver v0.5.8 h1:y410WQ3vRCgE7437eyA55cNMZRP32qYXiokLejkFQeg=
|
||||
darvaza.org/resolver v0.5.8/go.mod h1:QnfX+eSZZZbmnE3n+6w4gfqXDH1Gj2MWJVQxhlQDHq8=
|
||||
darvaza.org/sidecar v0.0.8 h1:vsWK2SZfBYzU999brmT8gzVeCRKbuNQZOVdG5zxjO6U=
|
||||
darvaza.org/sidecar v0.0.8/go.mod h1:G96TMPge2jqpKMpaCWc9zwdfaJTmko7dMMWXwDsdocM=
|
||||
darvaza.org/slog v0.5.4 h1:xzlWVzYh4tuZLnj4A9tOHXfn/SAEIkApXPvK3YDiW9g=
|
||||
darvaza.org/slog v0.5.4/go.mod h1:QFtY3QoQ7xxww85umlEKPcMCNzqNrHYqnj53KehsmBU=
|
||||
darvaza.org/slog/handlers/discard v0.4.6 h1:TatHJn34y6eKQzNRHSo6lGZnJg4SLOGaWstlvwwOyrE=
|
||||
darvaza.org/slog/handlers/discard v0.4.6/go.mod h1:AG8WKr7m11NPPzvHW/b8nCT5RvYR9RZcIT/NWUOoMAo=
|
||||
darvaza.org/slog/handlers/filter v0.4.6 h1:AI5AQDyXS534QeXIV54pAKxplA6AVZNr4H2PEmAXT0k=
|
||||
darvaza.org/slog/handlers/filter v0.4.6/go.mod h1:MGTKdlnA/FanOn3GU2mltzwBn41HgSxxNeWUQEKFbl8=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.6 h1:Di+FXUD2R2pKUrynaidyXzS0WsrEiwbL11LQlQzwZv4=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.6/go.mod h1:r5B9/FQ256R3Wo5vFLOa2YarM2P8WOjVjFn8xHikNjk=
|
||||
darvaza.org/cache/x/simplelru v0.1.8 h1:rvFucut4wKYbsYc994yR3P0M08NqlsvZxr5G4QK82tw=
|
||||
darvaza.org/cache/x/simplelru v0.1.8/go.mod h1:Mv1isOJTcXYK+aK0AvUe+/3KpRTXDsYga6rdTS/upNs=
|
||||
darvaza.org/core v0.12.0 h1:LLtYh9RZJSd0sgPTDocofCc4H5jbutSUQgPbKt+8gcI=
|
||||
darvaza.org/core v0.12.0/go.mod h1:47Ydh67KnzjLNu1mzX3r2zpphbxQqEaihMsUq5GflQ4=
|
||||
darvaza.org/resolver v0.9.2 h1:sUX6LZ1eN5TzJW7L4m7HM+BvwBeWl8dYYDGVSe+AIhk=
|
||||
darvaza.org/resolver v0.9.2/go.mod h1:XWqPhrxoOKNzRuSozOwmE1M6QVqQL28jEdxylnIO8Nw=
|
||||
darvaza.org/sidecar v0.4.0 h1:wHghxzLsiT82WDBBUf34aTqtOvRBg4UbxVIJgKNXRVA=
|
||||
darvaza.org/sidecar v0.4.0/go.mod h1:fUzjcFM4rN3bSEl4BKvok3MLpZWEhEa9+0/egmtpfMY=
|
||||
darvaza.org/slog v0.5.7 h1:JWC0OqvzR435AidIRDp4T9kdWTURWkUjzP4R78Koq1Q=
|
||||
darvaza.org/slog v0.5.7/go.mod h1:12L03t+KYhsZ9IbfF+8if5w9Y91af2par+bSzeBVqIQ=
|
||||
darvaza.org/slog/handlers/discard v0.4.11 h1:wr34OnDoRaMV1eGgW7yUaupQxjkTnuHrJmYRPj64RHM=
|
||||
darvaza.org/slog/handlers/discard v0.4.11/go.mod h1:ynxyLmZzZ5mP4ACLhQs4MEuDyhkIzjz6DfBHUjhnIK4=
|
||||
darvaza.org/slog/handlers/filter v0.4.9 h1:xD8OBwlJytpiwTSDDZqUuNSOsJuaManXQiOj9WEStr8=
|
||||
darvaza.org/slog/handlers/filter v0.4.9/go.mod h1:t+sjcf1c46kAdf1TRiQmop91xlkteZrC4WDXoVwHgP8=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.9 h1:08FjRnwRGtJsLLBnbgxVorb/bkgm5QEM/LXD2cxeCbM=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.9/go.mod h1:PZYfx6eOxQfD+cXJQp52iwKgcD30QVYHoXxOCojAOdw=
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
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/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
|
||||
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
@@ -37,11 +39,8 @@ 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/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/libdns/cloudflare v0.1.0 h1:93WkJaGaiXCe353LHEP36kAWCUw0YjFqwhkBkU2/iic=
|
||||
@@ -60,10 +59,10 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
|
||||
github.com/mgechev/revive v1.3.4 h1:k/tO3XTaWY4DEHal9tWBkkUMJYO/dLDVyMmAQxmIMDc=
|
||||
github.com/mgechev/revive v1.3.4/go.mod h1:W+pZCMu9qj8Uhfs1iJMQsEFLRozUfvwFwqVvRbSNLVw=
|
||||
github.com/miekg/dns v1.1.56 h1:5imZaSeoRNvpM9SzWNhEcP9QliKiz20/dA2QabIGVnE=
|
||||
github.com/miekg/dns v1.1.56/go.mod h1:cRm6Oo2C8TY9ZS/TqsSrseAcncm74lfK5G+ikN2SWWY=
|
||||
github.com/mgechev/revive v1.3.7 h1:502QY0vQGe9KtYJ9FpxMz9rL+Fc/P13CI5POL4uHCcE=
|
||||
github.com/mgechev/revive v1.3.7/go.mod h1:RJ16jUbF0OWC3co/+XTxmFNgEpUPwnnA0BRllX2aDNA=
|
||||
github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
|
||||
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
@@ -74,17 +73,19 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
|
||||
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97 h1:3RPlVWzZ/PDqmVuf/FKHARG5EMid/tl7cv54Sw/QRVY=
|
||||
github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
|
||||
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
|
||||
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
|
||||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
|
||||
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
|
||||
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
@@ -94,30 +95,26 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
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/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||
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/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
|
||||
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
|
||||
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
|
||||
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
|
||||
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
|
||||
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
|
||||
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
||||
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
|
||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -39,6 +39,9 @@ func writeGlobalToBuffer(w *bytes.Buffer, c *GlobalConfig) {
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "mon_initial_members", strings.Join(c.Monitors, ", "))
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "mon_host", joinAddrs(c.MonitorsAddr, ", "))
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "cluster_network", c.ClusterNetwork.String())
|
||||
|
||||
_, _ = fmt.Fprintf(w, "\n; %s\n", "don't rewrite labels on startup")
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "osd_class_update_on_start", "false")
|
||||
}
|
||||
|
||||
func joinAddrs(addrs []netip.Addr, sep string) string {
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
fs "github.com/hack-pad/hackpadfs"
|
||||
)
|
||||
@@ -40,6 +43,21 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose
|
||||
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
|
||||
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
if len(args) > 0 {
|
||||
@@ -48,3 +66,50 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
|
||||
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 (
|
||||
"io/fs"
|
||||
"path"
|
||||
"sort"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
const (
|
||||
// ZoneRegionsFileName indicates the file containing
|
||||
// region names as references
|
||||
ZoneRegionsFileName = "regions"
|
||||
)
|
||||
|
||||
func (m *Cluster) scan(opts *ScanOptions) error {
|
||||
for _, fn := range []func(*ScanOptions) error{
|
||||
m.scanDirectory,
|
||||
m.scanMachines,
|
||||
m.scanZoneIDs,
|
||||
m.scanSort,
|
||||
m.initRegions,
|
||||
m.scanGateways,
|
||||
m.scanCephMonitors,
|
||||
} {
|
||||
@@ -24,7 +32,7 @@ func (m *Cluster) scan(opts *ScanOptions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
||||
func (m *Cluster) scanDirectory(opts *ScanOptions) error {
|
||||
// each directory is a zone
|
||||
entries, err := fs.ReadDir(m.dir, ".")
|
||||
if err != nil {
|
||||
@@ -33,16 +41,14 @@ func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
z, err := m.newZone(e.Name())
|
||||
ok, err := m.scanSubdirectory(opts, e.Name())
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, e.Name())
|
||||
case z.Machines.Len() == 0:
|
||||
z.warn(nil).
|
||||
WithField("zone", z.Name).
|
||||
case !ok:
|
||||
m.warn(nil).
|
||||
WithField("zone", e.Name()).
|
||||
Print("empty")
|
||||
default:
|
||||
m.Zones = append(m.Zones, z)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,6 +56,27 @@ func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
||||
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) {
|
||||
z := &Zone{
|
||||
zones: m,
|
||||
@@ -73,6 +100,10 @@ func (m *Cluster) scanMachines(opts *ScanOptions) error {
|
||||
err = p.scan(opts)
|
||||
return err != nil
|
||||
})
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.scanWrapUp(opts)
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -150,34 +181,68 @@ func (z *Zone) scan() error {
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
m := &Machine{
|
||||
zone: z,
|
||||
logger: z,
|
||||
Name: e.Name(),
|
||||
}
|
||||
name := e.Name()
|
||||
|
||||
m.debug().
|
||||
WithField("node", m.Name).
|
||||
switch {
|
||||
case name == ZoneRegionsFileName:
|
||||
err = z.loadRegions()
|
||||
case e.IsDir():
|
||||
err = z.scanSubdirectory(name)
|
||||
default:
|
||||
z.warn(nil).
|
||||
WithField("zone", z.Name).
|
||||
Print("found")
|
||||
WithField("filename", name).
|
||||
Print("unknown")
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
// files will be created to enable the first [Machine] to
|
||||
// be one
|
||||
|
||||
@@ -27,7 +27,7 @@ type ScanOptions struct {
|
||||
// the DNS resolver to get PublicAddresses of nodes.
|
||||
// Default is true
|
||||
func ResolvePublicAddresses(resolve bool) ScanOption {
|
||||
return func(m *Cluster, opt *ScanOptions) error {
|
||||
return func(_ *Cluster, opt *ScanOptions) error {
|
||||
opt.DontResolvePublicAddresses = !resolve
|
||||
return nil
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func ResolvePublicAddresses(resolve bool) ScanOption {
|
||||
// WithLookuper specifies what resolver.Lookuper to use to
|
||||
// find public addresses
|
||||
func WithLookuper(h resolver.Lookuper) ScanOption {
|
||||
return func(m *Cluster, opt *ScanOptions) error {
|
||||
return func(m *Cluster, _ *ScanOptions) error {
|
||||
if h == nil {
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
@@ -49,7 +49,7 @@ func WithLookuper(h resolver.Lookuper) ScanOption {
|
||||
// public addresses. if nil is passed, the [net.Resolver] will be used.
|
||||
// The default is using Cloudflare's 1.1.1.1.
|
||||
func WithResolver(h resolver.Resolver) ScanOption {
|
||||
return func(m *Cluster, opt *ScanOptions) error {
|
||||
return func(m *Cluster, _ *ScanOptions) error {
|
||||
if h == nil {
|
||||
h = resolver.SystemResolver(true)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ type Machine struct {
|
||||
ID int
|
||||
Name string `json:"-" yaml:"-"`
|
||||
|
||||
Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"`
|
||||
CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
|
||||
PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"`
|
||||
Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"`
|
||||
@@ -43,6 +44,11 @@ func (m *Machine) FullName() string {
|
||||
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
|
||||
func (m *Machine) IsGateway() bool {
|
||||
_, ok := m.getRingInfo(0)
|
||||
|
||||
+20
-21
@@ -1,7 +1,6 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -12,10 +11,9 @@ import (
|
||||
|
||||
// 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) {
|
||||
base := m.zone.zones.dir
|
||||
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
|
||||
@@ -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
|
||||
func (m *Machine) RemoveFile(name string, args ...any) error {
|
||||
base := m.zone.zones.dir
|
||||
fullName := m.getFilename(name, args...)
|
||||
err := fs.Remove(base, fullName)
|
||||
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
return m.zone.zones.RemoveFile(fullName)
|
||||
}
|
||||
|
||||
// ReadFile reads a file from the machine's config directory
|
||||
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
base := m.zone.zones.dir
|
||||
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
|
||||
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
||||
f, err := m.CreateTruncFile(name, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
fullName := m.getFilename(name, args...)
|
||||
|
||||
buf := bytes.NewBufferString(value)
|
||||
_, err = buf.WriteTo(f)
|
||||
return err
|
||||
return m.zone.zones.WriteStringFile(value, fullName)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
||||
@@ -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()
|
||||
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
||||
if !ok {
|
||||
return fmt.Errorf("%s: invalid address", addr)
|
||||
}
|
||||
if !core.IsZero(addr) {
|
||||
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
||||
if !ok {
|
||||
return fmt.Errorf("%s: invalid address", addr)
|
||||
}
|
||||
|
||||
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||
return core.Wrap(err, "%s: invalid address", addr)
|
||||
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||
return core.Wrap(err, "%s: invalid address", addr)
|
||||
}
|
||||
}
|
||||
|
||||
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
||||
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 {
|
||||
err := m.applyWireguardPeerConfig(ring, peer)
|
||||
switch {
|
||||
@@ -230,6 +240,23 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
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
|
||||
// config directory.
|
||||
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
||||
|
||||
@@ -3,6 +3,7 @@ package cluster
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -68,7 +69,8 @@ func (m *Machine) setID() error {
|
||||
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++ {
|
||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||
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 {
|
||||
return m.UpdatePublicAddresses()
|
||||
}
|
||||
|
||||
+105
-4
@@ -1,5 +1,10 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
_ MachineIterator = (*Region)(nil)
|
||||
_ ZoneIterator = (*Region)(nil)
|
||||
@@ -32,7 +37,9 @@ func (r *Region) ForEachMachine(fn func(*Machine) bool) {
|
||||
var term bool
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
term = fn(p)
|
||||
if p.IsActive() {
|
||||
term = fn(p)
|
||||
}
|
||||
return term
|
||||
})
|
||||
|
||||
@@ -56,6 +63,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
||||
|
||||
// first regions defined by zones
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
SortRegions(z.Regions)
|
||||
for _, region := range z.Regions {
|
||||
regions[region] = append(regions[region], z)
|
||||
}
|
||||
@@ -65,7 +73,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
||||
|
||||
// bind first level regions and their zones
|
||||
for name, zones := range regions {
|
||||
m.syncRegions(name, zones...)
|
||||
m.setRegionZones(name, zones...)
|
||||
}
|
||||
|
||||
// and combine zones to produce larger regions
|
||||
@@ -74,11 +82,14 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
||||
m.finishRegion(r)
|
||||
}
|
||||
|
||||
m.sortRegions()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Cluster) syncRegions(name string, zones ...*Zone) {
|
||||
for _, r := range m.Regions {
|
||||
func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
|
||||
for i := range m.Regions {
|
||||
r := &m.Regions[i]
|
||||
|
||||
if r.Name == name {
|
||||
// found
|
||||
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) {
|
||||
if r.m != nil {
|
||||
// ready
|
||||
@@ -128,3 +171,61 @@ func (m *Cluster) getRegion(name string) (*Region, bool) {
|
||||
|
||||
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
|
||||
return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled)
|
||||
case !canMergeKeyPairs(ri.Keys, alter.Keys):
|
||||
// incompatible keypairs
|
||||
// incompatible key pairs
|
||||
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@ package cluster
|
||||
// SyncAll updates all config files
|
||||
func (m *Cluster) SyncAll() error {
|
||||
for _, fn := range []func() error{
|
||||
m.SyncMkdirAll,
|
||||
m.SyncAllWireguard,
|
||||
m.SyncAllCeph,
|
||||
m.SyncAllRegions,
|
||||
m.WriteHosts,
|
||||
} {
|
||||
if err := fn(); err != nil {
|
||||
@@ -15,6 +17,20 @@ func (m *Cluster) SyncAll() error {
|
||||
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
|
||||
func (m *Cluster) SyncAllWireguard() error {
|
||||
var err error
|
||||
@@ -43,3 +59,24 @@ func (m *Cluster) SyncAllCeph() error {
|
||||
|
||||
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
|
||||
}
|
||||
+54
-20
@@ -6,10 +6,13 @@ import (
|
||||
"io"
|
||||
"net/netip"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"darvaza.org/core"
|
||||
"github.com/libdns/libdns"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||
)
|
||||
|
||||
func (mgr *Manager) fqdn(name string) string {
|
||||
@@ -38,30 +41,51 @@ func SortAddrRecords(s []AddrRecord) []AddrRecord {
|
||||
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]
|
||||
// SortRecords sorts a slice of [libdns.Record], by Name, Type and Value
|
||||
func SortRecords(s []libdns.Record) []libdns.Record {
|
||||
sort.Slice(s, func(i, j int) bool {
|
||||
return lessRecord(s[i], s[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 {
|
||||
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:
|
||||
case aa.UnmarshalText([]byte(a.Value)) != nil:
|
||||
// bad address on a
|
||||
return true
|
||||
case ba.UnmarshalText([]byte(b.Value)) != nil:
|
||||
// bad address on b
|
||||
return false
|
||||
default:
|
||||
return r1 < r2
|
||||
return aa.Less(ba)
|
||||
}
|
||||
})
|
||||
return regions
|
||||
default:
|
||||
// text
|
||||
return a.Value < b.Value
|
||||
}
|
||||
}
|
||||
|
||||
// AddrRecord represents an A or AAAA record
|
||||
@@ -124,7 +148,17 @@ func (mgr *Manager) genRegionsSorted() []string {
|
||||
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 {
|
||||
|
||||
+37
-8
@@ -1,11 +1,15 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"darvaza.org/core"
|
||||
"github.com/libdns/libdns"
|
||||
)
|
||||
|
||||
// Show shows current DNS entries
|
||||
@@ -15,15 +19,40 @@ func (mgr *Manager) Show(ctx context.Context, names ...string) error {
|
||||
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 {
|
||||
_, _ = fmt.Printf("%s\t%v\tIN\t%s\t%s\t; %s\n",
|
||||
rr.Name,
|
||||
int(rr.TTL/time.Second),
|
||||
rr.Type,
|
||||
rr.Value,
|
||||
rr.ID)
|
||||
_ = 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
|
||||
}
|
||||
|
||||
_, _ = fmt.Printf("; %v records\n", len(recs))
|
||||
return nil
|
||||
_, 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
|
||||
}
|
||||
|
||||
+9
-4
@@ -54,7 +54,7 @@ func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mgr.filteredSyncRecords(recs)
|
||||
return mgr.asSyncRecords(recs)
|
||||
}
|
||||
|
||||
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
|
||||
@@ -89,9 +89,9 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
|
||||
return out, true, nil
|
||||
}
|
||||
|
||||
func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
||||
func (mgr *Manager) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr {
|
||||
// filter and convert
|
||||
cache := make(map[string][]SyncAddr)
|
||||
out := make(map[string][]SyncAddr)
|
||||
for _, rr := range recs {
|
||||
addr, ok, err := mgr.AsSyncAddr(rr)
|
||||
switch {
|
||||
@@ -106,9 +106,14 @@ func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord,
|
||||
Print()
|
||||
case ok:
|
||||
// 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
|
||||
out := make([]SyncAddrRecord, len(cache))
|
||||
|
||||
+3
-2
@@ -14,12 +14,13 @@ func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
|
||||
cache := make(map[string][]netip.Addr)
|
||||
|
||||
// zones
|
||||
for _, z := range mgr.zones {
|
||||
for _, zoneName := range mgr.genZonesSorted() {
|
||||
z := mgr.zones[zoneName]
|
||||
|
||||
mgr.writeZoneHosts(&buf, z)
|
||||
|
||||
// zone alias
|
||||
addrs := mgr.genZoneAddresses(z)
|
||||
zoneName := z.Name
|
||||
|
||||
rr := AddrRecord{
|
||||
Name: mgr.fqdn(zoneName + mgr.suffix),
|
||||
|
||||
+14
-110
@@ -2,7 +2,6 @@ package wireguard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/netip"
|
||||
@@ -10,8 +9,8 @@ import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"asciigoat.org/ini/basic"
|
||||
"darvaza.org/core"
|
||||
"gopkg.in/gcfg.v1"
|
||||
)
|
||||
|
||||
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
||||
@@ -107,6 +106,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"
|
||||
func (ep *EndpointAddress) FromString(s string) error {
|
||||
host, port, err := core.SplitHostPort(s)
|
||||
@@ -127,98 +131,6 @@ func (ep *EndpointAddress) FromString(s string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type intermediateConfig struct {
|
||||
Interface interfaceConfig
|
||||
Peer peersConfig
|
||||
}
|
||||
|
||||
func (v *intermediateConfig) Export() (*Config, error) {
|
||||
var out Config
|
||||
var err error
|
||||
|
||||
// Interface
|
||||
out.Interface, err = v.Interface.Export()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Peers
|
||||
peers, ok := v.PeersCount()
|
||||
if !ok {
|
||||
return nil, errors.New("inconsistent Peer data")
|
||||
}
|
||||
|
||||
for i := 0; i < peers; i++ {
|
||||
p, err := v.ExportPeer(i)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "Peer[%v]:", i)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out.Peer = append(out.Peer, p)
|
||||
}
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
type interfaceConfig struct {
|
||||
Address netip.Addr
|
||||
PrivateKey string
|
||||
ListenPort uint16
|
||||
}
|
||||
|
||||
func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
||||
var err error
|
||||
|
||||
out := InterfaceConfig{
|
||||
Address: p.Address,
|
||||
ListenPort: p.ListenPort,
|
||||
}
|
||||
|
||||
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PrivateKey")
|
||||
return InterfaceConfig{}, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type peersConfig struct {
|
||||
PublicKey []string
|
||||
Endpoint []string
|
||||
AllowedIPs []string
|
||||
}
|
||||
|
||||
func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
||||
var out PeerConfig
|
||||
|
||||
// Endpoint
|
||||
s := v.Peer.Endpoint[i]
|
||||
err := out.Endpoint.FromString(s)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "Endpoint")
|
||||
return out, err
|
||||
}
|
||||
|
||||
// PublicKey
|
||||
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i])
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PublicKey")
|
||||
return out, err
|
||||
}
|
||||
|
||||
// AllowedIPs
|
||||
s = v.Peer.AllowedIPs[i]
|
||||
out.AllowedIPs, err = parseAllowedIPs(s)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "AllowedIPs")
|
||||
return out, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
||||
var out []netip.Prefix
|
||||
|
||||
@@ -235,25 +147,17 @@ func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (v *intermediateConfig) PeersCount() (int, bool) {
|
||||
c0 := len(v.Peer.Endpoint)
|
||||
c1 := len(v.Peer.PublicKey)
|
||||
c2 := len(v.Peer.AllowedIPs)
|
||||
|
||||
if c0 != c1 || c1 != c2 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return c0, true
|
||||
}
|
||||
|
||||
// NewConfigFromReader parses a wgN.conf file
|
||||
func NewConfigFromReader(r io.Reader) (*Config, error) {
|
||||
temp := &intermediateConfig{}
|
||||
|
||||
if err := gcfg.ReadInto(temp, r); err != nil {
|
||||
doc, err := basic.Decode(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return temp.Export()
|
||||
cfg, err := newConfigFromDocument(doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"strconv"
|
||||
|
||||
"asciigoat.org/ini/basic"
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
type sectionHandler func(*Config, *basic.Section) error
|
||||
|
||||
var sectionMap = map[string]func(*Config, *basic.Section) error{
|
||||
"Interface": loadInterfaceConfSection,
|
||||
"Peer": loadPeerConfSection,
|
||||
}
|
||||
|
||||
func loadConfSection(out *Config, src *basic.Section) error {
|
||||
h, ok := sectionMap[src.Key]
|
||||
if !ok {
|
||||
return core.Wrap(fs.ErrInvalid, "unknown section %q", src.Key)
|
||||
}
|
||||
|
||||
return h(out, src)
|
||||
}
|
||||
|
||||
func loadInterfaceConfSection(out *Config, src *basic.Section) error {
|
||||
var cfg InterfaceConfig
|
||||
|
||||
for _, field := range src.Fields {
|
||||
if err := loadInterfaceConfField(&cfg, field); err != nil {
|
||||
return core.Wrap(err, "Interface")
|
||||
}
|
||||
}
|
||||
|
||||
out.Interface = cfg
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadPeerConfSection(out *Config, src *basic.Section) error {
|
||||
var cfg PeerConfig
|
||||
|
||||
for _, field := range src.Fields {
|
||||
if err := loadPeerConfField(&cfg, field); err != nil {
|
||||
return core.Wrap(err, "Peer[%v]", len(out.Peer))
|
||||
}
|
||||
}
|
||||
|
||||
out.Peer = append(out.Peer, cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// revive:disable:cyclomatic
|
||||
// revive:disable:cognitive-complexity
|
||||
|
||||
func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error {
|
||||
// revive:enable:cyclomatic
|
||||
// revive:enable:cognitive-complexity
|
||||
|
||||
// TODO: refactor when asciigoat's ini parser learns to do reflection
|
||||
switch field.Key {
|
||||
case "Address":
|
||||
if !core.IsZero(cfg.Address) {
|
||||
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.Address.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "PrivateKey":
|
||||
if !core.IsZero(cfg.PrivateKey) {
|
||||
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.PrivateKey.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "ListenPort":
|
||||
if cfg.ListenPort > 0 {
|
||||
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
u64, err := strconv.ParseUint(field.Value, 10, 16)
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
case u64 == 0:
|
||||
return core.Wrap(fs.ErrInvalid, "invalid %q value", field.Key)
|
||||
default:
|
||||
cfg.ListenPort = uint16(u64)
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key)
|
||||
}
|
||||
}
|
||||
|
||||
// revive:disable:cyclomatic
|
||||
// revive:disable:cognitive-complexity
|
||||
|
||||
func loadPeerConfField(cfg *PeerConfig, field basic.Field) error {
|
||||
// revive:enable:cyclomatic
|
||||
// revive:enable:cognitive-complexity
|
||||
|
||||
switch field.Key {
|
||||
case "PublicKey":
|
||||
if !core.IsZero(cfg.PublicKey) {
|
||||
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.PublicKey.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "Endpoint":
|
||||
if cfg.Endpoint.String() != "" {
|
||||
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.Endpoint.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "AllowedIPs":
|
||||
s, err := parseAllowedIPs(field.Value)
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
case len(s) > 0:
|
||||
cfg.AllowedIPs = append(cfg.AllowedIPs, s...)
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newConfigFromDocument(doc *basic.Document) (*Config, error) {
|
||||
var out Config
|
||||
|
||||
if len(doc.Global) > 0 {
|
||||
err := core.Wrap(fs.ErrInvalid, "fields before the first section")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range doc.Sections {
|
||||
src := &doc.Sections[i]
|
||||
if err := loadConfSection(&out, src); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||
return encodeKeyJSON(key.String())
|
||||
@@ -183,20 +205,14 @@ type KeyPair struct {
|
||||
// Validate checks the PublicKey matches the PrivateKey,
|
||||
// and sets the PublicKey if missing
|
||||
func (kp *KeyPair) Validate() error {
|
||||
keyLen := len(kp.PrivateKey)
|
||||
pubLen := len(kp.PublicKey)
|
||||
|
||||
switch {
|
||||
case keyLen != PrivateKeySize:
|
||||
// bad private key
|
||||
case kp.PrivateKey.IsZero():
|
||||
// no private key
|
||||
return ErrInvalidPrivateKey
|
||||
case pubLen == 0:
|
||||
case kp.PublicKey.IsZero():
|
||||
// no public key, set it
|
||||
kp.PublicKey = kp.PrivateKey.Public()
|
||||
return nil
|
||||
case pubLen != PublicKeySize:
|
||||
// bad public key
|
||||
return ErrInvalidPublicKey
|
||||
case !kp.PrivateKey.Public().Equal(kp.PublicKey):
|
||||
// wrong public key
|
||||
return ErrInvalidPublicKey
|
||||
|
||||
Reference in New Issue
Block a user