|
|
|
@ -16,6 +16,7 @@ type Region struct {
|
|
|
|
|
zones []*Zone |
|
|
|
|
|
|
|
|
|
Name string |
|
|
|
|
Cluster *string `json:",omitempty" yaml:",omitempty"` |
|
|
|
|
Regions []string `json:",omitempty" yaml:",omitempty"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -106,6 +107,26 @@ func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Cluster) setRegionClusterToken(name string, token string) error { |
|
|
|
|
for i := range m.Regions { |
|
|
|
|
r := &m.Regions[i] |
|
|
|
|
|
|
|
|
|
if r.Name == name { |
|
|
|
|
// found
|
|
|
|
|
r.Cluster = &token |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// new
|
|
|
|
|
m.Regions = append(m.Regions, Region{ |
|
|
|
|
m: m, |
|
|
|
|
Name: name, |
|
|
|
|
Cluster: &token, |
|
|
|
|
}) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Cluster) appendRegionRegions(name string, subs ...string) { |
|
|
|
|
for i := range m.Regions { |
|
|
|
|
r := &m.Regions[i] |
|
|
|
@ -178,20 +199,32 @@ func (z *Zone) SyncRegions() error {
|
|
|
|
|
err := z.syncZoneRegions() |
|
|
|
|
if err == nil { |
|
|
|
|
z.ForEachMachine(func(p *Machine) bool { |
|
|
|
|
err = z.syncMachineRegions(p) |
|
|
|
|
return err != nil |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (*Zone) syncMachineRegions(p *Machine) error { |
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
if p.IsActive() { |
|
|
|
|
err = p.RemoveFile("region") |
|
|
|
|
} else { |
|
|
|
|
err = p.WriteStringFile("none\n", "region") |
|
|
|
|
} |
|
|
|
|
return err != nil |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
if err == nil { |
|
|
|
|
err = p.RemoveFile(RegionClusterTokenFileName) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (z *Zone) syncZoneRegions() error { |
|
|
|
|
name := filepath.Join(z.Name, "regions") |
|
|
|
|
name := filepath.Join(z.Name, ZoneRegionsFileName) |
|
|
|
|
|
|
|
|
|
if len(z.Regions) > 0 { |
|
|
|
|
var buf bytes.Buffer |
|
|
|
@ -210,9 +243,25 @@ func (z *Zone) syncZoneRegions() error {
|
|
|
|
|
// 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 err := r.syncRegionsFile(); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return r.syncClusterFile() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *Region) mkdir() error { |
|
|
|
|
return r.m.MkdirAll(r.Name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *Region) syncRegionsFile() error { |
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
if len(r.Regions) > 0 { |
|
|
|
|
name := filepath.Join(r.Name, ZoneRegionsFileName) |
|
|
|
|
|
|
|
|
|
if len(r.Regions) == 0 { |
|
|
|
|
err = r.m.RemoveFile(name) |
|
|
|
|
} else if err = r.mkdir(); err == nil { |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
for _, s := range r.Regions { |
|
|
|
@ -220,12 +269,29 @@ func (r *Region) SyncRegions() error {
|
|
|
|
|
_, _ = buf.WriteRune('\n') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := r.m.MkdirAll(r.Name); err != nil { |
|
|
|
|
err = r.m.WriteStringFile(buf.String(), name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return r.m.WriteStringFile(buf.String(), name) |
|
|
|
|
func (r *Region) syncClusterFile() error { |
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
name := filepath.Join(r.Name, RegionClusterTokenFileName) |
|
|
|
|
|
|
|
|
|
if r.Cluster == nil { |
|
|
|
|
err = r.m.RemoveFile(name) |
|
|
|
|
} else if err = r.mkdir(); err == nil { |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
_, _ = buf.WriteString(*r.Cluster) |
|
|
|
|
if buf.Len() > 0 { |
|
|
|
|
_, _ = buf.WriteRune('\n') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return r.m.RemoveFile(name) |
|
|
|
|
err = r.m.WriteStringFile(buf.String(), name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|