Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1fe1cf940d | |||
| f10ea1dc22 | |||
| ac87757b06 | |||
| fe081a4297 | |||
| cea8362fe6 | |||
| b772ec0a3d | |||
| 77ad016e99 | |||
| bf4bfeb3fc | |||
| e3ab931eb1 | |||
| 05e04c758b | |||
| 94011a3a03 | |||
| 025b9072b4 | |||
| 0fb8c1d44b | |||
| a8849b747c | |||
| 879d2b4d1c | |||
| ff4bb97599 | |||
| c95bf06da2 | |||
| 2e48f34f7f | |||
| 8e5fc44e62 | |||
|
3d5a766161
|
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"ceph",
|
||||
"darvaza",
|
||||
"jpictl",
|
||||
"zerolog"
|
||||
]
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -3,13 +3,15 @@ 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
|
||||
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 +19,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 +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/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,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 +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=
|
||||
@@ -70,8 +76,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 +90,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,118 @@
|
||||
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) {
|
||||
// TODO: generate one
|
||||
return uuid.Nil, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -59,6 +59,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 +120,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()
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ type Machine struct {
|
||||
|
||||
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
|
||||
|
||||
@@ -12,6 +12,7 @@ func (m *Zones) scan(opts *ScanOptions) error {
|
||||
m.scanZoneIDs,
|
||||
m.scanSort,
|
||||
m.scanGateways,
|
||||
m.scanCephMonitors,
|
||||
} {
|
||||
if err := fn(opts); err != nil {
|
||||
return err
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"sort"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -135,15 +136,21 @@ 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
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user