|
|
|
@ -1,5 +1,10 @@
|
|
|
|
|
package cluster |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"bytes" |
|
|
|
|
"path/filepath" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
_ MachineIterator = (*Region)(nil) |
|
|
|
|
_ ZoneIterator = (*Region)(nil) |
|
|
|
@ -68,7 +73,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|
|
|
|
|
|
|
|
|
// bind first level regions and their zones
|
|
|
|
|
for name, zones := range regions { |
|
|
|
|
m.syncRegions(name, zones...) |
|
|
|
|
m.setRegionZones(name, zones...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// and combine zones to produce larger regions
|
|
|
|
@ -81,8 +86,10 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Cluster) syncRegions(name string, zones ...*Zone) { |
|
|
|
|
for _, r := range m.Regions { |
|
|
|
|
func (m *Cluster) setRegionZones(name string, zones ...*Zone) { |
|
|
|
|
for i := range m.Regions { |
|
|
|
|
r := &m.Regions[i] |
|
|
|
|
|
|
|
|
|
if r.Name == name { |
|
|
|
|
// found
|
|
|
|
|
r.m = m |
|
|
|
@ -99,6 +106,38 @@ func (m *Cluster) syncRegions(name string, zones ...*Zone) {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Cluster) appendRegionRegions(name string, subs ...string) { |
|
|
|
|
for i := range m.Regions { |
|
|
|
|
r := &m.Regions[i] |
|
|
|
|
|
|
|
|
|
if name == r.Name { |
|
|
|
|
// found
|
|
|
|
|
r.Regions = append(r.Regions, subs...) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// new
|
|
|
|
|
m.Regions = append(m.Regions, Region{ |
|
|
|
|
Name: name, |
|
|
|
|
Regions: subs, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (z *Zone) appendRegions(regions ...string) error { |
|
|
|
|
for _, s := range regions { |
|
|
|
|
// TODO: validate
|
|
|
|
|
z.debug(). |
|
|
|
|
WithField("zone", z.Name). |
|
|
|
|
WithField("region", s). |
|
|
|
|
Print("attached") |
|
|
|
|
|
|
|
|
|
z.Regions = append(z.Regions, s) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Cluster) finishRegion(r *Region) { |
|
|
|
|
if r.m != nil { |
|
|
|
|
// ready
|
|
|
|
@ -132,3 +171,61 @@ func (m *Cluster) getRegion(name string) (*Region, bool) {
|
|
|
|
|
|
|
|
|
|
return nil, false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SyncRegions writes to the file system the regions this [Zone]
|
|
|
|
|
// belongs to.
|
|
|
|
|
func (z *Zone) SyncRegions() error { |
|
|
|
|
err := z.syncZoneRegions() |
|
|
|
|
if err == nil { |
|
|
|
|
z.ForEachMachine(func(p *Machine) bool { |
|
|
|
|
if p.IsActive() { |
|
|
|
|
err = p.RemoveFile("region") |
|
|
|
|
} else { |
|
|
|
|
err = p.WriteStringFile("none\n", "region") |
|
|
|
|
} |
|
|
|
|
return err != nil |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (z *Zone) syncZoneRegions() error { |
|
|
|
|
name := filepath.Join(z.Name, "regions") |
|
|
|
|
|
|
|
|
|
if len(z.Regions) > 0 { |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
for _, s := range z.Regions { |
|
|
|
|
_, _ = buf.WriteString(s) |
|
|
|
|
_, _ = buf.WriteRune('\n') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return z.zones.WriteStringFile(buf.String(), name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return z.zones.RemoveFile(name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SyncRegions writes to the file system the regions covered
|
|
|
|
|
// by this meta-region
|
|
|
|
|
func (r *Region) SyncRegions() error { |
|
|
|
|
name := filepath.Join(r.Name, "regions") |
|
|
|
|
|
|
|
|
|
if len(r.Regions) > 0 { |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
for _, s := range r.Regions { |
|
|
|
|
_, _ = buf.WriteString(s) |
|
|
|
|
_, _ = buf.WriteRune('\n') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := r.m.MkdirAll(r.Name); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return r.m.WriteStringFile(buf.String(), name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return r.m.RemoveFile(name) |
|
|
|
|
} |
|
|
|
|