2022-10-15 14:28:03 +00:00
|
|
|
package bootstrap
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
const (
|
|
|
|
hostsDirPath = "hosts"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NebulaHost describes the nebula configuration of a Host which is relevant for
|
|
|
|
// other hosts to know.
|
2021-04-20 21:31:37 +00:00
|
|
|
type NebulaHost struct {
|
|
|
|
IP string `yaml:"ip"`
|
|
|
|
PublicAddr string `yaml:"public_addr,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
// GarageHost describes a single garage instance in the GarageHost.
|
2021-04-20 21:31:37 +00:00
|
|
|
type GarageHostInstance struct {
|
2022-10-28 22:09:18 +00:00
|
|
|
ID string `yaml:"id"`
|
|
|
|
RPCPort int `yaml:"rpc_port"`
|
|
|
|
S3APIPort int `yaml:"s3_api_port"`
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
// GarageHost describes the garage configuration of a Host which is relevant for
|
|
|
|
// other hosts to know.
|
2021-04-20 21:31:37 +00:00
|
|
|
type GarageHost struct {
|
|
|
|
Instances []GarageHostInstance `yaml:"instances"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host consolidates all information about a single host from the bootstrap
|
|
|
|
// file.
|
|
|
|
type Host struct {
|
2022-10-15 16:41:07 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Nebula NebulaHost `yaml:"nebula"`
|
|
|
|
Garage *GarageHost `yaml:"garage,omitempty"`
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
func loadHosts(bootstrapFS fs.FS) (map[string]Host, error) {
|
|
|
|
|
|
|
|
hosts := map[string]Host{}
|
|
|
|
|
|
|
|
readAsYaml := func(into interface{}, path string) error {
|
|
|
|
b, err := fs.ReadFile(bootstrapFS, path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading file from fs: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return yaml.Unmarshal(b, into)
|
|
|
|
}
|
|
|
|
|
|
|
|
globPath := filepath.Join(hostsDirPath, "*.yml")
|
|
|
|
|
|
|
|
hostPaths, err := fs.Glob(bootstrapFS, globPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("listing host files at %q in fs: %w", globPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hostPath := range hostPaths {
|
|
|
|
|
|
|
|
hostName := filepath.Base(hostPath)
|
|
|
|
hostName = strings.TrimSuffix(hostName, filepath.Ext(hostName))
|
|
|
|
|
|
|
|
var host Host
|
|
|
|
if err := readAsYaml(&host, hostPath); err != nil {
|
|
|
|
return nil, fmt.Errorf("reading %q as yaml: %w", hostPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts[hostName] = host
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hosts) == 0 {
|
|
|
|
return nil, fmt.Errorf("failed to load any hosts from fs")
|
|
|
|
}
|
|
|
|
|
|
|
|
return hosts, nil
|
|
|
|
}
|