env: export REGIONS and ZONE{zoneID}_REGION #45

Merged
amery merged 4 commits from pr-amery-env-regions into main 8 months ago
  1. 79
      pkg/cluster/env.go

79
pkg/cluster/env.go

@ -4,12 +4,14 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"sort"
"strings" "strings"
) )
// Env is a shell environment factory for this cluster // Env is a shell environment factory for this cluster
type Env struct { type Env struct {
ZoneIterator ZoneIterator
RegionIterator
cephFSID string cephFSID string
export bool export bool
@ -23,9 +25,10 @@ func (m *Cluster) Env(export bool) (*Env, error) {
} }
env := &Env{ env := &Env{
ZoneIterator: m, ZoneIterator: m,
cephFSID: fsid.String(), RegionIterator: m,
export: export, cephFSID: fsid.String(),
export: export,
} }
return env, nil return env, nil
@ -43,6 +46,22 @@ func (m *Env) Zones() []int {
return zones return zones
} }
// Regions returns the list of primary regions
func (m *Env) Regions() []string {
var regions []string
m.ForEachRegion(func(r *Region) bool {
if r.Cluster != nil {
regions = append(regions, r.Name)
}
return false
})
sort.Strings(regions)
return regions
}
// WriteTo generates environment variables for shell scripts // WriteTo generates environment variables for shell scripts
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
@ -51,6 +70,7 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) {
m.writeEnvVar(&buf, m.cephFSID, "FSID") m.writeEnvVar(&buf, m.cephFSID, "FSID")
} }
m.writeEnvVarStrings(&buf, m.Regions(), "REGIONS")
m.writeEnvVarInts(&buf, m.Zones(), "ZONES") m.writeEnvVarInts(&buf, m.Zones(), "ZONES")
m.ForEachZone(func(z *Zone) bool { m.ForEachZone(func(z *Zone) bool {
@ -74,6 +94,9 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
gateways, _ := z.GatewayIDs() gateways, _ := z.GatewayIDs()
m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW") m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW")
// ZONE{zoneID}_REGION
m.writeEnvVar(w, genEnvZoneRegion(z), "ZONE%v_%s", zoneID, "REGION")
// Ceph // Ceph
monitors := z.GetCephMonitors() monitors := z.GetCephMonitors()
// MON{zoneID}_NAME // MON{zoneID}_NAME
@ -85,22 +108,29 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
} }
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) {
var s string var buf bytes.Buffer
if n := len(value); n > 0 {
var buf bytes.Buffer
for i, v := range value { for _, v := range value {
if i != 0 { if buf.Len() > 0 {
_, _ = fmt.Fprint(&buf, " ") _, _ = fmt.Fprint(&buf, " ")
}
_, _ = fmt.Fprintf(&buf, "%v", v)
} }
_, _ = fmt.Fprintf(&buf, "%v", v)
}
m.writeEnvVar(w, buf.String(), name, args...)
}
func (m *Env) writeEnvVarStrings(w io.Writer, value []string, name string, args ...any) {
var buf bytes.Buffer
s = buf.String() for _, v := range value {
if buf.Len() > 0 {
_, _ = fmt.Fprint(&buf, " ")
}
_, _ = fmt.Fprintf(&buf, "%s", v)
} }
m.writeEnvVar(w, s, name, args...) m.writeEnvVar(w, buf.String(), name, args...)
} }
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) { func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
@ -117,7 +147,11 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
if name != "" { if name != "" {
value = strings.TrimSpace(value) value = strings.TrimSpace(value)
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value) if value == "" {
_, _ = fmt.Fprintf(w, "%s%s=\n", prefix, name)
} else {
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
}
} }
} }
@ -135,6 +169,21 @@ func genEnvZoneNodes(z *Zone) string {
return "" return ""
} }
func genEnvZoneRegion(z *Zone) string {
var region string
z.ForEachRegion(func(r *Region) bool {
if r.Cluster != nil {
region = r.Name
return true
}
return false
})
return region
}
func genEnvZoneCephMonNames(m Machines) string { func genEnvZoneCephMonNames(m Machines) string {
var buf strings.Builder var buf strings.Builder
m.ForEachMachine(func(p *Machine) bool { m.ForEachMachine(func(p *Machine) bool {

Loading…
Cancel
Save