There was one Terraform environment for every site. Placement could put a cluster on a second site and the build would go to the first - wrong for two Proxmox clusters, impossible for two kinds of backend. The runner now builds from <template>/<site>, the site the cluster's record names, and refuses a site with no environment. Setup writes the environment there and moves one written flat by an older setup into its site's directory: renamed, never regenerated, since secrets.auto.tfvars holds a token written once; a flat file that differs from the site's copy stops setup instead of overwriting either. `make deploy-portal` replaces the binary without running setup, so the runner still builds from a flat template - for the one configured site only (LegacySite), never for a site it was not written for. Tests: per-site selection, the legacy rule, site ids that would name another directory, the move (twice, and with a conflicting copy), and setup's output handed to the real runner. The migration and contract tests were checked to fail with the code under test disabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
99 lines
2.4 KiB
Go
99 lines
2.4 KiB
Go
// Package inventory loads the site catalogue and discovers all of it.
|
|
package inventory
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/model"
|
|
"github.com/ilkermanap/hpcod/internal/provider"
|
|
)
|
|
|
|
// Catalogue is the configured set of backing clouds.
|
|
type Catalogue struct {
|
|
Sites []provider.SiteConfig `yaml:"sites"`
|
|
}
|
|
|
|
// Load reads a catalogue and instantiates every provider. Configuration errors
|
|
// are fatal here - a site we cannot even construct is an operator mistake, not
|
|
// a transient condition.
|
|
func Load(path string) ([]provider.Provider, error) {
|
|
cat, err := ReadCatalogue(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(cat.Sites) == 0 {
|
|
return nil, fmt.Errorf("%s: no sites defined", path)
|
|
}
|
|
|
|
seen := map[string]bool{}
|
|
out := make([]provider.Provider, 0, len(cat.Sites))
|
|
for _, sc := range cat.Sites {
|
|
if sc.ID == "" {
|
|
return nil, fmt.Errorf("%s: a site is missing its id", path)
|
|
}
|
|
if seen[sc.ID] {
|
|
return nil, fmt.Errorf("%s: duplicate site id %q", path, sc.ID)
|
|
}
|
|
seen[sc.ID] = true
|
|
|
|
p, err := provider.New(sc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, p)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ReadCatalogue parses a catalogue without building any provider, for a
|
|
// caller that needs only what is configured - the site ids - and not the
|
|
// credentials to reach it.
|
|
func ReadCatalogue(path string) (Catalogue, error) {
|
|
var cat Catalogue
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return cat, err
|
|
}
|
|
if err := yaml.Unmarshal(raw, &cat); err != nil {
|
|
return cat, fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
return cat, nil
|
|
}
|
|
|
|
// DiscoverAll queries every site concurrently. One slow or dead site must not
|
|
// hold up the rest, so each gets its own timeout and failures are recorded in
|
|
// the returned Site rather than aborting the sweep.
|
|
func DiscoverAll(ctx context.Context, provs []provider.Provider, perSite time.Duration) []model.Site {
|
|
out := make([]model.Site, len(provs))
|
|
var wg sync.WaitGroup
|
|
|
|
for i, p := range provs {
|
|
wg.Add(1)
|
|
go func(i int, p provider.Provider) {
|
|
defer wg.Done()
|
|
c, cancel := context.WithTimeout(ctx, perSite)
|
|
defer cancel()
|
|
|
|
s, err := p.Discover(c)
|
|
if err != nil {
|
|
s = model.Site{
|
|
ID: p.ID(), Kind: p.Kind(),
|
|
LastSeen: time.Now().UTC(), Error: err.Error(),
|
|
}
|
|
}
|
|
out[i] = s
|
|
}(i, p)
|
|
}
|
|
wg.Wait()
|
|
|
|
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
|
return out
|
|
}
|