|
|
@ -7,6 +7,8 @@ import ( |
|
|
|
"sort" |
|
|
|
"sort" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"darvaza.org/core" |
|
|
|
|
|
|
|
|
|
|
|
"git.jpi.io/amery/jpictl/pkg/rings" |
|
|
|
"git.jpi.io/amery/jpictl/pkg/rings" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -36,20 +38,24 @@ func (m *Cluster) Env(export bool) (*Env, error) { |
|
|
|
return env, nil |
|
|
|
return env, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Zones returns the list of Zone IDs
|
|
|
|
// Zones returns the list of Zone IDs of a region,
|
|
|
|
func (m *Env) Zones() []rings.ZoneID { |
|
|
|
// or from all if none is specified.
|
|
|
|
|
|
|
|
func (m *Env) Zones(r *Region) []rings.ZoneID { |
|
|
|
var zones []rings.ZoneID |
|
|
|
var zones []rings.ZoneID |
|
|
|
|
|
|
|
|
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
iter := core.IIf[ZoneIterator](r != nil, r, m) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iter.ForEachZone(func(z *Zone) bool { |
|
|
|
zones = append(zones, z.ID) |
|
|
|
zones = append(zones, z.ID) |
|
|
|
return false |
|
|
|
return false |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
core.SliceSort(zones, cmpOrdered[rings.ZoneID]) |
|
|
|
return zones |
|
|
|
return zones |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Regions returns the list of primary regions
|
|
|
|
// RegionsNames returns a sorted list of primary regions names
|
|
|
|
func (m *Env) Regions() []string { |
|
|
|
func (m *Env) RegionsNames() []string { |
|
|
|
var regions []string |
|
|
|
var regions []string |
|
|
|
|
|
|
|
|
|
|
|
m.ForEachRegion(func(r *Region) bool { |
|
|
|
m.ForEachRegion(func(r *Region) bool { |
|
|
@ -64,6 +70,20 @@ func (m *Env) Regions() []string { |
|
|
|
return regions |
|
|
|
return regions |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Regions returns a sorted list of primary regions IDs
|
|
|
|
|
|
|
|
func (m *Env) Regions() (regions []rings.RegionID) { |
|
|
|
|
|
|
|
m.ForEachRegion(func(r *Region) bool { |
|
|
|
|
|
|
|
if r.IsPrimary() { |
|
|
|
|
|
|
|
regions = append(regions, r.ID) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
core.SliceSort(regions, cmpOrdered[rings.RegionID]) |
|
|
|
|
|
|
|
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 |
|
|
@ -72,41 +92,75 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) { |
|
|
|
m.writeEnvVar(&buf, m.cephFSID, "FSID") |
|
|
|
m.writeEnvVar(&buf, m.cephFSID, "FSID") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
m.writeEnvVar(&buf, genEnvStrings(m.Regions()), "REGIONS") |
|
|
|
regions := m.getRegions() |
|
|
|
m.writeEnvVar(&buf, genEnvInts(m.Zones()), "ZONES") |
|
|
|
ids := core.SliceMap(regions, func(_ []rings.RegionID, r *Region) (out []rings.RegionID) { |
|
|
|
|
|
|
|
return append(out, r.ID) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
names := core.SliceMap(regions, func(_ []string, r *Region) (out []string) { |
|
|
|
|
|
|
|
return append(out, r.Name) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m.writeEnvVar(&buf, genEnvInts(ids), "REGIONS") |
|
|
|
|
|
|
|
m.writeEnvVar(&buf, genEnvStrings(names), "REGIONS_NAMES") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, r := range regions { |
|
|
|
|
|
|
|
m.writeEnvRegion(&buf, r) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return buf.WriteTo(w) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
m.ForEachZone(func(z *Zone) bool { |
|
|
|
func (m *Env) getRegions() (out []*Region) { |
|
|
|
m.writeEnvZone(&buf, z) |
|
|
|
m.ForEachRegion(func(r *Region) bool { |
|
|
|
|
|
|
|
if r.IsPrimary() { |
|
|
|
|
|
|
|
out = append(out, r) |
|
|
|
|
|
|
|
} |
|
|
|
return false |
|
|
|
return false |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return buf.WriteTo(w) |
|
|
|
core.SliceSort(out, func(a, b *Region) int { |
|
|
|
|
|
|
|
return cmpOrdered(a.ID, b.ID) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return out |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (m *Env) writeEnvRegion(w io.Writer, r *Region) { |
|
|
|
|
|
|
|
regionID := r.ID |
|
|
|
|
|
|
|
|
|
|
|
func (m *Env) writeEnvZone(w io.Writer, z *Zone) { |
|
|
|
// REGION{regionID}_NAME
|
|
|
|
zoneID := z.ID |
|
|
|
m.writeEnvVar(w, r.Name, "REGION%v_%s", regionID, "NAME") |
|
|
|
|
|
|
|
|
|
|
|
// ZONE{zoneID}
|
|
|
|
// REGION{regionID}_ZONES
|
|
|
|
m.writeEnvVar(w, genEnvZoneNodes(z), "ZONE%v", zoneID) |
|
|
|
m.writeEnvVar(w, genEnvInts(m.Zones(r)), "REGION%v_%s", regionID, "ZONES") |
|
|
|
|
|
|
|
|
|
|
|
// ZONE{zoneID}_NAME
|
|
|
|
r.ForEachZone(func(z *Zone) bool { |
|
|
|
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME") |
|
|
|
m.writeEnvZone(w, r, z) |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ZONE{zoneID}_GW
|
|
|
|
func (m *Env) writeEnvZone(w io.Writer, r *Region, z *Zone) { |
|
|
|
gateways, _ := z.GatewayIDs() |
|
|
|
zonePrefix := fmt.Sprintf("REGION%v_ZONE%v", r.ID, z.ID) |
|
|
|
m.writeEnvVar(w, genEnvInts(gateways), "ZONE%v_%s", zoneID, "GW") |
|
|
|
monPrefix := zonePrefix + "_MON" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// REGION{regionID}_ZONE{zoneID}
|
|
|
|
|
|
|
|
m.writeEnvVar(w, genEnvZoneNodes(z), zonePrefix) |
|
|
|
|
|
|
|
|
|
|
|
// ZONE{zoneID}_REGION
|
|
|
|
// REGION{regionID}_ZONE{zoneID}_NAME
|
|
|
|
m.writeEnvVar(w, genEnvZoneRegion(z), "ZONE%v_%s", zoneID, "REGION") |
|
|
|
m.writeEnvVar(w, z.Name, zonePrefix+"_NAME") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// REGION{regionID}_ZONE{zoneID}_GW
|
|
|
|
|
|
|
|
gateways, _ := z.GatewayIDs() |
|
|
|
|
|
|
|
m.writeEnvVar(w, genEnvInts(gateways), zonePrefix+"_GW") |
|
|
|
|
|
|
|
|
|
|
|
// Ceph
|
|
|
|
// Ceph
|
|
|
|
monitors := z.GetCephMonitors() |
|
|
|
monitors := z.GetCephMonitors() |
|
|
|
// MON{zoneID}_NAME
|
|
|
|
// REGION{regionID}_MON{zone_ID}
|
|
|
|
m.writeEnvVar(w, genEnvZoneCephMonNames(monitors), "MON%v_%s", zoneID, "NAME") |
|
|
|
m.writeEnvVar(w, genEnvZoneCephMonNames(monitors), monPrefix) |
|
|
|
// MON{zoneID}_IP
|
|
|
|
// REGION{regionID}_MON{zone_ID}_IP
|
|
|
|
m.writeEnvVar(w, genEnvZoneCephMonIPs(monitors), "MON%v_%s", zoneID, "IP") |
|
|
|
m.writeEnvVar(w, genEnvZoneCephMonIPs(monitors), monPrefix+"_IP") |
|
|
|
// MON{zoneID}_ID
|
|
|
|
// REGION{regionID}_MON{zone_ID}_ID
|
|
|
|
m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), "MON%v_%s", zoneID, "ID") |
|
|
|
m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), monPrefix+"_ID") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
@ -131,14 +185,15 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func genEnvInts[T ~int | ~uint](values []T) string { |
|
|
|
func genEnvInts[T core.Signed](values []T) string { |
|
|
|
var buf bytes.Buffer |
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
|
|
for _, v := range values { |
|
|
|
for _, v := range values { |
|
|
|
if buf.Len() > 0 { |
|
|
|
if buf.Len() > 0 { |
|
|
|
_, _ = buf.WriteRune(' ') |
|
|
|
_, _ = buf.WriteRune(' ') |
|
|
|
} |
|
|
|
} |
|
|
|
_, _ = buf.WriteString(fmt.Sprintf("%v", v)) |
|
|
|
|
|
|
|
|
|
|
|
_, _ = buf.WriteString(fmt.Sprintf("%v", int64(v))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return buf.String() |
|
|
|
return buf.String() |
|
|
@ -162,13 +217,6 @@ func genEnvZoneNodes(z *Zone) string { |
|
|
|
return "" |
|
|
|
return "" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func genEnvZoneRegion(z *Zone) string { |
|
|
|
|
|
|
|
if z != nil && z.region != nil { |
|
|
|
|
|
|
|
return z.region.Name |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
@ -209,3 +257,14 @@ func genEnvZoneCephMonIDs(m Machines) string { |
|
|
|
}) |
|
|
|
}) |
|
|
|
return buf.String() |
|
|
|
return buf.String() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func cmpOrdered[T core.Ordered](a, b T) int { |
|
|
|
|
|
|
|
switch { |
|
|
|
|
|
|
|
case a == b: |
|
|
|
|
|
|
|
return 0 |
|
|
|
|
|
|
|
case a < b: |
|
|
|
|
|
|
|
return -1 |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return 1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|