Browse Source

cluster: introduce SyncRegions() to write regions file

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/39/head
Alejandro Mery 1 year ago
parent
commit
0db3e18227
  1. 63
      pkg/cluster/regions.go
  2. 22
      pkg/cluster/sync.go

63
pkg/cluster/regions.go

@ -1,5 +1,10 @@
package cluster package cluster
import (
"bytes"
"path/filepath"
)
var ( var (
_ MachineIterator = (*Region)(nil) _ MachineIterator = (*Region)(nil)
_ ZoneIterator = (*Region)(nil) _ ZoneIterator = (*Region)(nil)
@ -134,3 +139,61 @@ func (m *Cluster) getRegion(name string) (*Region, bool) {
return nil, false 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)
}

22
pkg/cluster/sync.go

@ -6,6 +6,7 @@ func (m *Cluster) SyncAll() error {
m.SyncMkdirAll, m.SyncMkdirAll,
m.SyncAllWireguard, m.SyncAllWireguard,
m.SyncAllCeph, m.SyncAllCeph,
m.SyncAllRegions,
m.WriteHosts, m.WriteHosts,
} { } {
if err := fn(); err != nil { if err := fn(); err != nil {
@ -58,3 +59,24 @@ func (m *Cluster) SyncAllCeph() error {
return m.WriteCephConfig(cfg) return m.WriteCephConfig(cfg)
} }
// SyncAllRegions rewrites all region data
func (m *Cluster) SyncAllRegions() error {
var err error
m.ForEachZone(func(z *Zone) bool {
err := z.SyncRegions()
return err != nil
})
if err != nil {
return err
}
m.ForEachRegion(func(r *Region) bool {
err = r.SyncRegions()
return err != nil
})
return err
}

Loading…
Cancel
Save