Compare commits
136 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3de7fcb605 | |||
| d0b0698c10 | |||
| 2a2e6c121e | |||
| 24059dc9ee | |||
| bedf62977f | |||
| 5abb4c2f92 | |||
| 046c9a508b | |||
|
f6766547f9
|
|||
| 6aec17d079 | |||
| ab8ffdd507 | |||
| 422e119f88 | |||
| 204f3a49a1 | |||
|
d1198328f6
|
|||
| 7795610caf | |||
| 32046fc1ec | |||
| 2016b27707 | |||
| c038ad4431 | |||
| 159ccf59ac | |||
| 6a071ba5f0 | |||
| 3e90e3ab1e | |||
| 90dd0c1239 | |||
| 033ca2f20e | |||
| 8c32b88e24 | |||
| 1bca1f7da1 | |||
| 5e5958d22e | |||
| 45447275a7 | |||
| e03e5e0d05 | |||
| a655603343 | |||
| c291b218a4 | |||
| 3911a51ccf | |||
| 1fe1cf940d | |||
| f10ea1dc22 | |||
| ac87757b06 | |||
| fe081a4297 | |||
| cea8362fe6 | |||
| b772ec0a3d | |||
| 77ad016e99 | |||
| bf4bfeb3fc | |||
| e3ab931eb1 | |||
| 05e04c758b | |||
| 94011a3a03 | |||
| 025b9072b4 | |||
| 0fb8c1d44b | |||
| a8849b747c | |||
| 879d2b4d1c | |||
| ff4bb97599 | |||
| c95bf06da2 | |||
| 2e48f34f7f | |||
| 8e5fc44e62 | |||
|
3d5a766161
|
|||
| 6f5ca3f235 | |||
| 296d4007ff | |||
| 1a03404a07 | |||
| d2f0a0744b | |||
| 71a1d1a7c2 | |||
| de45fa6c30 | |||
| 6e46d23b45 | |||
| 94daf5ad59 | |||
| 0989dec5e8 | |||
| 216bf5aa29 | |||
| 9af88f6593 | |||
| af2d836000 | |||
| 1655ce85bc | |||
| 9c4f6d987d | |||
| fb82a7f358 | |||
| f63ce6c4e7 | |||
| 1885c76198 | |||
| 2224e70638 | |||
| 6ee848e6ca | |||
| 864eb02f9d | |||
| 9da2f8711f | |||
| 2a14205e7e | |||
| a5d9466fb8 | |||
| 3d638e9a85 | |||
| 60d3a2c290 | |||
| af90825f13 | |||
| b4f1d2e4d9 | |||
| acf9e0e81d | |||
| 3b43e0c9ea | |||
| 9762e78f5e | |||
| 3534e7b755 | |||
| b80dc84a26 | |||
| c0ef6ae9c4 | |||
| 58867031ea | |||
| b95d1f1878 | |||
| d38c909b0b | |||
| 7dd3ea8f96 | |||
| 07b4a22752 | |||
| 609f48a2d1 | |||
| d1f7d225ae | |||
| dfbb358187 | |||
| 26c49dff72 | |||
| 2043708949 | |||
| 311ae572da | |||
| 4ca77b0ac0 | |||
| 1859c8e04b | |||
| 202f2e6dfc | |||
| 20484a5061 | |||
| 45b25c63d4 | |||
| c0e2ae9bf1 | |||
| 080021b427 | |||
| 4514b44211 | |||
| 49b82ace71 | |||
| 2207e4a4a4 | |||
| 7ca01aa1e4 | |||
| 8b72667f4d | |||
| 49694eb7cb | |||
| 15a98c05ec | |||
| a005823d44 | |||
| 7af8484acc | |||
| 0f1f1ce968 | |||
| 5058f286c6 | |||
| 86075eb47f | |||
| c81b782b26 | |||
| 0f62ee2e53 | |||
| 30a7bceda3 | |||
| 60e2687d04 | |||
| 1419e55d5b | |||
| ffdacb833b | |||
| aca0a5e834 | |||
| 61374d4cc5 | |||
| 975e166da7 | |||
| b16c648f2c | |||
| 47d79f7576 | |||
| e2f831fd6a | |||
| 1d8c818ec4 | |||
| 2f51a463b2 | |||
| 0c0cba6fb5 | |||
| 75206e4fa5 | |||
| b084e103b9 | |||
| 223edf846b | |||
| fdb0f0324f | |||
| 9aef92f32d | |||
| e5baf53758 | |||
| 0fe451eed0 | |||
| cb5ea80e66 |
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"ceph",
|
||||
"darvaza",
|
||||
"gofrs",
|
||||
"jpictl",
|
||||
"zerolog"
|
||||
]
|
||||
}
|
||||
+12
-2
@@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
import "git.jpi.io/amery/jpictl/pkg/cluster"
|
||||
|
||||
// Config describes the repository
|
||||
type Config struct {
|
||||
Base string
|
||||
@@ -7,6 +9,14 @@ type Config struct {
|
||||
}
|
||||
|
||||
var cfg = &Config{
|
||||
Base: "./m",
|
||||
Domain: "m.jpi.cloud",
|
||||
Base: "m",
|
||||
Domain: "jpi.cloud",
|
||||
}
|
||||
|
||||
// LoadZones loads all zones and machines in the config directory
|
||||
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
|
||||
return cluster.NewFromDirectory(cfg.Base, cfg.Domain,
|
||||
cluster.ResolvePublicAddresses(resolve),
|
||||
cluster.WithLogger(log),
|
||||
)
|
||||
}
|
||||
|
||||
+48
-7
@@ -2,27 +2,68 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/burntSushi/toml"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Encoder represents an object that encodes another internally
|
||||
type Encoder interface {
|
||||
Encode(any) error
|
||||
}
|
||||
|
||||
// Encoding represents a type of [Encoder]
|
||||
type Encoding int
|
||||
|
||||
const (
|
||||
// UndefinedEncoding implies the default encoding
|
||||
UndefinedEncoding Encoding = iota
|
||||
// JSONEncoding represents JSON encoding
|
||||
JSONEncoding
|
||||
// YAMLEncoding represents YAML encoding
|
||||
YAMLEncoding
|
||||
)
|
||||
|
||||
// NewJSONEncoder returns a JSON [Encoder] to work on the given [io.Writer]
|
||||
func NewJSONEncoder(w io.Writer) Encoder {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent(``, ` `)
|
||||
return enc
|
||||
}
|
||||
|
||||
// NewYAMLEncoder returns a YAML [Encoder] to work on the given [io.Writer]
|
||||
func NewYAMLEncoder(w io.Writer) Encoder {
|
||||
enc := yaml.NewEncoder(w)
|
||||
enc.SetIndent(2)
|
||||
return enc
|
||||
}
|
||||
|
||||
const encoding = YAMLEncoding
|
||||
|
||||
// Command
|
||||
var dumpCmd = &cobra.Command{
|
||||
Use: "dump",
|
||||
Short: "generates a toml representation of the config",
|
||||
Use: "dump",
|
||||
Short: "generates a text representation of the config",
|
||||
PreRun: setVerbosity,
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
var buf bytes.Buffer
|
||||
var enc Encoder
|
||||
|
||||
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||
m, err := cfg.LoadZones(true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enc := toml.NewEncoder(&buf)
|
||||
switch encoding {
|
||||
case JSONEncoding:
|
||||
enc = NewJSONEncoder(&buf)
|
||||
default:
|
||||
enc = NewYAMLEncoder(&buf)
|
||||
}
|
||||
|
||||
if err = enc.Encode(m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Command
|
||||
var envCmd = &cobra.Command{
|
||||
Use: "env",
|
||||
Short: "generates environment variables for shell scripts",
|
||||
PreRun: setVerbosity,
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
env, err := m.Env(*envExport)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = env.WriteTo(os.Stdout)
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
// Command Flags
|
||||
var (
|
||||
envExport *bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(envCmd)
|
||||
|
||||
envExport = envCmd.PersistentFlags().BoolP("export", "e", false,
|
||||
"export generated variables")
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||
)
|
||||
|
||||
// Command
|
||||
var gatewayCmd = &cobra.Command{
|
||||
Use: "gateway",
|
||||
Short: "gateway operates on ring0/ring1 gateways",
|
||||
}
|
||||
|
||||
// gateway set
|
||||
var gatewaySetCmd = &cobra.Command{
|
||||
Use: "set",
|
||||
Short: "gateway set sets machines as gateways",
|
||||
PreRun: setVerbosity,
|
||||
RunE: func(_ *cobra.Command, args []string) error {
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, arg := range args {
|
||||
err = gatewaySet(m, arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func gatewaySet(zi cluster.ZoneIterator, gw string) error {
|
||||
var err error
|
||||
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||
for _, m := range z.Machines {
|
||||
if m.Name == gw {
|
||||
z.SetGateway(m.ID, true)
|
||||
return true
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("machine %s not found", gw)
|
||||
return false
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// gateway unset
|
||||
var gatewayUnsetCmd = &cobra.Command{
|
||||
Use: "unset",
|
||||
Short: "gateway unset sets machines as non-gateways",
|
||||
RunE: func(_ *cobra.Command, args []string) error {
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, arg := range args {
|
||||
err = gatewayUnset(m, arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func gatewayUnset(zi cluster.ZoneIterator, ngw string) error {
|
||||
var err error
|
||||
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||
for _, m := range z.Machines {
|
||||
if m.Name == ngw && m.IsGateway() {
|
||||
z.SetGateway(m.ID, false)
|
||||
m.RemoveWireguardConfig(0)
|
||||
return true
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("machine %s not found", ngw)
|
||||
return false
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// gateway list
|
||||
var gatewayListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "gateway list lists gateways",
|
||||
RunE: func(_ *cobra.Command, args []string) error {
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch {
|
||||
case len(args) == 0:
|
||||
return gatewayListAll(m)
|
||||
default:
|
||||
for _, arg := range args {
|
||||
err = gatewayList(m, arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func gatewayListAll(zi cluster.ZoneIterator) error {
|
||||
var b bytes.Buffer
|
||||
var err error
|
||||
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||
b.WriteString(z.Name + ":")
|
||||
var sIDs []string
|
||||
ids, num := z.GatewayIDs()
|
||||
if num == 0 {
|
||||
err = fmt.Errorf("no gateway in zone %s", z.Name)
|
||||
return false
|
||||
}
|
||||
for _, i := range ids {
|
||||
sIDs = append(sIDs, strconv.Itoa(i))
|
||||
}
|
||||
b.WriteString(strings.Join(sIDs, ", "))
|
||||
b.WriteString("\n")
|
||||
_, err = b.WriteTo(os.Stdout)
|
||||
return false
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func gatewayList(zi cluster.ZoneIterator, m string) error {
|
||||
var b bytes.Buffer
|
||||
var err error
|
||||
zi.ForEachZone(func(z *cluster.Zone) bool {
|
||||
if z.Name == m {
|
||||
b.WriteString(z.Name + ":")
|
||||
ids, num := z.GatewayIDs()
|
||||
if num == 0 {
|
||||
err = fmt.Errorf("no gateway in zone %s", z.Name)
|
||||
return true
|
||||
}
|
||||
|
||||
b.WriteString(fmt.Sprint(ids))
|
||||
b.WriteString("\n")
|
||||
_, err = b.WriteTo(os.Stdout)
|
||||
return true
|
||||
}
|
||||
err = fmt.Errorf("zone %s not found", m)
|
||||
return false
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(gatewayCmd)
|
||||
|
||||
gatewayCmd.AddCommand(gatewaySetCmd)
|
||||
gatewayCmd.AddCommand(gatewayUnsetCmd)
|
||||
gatewayCmd.AddCommand(gatewayListCmd)
|
||||
}
|
||||
@@ -3,14 +3,9 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"darvaza.org/sidecar/pkg/logger/zerolog"
|
||||
"darvaza.org/slog"
|
||||
)
|
||||
|
||||
var (
|
||||
log = zerolog.New(nil, slog.Debug)
|
||||
)
|
||||
|
||||
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
|
||||
func fatal(err error, msg string, args ...any) {
|
||||
l := log.Fatal()
|
||||
|
||||
+23
-4
@@ -2,6 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"darvaza.org/sidecar/pkg/logger/zerolog"
|
||||
"darvaza.org/slog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -10,13 +12,30 @@ const (
|
||||
CmdName = "jpictl"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: CmdName,
|
||||
Short: "control tool for jpi.cloud",
|
||||
}
|
||||
var (
|
||||
log = zerolog.New(nil, slog.Error)
|
||||
verbosity int
|
||||
rootCmd = &cobra.Command{
|
||||
Use: CmdName,
|
||||
Short: "control tool for jpi.cloud",
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
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 = log.WithLevel(slog.LogLevel(desired))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Command
|
||||
var writeCmd = &cobra.Command{
|
||||
Use: "write",
|
||||
Short: "rewrites all config files",
|
||||
PreRun: setVerbosity,
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.SyncAll()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(writeCmd)
|
||||
}
|
||||
@@ -3,21 +3,27 @@ module git.jpi.io/amery/jpictl
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
darvaza.org/core v0.9.5
|
||||
darvaza.org/resolver v0.5.2
|
||||
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
|
||||
darvaza.org/slog v0.5.2
|
||||
github.com/burntSushi/toml v0.3.1
|
||||
github.com/mgechev/revive v1.3.2
|
||||
asciigoat.org/ini v0.2.5
|
||||
darvaza.org/core v0.9.8
|
||||
darvaza.org/resolver v0.5.4
|
||||
darvaza.org/sidecar v0.0.2
|
||||
darvaza.org/slog v0.5.3
|
||||
darvaza.org/slog/handlers/discard v0.4.5
|
||||
github.com/gofrs/uuid/v5 v5.0.0
|
||||
github.com/hack-pad/hackpadfs v0.2.1
|
||||
github.com/mgechev/revive v1.3.3
|
||||
github.com/spf13/cobra v1.7.0
|
||||
golang.org/x/crypto v0.12.0
|
||||
gopkg.in/gcfg.v1 v1.2.3
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
darvaza.org/slog/handlers/filter v0.4.4 // indirect
|
||||
darvaza.org/slog/handlers/zerolog v0.4.4 // indirect
|
||||
asciigoat.org/core v0.3.9 // indirect
|
||||
darvaza.org/slog/handlers/filter v0.4.5 // indirect
|
||||
darvaza.org/slog/handlers/zerolog v0.4.5 // indirect
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 // indirect
|
||||
github.com/chavacava/garif v0.1.0 // indirect
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/fatih/structtag v1.2.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
@@ -34,8 +40,8 @@ require (
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/mod v0.12.0 // indirect
|
||||
golang.org/x/net v0.14.0 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
golang.org/x/text v0.12.0 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
golang.org/x/tools v0.12.0 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
)
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
darvaza.org/core v0.9.5 h1:sS5pZFwicaxJIQixEiqkMr9GknVHYL+EbKDMkR/4jDM=
|
||||
darvaza.org/core v0.9.5/go.mod h1:O3tHBMlw+xB47uGh5CUx7dXAujBAMmD8BCRFPZmIw54=
|
||||
darvaza.org/resolver v0.5.2 h1:VjHhEr/MJBszeDb7tYlXQ9Bsyh4xrDR7Sd10WAmPD6k=
|
||||
darvaza.org/resolver v0.5.2/go.mod h1:fFvsVPEFeMzUIWlLG47Go/6uJYtRLb9R8HIgYg3uaxE=
|
||||
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf h1:ya5ZQicBb/GWll3rlqra8No7oJXks7y1m/cJGYBypv4=
|
||||
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf/go.mod h1:by+bPsMa7Rxc/ZYG1qBunrtKocv/DkrPBmyFlmq/j2Q=
|
||||
darvaza.org/slog v0.5.2 h1:8TG1WyHjOyh2vW6t3pjzZVaWzpko5MIIpeI7LWqHFvs=
|
||||
darvaza.org/slog v0.5.2/go.mod h1:HAkEpxTA/mkiLNUXJo5qsCh8EVCtA3evje8GAaCDWHI=
|
||||
darvaza.org/slog/handlers/filter v0.4.4 h1:b2e2T9fQzMdJ0ia+f6b7kw9/T9GFwhFCKob/2tqhGGU=
|
||||
darvaza.org/slog/handlers/filter v0.4.4/go.mod h1:cQlJWuolB6guLug09sX/8Zrzct++M6SPCGvXR37E7Cc=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.4 h1:OR1ASvH1fBCq3t85t4OU6oJPPuqMB1tsDoSpsh6HVJU=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.4/go.mod h1:t60TeEbFcMLo74CkXC2S0rKlnwF4ixZyBR4fqIJV1GE=
|
||||
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.9.8 h1:luLxgfUc2pzuusYPo/Z/dC/qr9XZPKpSQw8/kS7zNUM=
|
||||
darvaza.org/core v0.9.8/go.mod h1:Dbme64naxeshQfxcVJX9ZT7AiGyIY8kldfuELVtf8mw=
|
||||
darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs=
|
||||
darvaza.org/resolver v0.5.4/go.mod h1:vHMkQUmHjaetFqG2ZLZJiQHsXEMGoTOFGm+NXwfndhE=
|
||||
darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0=
|
||||
darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo=
|
||||
darvaza.org/slog v0.5.3 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0=
|
||||
darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0=
|
||||
darvaza.org/slog/handlers/discard v0.4.5 h1:RRykOItNolHyiUav57lG/GFBL33rcljoa0nWTpY+T0g=
|
||||
darvaza.org/slog/handlers/discard v0.4.5/go.mod h1:HYHfISQjMqcPbPoPZ92ib/u7s9JcXvF6OaygpPFwdF8=
|
||||
darvaza.org/slog/handlers/filter v0.4.5 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc=
|
||||
darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.5/go.mod h1:mCoh/mIl8Nsa6Yu1Um7d7cos6RuEJzgaTXaX5LDRUao=
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/burntSushi/toml v0.3.1 h1:Hu1cOEC2qtKULZJCzym5tyA35bZr3HREuolgiAzMlhY=
|
||||
github.com/burntSushi/toml v0.3.1/go.mod h1:sGTquCpRYr9McuHdv0m6YKIhx8DJGJa4t04/Y9pfSio=
|
||||
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 h1:W9o46d2kbNL06lq7UNDPV0zYLzkrde/bjIqO02eoll0=
|
||||
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8/go.mod h1:gakxgyXaaPkxvLw1XQxNGK4I37ys9iBRzNUx/B7pUCo=
|
||||
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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -26,6 +30,10 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD
|
||||
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=
|
||||
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
|
||||
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
|
||||
github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMIxMw=
|
||||
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/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
@@ -40,8 +48,8 @@ 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.2 h1:Wb8NQKBaALBJ3xrrj4zpwJwqwNA6nDpyJSEQWcCka6U=
|
||||
github.com/mgechev/revive v1.3.2/go.mod h1:UCLtc7o5vg5aXCwdUTU1kEBQ1v+YXPAkYDIDXbrs5I0=
|
||||
github.com/mgechev/revive v1.3.3 h1:GUWzV3g185agbHN4ZdaQvR6zrLVYTUSA2ktvIinivK0=
|
||||
github.com/mgechev/revive v1.3.3/go.mod h1:NhpOtVtDbjYNDj697eDUBTobijCDHQKar4HDKc0TuTo=
|
||||
github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo=
|
||||
github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
@@ -68,8 +76,10 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
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.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
|
||||
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
||||
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
|
||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
@@ -80,12 +90,13 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
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.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||
golang.org/x/sys v0.12.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.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
||||
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package ceph deals with ceph config
|
||||
package ceph
|
||||
@@ -0,0 +1,67 @@
|
||||
package ceph
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
"github.com/gofrs/uuid/v5"
|
||||
|
||||
"asciigoat.org/ini/basic"
|
||||
)
|
||||
|
||||
// Config represents a ceph.conf file
|
||||
type Config struct {
|
||||
Global GlobalConfig `ini:"global"`
|
||||
}
|
||||
|
||||
// GlobalConfig represents the [global] section of a ceph.conf file
|
||||
type GlobalConfig struct {
|
||||
FSID uuid.UUID `ini:"fsid"`
|
||||
Monitors []string `ini:"mon_initial_members,comma"`
|
||||
MonitorsAddr []netip.Addr `ini:"mon_host,comma"`
|
||||
ClusterNetwork netip.Prefix `ini:"cluster_network"`
|
||||
}
|
||||
|
||||
// WriteTo writes a Wireguard [Config] onto the provided [io.Writer]
|
||||
func (cfg *Config) WriteTo(w io.Writer) (int64, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
writeGlobalToBuffer(&buf, &cfg.Global)
|
||||
return buf.WriteTo(w)
|
||||
}
|
||||
|
||||
func writeGlobalToBuffer(w *bytes.Buffer, c *GlobalConfig) {
|
||||
_, _ = w.WriteString("[global]\n")
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "fsid", c.FSID.String())
|
||||
_, _ = 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())
|
||||
}
|
||||
|
||||
func joinAddrs(addrs []netip.Addr, sep string) string {
|
||||
s := make([]string, len(addrs))
|
||||
|
||||
for i, addr := range addrs {
|
||||
s[i] = addr.String()
|
||||
}
|
||||
|
||||
return strings.Join(s, sep)
|
||||
}
|
||||
|
||||
// NewConfigFromReader parses the ceph.conf file
|
||||
func NewConfigFromReader(r io.Reader) (*Config, error) {
|
||||
doc, err := basic.Decode(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg, err := newConfigFromDocument(doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package ceph
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/netip"
|
||||
|
||||
"asciigoat.org/ini/basic"
|
||||
"asciigoat.org/ini/parser"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
var sectionMap = map[string]func(*Config, *basic.Section) error{
|
||||
"global": loadGlobalConfSection,
|
||||
}
|
||||
|
||||
func loadConfSection(out *Config, src *basic.Section) error {
|
||||
h, ok := sectionMap[src.Key]
|
||||
if !ok {
|
||||
return core.Wrapf(fs.ErrInvalid, "unknown section %q", src.Key)
|
||||
}
|
||||
|
||||
return h(out, src)
|
||||
}
|
||||
|
||||
func loadGlobalConfSection(out *Config, src *basic.Section) error {
|
||||
var cfg GlobalConfig
|
||||
|
||||
for _, field := range src.Fields {
|
||||
if err := loadGlobalConfField(&cfg, field); err != nil {
|
||||
return core.Wrap(err, "global")
|
||||
}
|
||||
}
|
||||
|
||||
out.Global = cfg
|
||||
return nil
|
||||
}
|
||||
|
||||
// revive:disable:cyclomatic
|
||||
// revive:disable:cognitive-complexity
|
||||
|
||||
func loadGlobalConfField(cfg *GlobalConfig, 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 "fsid":
|
||||
if !core.IsZero(cfg.FSID) {
|
||||
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.FSID.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "mon_host":
|
||||
entries, _ := parser.SplitCommaArray(field.Value)
|
||||
for _, s := range entries {
|
||||
var addr netip.Addr
|
||||
|
||||
if err := addr.UnmarshalText([]byte(s)); err != nil {
|
||||
return core.Wrap(err, field.Key)
|
||||
}
|
||||
|
||||
cfg.MonitorsAddr = append(cfg.MonitorsAddr, addr)
|
||||
}
|
||||
return nil
|
||||
case "mon_initial_members":
|
||||
entries, _ := parser.SplitCommaArray(field.Value)
|
||||
cfg.Monitors = append(cfg.Monitors, entries...)
|
||||
return nil
|
||||
case "cluster_network":
|
||||
if !core.IsZero(cfg.ClusterNetwork) {
|
||||
err := core.Wrap(fs.ErrInvalid, "fields before the first section")
|
||||
return err
|
||||
}
|
||||
|
||||
err := cfg.ClusterNetwork.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/netip"
|
||||
"sort"
|
||||
|
||||
"darvaza.org/core"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/ceph"
|
||||
)
|
||||
|
||||
// GetCephFSID returns our Ceph's FSID
|
||||
func (m *Cluster) GetCephFSID() (uuid.UUID, error) {
|
||||
if core.IsZero(m.CephFSID) {
|
||||
// generate one
|
||||
v, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
m.CephFSID = v
|
||||
}
|
||||
return m.CephFSID, nil
|
||||
}
|
||||
|
||||
// GetCephConfig reads the ceph.conf file
|
||||
func (m *Cluster) GetCephConfig() (*ceph.Config, error) {
|
||||
data, err := m.ReadFile("ceph.conf")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := bytes.NewReader(data)
|
||||
return ceph.NewConfigFromReader(r)
|
||||
}
|
||||
|
||||
// WriteCephConfig writes the ceph.conf file
|
||||
func (m *Cluster) WriteCephConfig(cfg *ceph.Config) error {
|
||||
f, err := m.CreateTruncFile("ceph.conf")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = cfg.WriteTo(f)
|
||||
return err
|
||||
}
|
||||
|
||||
// GenCephConfig prepares a ceph.Config using the cluster information
|
||||
func (m *Cluster) GenCephConfig() (*ceph.Config, error) {
|
||||
fsid, err := m.GetCephFSID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg := &ceph.Config{
|
||||
Global: ceph.GlobalConfig{
|
||||
FSID: fsid,
|
||||
ClusterNetwork: netip.PrefixFrom(
|
||||
netip.AddrFrom4([4]byte{10, 0, 0, 0}),
|
||||
8,
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
for _, p := range z.GetCephMonitors() {
|
||||
addr, _ := RingOneAddress(z.ID, p.ID)
|
||||
|
||||
cfg.Global.Monitors = append(cfg.Global.Monitors, p.Name)
|
||||
cfg.Global.MonitorsAddr = append(cfg.Global.MonitorsAddr, addr)
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// GetCephMonitors returns the set of Ceph monitors on
|
||||
// the zone
|
||||
func (z *Zone) GetCephMonitors() Machines {
|
||||
var mons Machines
|
||||
var first, second *Machine
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
switch {
|
||||
case p.CephMonitor:
|
||||
// it is a monitor
|
||||
mons = append(mons, p)
|
||||
case len(mons) > 0:
|
||||
// zone has a monitor
|
||||
case first == nil && !p.IsGateway():
|
||||
// first option for monitor
|
||||
first = p
|
||||
case second == nil:
|
||||
// second option for monitor
|
||||
second = p
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
switch {
|
||||
case len(mons) > 0:
|
||||
// ready
|
||||
case first != nil:
|
||||
// make first option our monitor
|
||||
first.CephMonitor = true
|
||||
mons = append(mons, first)
|
||||
case second != nil:
|
||||
// make second option our monitor
|
||||
second.CephMonitor = true
|
||||
mons = append(mons, second)
|
||||
default:
|
||||
// zone without machines??
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
sort.Sort(mons)
|
||||
return mons
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"darvaza.org/slog"
|
||||
"git.jpi.io/amery/jpictl/pkg/ceph"
|
||||
)
|
||||
|
||||
type cephScanTODO struct {
|
||||
names map[string]bool
|
||||
addrs map[string]bool
|
||||
}
|
||||
|
||||
func (todo *cephScanTODO) checkMachine(p *Machine) bool {
|
||||
// on ceph all addresses are ring1
|
||||
ring1, _ := RingOneAddress(p.Zone(), p.ID)
|
||||
addr := ring1.String()
|
||||
|
||||
if _, found := todo.names[p.Name]; found {
|
||||
// found on the TODO by name
|
||||
todo.names[p.Name] = true
|
||||
todo.addrs[addr] = true
|
||||
return true
|
||||
}
|
||||
|
||||
if _, found := todo.addrs[addr]; found {
|
||||
// found on the TODO by address
|
||||
todo.names[p.Name] = true
|
||||
todo.addrs[addr] = true
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (todo *cephScanTODO) LogMissing(log slog.Logger) {
|
||||
for name, found := range todo.names {
|
||||
if !found {
|
||||
log.Warn().
|
||||
WithField("subsystem", "ceph").
|
||||
WithField("monitor", name).
|
||||
Print("unknown monitor")
|
||||
}
|
||||
}
|
||||
|
||||
for addr, found := range todo.addrs {
|
||||
if !found {
|
||||
log.Warn().
|
||||
WithField("subsystem", "ceph").
|
||||
WithField("monitor", addr).
|
||||
Print("unknown monitor")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
|
||||
todo := &cephScanTODO{
|
||||
names: make(map[string]bool),
|
||||
addrs: make(map[string]bool),
|
||||
}
|
||||
|
||||
for _, name := range cfg.Global.Monitors {
|
||||
todo.names[name] = false
|
||||
}
|
||||
|
||||
for _, addr := range cfg.Global.MonitorsAddr {
|
||||
todo.addrs[addr.String()] = false
|
||||
}
|
||||
|
||||
return todo
|
||||
}
|
||||
|
||||
func (m *Cluster) scanCephMonitors(_ *ScanOptions) error {
|
||||
cfg, err := m.GetCephConfig()
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
err = nil
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg != nil {
|
||||
// store FSID
|
||||
m.CephFSID = cfg.Global.FSID
|
||||
|
||||
// flag monitors based on config
|
||||
todo := newCephScanTODO(cfg)
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
p.CephMonitor = todo.checkMachine(p)
|
||||
return false
|
||||
})
|
||||
|
||||
todo.LogMissing(m.log)
|
||||
}
|
||||
|
||||
// make sure every zone has one
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
_ = z.GetCephMonitors()
|
||||
return false
|
||||
})
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Package cluster contains information about the cluster
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"darvaza.org/slog"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
_ MachineIterator = (*Cluster)(nil)
|
||||
_ ZoneIterator = (*Cluster)(nil)
|
||||
)
|
||||
|
||||
// revive:disable:line-length-limit
|
||||
|
||||
// Cluster represents all zones in a cluster
|
||||
type Cluster struct {
|
||||
dir fs.FS
|
||||
log slog.Logger
|
||||
resolver resolver.Resolver
|
||||
|
||||
BaseDir string `json:"dir,omitempty" yaml:"dir,omitempty"`
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
|
||||
|
||||
CephFSID uuid.UUID `json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"`
|
||||
Zones []*Zone `json:"zones,omitempty" yaml:"zones,omitempty"`
|
||||
}
|
||||
|
||||
// revive:enable:line-length-limit
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the cluster
|
||||
// until instructed to terminate the loop
|
||||
func (m *Cluster) ForEachMachine(fn func(*Machine) bool) {
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
var term bool
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
term = fn(p)
|
||||
return term
|
||||
})
|
||||
|
||||
return term
|
||||
})
|
||||
}
|
||||
|
||||
// ForEachZone calls a function for each Zone in the cluster
|
||||
// until instructed to terminate the loop
|
||||
func (m *Cluster) ForEachZone(fn func(*Zone) bool) {
|
||||
for _, p := range m.Zones {
|
||||
if fn(p) {
|
||||
// terminate
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetMachineByName looks for a machine with the specified
|
||||
// name on any zone
|
||||
func (m *Cluster) GetMachineByName(name string) (*Machine, bool) {
|
||||
var out *Machine
|
||||
|
||||
if name != "" {
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
if p.Name == name {
|
||||
out = p
|
||||
}
|
||||
|
||||
return out != nil
|
||||
})
|
||||
}
|
||||
|
||||
return out, out != nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
fs "github.com/hack-pad/hackpadfs"
|
||||
)
|
||||
|
||||
// OpenFile opens a file on the cluster's config directory with the specified flags
|
||||
func (m *Cluster) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
return fs.OpenFile(m.dir, name, flags, 0644)
|
||||
}
|
||||
|
||||
// CreateTruncFile creates or truncates a file on the cluster's config directory
|
||||
func (m *Cluster) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
||||
}
|
||||
|
||||
// CreateFile creates a file on the cluster's config directory
|
||||
func (m *Cluster) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE, args...)
|
||||
}
|
||||
|
||||
func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||||
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return f.(io.WriteCloser), nil
|
||||
}
|
||||
|
||||
// ReadFile reads a file from the cluster's config directory
|
||||
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
return fs.ReadFile(m.dir, name)
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"sort"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
func (m *Cluster) scan(opts *ScanOptions) error {
|
||||
for _, fn := range []func(*ScanOptions) error{
|
||||
m.scanDirectory,
|
||||
m.scanMachines,
|
||||
m.scanZoneIDs,
|
||||
m.scanSort,
|
||||
m.scanGateways,
|
||||
m.scanCephMonitors,
|
||||
} {
|
||||
if err := fn(opts); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
||||
// each directory is a zone
|
||||
entries, err := fs.ReadDir(m.dir, ".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
z, err := m.newZone(e.Name())
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, e.Name())
|
||||
case z.Machines.Len() == 0:
|
||||
z.warn(nil).
|
||||
WithField("zone", z.Name).
|
||||
Print("empty")
|
||||
default:
|
||||
m.Zones = append(m.Zones, z)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Cluster) newZone(name string) (*Zone, error) {
|
||||
z := &Zone{
|
||||
zones: m,
|
||||
logger: m,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
z.debug().
|
||||
WithField("zone", z.Name).
|
||||
Print("found")
|
||||
|
||||
if err := z.scan(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return z, nil
|
||||
}
|
||||
|
||||
func (m *Cluster) scanMachines(opts *ScanOptions) error {
|
||||
var err error
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.scan(opts)
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Cluster) scanZoneIDs(_ *ScanOptions) error {
|
||||
var hasMissing bool
|
||||
var lastZoneID int
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
switch {
|
||||
case z.ID == 0:
|
||||
hasMissing = true
|
||||
case z.ID > lastZoneID:
|
||||
lastZoneID = z.ID
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if hasMissing {
|
||||
next := lastZoneID + 1
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
if z.ID == 0 {
|
||||
z.ID, next = next, next+1
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Cluster) scanSort(_ *ScanOptions) error {
|
||||
sort.SliceStable(m.Zones, func(i, j int) bool {
|
||||
id1 := m.Zones[i].ID
|
||||
id2 := m.Zones[j].ID
|
||||
return id1 < id2
|
||||
})
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
sort.Sort(z)
|
||||
return false
|
||||
})
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
sort.SliceStable(p.Rings, func(i, j int) bool {
|
||||
ri1 := p.Rings[i]
|
||||
ri2 := p.Rings[j]
|
||||
|
||||
return ri1.Ring < ri2.Ring
|
||||
})
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Cluster) scanGateways(_ *ScanOptions) error {
|
||||
var err error
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
_, _, err = z.GetGateway()
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (z *Zone) scan() error {
|
||||
// each directory is a machine
|
||||
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
m := &Machine{
|
||||
zone: z,
|
||||
logger: z,
|
||||
Name: e.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
|
||||
func (z *Zone) GetGateway() (*Machine, bool, error) {
|
||||
var first *Machine
|
||||
var gateway *Machine
|
||||
|
||||
z.zones.ForEachMachine(func(p *Machine) bool {
|
||||
switch {
|
||||
case p.IsGateway():
|
||||
// found
|
||||
gateway = p
|
||||
case first == nil:
|
||||
// remember
|
||||
first = p
|
||||
default:
|
||||
// keep looking
|
||||
}
|
||||
|
||||
return gateway != nil
|
||||
})
|
||||
|
||||
switch {
|
||||
case gateway != nil:
|
||||
// found one
|
||||
return gateway, false, nil
|
||||
case first != nil:
|
||||
// make one
|
||||
if err := first.SetGateway(true); err != nil {
|
||||
return first, false, err
|
||||
}
|
||||
return first, true, nil
|
||||
default:
|
||||
// Zone without nodes?
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"darvaza.org/slog"
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
)
|
||||
|
||||
// A ScanOption pre-configures the Zones before scanning
|
||||
type ScanOption func(*Cluster, *ScanOptions) error
|
||||
|
||||
// ScanOptions contains flags used by the initial scan
|
||||
type ScanOptions struct {
|
||||
// DontResolvePublicAddresses indicates we shouldn't
|
||||
// pre-populate Machine.PublicAddresses during the
|
||||
// initial scan
|
||||
DontResolvePublicAddresses bool
|
||||
|
||||
// Logger specifies the logger to be used. otherwise
|
||||
// the scanner will be mute
|
||||
slog.Logger
|
||||
}
|
||||
|
||||
// ResolvePublicAddresses instructs the scanner to use
|
||||
// the DNS resolver to get PublicAddresses of nodes.
|
||||
// Default is true
|
||||
func ResolvePublicAddresses(resolve bool) ScanOption {
|
||||
return func(m *Cluster, opt *ScanOptions) error {
|
||||
opt.DontResolvePublicAddresses = !resolve
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithLookuper specifies what resolver.Lookuper to use to
|
||||
// find public addresses
|
||||
func WithLookuper(h resolver.Lookuper) ScanOption {
|
||||
return func(m *Cluster, opt *ScanOptions) error {
|
||||
if h == nil {
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
m.resolver = resolver.NewResolver(h)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithResolver specifies what resolver to use to find
|
||||
// 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 {
|
||||
if h == nil {
|
||||
h = resolver.SystemResolver(true)
|
||||
}
|
||||
|
||||
m.resolver = h
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogger specifies what to use for logging
|
||||
func WithLogger(log slog.Logger) ScanOption {
|
||||
return func(m *Cluster, opt *ScanOptions) error {
|
||||
if log == nil {
|
||||
log = DefaultLogger()
|
||||
}
|
||||
|
||||
opt.Logger = log
|
||||
m.log = log
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Cluster) setScanDefaults(opt *ScanOptions) error {
|
||||
if m.resolver == nil {
|
||||
h := DefaultLookuper()
|
||||
|
||||
if err := WithLookuper(h)(m, opt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if opt.Logger == nil {
|
||||
if err := WithLogger(nil)(m, opt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewFromDirectory builds a [Cluster] tree using the given directory
|
||||
func NewFromDirectory(dir, domain string, opts ...ScanOption) (*Cluster, error) {
|
||||
var scanOptions ScanOptions
|
||||
|
||||
dir = filepath.Clean(dir)
|
||||
fullPath, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sub, err := os.NewFS().Sub(fullPath[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := &Cluster{
|
||||
dir: sub,
|
||||
BaseDir: dir,
|
||||
Name: filepath.Base(fullPath),
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if err := opt(m, &scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := m.setScanDefaults(&scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := m.scan(&scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"darvaza.org/resolver"
|
||||
"darvaza.org/slog"
|
||||
"darvaza.org/slog/handlers/discard"
|
||||
)
|
||||
|
||||
// DefaultLogger returns a logger that doesn't log anything
|
||||
func DefaultLogger() slog.Logger {
|
||||
return discard.New()
|
||||
}
|
||||
|
||||
// DefaultLookuper returns a [resolver.Lookuper] using Cloudflare's 1.1.1.1
|
||||
func DefaultLookuper() resolver.Lookuper {
|
||||
return resolver.NewCloudflareLookuper()
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Env is a shell environment factory for this cluster
|
||||
type Env struct {
|
||||
ZoneIterator
|
||||
|
||||
cephFSID string
|
||||
export bool
|
||||
}
|
||||
|
||||
// Env returns a shell environment factory
|
||||
func (m *Cluster) Env(export bool) (*Env, error) {
|
||||
fsid, err := m.GetCephFSID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
env := &Env{
|
||||
ZoneIterator: m,
|
||||
cephFSID: fsid.String(),
|
||||
export: export,
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// Zones returns the list of Zone IDs
|
||||
func (m *Env) Zones() []int {
|
||||
var zones []int
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
zones = append(zones, z.ID)
|
||||
return false
|
||||
})
|
||||
|
||||
return zones
|
||||
}
|
||||
|
||||
// WriteTo generates environment variables for shell scripts
|
||||
func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if m.cephFSID != "" {
|
||||
m.writeEnvVar(&buf, m.cephFSID, "FSID")
|
||||
}
|
||||
|
||||
m.writeEnvVarInts(&buf, m.Zones(), "ZONES")
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
m.writeEnvZone(&buf, z)
|
||||
return false
|
||||
})
|
||||
|
||||
return buf.WriteTo(w)
|
||||
}
|
||||
|
||||
func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
||||
zoneID := z.ID
|
||||
|
||||
// ZONE{zoneID}
|
||||
m.writeEnvVar(w, genEnvZoneNodes(z), "ZONE%v", zoneID)
|
||||
|
||||
// ZONE{zoneID}_NAME
|
||||
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
|
||||
|
||||
// ZONE{zoneID}_GW
|
||||
gateways, _ := z.GatewayIDs()
|
||||
m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW")
|
||||
|
||||
// Ceph
|
||||
monitors := z.GetCephMonitors()
|
||||
// MON{zoneID}_NAME
|
||||
m.writeEnvVar(w, genEnvZoneCephMonNames(monitors), "MON%v_%s", zoneID, "NAME")
|
||||
// MON{zoneID}_IP
|
||||
m.writeEnvVar(w, genEnvZoneCephMonIPs(monitors), "MON%v_%s", zoneID, "IP")
|
||||
// MON{zoneID}_ID
|
||||
m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), "MON%v_%s", zoneID, "ID")
|
||||
}
|
||||
|
||||
func (m *Env) writeEnvVarInts(w io.Writer, value []int, name string, args ...any) {
|
||||
var s string
|
||||
|
||||
if n := len(value); n > 0 {
|
||||
var buf bytes.Buffer
|
||||
|
||||
for i, v := range value {
|
||||
if i != 0 {
|
||||
_, _ = fmt.Fprint(&buf, " ")
|
||||
}
|
||||
_, _ = fmt.Fprintf(&buf, "%v", v)
|
||||
}
|
||||
|
||||
s = buf.String()
|
||||
}
|
||||
|
||||
m.writeEnvVar(w, s, name, args...)
|
||||
}
|
||||
|
||||
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
||||
var prefix string
|
||||
|
||||
if m.export {
|
||||
prefix = "export "
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
if name != "" {
|
||||
value = strings.TrimSpace(value)
|
||||
|
||||
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
|
||||
}
|
||||
}
|
||||
|
||||
func genEnvZoneNodes(z *Zone) string {
|
||||
if n := z.Len(); n > 0 {
|
||||
s := make([]string, 0, n)
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
s = append(s, p.Name)
|
||||
return false
|
||||
})
|
||||
|
||||
return strings.Join(s, " ")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func genEnvZoneCephMonNames(m Machines) string {
|
||||
var buf strings.Builder
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
if buf.Len() > 0 {
|
||||
_, _ = buf.WriteRune(' ')
|
||||
}
|
||||
_, _ = buf.WriteString(p.Name)
|
||||
|
||||
return false
|
||||
})
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func genEnvZoneCephMonIPs(m Machines) string {
|
||||
var buf strings.Builder
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
addr, _ := RingOneAddress(p.Zone(), p.ID)
|
||||
|
||||
if buf.Len() > 0 {
|
||||
_, _ = buf.WriteRune(' ')
|
||||
}
|
||||
_, _ = buf.WriteString(addr.String())
|
||||
|
||||
return false
|
||||
})
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func genEnvZoneCephMonIDs(m Machines) string {
|
||||
var buf strings.Builder
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
if buf.Len() > 0 {
|
||||
_, _ = buf.WriteRune(' ')
|
||||
}
|
||||
_, _ = fmt.Fprintf(&buf, "%v", p.ID)
|
||||
|
||||
return false
|
||||
})
|
||||
return buf.String()
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cluster
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrInvalidName indicates the name isn't valid
|
||||
ErrInvalidName = errors.New("invalid name")
|
||||
|
||||
// ErrUnknownNode indicates there is a reference to a node
|
||||
// we don't have on the tree
|
||||
ErrUnknownNode = errors.New("node does not exist")
|
||||
|
||||
// ErrInvalidNode indicates the nodes can't be used for
|
||||
// the intended purpose
|
||||
ErrInvalidNode = errors.New("invalid node")
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
package cluster
|
||||
|
||||
import "darvaza.org/slog"
|
||||
|
||||
type logger interface {
|
||||
withDebug() (slog.Logger, bool)
|
||||
withInfo() (slog.Logger, bool)
|
||||
|
||||
debug() slog.Logger
|
||||
info() slog.Logger
|
||||
warn(error) slog.Logger
|
||||
error(error) slog.Logger
|
||||
}
|
||||
|
||||
var (
|
||||
_ logger = (*Cluster)(nil)
|
||||
)
|
||||
|
||||
func (z *Cluster) withDebug() (slog.Logger, bool) {
|
||||
return z.debug().WithEnabled()
|
||||
}
|
||||
|
||||
func (z *Cluster) withInfo() (slog.Logger, bool) {
|
||||
return z.debug().WithEnabled()
|
||||
}
|
||||
|
||||
func (z *Cluster) debug() slog.Logger {
|
||||
return z.log.Debug()
|
||||
}
|
||||
|
||||
func (z *Cluster) info() slog.Logger {
|
||||
return z.log.Info()
|
||||
}
|
||||
|
||||
func (z *Cluster) warn(err error) slog.Logger {
|
||||
l := z.log.Warn()
|
||||
if err != nil {
|
||||
l = l.WithField(slog.ErrorFieldName, err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (z *Cluster) error(err error) slog.Logger {
|
||||
l := z.log.Error()
|
||||
if err != nil {
|
||||
l = l.WithField(slog.ErrorFieldName, err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// revive:disable:line-length-limit
|
||||
|
||||
// A Machine is a machine on a Zone
|
||||
type Machine struct {
|
||||
zone *Zone
|
||||
logger `json:"-" yaml:"-"`
|
||||
|
||||
ID int
|
||||
Name string `json:"-" yaml:"-"`
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
// revive:enable:line-length-limit
|
||||
|
||||
func (m *Machine) String() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// FullName returns the Name of the machine including domain name
|
||||
func (m *Machine) FullName() string {
|
||||
var name []string
|
||||
|
||||
for _, s := range []string{
|
||||
m.Name,
|
||||
m.zone.zones.Name,
|
||||
m.zone.zones.Domain,
|
||||
} {
|
||||
if s != "" {
|
||||
name = append(name, s)
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(name, ".")
|
||||
}
|
||||
|
||||
// IsGateway tells if the Machine is a ring0 gateway
|
||||
func (m *Machine) IsGateway() bool {
|
||||
_, ok := m.getRingInfo(0)
|
||||
return ok
|
||||
}
|
||||
|
||||
// SetGateway enables/disables a Machine ring0 integration
|
||||
func (m *Machine) SetGateway(enabled bool) error {
|
||||
ri, found := m.getRingInfo(0)
|
||||
switch {
|
||||
case !found && !enabled:
|
||||
return nil
|
||||
case !found:
|
||||
var err error
|
||||
|
||||
if ri, err = m.createRingInfo(0, false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
ri.Enabled = enabled
|
||||
return m.SyncWireguardConfig(0)
|
||||
}
|
||||
|
||||
// Zone indicates the [Zone] this machine belongs to
|
||||
func (m *Machine) Zone() int {
|
||||
return m.zone.ID
|
||||
}
|
||||
|
||||
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
||||
return m.zone.zones.GetMachineByName(name)
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
fs "github.com/hack-pad/hackpadfs"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||||
func (m *Machine) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
||||
}
|
||||
|
||||
// CreateFile creates a file on the machine's config directory
|
||||
func (m *Machine) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE, args...)
|
||||
}
|
||||
|
||||
func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||||
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return f.(io.WriteCloser), nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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()
|
||||
|
||||
buf := bytes.NewBufferString(value)
|
||||
_, err = buf.WriteTo(f)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Machine) getFilename(name string, args ...any) string {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
s := []string{
|
||||
m.zone.Name,
|
||||
m.Name,
|
||||
name,
|
||||
}
|
||||
|
||||
return filepath.Join(s...)
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"darvaza.org/core"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||
)
|
||||
|
||||
// GetWireguardKeys reads a wgN.key/wgN.pub files
|
||||
func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) {
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
out wireguard.KeyPair
|
||||
)
|
||||
|
||||
data, err = m.ReadFile("wg%v.key", ring)
|
||||
if err != nil {
|
||||
// failed to read
|
||||
return out, err
|
||||
}
|
||||
|
||||
out.PrivateKey, err = wireguard.PrivateKeyFromBase64(string(data))
|
||||
if err != nil {
|
||||
// bad key
|
||||
err = core.Wrapf(err, "wg%v.key", ring)
|
||||
return out, err
|
||||
}
|
||||
|
||||
data, err = m.ReadFile("wg%v.pub", ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
// no wgN.pub is fine
|
||||
case err != nil:
|
||||
// failed to read
|
||||
return out, err
|
||||
default:
|
||||
// good read
|
||||
out.PublicKey, err = wireguard.PublicKeyFromBase64(string(data))
|
||||
if err != nil {
|
||||
// bad key
|
||||
err = core.Wrapf(err, "wg%v.pub", ring)
|
||||
return out, err
|
||||
}
|
||||
}
|
||||
|
||||
err = out.Validate()
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (m *Machine) tryReadWireguardKeys(ring int) error {
|
||||
kp, err := m.GetWireguardKeys(ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
// ignore
|
||||
return nil
|
||||
case err != nil:
|
||||
// something went wrong
|
||||
return err
|
||||
default:
|
||||
// import keys
|
||||
ri := &RingInfo{
|
||||
Ring: ring,
|
||||
Keys: kp,
|
||||
}
|
||||
|
||||
return m.applyRingInfo(ring, ri)
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
||||
// the machine's config directory
|
||||
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||
var err error
|
||||
|
||||
err = m.RemoveFile("wg%v.pub", ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
// ignore
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.RemoveFile("wg%v.key", ring)
|
||||
if os.IsNotExist(err) {
|
||||
// ignore
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetWireguardConfig reads a wgN.conf file
|
||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||
data, err := m.ReadFile("wg%v.conf", ring)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := bytes.NewReader(data)
|
||||
return wireguard.NewConfigFromReader(r)
|
||||
}
|
||||
|
||||
func (m *Machine) tryApplyWireguardConfig(ring int) error {
|
||||
wg, err := m.GetWireguardConfig(ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
default:
|
||||
return m.applyWireguardConfig(ring, wg)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Machine) applyWireguardConfig(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 err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||
return core.Wrapf(err, "%s: invalid address", addr)
|
||||
}
|
||||
|
||||
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
||||
return core.Wrap(err, "interface")
|
||||
}
|
||||
|
||||
for _, peer := range wg.Peer {
|
||||
err := m.applyWireguardPeerConfig(ring, peer)
|
||||
switch {
|
||||
case errors.Is(err, ErrUnknownNode):
|
||||
// ignore unknown peers
|
||||
m.warn(nil).
|
||||
WithField("subsystem", "wireguard").
|
||||
WithField("node", m.Name).
|
||||
WithField("peer", peer.Endpoint.Host).
|
||||
WithField("ring", ring).
|
||||
Print("ignoring unknown endpoint")
|
||||
case err != nil:
|
||||
return core.Wrap(err, "peer")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
||||
for _, ri := range m.Rings {
|
||||
if ri.Ring == ring {
|
||||
return ri, ri.Enabled
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
||||
cur, _ := m.getRingInfo(ring)
|
||||
if cur == nil {
|
||||
// first, append
|
||||
m.debug().
|
||||
WithField("node", m.Name).
|
||||
WithField("ring", ring).
|
||||
Print("found")
|
||||
m.Rings = append(m.Rings, new)
|
||||
return nil
|
||||
}
|
||||
|
||||
// extra, merge
|
||||
return cur.Merge(new)
|
||||
}
|
||||
|
||||
func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.InterfaceConfig) error {
|
||||
ri := &RingInfo{
|
||||
Ring: ring,
|
||||
Enabled: true,
|
||||
Keys: wireguard.KeyPair{
|
||||
PrivateKey: data.PrivateKey,
|
||||
},
|
||||
}
|
||||
|
||||
return m.applyRingInfo(ring, ri)
|
||||
}
|
||||
|
||||
func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) error {
|
||||
peer, found := m.getPeerByName(pc.Endpoint.Name())
|
||||
switch {
|
||||
case !found:
|
||||
// unknown
|
||||
return core.Wrap(ErrUnknownNode, pc.Endpoint.Host)
|
||||
case ring == 1 && m.zone != peer.zone:
|
||||
// invalid zone
|
||||
return core.Wrap(ErrInvalidNode, peer.Name)
|
||||
default:
|
||||
// apply RingInfo
|
||||
ri := &RingInfo{
|
||||
Ring: ring,
|
||||
Enabled: true,
|
||||
Keys: wireguard.KeyPair{
|
||||
PublicKey: pc.PublicKey,
|
||||
},
|
||||
}
|
||||
|
||||
return peer.applyRingInfo(ring, ri)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
switch {
|
||||
case zoneID == 0:
|
||||
return fmt.Errorf("invalid %s", "zoneID")
|
||||
case nodeID == 0:
|
||||
return fmt.Errorf("invalid %s", "nodeID")
|
||||
case m.ID != nodeID:
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID, nodeID)
|
||||
case m.zone.ID != 0 && m.zone.ID != zoneID:
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID)
|
||||
case m.zone.ID == 0:
|
||||
m.zone.ID = zoneID
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
||||
// config directory.
|
||||
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
||||
err := m.RemoveFile("wg%v.conf", ring)
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
|
||||
keys, err := wireguard.NewKeyPair()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ri := &RingInfo{
|
||||
Ring: ring,
|
||||
Enabled: enabled,
|
||||
Keys: keys,
|
||||
}
|
||||
|
||||
err = m.applyRingInfo(ring, ri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ri, nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
// LookupNetIP uses the DNS Resolver to get the public addresses associated
|
||||
// to a Machine
|
||||
func (m *Machine) LookupNetIP(timeout time.Duration) ([]netip.Addr, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
|
||||
defer cancel()
|
||||
|
||||
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
|
||||
}
|
||||
|
||||
// UpdatePublicAddresses uses the DNS Resolver to set Machine.PublicAddresses
|
||||
func (m *Machine) UpdatePublicAddresses() error {
|
||||
addrs, err := m.LookupNetIP(2 * time.Second)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.PublicAddresses = addrs
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) init() error {
|
||||
if err := m.setID(); err != nil {
|
||||
return core.Wrap(err, m.Name)
|
||||
}
|
||||
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryReadWireguardKeys(i); err != nil {
|
||||
return core.Wrap(err, m.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) setID() error {
|
||||
zoneName := m.zone.Name
|
||||
|
||||
l := len(zoneName)
|
||||
switch {
|
||||
case len(m.Name) < l+2:
|
||||
return ErrInvalidName
|
||||
case !strings.HasPrefix(m.Name, zoneName):
|
||||
return ErrInvalidName
|
||||
case m.Name[l] != '-':
|
||||
return ErrInvalidName
|
||||
}
|
||||
|
||||
suffix := m.Name[l+1:]
|
||||
id, err := strconv.ParseInt(suffix, 10, 8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.ID = int(id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) scan(opts *ScanOptions) error {
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||
m.error(err).
|
||||
WithField("subsystem", "wireguard").
|
||||
WithField("node", m.Name).
|
||||
WithField("ring", i).
|
||||
Print()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !opts.DontResolvePublicAddresses {
|
||||
return m.UpdatePublicAddresses()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package cluster
|
||||
|
||||
import "sort"
|
||||
|
||||
var (
|
||||
_ MachineIterator = Machines(nil)
|
||||
_ sort.Interface = Machines(nil)
|
||||
)
|
||||
|
||||
// A MachineIterator is a set of Machines we can iterate on
|
||||
type MachineIterator interface {
|
||||
ForEachMachine(func(*Machine) bool)
|
||||
}
|
||||
|
||||
// Machines is a list of Machine objects
|
||||
type Machines []*Machine
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the list
|
||||
// until instructed to terminate the loop
|
||||
func (m Machines) ForEachMachine(fn func(*Machine) bool) {
|
||||
for _, p := range m {
|
||||
if fn(p) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Len returns the number of machines in the list
|
||||
func (m Machines) Len() int {
|
||||
return len(m)
|
||||
}
|
||||
|
||||
// Less implements sort.Interface to sort the list
|
||||
func (m Machines) Less(i, j int) bool {
|
||||
a, b := m[i], m[j]
|
||||
za, zb := a.Zone(), b.Zone()
|
||||
|
||||
switch {
|
||||
case za == zb:
|
||||
return a.ID < b.ID
|
||||
default:
|
||||
return za < zb
|
||||
}
|
||||
}
|
||||
|
||||
// Swap implements sort.Interface to sort the list
|
||||
func (m Machines) Swap(i, j int) {
|
||||
m[i], m[j] = m[j], m[i]
|
||||
}
|
||||
|
||||
// FilterMachines produces a subset of the machines offered by the given
|
||||
// iterator fulfilling a condition
|
||||
func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) {
|
||||
var out []*Machine
|
||||
|
||||
if cond == nil {
|
||||
// unconditional
|
||||
cond = func(*Machine) bool { return true }
|
||||
}
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
if cond(p) {
|
||||
out = append(out, p)
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return out, len(out)
|
||||
}
|
||||
@@ -0,0 +1,349 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/netip"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxZoneID indicates the highest ID allowed for a Zone
|
||||
MaxZoneID = 0xf
|
||||
// MaxNodeID indicates the highest Machine ID allowed within a Zone
|
||||
MaxNodeID = 0xff - 1
|
||||
// RingsCount indicates how many wireguard rings we have
|
||||
RingsCount = 2
|
||||
// RingZeroPort is the port wireguard uses for ring0
|
||||
RingZeroPort = 51800
|
||||
// RingOnePort is the port wireguard uses for ring1
|
||||
RingOnePort = 51810
|
||||
)
|
||||
|
||||
// RingInfo contains represents the Wireguard endpoint details
|
||||
// for a Machine on a particular ring
|
||||
type RingInfo struct {
|
||||
Ring int
|
||||
Enabled bool
|
||||
Keys wireguard.KeyPair
|
||||
}
|
||||
|
||||
// Merge attempts to combine two RingInfo structs
|
||||
func (ri *RingInfo) Merge(alter *RingInfo) error {
|
||||
switch {
|
||||
case alter == nil:
|
||||
return nil
|
||||
case ri.Ring != alter.Ring:
|
||||
// different ring
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "ring", ri.Ring, alter.Ring)
|
||||
case ri.Enabled && !alter.Enabled:
|
||||
// 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
|
||||
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
||||
}
|
||||
|
||||
return ri.unsafeMerge(alter)
|
||||
}
|
||||
|
||||
func (ri *RingInfo) unsafeMerge(alter *RingInfo) error {
|
||||
// enable via Merge
|
||||
if alter.Enabled {
|
||||
ri.Enabled = true
|
||||
}
|
||||
|
||||
// fill the gaps on our keypair
|
||||
if ri.Keys.PrivateKey.IsZero() {
|
||||
ri.Keys.PrivateKey = alter.Keys.PrivateKey
|
||||
}
|
||||
if ri.Keys.PublicKey.IsZero() {
|
||||
ri.Keys.PublicKey = alter.Keys.PublicKey
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool {
|
||||
switch {
|
||||
case !p1.PrivateKey.IsZero() && !p2.PrivateKey.IsZero() && !p1.PrivateKey.Equal(p2.PrivateKey):
|
||||
return false
|
||||
case !p1.PublicKey.IsZero() && !p2.PublicKey.IsZero() && !p1.PublicKey.Equal(p2.PublicKey):
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// RingAddressEncoder provides encoder/decoder access for a particular
|
||||
// Wireguard ring
|
||||
type RingAddressEncoder struct {
|
||||
ID int
|
||||
Port uint16
|
||||
Encode func(zoneID, nodeID int) (netip.Addr, bool)
|
||||
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
|
||||
}
|
||||
|
||||
var (
|
||||
// RingZero is a wg0 address encoder/decoder
|
||||
RingZero = RingAddressEncoder{
|
||||
ID: 0,
|
||||
Port: RingZeroPort,
|
||||
Decode: ParseRingZeroAddress,
|
||||
Encode: RingZeroAddress,
|
||||
}
|
||||
// RingOne is a wg1 address encoder/decoder
|
||||
RingOne = RingAddressEncoder{
|
||||
ID: 1,
|
||||
Port: RingOnePort,
|
||||
Decode: ParseRingOneAddress,
|
||||
Encode: RingOneAddress,
|
||||
}
|
||||
// Rings provides indexed access to the ring address encoders
|
||||
Rings = [RingsCount]RingAddressEncoder{
|
||||
RingZero,
|
||||
RingOne,
|
||||
}
|
||||
)
|
||||
|
||||
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
|
||||
//
|
||||
// 0 is reserved, and only allowed when composing CIDRs.
|
||||
func ValidZoneID(zoneID int) bool {
|
||||
switch {
|
||||
case zoneID < 0 || zoneID > MaxZoneID:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
|
||||
// nodeID is unique within a Zone.
|
||||
// 0 is reserved, and only allowed when composing CIDRs.
|
||||
func ValidNodeID(nodeID int) bool {
|
||||
switch {
|
||||
case nodeID < 0 || nodeID > MaxNodeID:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
|
||||
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
|
||||
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
||||
if addr.IsValid() {
|
||||
a4 := addr.As4()
|
||||
|
||||
if a4[0] == 10 && a4[1] == 0 {
|
||||
return int(a4[2]), int(a4[3]), true
|
||||
}
|
||||
}
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
// RingZeroAddress returns a wg0 IP address
|
||||
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||
switch {
|
||||
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||
return netip.Addr{}, false
|
||||
default:
|
||||
a4 := [4]uint8{10, 0, uint8(zoneID), uint8(nodeID)}
|
||||
return netip.AddrFrom4(a4), true
|
||||
}
|
||||
}
|
||||
|
||||
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
|
||||
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
|
||||
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
||||
if addr.IsValid() {
|
||||
a4 := addr.As4()
|
||||
|
||||
if a4[0] == 10 && a4[2] == 0 {
|
||||
zoneID = int(a4[1] >> 4)
|
||||
nodeID = int(a4[3])
|
||||
return zoneID, nodeID, true
|
||||
}
|
||||
}
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
// RingOneAddress returns a wg1 IP address
|
||||
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||
switch {
|
||||
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||
return netip.Addr{}, false
|
||||
default:
|
||||
a4 := [4]uint8{10, uint8(zoneID << 4), 0, uint8(nodeID)}
|
||||
return netip.AddrFrom4(a4), true
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ MachineIterator = (*Ring)(nil)
|
||||
_ ZoneIterator = (*Ring)(nil)
|
||||
)
|
||||
|
||||
// A Ring describes all peers on a ring
|
||||
type Ring struct {
|
||||
RingAddressEncoder
|
||||
ZoneIterator
|
||||
|
||||
Peers []*RingPeer
|
||||
}
|
||||
|
||||
// AddPeer adds a [Machine] to the ring
|
||||
func (r *Ring) AddPeer(p *Machine) bool {
|
||||
ri, ok := p.getRingInfo(r.ID)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
nodeID := p.ID
|
||||
zoneID := p.Zone()
|
||||
addr, _ := r.Encode(zoneID, nodeID)
|
||||
|
||||
rp := &RingPeer{
|
||||
Node: p,
|
||||
Address: addr,
|
||||
PrivateKey: ri.Keys.PrivateKey,
|
||||
PeerConfig: wireguard.PeerConfig{
|
||||
Name: fmt.Sprintf("%s-%v", p.Name, r.ID),
|
||||
PublicKey: ri.Keys.PublicKey,
|
||||
Endpoint: wireguard.EndpointAddress{
|
||||
Host: p.FullName(),
|
||||
Port: r.Port,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
switch {
|
||||
case r.ID == 0:
|
||||
r.setRingZeroAllowedIPs(rp)
|
||||
case p.IsGateway():
|
||||
r.setRingOneGatewayAllowedIPs(rp)
|
||||
default:
|
||||
r.setRingOneNodeAllowedIPs(rp)
|
||||
}
|
||||
|
||||
r.Peers = append(r.Peers, rp)
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *Ring) setRingZeroAllowedIPs(rp *RingPeer) {
|
||||
zoneID, _, _ := r.Decode(rp.Address)
|
||||
|
||||
// everyone on ring0 is a gateway to ring1
|
||||
addr, _ := RingOneAddress(zoneID, 0)
|
||||
rp.AllowCIDR(addr, 12)
|
||||
|
||||
// peer
|
||||
rp.AllowCIDR(rp.Address, 32)
|
||||
}
|
||||
|
||||
func (r *Ring) setRingOneGatewayAllowedIPs(rp *RingPeer) {
|
||||
zoneID, _, _ := r.Decode(rp.Address)
|
||||
|
||||
// peer
|
||||
rp.AllowCIDR(rp.Address, 32)
|
||||
|
||||
// ring1 gateways connect to all other ring1 networks
|
||||
r.ForEachZone(func(z *Zone) bool {
|
||||
if z.ID != zoneID {
|
||||
addr, _ := r.Encode(z.ID, 0)
|
||||
rp.AllowCIDR(addr, 12)
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
// ring1 gateways also connect to all ring0 addresses
|
||||
r.ForEachZone(func(z *Zone) bool {
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
if p.IsGateway() {
|
||||
addr, _ := RingZeroAddress(z.ID, p.ID)
|
||||
rp.AllowCIDR(addr, 32)
|
||||
}
|
||||
return false
|
||||
})
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
func (*Ring) setRingOneNodeAllowedIPs(rp *RingPeer) {
|
||||
// only to the peer itself
|
||||
rp.AllowCIDR(rp.Address, 32)
|
||||
}
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the ring
|
||||
// until instructed to terminate the loop
|
||||
func (r *Ring) ForEachMachine(fn func(*Machine) bool) {
|
||||
for _, pp := range r.Peers {
|
||||
if fn(pp.Node) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ExportConfig builds a wgN.conf for the specified machine on the ring
|
||||
func (r *Ring) ExportConfig(p *Machine) (*wireguard.Config, error) {
|
||||
var found bool
|
||||
|
||||
out := &wireguard.Config{
|
||||
Interface: wireguard.InterfaceConfig{
|
||||
ListenPort: r.Port,
|
||||
},
|
||||
}
|
||||
|
||||
for _, pp := range r.Peers {
|
||||
switch {
|
||||
case pp.Node == p:
|
||||
// current
|
||||
found = true
|
||||
out.Interface.Name = pp.PeerConfig.Name
|
||||
out.Interface.Address = pp.Address
|
||||
out.Interface.PrivateKey = pp.PrivateKey
|
||||
default:
|
||||
// peer
|
||||
pc := pp.PeerConfig
|
||||
out.Peer = append(out.Peer, pc)
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// A RingPeer is a node on a [Ring]
|
||||
type RingPeer struct {
|
||||
Node *Machine
|
||||
|
||||
Address netip.Addr
|
||||
PrivateKey wireguard.PrivateKey
|
||||
PeerConfig wireguard.PeerConfig
|
||||
}
|
||||
|
||||
// AllowCIDR allows an IP range via this peer
|
||||
func (rp *RingPeer) AllowCIDR(addr netip.Addr, bits int) {
|
||||
cidr := netip.PrefixFrom(addr, bits)
|
||||
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs, cidr)
|
||||
}
|
||||
|
||||
// NewRing composes a new Ring for Wireguard setup
|
||||
func NewRing(z ZoneIterator, m MachineIterator, ring int) (*Ring, error) {
|
||||
r := &Ring{
|
||||
RingAddressEncoder: Rings[ring],
|
||||
ZoneIterator: z,
|
||||
}
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
r.AddPeer(p)
|
||||
return false
|
||||
})
|
||||
|
||||
return r, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cluster
|
||||
|
||||
// SyncAll updates all config files
|
||||
func (m *Cluster) SyncAll() error {
|
||||
for _, fn := range []func() error{
|
||||
m.SyncAllWireguard,
|
||||
m.SyncAllCeph,
|
||||
} {
|
||||
if err := fn(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncAllWireguard updates all wireguard config files
|
||||
func (m *Cluster) SyncAllWireguard() error {
|
||||
var err error
|
||||
|
||||
for ring := 0; ring < RingsCount; ring++ {
|
||||
err = m.WriteWireguardKeys(ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.SyncWireguardConfig(ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncAllCeph updates the ceph.conf file
|
||||
func (m *Cluster) SyncAllCeph() error {
|
||||
cfg, err := m.GenCephConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.WriteCephConfig(cfg)
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
_ WireguardConfigPruner = (*Cluster)(nil)
|
||||
_ WireguardConfigPruner = (*Zone)(nil)
|
||||
_ WireguardConfigPruner = (*Machine)(nil)
|
||||
|
||||
_ WireguardConfigWriter = (*Cluster)(nil)
|
||||
_ WireguardConfigWriter = (*Zone)(nil)
|
||||
_ WireguardConfigWriter = (*Machine)(nil)
|
||||
|
||||
_ WireguardConfigSyncer = (*Cluster)(nil)
|
||||
_ WireguardConfigSyncer = (*Zone)(nil)
|
||||
_ WireguardConfigSyncer = (*Machine)(nil)
|
||||
|
||||
_ WireguardKeysWriter = (*Cluster)(nil)
|
||||
_ WireguardKeysWriter = (*Zone)(nil)
|
||||
_ WireguardKeysWriter = (*Machine)(nil)
|
||||
)
|
||||
|
||||
// A WireguardConfigPruner deletes wgN.conf on all machines under
|
||||
// its scope with the specified ring disabled
|
||||
type WireguardConfigPruner interface {
|
||||
PruneWireguardConfig(ring int) error
|
||||
}
|
||||
|
||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||
// the corresponding ring disabled on all zones
|
||||
func (m *Cluster) PruneWireguardConfig(ring int) error {
|
||||
return pruneWireguardConfig(m, ring)
|
||||
}
|
||||
|
||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||
// the corresponding ring disabled.
|
||||
func (z *Zone) PruneWireguardConfig(ring int) error {
|
||||
return pruneWireguardConfig(z, ring)
|
||||
}
|
||||
|
||||
func pruneWireguardConfig(m MachineIterator, ring int) error {
|
||||
var err error
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.PruneWireguardConfig(ring)
|
||||
if os.IsNotExist(err) {
|
||||
// ignore
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err != nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// PruneWireguardConfig deletes the wgN.conf file if its
|
||||
// presence on the ring is disabled
|
||||
func (m *Machine) PruneWireguardConfig(ring int) error {
|
||||
_, ok := m.getRingInfo(ring)
|
||||
if !ok {
|
||||
return m.RemoveWireguardConfig(ring)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// A WireguardConfigWriter rewrites all wgN.conf on all machines under
|
||||
// its scope attached to that ring
|
||||
type WireguardConfigWriter interface {
|
||||
WriteWireguardConfig(ring int) error
|
||||
}
|
||||
|
||||
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
||||
// attached to that ring
|
||||
func (m *Cluster) WriteWireguardConfig(ring int) error {
|
||||
switch ring {
|
||||
case 0:
|
||||
return writeWireguardConfig(m, m, ring)
|
||||
case 1:
|
||||
var err error
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
err = writeWireguardConfig(m, z, ring)
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
default:
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
}
|
||||
|
||||
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
||||
// on the Zone attached to that ring
|
||||
func (z *Zone) WriteWireguardConfig(ring int) error {
|
||||
switch ring {
|
||||
case 0:
|
||||
return writeWireguardConfig(z.zones, z.zones, ring)
|
||||
case 1:
|
||||
return writeWireguardConfig(z.zones, z, ring)
|
||||
default:
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
}
|
||||
|
||||
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
||||
r, err := NewRing(z, m, ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.writeWireguardRingConfig(r)
|
||||
return err != nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteWireguardConfig rewrites the wgN.conf file of this Machine
|
||||
// if enabled
|
||||
func (m *Machine) WriteWireguardConfig(ring int) error {
|
||||
r, err := NewRing(m.zone.zones, m.zone, ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.writeWireguardRingConfig(r)
|
||||
}
|
||||
|
||||
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
|
||||
wg, err := r.ExportConfig(m)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = wg.WriteTo(f)
|
||||
return err
|
||||
}
|
||||
|
||||
// A WireguardConfigSyncer updates all wgN.conf on all machines under
|
||||
// its scope reflecting the state of the ring
|
||||
type WireguardConfigSyncer interface {
|
||||
SyncWireguardConfig(ring int) error
|
||||
}
|
||||
|
||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||
// ring
|
||||
func (m *Cluster) SyncWireguardConfig(ring int) error {
|
||||
switch ring {
|
||||
case 0:
|
||||
return syncWireguardConfig(m, m, ring)
|
||||
case 1:
|
||||
var err error
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
err = syncWireguardConfig(m, z, ring)
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
default:
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
}
|
||||
|
||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||
// ring
|
||||
func (z *Zone) SyncWireguardConfig(ring int) error {
|
||||
switch ring {
|
||||
case 0:
|
||||
return syncWireguardConfig(z.zones, z.zones, ring)
|
||||
case 1:
|
||||
return syncWireguardConfig(z.zones, z, ring)
|
||||
default:
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
}
|
||||
|
||||
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
||||
r, err := NewRing(z, m, ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.ForEachMachine(func(p *Machine) bool {
|
||||
if _, ok := p.getRingInfo(ring); ok {
|
||||
err = p.writeWireguardRingConfig(r)
|
||||
} else {
|
||||
err = p.RemoveWireguardConfig(ring)
|
||||
}
|
||||
return err != nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||
// ring
|
||||
func (m *Machine) SyncWireguardConfig(ring int) error {
|
||||
return m.zone.SyncWireguardConfig(ring)
|
||||
}
|
||||
|
||||
// A WireguardKeysWriter writes the Wireguard Keys for all machines
|
||||
// under its scope for the specified ring
|
||||
type WireguardKeysWriter interface {
|
||||
WriteWireguardKeys(ring int) error
|
||||
}
|
||||
|
||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
||||
func (m *Cluster) WriteWireguardKeys(ring int) error {
|
||||
return writeWireguardKeys(m, ring)
|
||||
}
|
||||
|
||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
||||
func (z *Zone) WriteWireguardKeys(ring int) error {
|
||||
return writeWireguardKeys(z, ring)
|
||||
}
|
||||
|
||||
func writeWireguardKeys(m MachineIterator, ring int) error {
|
||||
var err error
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.WriteWireguardKeys(ring)
|
||||
if os.IsNotExist(err) {
|
||||
// ignore
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err != nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
||||
func (m *Machine) WriteWireguardKeys(ring int) error {
|
||||
var err error
|
||||
var key, pub string
|
||||
var ri *RingInfo
|
||||
|
||||
ri, _ = m.getRingInfo(ring)
|
||||
if ri != nil {
|
||||
key = ri.Keys.PrivateKey.String()
|
||||
pub = ri.Keys.PublicKey.String()
|
||||
}
|
||||
|
||||
switch {
|
||||
case key == "":
|
||||
return fs.ErrNotExist
|
||||
case pub == "":
|
||||
pub = ri.Keys.PrivateKey.Public().String()
|
||||
}
|
||||
|
||||
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
var (
|
||||
_ MachineIterator = (*Zone)(nil)
|
||||
)
|
||||
|
||||
// A ZoneIterator is a set of Zones we can iterate on
|
||||
type ZoneIterator interface {
|
||||
ForEachZone(func(*Zone) bool)
|
||||
}
|
||||
|
||||
// A Zone is a set of machines in close proximity and strong
|
||||
// affinity.
|
||||
type Zone struct {
|
||||
zones *Cluster
|
||||
logger `json:"-" yaml:"-"`
|
||||
|
||||
ID int
|
||||
Name string
|
||||
|
||||
Machines
|
||||
}
|
||||
|
||||
func (z *Zone) String() string {
|
||||
return z.Name
|
||||
}
|
||||
|
||||
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||
var err error
|
||||
var found bool
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
if p.ID == gatewayID {
|
||||
found = true
|
||||
err = p.SetGateway(enabled)
|
||||
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case !found:
|
||||
return fs.ErrNotExist
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// GatewayIDs returns the list of IDs of machines that act as ring0 gateways
|
||||
func (z *Zone) GatewayIDs() ([]int, int) {
|
||||
var out []int
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
if p.IsGateway() {
|
||||
out = append(out, p.ID)
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return out, len(out)
|
||||
}
|
||||
+44
-4
@@ -1,17 +1,44 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"darvaza.org/core"
|
||||
"gopkg.in/gcfg.v1"
|
||||
)
|
||||
|
||||
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
||||
"PrefixJoin": func(a []netip.Prefix, sep string) string {
|
||||
s := make([]string, len(a))
|
||||
for i, p := range a {
|
||||
s[i] = p.String()
|
||||
}
|
||||
return strings.Join(s, sep)
|
||||
},
|
||||
}).Parse(`[Interface]
|
||||
{{if .Interface.Name}}# Name: {{.Interface.Name}}
|
||||
{{end -}}
|
||||
Address = {{.Interface.Address}}
|
||||
PrivateKey = {{.Interface.PrivateKey}}
|
||||
ListenPort = {{.Interface.ListenPort}}
|
||||
{{- range .Peer }}
|
||||
|
||||
[Peer]
|
||||
{{if .Name}}# Name: {{.Name}}
|
||||
{{end -}}
|
||||
PublicKey = {{.PublicKey}}
|
||||
Endpoint = {{.Endpoint}}
|
||||
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
|
||||
{{- end }}
|
||||
`))
|
||||
|
||||
// Config represents a wgN.conf file
|
||||
type Config struct {
|
||||
Interface InterfaceConfig
|
||||
@@ -28,16 +55,29 @@ func (f *Config) Peers() int {
|
||||
return len(f.Peer)
|
||||
}
|
||||
|
||||
// WriteTo writes a Wireguard [Config] onto the provided [io.Writer]
|
||||
func (f *Config) WriteTo(w io.Writer) (int64, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if err := configTemplate.Execute(&buf, f); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return buf.WriteTo(w)
|
||||
}
|
||||
|
||||
// InterfaceConfig represents the [Interface] section
|
||||
type InterfaceConfig struct {
|
||||
Name string
|
||||
Address netip.Addr
|
||||
PrivateKey BinaryKey
|
||||
PrivateKey PrivateKey
|
||||
ListenPort uint16
|
||||
}
|
||||
|
||||
// PeerConfig represents a [Peer] section
|
||||
type PeerConfig struct {
|
||||
PublicKey BinaryKey
|
||||
Name string
|
||||
PublicKey PublicKey
|
||||
Endpoint EndpointAddress
|
||||
AllowedIPs []netip.Prefix
|
||||
}
|
||||
@@ -135,7 +175,7 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
||||
ListenPort: p.ListenPort,
|
||||
}
|
||||
|
||||
out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
|
||||
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PrivateKey")
|
||||
return InterfaceConfig{}, err
|
||||
@@ -162,7 +202,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
||||
}
|
||||
|
||||
// PublicKey
|
||||
out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
|
||||
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i])
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PublicKey")
|
||||
return out, err
|
||||
|
||||
+198
-15
@@ -2,35 +2,218 @@ package wireguard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/curve25519"
|
||||
)
|
||||
|
||||
// BinaryKey is a binary blob
|
||||
type BinaryKey []byte
|
||||
const (
|
||||
// PrivateKeySize is the length in bytes of a Wireguard Private Key
|
||||
PrivateKeySize = 32
|
||||
// PublicKeySize is the length in bytes of a Wireguard Public Key
|
||||
PublicKeySize = 32
|
||||
)
|
||||
|
||||
func (k BinaryKey) String() string {
|
||||
return base64.StdEncoding.EncodeToString(k)
|
||||
var (
|
||||
// ErrInvalidKeySize indicates the key size is wrong
|
||||
ErrInvalidKeySize = errors.New("invalid key size")
|
||||
// ErrInvalidPrivateKey indicates the private key is invalid
|
||||
ErrInvalidPrivateKey = errors.New("invalid private key")
|
||||
// ErrInvalidPublicKey indicates the public key is invalid
|
||||
ErrInvalidPublicKey = errors.New("invalid public key")
|
||||
)
|
||||
|
||||
type (
|
||||
// PrivateKey is a binary Wireguard Private Key
|
||||
PrivateKey [PrivateKeySize]byte
|
||||
// PublicKey is a binary Wireguard Public Key
|
||||
PublicKey [PublicKeySize]byte
|
||||
)
|
||||
|
||||
func (key PrivateKey) String() string {
|
||||
switch {
|
||||
case key.IsZero():
|
||||
return ""
|
||||
default:
|
||||
return base64.StdEncoding.EncodeToString(key[:])
|
||||
}
|
||||
}
|
||||
|
||||
func (pub PublicKey) String() string {
|
||||
switch {
|
||||
case pub.IsZero():
|
||||
return ""
|
||||
default:
|
||||
return base64.StdEncoding.EncodeToString(pub[:])
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||
return encodeKeyJSON(key.String())
|
||||
}
|
||||
|
||||
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||
func (pub PublicKey) MarshalJSON() ([]byte, error) {
|
||||
return encodeKeyJSON(pub.String())
|
||||
}
|
||||
|
||||
func encodeKeyJSON(s string) ([]byte, error) {
|
||||
var out []byte
|
||||
if s != "" {
|
||||
out = []byte(fmt.Sprintf("%q", s))
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// MarshalYAML encodes the key for YAML, omitting empty.
|
||||
func (key PrivateKey) MarshalYAML() (any, error) {
|
||||
return encodeKeyYAML(key.String())
|
||||
}
|
||||
|
||||
// MarshalYAML encodes the key for YAML, omitting empty.
|
||||
func (pub PublicKey) MarshalYAML() (any, error) {
|
||||
return encodeKeyYAML(pub.String())
|
||||
}
|
||||
|
||||
func encodeKeyYAML(s string) (any, error) {
|
||||
if s == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// IsZero tells if the key hasn't been set
|
||||
func (k BinaryKey) IsZero() bool {
|
||||
return len(k) == 0
|
||||
func (key PrivateKey) IsZero() bool {
|
||||
var zero PrivateKey
|
||||
return key.Equal(zero)
|
||||
}
|
||||
|
||||
// Equal checks if two keys are identical
|
||||
func (k BinaryKey) Equal(alter BinaryKey) bool {
|
||||
return bytes.Equal(k, alter)
|
||||
// IsZero tells if the key hasn't been set
|
||||
func (pub PublicKey) IsZero() bool {
|
||||
var zero PublicKey
|
||||
return pub.Equal(zero)
|
||||
}
|
||||
|
||||
// BinaryKeyFromBase64 decodes a base64-based string into
|
||||
// a [BinaryKey]
|
||||
func BinaryKeyFromBase64(data string) (BinaryKey, error) {
|
||||
// Equal checks if two private keys are identical
|
||||
func (key PrivateKey) Equal(alter PrivateKey) bool {
|
||||
return bytes.Equal(key[:], alter[:])
|
||||
}
|
||||
|
||||
// Equal checks if two public keys are identical
|
||||
func (pub PublicKey) Equal(alter PublicKey) bool {
|
||||
return bytes.Equal(pub[:], alter[:])
|
||||
}
|
||||
|
||||
// PrivateKeyFromBase64 decodes a base64-based string into
|
||||
// a [PrivateKey]
|
||||
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
|
||||
b, err := decodeKey(data, PrivateKeySize)
|
||||
if err != nil {
|
||||
var zero PrivateKey
|
||||
return zero, err
|
||||
}
|
||||
return *(*[PrivateKeySize]byte)(b), nil
|
||||
}
|
||||
|
||||
// PublicKeyFromBase64 decodes a base64-based string into
|
||||
// a [PublicKey]
|
||||
func PublicKeyFromBase64(data string) (PublicKey, error) {
|
||||
b, err := decodeKey(data, PublicKeySize)
|
||||
if err != nil {
|
||||
var zero PublicKey
|
||||
return zero, err
|
||||
}
|
||||
return *(*[PublicKeySize]byte)(b), nil
|
||||
}
|
||||
|
||||
func decodeKey(data string, size int) ([]byte, error) {
|
||||
b, err := base64.StdEncoding.DecodeString(data)
|
||||
return BinaryKey(b), err
|
||||
switch {
|
||||
case err != nil:
|
||||
return []byte{}, err
|
||||
case len(b) != size:
|
||||
err = ErrInvalidKeySize
|
||||
return []byte{}, err
|
||||
default:
|
||||
return b, nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewPrivateKey creates a new PrivateKey
|
||||
func NewPrivateKey() (PrivateKey, error) {
|
||||
var s [PrivateKeySize]byte
|
||||
|
||||
_, err := rand.Read(s[:])
|
||||
if err != nil {
|
||||
var zero PrivateKey
|
||||
return zero, err
|
||||
}
|
||||
|
||||
// apply same clamping as wireguard-go/device/noise-helpers.go
|
||||
s[0] &= 0xf8
|
||||
s[31] = (s[31] & 0x7f) | 0x40
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Public generates the corresponding PublicKey
|
||||
func (key PrivateKey) Public() PublicKey {
|
||||
var pub PublicKey
|
||||
if !key.IsZero() {
|
||||
in := (*[PrivateKeySize]byte)(&key)
|
||||
out := (*[PublicKeySize]byte)(&pub)
|
||||
|
||||
curve25519.ScalarBaseMult(out, in)
|
||||
}
|
||||
return pub
|
||||
}
|
||||
|
||||
// KeyPair holds a Key pair
|
||||
type KeyPair struct {
|
||||
PrivateKey BinaryKey
|
||||
PublicKey BinaryKey
|
||||
PrivateKey PrivateKey
|
||||
PublicKey PublicKey
|
||||
}
|
||||
|
||||
// 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
|
||||
return ErrInvalidPrivateKey
|
||||
case pubLen == 0:
|
||||
// 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
|
||||
default:
|
||||
// correct public key
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewKeyPair creates a new KeyPair for Wireguard
|
||||
func NewKeyPair() (KeyPair, error) {
|
||||
var out KeyPair
|
||||
|
||||
key, err := NewPrivateKey()
|
||||
if err == nil {
|
||||
out.PrivateKey = key
|
||||
out.PublicKey = key.Public()
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/netip"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A Machine is a machine on a Zone
|
||||
type Machine struct {
|
||||
mu sync.Mutex
|
||||
|
||||
zone *Zone
|
||||
id int
|
||||
Name string `toml:"name"`
|
||||
|
||||
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
||||
RingAddresses []*RingInfo `toml:"rings,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Machine) String() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// ID return the index within the [Zone] associated to this [Machine]
|
||||
func (m *Machine) ID() int {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.id == 0 {
|
||||
zoneName := m.zone.Name
|
||||
|
||||
s := m.Name[len(zoneName)+1:]
|
||||
|
||||
id, err := strconv.ParseInt(s, 10, 8)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m.id = int(id)
|
||||
}
|
||||
|
||||
return m.id
|
||||
}
|
||||
|
||||
// FullName returns the Name of the machine including domain name
|
||||
func (m *Machine) FullName() string {
|
||||
if domain := m.zone.zones.domain; domain != "" {
|
||||
var s = []string{
|
||||
m.Name,
|
||||
domain,
|
||||
}
|
||||
|
||||
return strings.Join(s, ".")
|
||||
}
|
||||
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
func (m *Machine) getFilename(name string, args ...any) string {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
s := []string{
|
||||
m.zone.Name,
|
||||
m.Name,
|
||||
name,
|
||||
}
|
||||
|
||||
return filepath.Join(s...)
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"darvaza.org/core"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||
)
|
||||
|
||||
// GetWireguardConfig reads a wgN.conf file
|
||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||
data, err := m.ReadFile("wg%v.conf", ring)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := bytes.NewReader(data)
|
||||
return wireguard.NewConfigFromReader(r)
|
||||
}
|
||||
|
||||
func (m *Machine) tryApplyWireguardConfig(ring int) error {
|
||||
wg, err := m.GetWireguardConfig(ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
default:
|
||||
return m.applyWireguardConfig(ring, wg)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
||||
addr := wg.GetAddress()
|
||||
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
||||
if !ok {
|
||||
return fmt.Errorf("%s: invalid wg%v address: %s", m.Name, ring, addr)
|
||||
}
|
||||
|
||||
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
switch {
|
||||
case zoneID == 0:
|
||||
return fmt.Errorf("invalid %s", "zoneID")
|
||||
case nodeID == 0:
|
||||
return fmt.Errorf("invalid %s", "nodeID")
|
||||
case m.ID() != nodeID:
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID(), nodeID)
|
||||
case m.zone.ID != 0 && m.zone.ID != zoneID:
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID)
|
||||
case m.zone.ID == 0:
|
||||
m.zone.ID = zoneID
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
|
||||
timeout := 2 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
|
||||
defer cancel()
|
||||
|
||||
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
|
||||
}
|
||||
|
||||
func (m *Machine) updatePublicAddresses() error {
|
||||
addrs, err := m.lookupNetIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.PublicAddresses = addrs
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) scan() error {
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return m.updatePublicAddresses()
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxZoneID indicates the highest ID allowed for a Zone
|
||||
MaxZoneID = 0xf
|
||||
// MaxNodeID indicates the highest Machine ID allowed within a Zone
|
||||
MaxNodeID = 0xff - 1
|
||||
// RingsCount indicates how many wireguard rings we have
|
||||
RingsCount = 2
|
||||
)
|
||||
|
||||
// RingInfo contains represents the Wireguard endpoint details
|
||||
// for a Machine on a particular ring
|
||||
type RingInfo struct {
|
||||
Ring int `toml:"ring"`
|
||||
Enabled bool `toml:"enabled,omitempty"`
|
||||
Keys *wireguard.KeyPair `toml:"keys,omitempty"`
|
||||
Address netip.Addr `toml:"address,omitempty"`
|
||||
}
|
||||
|
||||
// RingAddressEncoder provides encoder/decoder access for a particular
|
||||
// Wireguard ring
|
||||
type RingAddressEncoder struct {
|
||||
ID int
|
||||
Encode func(zoneID, nodeID int) (netip.Addr, bool)
|
||||
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
|
||||
}
|
||||
|
||||
var (
|
||||
// RingZero is a wg0 address encoder/decoder
|
||||
RingZero = RingAddressEncoder{
|
||||
ID: 0,
|
||||
Decode: ParseRingZeroAddress,
|
||||
Encode: RingZeroAddress,
|
||||
}
|
||||
// RingOne is a wg1 address encoder/decoder
|
||||
RingOne = RingAddressEncoder{
|
||||
ID: 1,
|
||||
Decode: ParseRingOneAddress,
|
||||
Encode: RingOneAddress,
|
||||
}
|
||||
// Rings provides indexed access to the ring address encoders
|
||||
Rings = [RingsCount]RingAddressEncoder{
|
||||
RingZero,
|
||||
RingOne,
|
||||
}
|
||||
)
|
||||
|
||||
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
|
||||
//
|
||||
// 0 is reserved, and only allowed when composing CIDRs.
|
||||
func ValidZoneID(zoneID int) bool {
|
||||
switch {
|
||||
case zoneID < 0 || zoneID > MaxZoneID:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
|
||||
// nodeID is unique within a Zone.
|
||||
// 0 is reserved, and only allowed when composing CIDRs.
|
||||
func ValidNodeID(nodeID int) bool {
|
||||
switch {
|
||||
case nodeID < 0 || nodeID > MaxNodeID:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
|
||||
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
|
||||
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
||||
if addr.IsValid() {
|
||||
a4 := addr.As4()
|
||||
|
||||
if a4[0] == 10 && a4[1] == 0 {
|
||||
return int(a4[2]), int(a4[3]), true
|
||||
}
|
||||
}
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
// RingZeroAddress returns a wg0 IP address
|
||||
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||
switch {
|
||||
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||
return netip.Addr{}, false
|
||||
default:
|
||||
a4 := [4]uint8{10, 0, uint8(zoneID), uint8(nodeID)}
|
||||
return netip.AddrFrom4(a4), true
|
||||
}
|
||||
}
|
||||
|
||||
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
|
||||
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
|
||||
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
||||
if addr.IsValid() {
|
||||
a4 := addr.As4()
|
||||
|
||||
if a4[0] == 10 && a4[2] == 0 {
|
||||
zoneID = int(a4[1] >> 4)
|
||||
nodeID = int(a4[3])
|
||||
return zoneID, nodeID, true
|
||||
}
|
||||
}
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
// RingOneAddress returns a wg1 IP address
|
||||
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||
switch {
|
||||
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||
return netip.Addr{}, false
|
||||
default:
|
||||
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
|
||||
return netip.AddrFrom4(a4), true
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
func (m *Zones) scan() error {
|
||||
// each directory is a zone
|
||||
entries, err := fs.ReadDir(m.dir, ".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
z := &Zone{
|
||||
zones: m,
|
||||
Name: e.Name(),
|
||||
}
|
||||
|
||||
if err := z.scan(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.Zones = append(m.Zones, z)
|
||||
}
|
||||
}
|
||||
|
||||
return m.scanMachines()
|
||||
}
|
||||
|
||||
func (m *Zones) scanMachines() error {
|
||||
var err error
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.scan()
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (z *Zone) scan() error {
|
||||
// each directory is a machine
|
||||
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
m := &Machine{
|
||||
zone: z,
|
||||
Name: e.Name(),
|
||||
}
|
||||
|
||||
z.Machines = append(z.Machines, m)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
// Package zones contains information about the cluster
|
||||
package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
)
|
||||
|
||||
// Zone represents one zone in a cluster
|
||||
type Zone struct {
|
||||
zones *Zones
|
||||
|
||||
ID int
|
||||
Name string
|
||||
|
||||
Machines []*Machine `toml:"machines"`
|
||||
}
|
||||
|
||||
func (z *Zone) String() string {
|
||||
return z.Name
|
||||
}
|
||||
|
||||
// Zones represents all zones in a cluster
|
||||
type Zones struct {
|
||||
dir fs.FS
|
||||
resolver resolver.Resolver
|
||||
domain string
|
||||
|
||||
Zones []*Zone `toml:"zones"`
|
||||
}
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the cluster
|
||||
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
||||
for _, z := range m.Zones {
|
||||
for _, p := range z.Machines {
|
||||
if fn(p) {
|
||||
// terminate
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ForEachZone calls a function for each Zone in the cluster
|
||||
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
|
||||
for _, p := range m.Zones {
|
||||
if fn(p) {
|
||||
// terminate
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewFS builds a [Zones] tree using the given directory
|
||||
func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
||||
lockuper := resolver.NewCloudflareLookuper()
|
||||
|
||||
z := &Zones{
|
||||
dir: dir,
|
||||
resolver: resolver.NewResolver(lockuper),
|
||||
domain: domain,
|
||||
}
|
||||
|
||||
if err := z.scan(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return z, nil
|
||||
}
|
||||
|
||||
// New builds a [Zones] tree using the given directory
|
||||
func New(dir, domain string) (*Zones, error) {
|
||||
return NewFS(os.DirFS(dir), domain)
|
||||
}
|
||||
Reference in New Issue
Block a user