From 5e57fe5f30d75c840ad9432df1f8af5e118e4d0d Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 4 Sep 2023 19:45:04 +0000 Subject: [PATCH] ceph: zones.Zones.WriteCephConfig() and ceph.Config.WriteTo() Signed-off-by: Alejandro Mery --- pkg/ceph/config.go | 29 +++++++++++++++++++++++++++++ pkg/zones/ceph.go | 12 ++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pkg/ceph/config.go b/pkg/ceph/config.go index 64c5bff..74e6cf1 100644 --- a/pkg/ceph/config.go +++ b/pkg/ceph/config.go @@ -1,8 +1,11 @@ package ceph import ( + "bytes" + "fmt" "io" "net/netip" + "strings" "github.com/gofrs/uuid/v5" @@ -22,6 +25,32 @@ type GlobalConfig struct { 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) diff --git a/pkg/zones/ceph.go b/pkg/zones/ceph.go index 63a030c..7b828e3 100644 --- a/pkg/zones/ceph.go +++ b/pkg/zones/ceph.go @@ -31,6 +31,18 @@ func (m *Zones) GetCephConfig() (*ceph.Config, error) { 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()