Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 | |||
| 6e46d23b45 | |||
| 94daf5ad59 | |||
| 0989dec5e8 | |||
| 216bf5aa29 | |||
| 9af88f6593 |
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"ceph",
|
||||
"darvaza",
|
||||
"gofrs",
|
||||
"jpictl",
|
||||
"zerolog"
|
||||
]
|
||||
}
|
||||
@@ -14,6 +14,9 @@ var cfg = &Config{
|
||||
}
|
||||
|
||||
// LoadZones loads all zones and machines in the config directory
|
||||
func (cfg *Config) LoadZones() (*zones.Zones, error) {
|
||||
return zones.New(cfg.Base, cfg.Domain)
|
||||
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
|
||||
return zones.New(cfg.Base, cfg.Domain,
|
||||
zones.ResolvePublicAddresses(resolve),
|
||||
zones.WithLogger(log),
|
||||
)
|
||||
}
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ var dumpCmd = &cobra.Command{
|
||||
var buf bytes.Buffer
|
||||
var enc Encoder
|
||||
|
||||
m, err := cfg.LoadZones()
|
||||
m, err := cfg.LoadZones(true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+6
-2
@@ -11,12 +11,16 @@ var envCmd = &cobra.Command{
|
||||
Use: "env",
|
||||
Short: "generates environment variables for shell scripts",
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := cfg.LoadZones()
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = m.Env(*envExport).WriteTo(os.Stdout)
|
||||
env, err := m.Env(*envExport)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = env.WriteTo(os.Stdout)
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// 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",
|
||||
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 zones.ZoneIterator, gw string) error {
|
||||
var err error
|
||||
zi.ForEachZone(func(z *zones.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 zones.ZoneIterator, ngw string) error {
|
||||
var err error
|
||||
zi.ForEachZone(func(z *zones.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 zones.ZoneIterator) error {
|
||||
var b bytes.Buffer
|
||||
var err error
|
||||
zi.ForEachZone(func(z *zones.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 zones.ZoneIterator, m string) error {
|
||||
var b bytes.Buffer
|
||||
var err error
|
||||
zi.ForEachZone(func(z *zones.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)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"darvaza.org/slog"
|
||||
)
|
||||
|
||||
// TODO: make log level configurable via flags
|
||||
var (
|
||||
log = zerolog.New(nil, slog.Debug)
|
||||
)
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ var writeCmd = &cobra.Command{
|
||||
Use: "write",
|
||||
Short: "rewrites all config files",
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := cfg.LoadZones()
|
||||
m, err := cfg.LoadZones(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,13 +3,16 @@ 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
|
||||
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/burntSushi/toml v0.3.1
|
||||
github.com/gofrs/uuid/v5 v5.0.0
|
||||
github.com/hack-pad/hackpadfs v0.2.1
|
||||
github.com/mgechev/revive v1.3.2
|
||||
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
|
||||
@@ -17,10 +20,11 @@ require (
|
||||
)
|
||||
|
||||
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
|
||||
@@ -37,8 +41,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,27 @@
|
||||
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 +32,8 @@ 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=
|
||||
@@ -42,8 +50,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=
|
||||
@@ -70,8 +78,8 @@ 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=
|
||||
@@ -84,10 +92,10 @@ 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=
|
||||
|
||||
@@ -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 zones
|
||||
|
||||
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 *Zones) 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 *Zones) 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 *Zones) 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 *Zones) 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,183 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"darvaza.org/core"
|
||||
"git.jpi.io/amery/jpictl/pkg/ceph"
|
||||
)
|
||||
|
||||
// CephMissingMonitorError is an error that contains ceph
|
||||
// monitors present in ceph.conf but not found on the cluster
|
||||
type CephMissingMonitorError struct {
|
||||
Names []string
|
||||
Addrs []netip.Addr
|
||||
}
|
||||
|
||||
func (err *CephMissingMonitorError) appendName(name string) {
|
||||
err.Names = append(err.Names, name)
|
||||
}
|
||||
|
||||
func (err *CephMissingMonitorError) appendAddr(addr netip.Addr) {
|
||||
err.Addrs = append(err.Addrs, addr)
|
||||
}
|
||||
|
||||
// OK tells if this instance actual shouldn't be treated as an error
|
||||
func (err CephMissingMonitorError) OK() bool {
|
||||
switch {
|
||||
case len(err.Names) > 0:
|
||||
return false
|
||||
case len(err.Addrs) > 0:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (err CephMissingMonitorError) Error() string {
|
||||
if !err.OK() {
|
||||
var buf strings.Builder
|
||||
|
||||
_, _ = buf.WriteString("missing:")
|
||||
err.writeNames(&buf)
|
||||
err.writeAddrs(&buf)
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// no error
|
||||
return ""
|
||||
}
|
||||
|
||||
func (err *CephMissingMonitorError) writeNames(w *strings.Builder) {
|
||||
if len(err.Names) > 0 {
|
||||
_, _ = w.WriteString(" mon_initial_members:")
|
||||
for i, name := range err.Names {
|
||||
if i != 0 {
|
||||
_, _ = w.WriteRune(',')
|
||||
}
|
||||
_, _ = w.WriteString(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (err *CephMissingMonitorError) writeAddrs(w *strings.Builder) {
|
||||
if len(err.Addrs) > 0 {
|
||||
_, _ = w.WriteString(" mon_host:")
|
||||
for i, addr := range err.Addrs {
|
||||
if i != 0 {
|
||||
_, _ = w.WriteRune(',')
|
||||
}
|
||||
_, _ = w.WriteString(addr.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AsError returns nil if the instance is actually OK
|
||||
func (err *CephMissingMonitorError) AsError() error {
|
||||
if err == nil || err.OK() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type cephScanTODO struct {
|
||||
names map[string]bool
|
||||
addrs map[string]bool
|
||||
}
|
||||
|
||||
func (todo *cephScanTODO) checkMachine(p *Machine) bool {
|
||||
// on ceph all addresses are ring1
|
||||
ring1, _ := RingOneAddress(p.Zone(), p.ID)
|
||||
addr := ring1.String()
|
||||
|
||||
if _, found := todo.names[p.Name]; found {
|
||||
// found on the TODO by name
|
||||
todo.names[p.Name] = true
|
||||
todo.addrs[addr] = true
|
||||
return true
|
||||
}
|
||||
|
||||
if _, found := todo.addrs[addr]; found {
|
||||
// found on the TODO by address
|
||||
todo.names[p.Name] = true
|
||||
todo.addrs[addr] = true
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (todo *cephScanTODO) Missing() error {
|
||||
var check CephMissingMonitorError
|
||||
|
||||
for name, found := range todo.names {
|
||||
if !found {
|
||||
check.appendName(name)
|
||||
}
|
||||
}
|
||||
|
||||
for addr, found := range todo.addrs {
|
||||
if !found {
|
||||
var a netip.Addr
|
||||
// it wouldn't be on the map if it wasn't valid
|
||||
_ = a.UnmarshalText([]byte(addr))
|
||||
|
||||
check.appendAddr(a)
|
||||
}
|
||||
}
|
||||
|
||||
return check.AsError()
|
||||
}
|
||||
|
||||
func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
|
||||
todo := &cephScanTODO{
|
||||
names: make(map[string]bool),
|
||||
addrs: make(map[string]bool),
|
||||
}
|
||||
|
||||
for _, name := range cfg.Global.Monitors {
|
||||
todo.names[name] = false
|
||||
}
|
||||
|
||||
for _, addr := range cfg.Global.MonitorsAddr {
|
||||
todo.addrs[addr.String()] = false
|
||||
}
|
||||
|
||||
return todo
|
||||
}
|
||||
|
||||
func (m *Zones) scanCephMonitors(_ *ScanOptions) error {
|
||||
cfg, err := m.GetCephConfig()
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
err = nil
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg != nil {
|
||||
// store FSID
|
||||
m.CephFSID = cfg.Global.FSID
|
||||
|
||||
// flag monitors based on config
|
||||
todo := newCephScanTODO(cfg)
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
p.CephMonitor = todo.checkMachine(p)
|
||||
return false
|
||||
})
|
||||
if err := todo.Missing(); err != nil {
|
||||
return core.Wrap(err, "ceph")
|
||||
}
|
||||
}
|
||||
|
||||
// make sure every zone has one
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
_ = z.GetCephMonitors()
|
||||
return false
|
||||
})
|
||||
return nil
|
||||
}
|
||||
+67
-3
@@ -11,15 +11,24 @@ import (
|
||||
type Env struct {
|
||||
ZoneIterator
|
||||
|
||||
export bool
|
||||
cephFSID string
|
||||
export bool
|
||||
}
|
||||
|
||||
// Env returns a shell environment factory
|
||||
func (m *Zones) Env(export bool) *Env {
|
||||
return &Env{
|
||||
func (m *Zones) 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
|
||||
@@ -38,7 +47,12 @@ func (m *Env) Zones() []int {
|
||||
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
|
||||
@@ -59,6 +73,15 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
||||
// 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) {
|
||||
@@ -111,3 +134,44 @@ func genEnvZoneNodes(z *Zone) string {
|
||||
}
|
||||
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,49 @@
|
||||
package zones
|
||||
|
||||
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 = (*Zones)(nil)
|
||||
)
|
||||
|
||||
func (z *Zones) withDebug() (slog.Logger, bool) {
|
||||
return z.debug().WithEnabled()
|
||||
}
|
||||
|
||||
func (z *Zones) withInfo() (slog.Logger, bool) {
|
||||
return z.debug().WithEnabled()
|
||||
}
|
||||
|
||||
func (z *Zones) debug() slog.Logger {
|
||||
return z.log.Debug()
|
||||
}
|
||||
|
||||
func (z *Zones) info() slog.Logger {
|
||||
return z.log.Info()
|
||||
}
|
||||
|
||||
func (z *Zones) warn(err error) slog.Logger {
|
||||
l := z.log.Warn()
|
||||
if err != nil {
|
||||
l = l.WithField(slog.ErrorFieldName, err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (z *Zones) error(err error) slog.Logger {
|
||||
l := z.log.Error()
|
||||
if err != nil {
|
||||
l = l.WithField(slog.ErrorFieldName, err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
@@ -10,11 +10,15 @@ import (
|
||||
// A Machine is a machine on a Zone
|
||||
type Machine struct {
|
||||
zone *Zone
|
||||
logger
|
||||
|
||||
ID int `toml:"id"`
|
||||
Name string `toml:"-" json:"-" yaml:"-"`
|
||||
|
||||
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
||||
Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,omitempty"`
|
||||
|
||||
CephMonitor bool `toml:"ceph_monitor,omitempty" json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
|
||||
}
|
||||
|
||||
// revive:enable:line-length-limit
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
|
||||
timeout := 2 * time.Second
|
||||
// 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()
|
||||
@@ -16,8 +17,9 @@ func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
|
||||
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
|
||||
}
|
||||
|
||||
func (m *Machine) updatePublicAddresses() error {
|
||||
addrs, err := m.lookupNetIP()
|
||||
// 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
|
||||
}
|
||||
@@ -52,12 +54,16 @@ func (m *Machine) setID() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) scan() error {
|
||||
func (m *Machine) scan(opts *ScanOptions) error {
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return m.updatePublicAddresses()
|
||||
if !opts.DontResolvePublicAddresses {
|
||||
return m.UpdatePublicAddresses()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"darvaza.org/slog"
|
||||
"darvaza.org/slog/handlers/discard"
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
)
|
||||
|
||||
// A ScanOption pre-configures the Zones before scanning
|
||||
type ScanOption func(*Zones, *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 *Zones, 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 *Zones, 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 *Zones, 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 *Zones, opt *ScanOptions) error {
|
||||
if log == nil {
|
||||
log = discard.New()
|
||||
}
|
||||
|
||||
opt.Logger = log
|
||||
m.log = log
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Zones) setDefaults(opt *ScanOptions) error {
|
||||
if m.resolver == nil {
|
||||
h := resolver.NewCloudflareLookuper()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// NewFS builds a [Zones] tree using the given directory
|
||||
func NewFS(dir fs.FS, domain string, opts ...ScanOption) (*Zones, error) {
|
||||
var scanOptions ScanOptions
|
||||
|
||||
z := &Zones{
|
||||
dir: dir,
|
||||
domain: domain,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if err := opt(z, &scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := z.setDefaults(&scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := z.scan(&scanOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return z, nil
|
||||
}
|
||||
|
||||
// New builds a [Zones] tree using the given directory
|
||||
func New(dir, domain string, opts ...ScanOption) (*Zones, error) {
|
||||
dir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base, err := os.NewFS().Sub(dir[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewFS(base, domain, opts...)
|
||||
}
|
||||
+16
-13
@@ -5,15 +5,16 @@ import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func (m *Zones) scan() error {
|
||||
for _, fn := range []func() error{
|
||||
func (m *Zones) 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(); err != nil {
|
||||
if err := fn(opts); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -21,7 +22,7 @@ func (m *Zones) scan() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanDirectory() error {
|
||||
func (m *Zones) scanDirectory(_ *ScanOptions) error {
|
||||
// each directory is a zone
|
||||
entries, err := fs.ReadDir(m.dir, ".")
|
||||
if err != nil {
|
||||
@@ -31,8 +32,9 @@ func (m *Zones) scanDirectory() error {
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
z := &Zone{
|
||||
zones: m,
|
||||
Name: e.Name(),
|
||||
zones: m,
|
||||
logger: m,
|
||||
Name: e.Name(),
|
||||
}
|
||||
|
||||
if err := z.scan(); err != nil {
|
||||
@@ -46,16 +48,16 @@ func (m *Zones) scanDirectory() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanMachines() error {
|
||||
func (m *Zones) scanMachines(opts *ScanOptions) error {
|
||||
var err error
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.scan()
|
||||
err = p.scan(opts)
|
||||
return err != nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Zones) scanZoneIDs() error {
|
||||
func (m *Zones) scanZoneIDs(_ *ScanOptions) error {
|
||||
var hasMissing bool
|
||||
var lastZoneID int
|
||||
|
||||
@@ -85,7 +87,7 @@ func (m *Zones) scanZoneIDs() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanSort() error {
|
||||
func (m *Zones) scanSort(_ *ScanOptions) error {
|
||||
sort.SliceStable(m.Zones, func(i, j int) bool {
|
||||
id1 := m.Zones[i].ID
|
||||
id2 := m.Zones[j].ID
|
||||
@@ -111,7 +113,7 @@ func (m *Zones) scanSort() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanGateways() error {
|
||||
func (m *Zones) scanGateways(_ *ScanOptions) error {
|
||||
var err error
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
@@ -131,8 +133,9 @@ func (z *Zone) scan() error {
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
m := &Machine{
|
||||
zone: z,
|
||||
Name: e.Name(),
|
||||
zone: z,
|
||||
logger: z,
|
||||
Name: e.Name(),
|
||||
}
|
||||
|
||||
if err := m.init(); err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ package zones
|
||||
func (m *Zones) SyncAll() error {
|
||||
for _, fn := range []func() error{
|
||||
m.SyncAllWireguard,
|
||||
m.SyncAllCeph,
|
||||
} {
|
||||
if err := fn(); err != nil {
|
||||
return err
|
||||
@@ -31,3 +32,13 @@ func (m *Zones) SyncAllWireguard() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncAllCeph updates the ceph.conf file
|
||||
func (m *Zones) SyncAllCeph() error {
|
||||
cfg, err := m.GenCephConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.WriteCephConfig(cfg)
|
||||
}
|
||||
|
||||
+10
-35
@@ -3,12 +3,11 @@ package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"darvaza.org/slog"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -89,6 +88,7 @@ func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int)
|
||||
// Zone represents one zone in a cluster
|
||||
type Zone struct {
|
||||
zones *Zones
|
||||
logger
|
||||
|
||||
ID int `toml:"id"`
|
||||
Name string `toml:"name"`
|
||||
@@ -138,15 +138,22 @@ func (z *Zone) GatewayIDs() ([]int, int) {
|
||||
return out, len(out)
|
||||
}
|
||||
|
||||
// revive:disable:line-length-limit
|
||||
|
||||
// Zones represents all zones in a cluster
|
||||
type Zones struct {
|
||||
dir fs.FS
|
||||
log slog.Logger
|
||||
resolver resolver.Resolver
|
||||
domain string
|
||||
|
||||
CephFSID uuid.UUID `toml:"ceph_fsid,omitempty" json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"`
|
||||
|
||||
Zones []*Zone `toml:"zones"`
|
||||
}
|
||||
|
||||
// revive:enable:line-length-limit
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the cluster
|
||||
// until instructed to terminate the loop
|
||||
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
||||
@@ -190,35 +197,3 @@ func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
|
||||
|
||||
return out, out != nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
dir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base, err := os.NewFS().Sub(dir[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewFS(base, domain)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package zones
|
||||
|
||||
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 *Zones) 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 *Zones) 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 *Zones) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE, args...)
|
||||
}
|
||||
|
||||
func (m *Zones) 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 *Zones) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
return fs.ReadFile(m.dir, name)
|
||||
}
|
||||
Reference in New Issue
Block a user