|
|
|
@ -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] |
|
|
|
@ -210,9 +231,25 @@ func (z *Zone) syncZoneRegions() error {
|
|
|
|
|
// SyncRegions writes to the file system the regions covered
|
|
|
|
|
// by this meta-region
|
|
|
|
|
func (r *Region) SyncRegions() error { |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
name := filepath.Join(r.Name, ZoneRegionsFileName) |
|
|
|
|
|
|
|
|
|
if len(r.Regions) > 0 { |
|
|
|
|
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 +257,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') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = r.m.WriteStringFile(buf.String(), name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return r.m.RemoveFile(name) |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|