You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

70 lines
1.7 KiB

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())
_, _ = fmt.Fprintf(w, "\n; %s\n", "don't rewrite labels on startup")
_, _ = fmt.Fprintf(w, "%s = %s\n", "osd_class_update_on_start", "false")
}
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
}