From 1ea1ab4ac4953a6dbc701ba0754ce0ea60b92010 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 21 Aug 2023 17:51:44 +0000 Subject: [PATCH] zones: add initial m/ scanner Signed-off-by: Alejandro Mery --- pkg/zones/machine.go | 40 ++++++++++++++++++++++++++++++++++ pkg/zones/scan.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ pkg/zones/zones.go | 46 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 pkg/zones/machine.go create mode 100644 pkg/zones/scan.go create mode 100644 pkg/zones/zones.go diff --git a/pkg/zones/machine.go b/pkg/zones/machine.go new file mode 100644 index 0000000..acefd7d --- /dev/null +++ b/pkg/zones/machine.go @@ -0,0 +1,40 @@ +package zones + +import ( + "strconv" + "sync" +) + +// A Machine is a machine on a Zone +type Machine struct { + mu sync.Mutex + + zone *Zone + id int + Name string +} + +func (m *Machine) String() string { + return m.Name +} + +// ID return the index within the [Zone] associated to this [Machine] +func (m *Machine) ID() int { + m.mu.Lock() + defer m.mu.Unlock() + + if m.id == 0 { + zoneName := m.zone.Name + + s := m.Name[len(zoneName)+1:] + + id, err := strconv.ParseInt(s, 10, 8) + if err != nil { + panic(err) + } + + m.id = int(id) + } + + return m.id +} diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go new file mode 100644 index 0000000..cbe1ff3 --- /dev/null +++ b/pkg/zones/scan.go @@ -0,0 +1,51 @@ +package zones + +import ( + "io/fs" +) + +func (m *Zones) scan() error { + // each directory is a zone + entries, err := fs.ReadDir(m.dir, ".") + if err != nil { + return err + } + + for _, e := range entries { + if e.IsDir() { + z := &Zone{ + zones: m, + Name: e.Name(), + } + + if err := z.scan(); err != nil { + return err + } + + m.Zones = append(m.Zones, z) + } + } + + return nil +} + +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{ + zone: z, + Name: e.Name(), + } + + z.Machines = append(z.Machines, m) + } + } + + return nil +} diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go new file mode 100644 index 0000000..a46485b --- /dev/null +++ b/pkg/zones/zones.go @@ -0,0 +1,46 @@ +// Package zones contains information about the cluster +package zones + +import ( + "io/fs" + "os" +) + +// Zone represents one zone in a cluster +type Zone struct { + zones *Zones + + ID int + Name string + + Machines []*Machine `toml:"machines"` +} + +func (z *Zone) String() string { + return z.Name +} + +// Zones represents all zones in a cluster +type Zones struct { + dir fs.FS + + Zones []*Zone `toml:"zones"` +} + +// NewFS builds a [Zones] tree using the given directory +func NewFS(dir fs.FS) (*Zones, error) { + z := &Zones{ + dir: dir, + } + + if err := z.scan(); err != nil { + return nil, err + } + + return z, nil +} + +// New builds a [Zones] tree using the given directory +func New(dir string) (*Zones, error) { + return NewFS(os.DirFS(dir)) +}