|
|
|
@ -6,6 +6,8 @@ import (
|
|
|
|
|
"io" |
|
|
|
|
"sort" |
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
"git.jpi.io/amery/jpictl/pkg/rings" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Env is a shell environment factory for this cluster
|
|
|
|
@ -35,8 +37,8 @@ func (m *Cluster) Env(export bool) (*Env, error) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Zones returns the list of Zone IDs
|
|
|
|
|
func (m *Env) Zones() []int { |
|
|
|
|
var zones []int |
|
|
|
|
func (m *Env) Zones() []rings.ZoneID { |
|
|
|
|
var zones []rings.ZoneID |
|
|
|
|
|
|
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
|
zones = append(zones, z.ID) |
|
|
|
@ -70,8 +72,8 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
|
|
|
|
m.writeEnvVar(&buf, m.cephFSID, "FSID") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
m.writeEnvVarStrings(&buf, m.Regions(), "REGIONS") |
|
|
|
|
m.writeEnvVarInts(&buf, m.Zones(), "ZONES") |
|
|
|
|
m.writeEnvVar(&buf, genEnvStrings(m.Regions()), "REGIONS") |
|
|
|
|
m.writeEnvVar(&buf, genEnvInts(m.Zones()), "ZONES") |
|
|
|
|
|
|
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
|
m.writeEnvZone(&buf, z) |
|
|
|
@ -92,7 +94,7 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
|
|
|
|
|
|
|
|
|
// ZONE{zoneID}_GW
|
|
|
|
|
gateways, _ := z.GatewayIDs() |
|
|
|
|
m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW") |
|
|
|
|
m.writeEnvVar(w, genEnvInts(gateways), "ZONE%v_%s", zoneID, "GW") |
|
|
|
|
|
|
|
|
|
// ZONE{zoneID}_REGION
|
|
|
|
|
m.writeEnvVar(w, genEnvZoneRegion(z), "ZONE%v_%s", zoneID, "REGION") |
|
|
|
@ -107,32 +109,6 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
|
|
|
|
m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), "MON%v_%s", zoneID, "ID") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Env) writeEnvVarInts(w io.Writer, value []int, name string, args ...any) { |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
for _, v := range value { |
|
|
|
|
if buf.Len() > 0 { |
|
|
|
|
_, _ = fmt.Fprint(&buf, " ") |
|
|
|
|
} |
|
|
|
|
_, _ = 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 |
|
|
|
|
|
|
|
|
|
for _, v := range value { |
|
|
|
|
if buf.Len() > 0 { |
|
|
|
|
_, _ = fmt.Fprint(&buf, " ") |
|
|
|
|
} |
|
|
|
|
_, _ = fmt.Fprintf(&buf, "%s", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
m.writeEnvVar(w, buf.String(), name, args...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) { |
|
|
|
|
var prefix string |
|
|
|
|
|
|
|
|
@ -155,6 +131,23 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func genEnvInts[T ~int | ~uint](values []T) string { |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
for _, v := range values { |
|
|
|
|
if buf.Len() > 0 { |
|
|
|
|
_, _ = buf.WriteRune(' ') |
|
|
|
|
} |
|
|
|
|
_, _ = buf.WriteString(fmt.Sprintf("%v", v)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return buf.String() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func genEnvStrings(values []string) string { |
|
|
|
|
return strings.Join(values, " ") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func genEnvZoneNodes(z *Zone) string { |
|
|
|
|
if n := z.Len(); n > 0 { |
|
|
|
|
s := make([]string, 0, n) |
|
|
|
@ -164,7 +157,7 @@ func genEnvZoneNodes(z *Zone) string {
|
|
|
|
|
return false |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return strings.Join(s, " ") |
|
|
|
|
return genEnvStrings(s) |
|
|
|
|
} |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|