Compare commits
5 Commits
v0.5.2
..
b33dd35bdc
| Author | SHA1 | Date | |
|---|---|---|---|
| b33dd35bdc | |||
| 6143dd8d3a | |||
| 106c58c5da | |||
| ecff053a7b | |||
| ebfe47a31a |
Vendored
+3
-3
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
|
"asciigoat",
|
||||||
"ceph",
|
"ceph",
|
||||||
|
"cyclomatic",
|
||||||
"darvaza",
|
"darvaza",
|
||||||
"gofrs",
|
"Wrapf"
|
||||||
"jpictl",
|
|
||||||
"zerolog"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,5 @@ var cfg = &Config{
|
|||||||
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
|
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
|
||||||
return zones.New(cfg.Base, cfg.Domain,
|
return zones.New(cfg.Base, cfg.Domain,
|
||||||
zones.ResolvePublicAddresses(resolve),
|
zones.ResolvePublicAddresses(resolve),
|
||||||
zones.WithLogger(log),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-5
@@ -16,11 +16,7 @@ var envCmd = &cobra.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
env, err := m.Env(*envExport)
|
_, err = m.Env(*envExport).WriteTo(os.Stdout)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = env.WriteTo(os.Stdout)
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"darvaza.org/slog"
|
"darvaza.org/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: make log level configurable via flags
|
|
||||||
var (
|
var (
|
||||||
log = zerolog.New(nil, slog.Debug)
|
log = zerolog.New(nil, slog.Debug)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,20 +2,19 @@ module git.jpi.io/amery/jpictl
|
|||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
replace asciigoat.org/ini => ../../../asciigoat.org/ini
|
||||||
|
|
||||||
require (
|
require (
|
||||||
asciigoat.org/ini v0.2.5
|
asciigoat.org/ini v0.2.3
|
||||||
darvaza.org/core v0.9.8
|
darvaza.org/core v0.9.8
|
||||||
darvaza.org/resolver v0.5.4
|
darvaza.org/resolver v0.5.2
|
||||||
darvaza.org/sidecar v0.0.2
|
darvaza.org/sidecar v0.0.2
|
||||||
darvaza.org/slog v0.5.3
|
darvaza.org/slog v0.5.3
|
||||||
darvaza.org/slog/handlers/discard v0.4.5
|
|
||||||
github.com/burntSushi/toml v0.3.1
|
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/hack-pad/hackpadfs v0.2.1
|
||||||
github.com/mgechev/revive v1.3.3
|
github.com/mgechev/revive v1.3.3
|
||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
golang.org/x/crypto v0.12.0
|
golang.org/x/crypto v0.12.0
|
||||||
gopkg.in/gcfg.v1 v1.2.3
|
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,8 +40,7 @@ require (
|
|||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
golang.org/x/mod v0.12.0 // indirect
|
golang.org/x/mod v0.12.0 // indirect
|
||||||
golang.org/x/net v0.14.0 // indirect
|
golang.org/x/net v0.14.0 // indirect
|
||||||
golang.org/x/sys v0.12.0 // indirect
|
golang.org/x/sys v0.11.0 // indirect
|
||||||
golang.org/x/text v0.13.0 // indirect
|
golang.org/x/text v0.13.0 // indirect
|
||||||
golang.org/x/tools v0.12.0 // indirect
|
golang.org/x/tools v0.12.0 // indirect
|
||||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,17 +1,13 @@
|
|||||||
asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
|
asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
|
||||||
asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI=
|
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 h1:luLxgfUc2pzuusYPo/Z/dC/qr9XZPKpSQw8/kS7zNUM=
|
||||||
darvaza.org/core v0.9.8/go.mod h1:Dbme64naxeshQfxcVJX9ZT7AiGyIY8kldfuELVtf8mw=
|
darvaza.org/core v0.9.8/go.mod h1:Dbme64naxeshQfxcVJX9ZT7AiGyIY8kldfuELVtf8mw=
|
||||||
darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs=
|
darvaza.org/resolver v0.5.2 h1:VjHhEr/MJBszeDb7tYlXQ9Bsyh4xrDR7Sd10WAmPD6k=
|
||||||
darvaza.org/resolver v0.5.4/go.mod h1:vHMkQUmHjaetFqG2ZLZJiQHsXEMGoTOFGm+NXwfndhE=
|
darvaza.org/resolver v0.5.2/go.mod h1:fFvsVPEFeMzUIWlLG47Go/6uJYtRLb9R8HIgYg3uaxE=
|
||||||
darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0=
|
darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0=
|
||||||
darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo=
|
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 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0=
|
||||||
darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0=
|
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 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc=
|
||||||
darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g=
|
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 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4=
|
||||||
@@ -32,8 +28,6 @@ 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 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
|
||||||
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
|
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/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 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMIxMw=
|
||||||
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
|
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 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
@@ -92,18 +86,14 @@ 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-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.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.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
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/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 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||||
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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=
|
|
||||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
|
|
||||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
// Package ceph deals with ceph config
|
|
||||||
package ceph
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
+14
-110
@@ -2,7 +2,6 @@ package wireguard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -10,8 +9,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"asciigoat.org/ini/basic"
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
"gopkg.in/gcfg.v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
||||||
@@ -107,6 +106,11 @@ func (ep EndpointAddress) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalText loads an endpoint address from text data
|
||||||
|
func (ep *EndpointAddress) UnmarshalText(b []byte) error {
|
||||||
|
return ep.FromString(string(b))
|
||||||
|
}
|
||||||
|
|
||||||
// FromString sets the EndpointAddress from a given "[host]:port"
|
// FromString sets the EndpointAddress from a given "[host]:port"
|
||||||
func (ep *EndpointAddress) FromString(s string) error {
|
func (ep *EndpointAddress) FromString(s string) error {
|
||||||
host, port, err := core.SplitHostPort(s)
|
host, port, err := core.SplitHostPort(s)
|
||||||
@@ -127,98 +131,6 @@ func (ep *EndpointAddress) FromString(s string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type intermediateConfig struct {
|
|
||||||
Interface interfaceConfig
|
|
||||||
Peer peersConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *intermediateConfig) Export() (*Config, error) {
|
|
||||||
var out Config
|
|
||||||
var err error
|
|
||||||
|
|
||||||
// Interface
|
|
||||||
out.Interface, err = v.Interface.Export()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Peers
|
|
||||||
peers, ok := v.PeersCount()
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("inconsistent Peer data")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < peers; i++ {
|
|
||||||
p, err := v.ExportPeer(i)
|
|
||||||
if err != nil {
|
|
||||||
err = core.Wrapf(err, "Peer[%v]:", i)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
out.Peer = append(out.Peer, p)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type interfaceConfig struct {
|
|
||||||
Address netip.Addr
|
|
||||||
PrivateKey string
|
|
||||||
ListenPort uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
out := InterfaceConfig{
|
|
||||||
Address: p.Address,
|
|
||||||
ListenPort: p.ListenPort,
|
|
||||||
}
|
|
||||||
|
|
||||||
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
|
||||||
if err != nil {
|
|
||||||
err = core.Wrap(err, "PrivateKey")
|
|
||||||
return InterfaceConfig{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type peersConfig struct {
|
|
||||||
PublicKey []string
|
|
||||||
Endpoint []string
|
|
||||||
AllowedIPs []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
|
||||||
var out PeerConfig
|
|
||||||
|
|
||||||
// Endpoint
|
|
||||||
s := v.Peer.Endpoint[i]
|
|
||||||
err := out.Endpoint.FromString(s)
|
|
||||||
if err != nil {
|
|
||||||
err = core.Wrap(err, "Endpoint")
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// PublicKey
|
|
||||||
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i])
|
|
||||||
if err != nil {
|
|
||||||
err = core.Wrap(err, "PublicKey")
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllowedIPs
|
|
||||||
s = v.Peer.AllowedIPs[i]
|
|
||||||
out.AllowedIPs, err = parseAllowedIPs(s)
|
|
||||||
if err != nil {
|
|
||||||
err = core.Wrap(err, "AllowedIPs")
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
||||||
var out []netip.Prefix
|
var out []netip.Prefix
|
||||||
|
|
||||||
@@ -235,25 +147,17 @@ func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *intermediateConfig) PeersCount() (int, bool) {
|
|
||||||
c0 := len(v.Peer.Endpoint)
|
|
||||||
c1 := len(v.Peer.PublicKey)
|
|
||||||
c2 := len(v.Peer.AllowedIPs)
|
|
||||||
|
|
||||||
if c0 != c1 || c1 != c2 {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return c0, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewConfigFromReader parses a wgN.conf file
|
// NewConfigFromReader parses a wgN.conf file
|
||||||
func NewConfigFromReader(r io.Reader) (*Config, error) {
|
func NewConfigFromReader(r io.Reader) (*Config, error) {
|
||||||
temp := &intermediateConfig{}
|
doc, err := basic.Decode(r)
|
||||||
|
if err != nil {
|
||||||
if err := gcfg.ReadInto(temp, r); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return temp.Export()
|
cfg, err := newConfigFromDocument(doc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,162 @@
|
|||||||
|
package wireguard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"asciigoat.org/ini/basic"
|
||||||
|
"darvaza.org/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func loadConfSection(out *Config, src *basic.Section) error {
|
||||||
|
switch src.Key {
|
||||||
|
case "Interface":
|
||||||
|
return loadInterfaceConfSection(out, src)
|
||||||
|
case "Peer":
|
||||||
|
return loadPeerConfSection(out, src)
|
||||||
|
default:
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "unknown section %q", src.Key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadInterfaceConfSection(out *Config, src *basic.Section) error {
|
||||||
|
var cfg InterfaceConfig
|
||||||
|
|
||||||
|
for _, field := range src.Fields {
|
||||||
|
if err := loadInterfaceConfField(&cfg, field); err != nil {
|
||||||
|
return core.Wrap(err, "Interface")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Interface = cfg
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPeerConfSection(out *Config, src *basic.Section) error {
|
||||||
|
var cfg PeerConfig
|
||||||
|
|
||||||
|
for _, field := range src.Fields {
|
||||||
|
if err := loadPeerConfField(&cfg, field); err != nil {
|
||||||
|
return core.Wrapf(err, "Peer[%v]", len(out.Peer))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Peer = append(out.Peer, cfg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// revive:disable:cyclomatic
|
||||||
|
// revive:disable:cognitive-complexity
|
||||||
|
|
||||||
|
func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error {
|
||||||
|
// revive:enable:cyclomatic
|
||||||
|
// revive:enable:cognitive-complexity
|
||||||
|
switch field.Key {
|
||||||
|
case "Address":
|
||||||
|
if !core.IsZero(cfg.Address) {
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cfg.Address.UnmarshalText([]byte(field.Value))
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, field.Key)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case "PrivateKey":
|
||||||
|
if !core.IsZero(cfg.PrivateKey) {
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cfg.PrivateKey.UnmarshalText([]byte(field.Value))
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, field.Key)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case "ListenPort":
|
||||||
|
if cfg.ListenPort > 0 {
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||||
|
}
|
||||||
|
|
||||||
|
u64, err := strconv.ParseUint(field.Value, 10, 16)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, field.Key)
|
||||||
|
case u64 == 0:
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "invalid %q value", field.Key)
|
||||||
|
default:
|
||||||
|
cfg.ListenPort = uint16(u64)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "unknown field %q", field.Key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// revive:disable:cyclomatic
|
||||||
|
// revive:disable:cognitive-complexity
|
||||||
|
|
||||||
|
func loadPeerConfField(cfg *PeerConfig, field basic.Field) error {
|
||||||
|
// revive:enable:cyclomatic
|
||||||
|
// revive:enable:cognitive-complexity
|
||||||
|
|
||||||
|
switch field.Key {
|
||||||
|
case "PublicKey":
|
||||||
|
if !core.IsZero(cfg.PublicKey) {
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cfg.PublicKey.UnmarshalText([]byte(field.Value))
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, field.Key)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case "Endpoint":
|
||||||
|
if cfg.Endpoint.String() != "" {
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cfg.Endpoint.UnmarshalText([]byte(field.Value))
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, field.Key)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case "AllowedIPs":
|
||||||
|
s, err := parseAllowedIPs(field.Value)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return core.Wrap(err, field.Key)
|
||||||
|
case len(s) > 0:
|
||||||
|
cfg.AllowedIPs = append(cfg.AllowedIPs, s...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return core.Wrapf(fs.ErrInvalid, "unknown field %q", field.Key)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConfigFromDocument(doc *basic.Document) (*Config, error) {
|
||||||
|
var out Config
|
||||||
|
|
||||||
|
if len(doc.Global) > 0 {
|
||||||
|
err := core.Wrap(fs.ErrInvalid, "fields before the first section")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range doc.Sections {
|
||||||
|
src := &doc.Sections[i]
|
||||||
|
if err := loadConfSection(&out, src); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &out, nil
|
||||||
|
}
|
||||||
@@ -51,6 +51,30 @@ func (pub PublicKey) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalText loads the value from base64
|
||||||
|
func (key *PrivateKey) UnmarshalText(b []byte) error {
|
||||||
|
v, err := PrivateKeyFromBase64(string(b))
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
*key = v
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText loads the value from base64
|
||||||
|
func (pub *PublicKey) UnmarshalText(b []byte) error {
|
||||||
|
v, err := PublicKeyFromBase64(string(b))
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
*pub = v
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omitting empty.
|
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||||
return encodeKeyJSON(key.String())
|
return encodeKeyJSON(key.String())
|
||||||
|
|||||||
@@ -1,122 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
+3
-67
@@ -11,24 +11,15 @@ import (
|
|||||||
type Env struct {
|
type Env struct {
|
||||||
ZoneIterator
|
ZoneIterator
|
||||||
|
|
||||||
cephFSID string
|
export bool
|
||||||
export bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Env returns a shell environment factory
|
// Env returns a shell environment factory
|
||||||
func (m *Zones) Env(export bool) (*Env, error) {
|
func (m *Zones) Env(export bool) *Env {
|
||||||
fsid, err := m.GetCephFSID()
|
return &Env{
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &Env{
|
|
||||||
ZoneIterator: m,
|
ZoneIterator: m,
|
||||||
cephFSID: fsid.String(),
|
|
||||||
export: export,
|
export: export,
|
||||||
}
|
}
|
||||||
|
|
||||||
return env, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zones returns the list of Zone IDs
|
// Zones returns the list of Zone IDs
|
||||||
@@ -47,12 +38,7 @@ func (m *Env) Zones() []int {
|
|||||||
func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
if m.cephFSID != "" {
|
|
||||||
m.writeEnvVar(&buf, m.cephFSID, "FSID")
|
|
||||||
}
|
|
||||||
|
|
||||||
m.writeEnvVarInts(&buf, m.Zones(), "ZONES")
|
m.writeEnvVarInts(&buf, m.Zones(), "ZONES")
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
m.writeEnvZone(&buf, z)
|
m.writeEnvZone(&buf, z)
|
||||||
return false
|
return false
|
||||||
@@ -73,15 +59,6 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
|||||||
// ZONE{zoneID}_GW
|
// ZONE{zoneID}_GW
|
||||||
gateways, _ := z.GatewayIDs()
|
gateways, _ := z.GatewayIDs()
|
||||||
m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW")
|
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) {
|
func (m *Env) writeEnvVarInts(w io.Writer, value []int, name string, args ...any) {
|
||||||
@@ -134,44 +111,3 @@ func genEnvZoneNodes(z *Zone) string {
|
|||||||
}
|
}
|
||||||
return ""
|
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()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
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,15 +10,11 @@ import (
|
|||||||
// A Machine is a machine on a Zone
|
// A Machine is a machine on a Zone
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
zone *Zone
|
zone *Zone
|
||||||
logger
|
|
||||||
|
|
||||||
ID int `toml:"id"`
|
ID int `toml:"id"`
|
||||||
Name string `toml:"-" json:"-" yaml:"-"`
|
Name string `toml:"-" json:"-" yaml:"-"`
|
||||||
|
|
||||||
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
||||||
Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,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
|
// revive:enable:line-length-limit
|
||||||
|
|||||||
+1
-26
@@ -5,12 +5,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
"darvaza.org/slog"
|
|
||||||
"darvaza.org/slog/handlers/discard"
|
|
||||||
"github.com/hack-pad/hackpadfs/os"
|
"github.com/hack-pad/hackpadfs/os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A ScanOption pre-configures the Zones before scanning
|
// A ScanOption preconfigures the Zones before scanning
|
||||||
type ScanOption func(*Zones, *ScanOptions) error
|
type ScanOption func(*Zones, *ScanOptions) error
|
||||||
|
|
||||||
// ScanOptions contains flags used by the initial scan
|
// ScanOptions contains flags used by the initial scan
|
||||||
@@ -19,10 +17,6 @@ type ScanOptions struct {
|
|||||||
// pre-populate Machine.PublicAddresses during the
|
// pre-populate Machine.PublicAddresses during the
|
||||||
// initial scan
|
// initial scan
|
||||||
DontResolvePublicAddresses bool
|
DontResolvePublicAddresses bool
|
||||||
|
|
||||||
// Logger specifies the logger to be used. otherwise
|
|
||||||
// the scanner will be mute
|
|
||||||
slog.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolvePublicAddresses instructs the scanner to use
|
// ResolvePublicAddresses instructs the scanner to use
|
||||||
@@ -61,19 +55,6 @@ func WithResolver(h resolver.Resolver) ScanOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
func (m *Zones) setDefaults(opt *ScanOptions) error {
|
||||||
if m.resolver == nil {
|
if m.resolver == nil {
|
||||||
h := resolver.NewCloudflareLookuper()
|
h := resolver.NewCloudflareLookuper()
|
||||||
@@ -83,12 +64,6 @@ func (m *Zones) setDefaults(opt *ScanOptions) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if opt.Logger == nil {
|
|
||||||
if err := WithLogger(nil)(m, opt); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-7
@@ -12,7 +12,6 @@ func (m *Zones) scan(opts *ScanOptions) error {
|
|||||||
m.scanZoneIDs,
|
m.scanZoneIDs,
|
||||||
m.scanSort,
|
m.scanSort,
|
||||||
m.scanGateways,
|
m.scanGateways,
|
||||||
m.scanCephMonitors,
|
|
||||||
} {
|
} {
|
||||||
if err := fn(opts); err != nil {
|
if err := fn(opts); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -32,9 +31,8 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
|
|||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if e.IsDir() {
|
if e.IsDir() {
|
||||||
z := &Zone{
|
z := &Zone{
|
||||||
zones: m,
|
zones: m,
|
||||||
logger: m,
|
Name: e.Name(),
|
||||||
Name: e.Name(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := z.scan(); err != nil {
|
if err := z.scan(); err != nil {
|
||||||
@@ -133,9 +131,8 @@ func (z *Zone) scan() error {
|
|||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if e.IsDir() {
|
if e.IsDir() {
|
||||||
m := &Machine{
|
m := &Machine{
|
||||||
zone: z,
|
zone: z,
|
||||||
logger: z,
|
Name: e.Name(),
|
||||||
Name: e.Name(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.init(); err != nil {
|
if err := m.init(); err != nil {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package zones
|
|||||||
func (m *Zones) SyncAll() error {
|
func (m *Zones) SyncAll() error {
|
||||||
for _, fn := range []func() error{
|
for _, fn := range []func() error{
|
||||||
m.SyncAllWireguard,
|
m.SyncAllWireguard,
|
||||||
m.SyncAllCeph,
|
|
||||||
} {
|
} {
|
||||||
if err := fn(); err != nil {
|
if err := fn(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -32,13 +31,3 @@ func (m *Zones) SyncAllWireguard() error {
|
|||||||
|
|
||||||
return nil
|
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,8 +6,6 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
"darvaza.org/slog"
|
|
||||||
"github.com/gofrs/uuid/v5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -88,7 +86,6 @@ func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int)
|
|||||||
// Zone represents one zone in a cluster
|
// Zone represents one zone in a cluster
|
||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Zones
|
zones *Zones
|
||||||
logger
|
|
||||||
|
|
||||||
ID int `toml:"id"`
|
ID int `toml:"id"`
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
@@ -138,22 +135,15 @@ func (z *Zone) GatewayIDs() ([]int, int) {
|
|||||||
return out, len(out)
|
return out, len(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// revive:disable:line-length-limit
|
|
||||||
|
|
||||||
// Zones represents all zones in a cluster
|
// Zones represents all zones in a cluster
|
||||||
type Zones struct {
|
type Zones struct {
|
||||||
dir fs.FS
|
dir fs.FS
|
||||||
log slog.Logger
|
|
||||||
resolver resolver.Resolver
|
resolver resolver.Resolver
|
||||||
domain string
|
domain string
|
||||||
|
|
||||||
CephFSID uuid.UUID `toml:"ceph_fsid,omitempty" json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"`
|
|
||||||
|
|
||||||
Zones []*Zone `toml:"zones"`
|
Zones []*Zone `toml:"zones"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// revive:enable:line-length-limit
|
|
||||||
|
|
||||||
// ForEachMachine calls a function for each Machine in the cluster
|
// ForEachMachine calls a function for each Machine in the cluster
|
||||||
// until instructed to terminate the loop
|
// until instructed to terminate the loop
|
||||||
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
||||||
|
|||||||
Reference in New Issue
Block a user