You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
3.5 KiB
218 lines
3.5 KiB
12 months ago
|
package cluster
|
||
1 year ago
|
|
||
|
import (
|
||
|
"io/fs"
|
||
1 year ago
|
"sort"
|
||
12 months ago
|
|
||
|
"darvaza.org/core"
|
||
1 year ago
|
)
|
||
|
|
||
12 months ago
|
func (m *Cluster) scan(opts *ScanOptions) error {
|
||
12 months ago
|
for _, fn := range []func(*ScanOptions) error{
|
||
1 year ago
|
m.scanDirectory,
|
||
|
m.scanMachines,
|
||
|
m.scanZoneIDs,
|
||
1 year ago
|
m.scanSort,
|
||
1 year ago
|
m.scanGateways,
|
||
12 months ago
|
m.scanCephMonitors,
|
||
1 year ago
|
} {
|
||
12 months ago
|
if err := fn(opts); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
12 months ago
|
func (m *Cluster) scanDirectory(_ *ScanOptions) error {
|
||
1 year ago
|
// each directory is a zone
|
||
|
entries, err := fs.ReadDir(m.dir, ".")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, e := range entries {
|
||
|
if e.IsDir() {
|
||
12 months ago
|
z, err := m.newZone(e.Name())
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return core.Wrap(err, e.Name())
|
||
12 months ago
|
case z.Machines.Len() == 0:
|
||
|
z.warn(nil).
|
||
|
WithField("zone", z.Name).
|
||
|
Print("empty")
|
||
|
default:
|
||
12 months ago
|
m.Zones = append(m.Zones, z)
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
return nil
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
func (m *Cluster) newZone(name string) (*Zone, error) {
|
||
12 months ago
|
z := &Zone{
|
||
|
zones: m,
|
||
|
logger: m,
|
||
|
Name: name,
|
||
|
}
|
||
|
|
||
12 months ago
|
z.debug().
|
||
|
WithField("zone", z.Name).
|
||
|
Print("found")
|
||
|
|
||
12 months ago
|
if err := z.scan(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return z, nil
|
||
|
}
|
||
|
|
||
12 months ago
|
func (m *Cluster) scanMachines(opts *ScanOptions) error {
|
||
1 year ago
|
var err error
|
||
1 year ago
|
m.ForEachMachine(func(p *Machine) bool {
|
||
12 months ago
|
err = p.scan(opts)
|
||
1 year ago
|
return err != nil
|
||
1 year ago
|
})
|
||
|
return err
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
func (m *Cluster) scanZoneIDs(_ *ScanOptions) error {
|
||
1 year ago
|
var hasMissing bool
|
||
|
var lastZoneID int
|
||
|
|
||
|
m.ForEachZone(func(z *Zone) bool {
|
||
|
switch {
|
||
|
case z.ID == 0:
|
||
|
hasMissing = true
|
||
|
case z.ID > lastZoneID:
|
||
|
lastZoneID = z.ID
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
})
|
||
|
|
||
|
if hasMissing {
|
||
|
next := lastZoneID + 1
|
||
|
|
||
|
m.ForEachZone(func(z *Zone) bool {
|
||
|
if z.ID == 0 {
|
||
|
z.ID, next = next, next+1
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
12 months ago
|
func (m *Cluster) scanSort(_ *ScanOptions) error {
|
||
1 year ago
|
sort.SliceStable(m.Zones, func(i, j int) bool {
|
||
|
id1 := m.Zones[i].ID
|
||
|
id2 := m.Zones[j].ID
|
||
|
return id1 < id2
|
||
|
})
|
||
|
|
||
|
m.ForEachZone(func(z *Zone) bool {
|
||
1 year ago
|
sort.Sort(z)
|
||
1 year ago
|
return false
|
||
|
})
|
||
|
|
||
|
m.ForEachMachine(func(p *Machine) bool {
|
||
1 year ago
|
sort.SliceStable(p.Rings, func(i, j int) bool {
|
||
|
ri1 := p.Rings[i]
|
||
|
ri2 := p.Rings[j]
|
||
1 year ago
|
|
||
|
return ri1.Ring < ri2.Ring
|
||
|
})
|
||
|
|
||
|
return false
|
||
|
})
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
12 months ago
|
func (m *Cluster) scanGateways(_ *ScanOptions) error {
|
||
1 year ago
|
var err error
|
||
|
|
||
|
m.ForEachZone(func(z *Zone) bool {
|
||
|
_, _, err = z.GetGateway()
|
||
|
return err != nil
|
||
|
})
|
||
|
return err
|
||
|
}
|
||
|
|
||
1 year ago
|
func (z *Zone) scan() error {
|
||
|
// each directory is a machine
|
||
|
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, e := range entries {
|
||
|
if e.IsDir() {
|
||
|
m := &Machine{
|
||
12 months ago
|
zone: z,
|
||
|
logger: z,
|
||
|
Name: e.Name(),
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
m.debug().
|
||
|
WithField("node", m.Name).
|
||
|
WithField("zone", z.Name).
|
||
|
Print("found")
|
||
|
|
||
1 year ago
|
if err := m.init(); err != nil {
|
||
12 months ago
|
m.error(err).
|
||
|
WithField("node", m.Name).
|
||
|
WithField("zone", z.Name).
|
||
|
Print()
|
||
|
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
|
||
1 year ago
|
z.Machines = append(z.Machines, m)
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
return nil
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
|
// GetGateway returns the first gateway found, if none
|
||
|
// files will be created to enable the first [Machine] to
|
||
|
// be one
|
||
|
func (z *Zone) GetGateway() (*Machine, bool, error) {
|
||
|
var first *Machine
|
||
|
var gateway *Machine
|
||
|
|
||
|
z.zones.ForEachMachine(func(p *Machine) bool {
|
||
|
switch {
|
||
|
case p.IsGateway():
|
||
|
// found
|
||
|
gateway = p
|
||
|
case first == nil:
|
||
|
// remember
|
||
|
first = p
|
||
|
default:
|
||
|
// keep looking
|
||
|
}
|
||
|
|
||
|
return gateway != nil
|
||
|
})
|
||
|
|
||
|
switch {
|
||
|
case gateway != nil:
|
||
|
// found one
|
||
|
return gateway, false, nil
|
||
|
case first != nil:
|
||
|
// make one
|
||
|
if err := first.SetGateway(true); err != nil {
|
||
|
return first, false, err
|
||
|
}
|
||
|
return first, true, nil
|
||
|
default:
|
||
|
// Zone without nodes?
|
||
|
panic("unreachable")
|
||
|
}
|
||
|
}
|