Files
ilkerandClaude Opus 5.5 67b31380bd provider: a setting may be file:/path, as well as env:NAME
A site connected from the portal keeps its secret in a file under the
portal's own directory: written without root, and read again on every
discovery without restarting anything, which a variable in the portal's
environment (/etc/hpcod/env) needs both. OpenStack and OpenNebula read
file: themselves; Proxmox's token and bare metal's BMC password come
through Setting, which now does too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 16:40:13 +01:00

94 lines
2.8 KiB
Go

package provider
import (
"fmt"
"os"
"strconv"
"strings"
)
// SiteConfig describes one backing cloud.
//
// Settings is intentionally untyped: each provider needs different fields
// (Proxmox wants an API token, OpenStack wants an application credential and a
// region) and the loader should not have to know about any of them.
type SiteConfig struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Kind string `yaml:"kind"`
Settings map[string]string `yaml:"settings"`
}
// Setting returns a required setting, resolving "env:NAME" and
// "file:/path" indirection so that secrets live in the environment or a
// file of their own rather than in the config file. A file is what a site
// connected from the portal keeps its secret in: the portal can write one
// under its own directory, and read it again without being restarted,
// which a variable in its environment would need.
func (c SiteConfig) Setting(key string) (string, error) {
v, ok := c.Settings[key]
if !ok || v == "" {
return "", fmt.Errorf("site %q: missing required setting %q", c.ID, key)
}
if env, found := strings.CutPrefix(v, "env:"); found {
got := os.Getenv(env)
if got == "" {
return "", fmt.Errorf("site %q: setting %q refers to $%s, which is unset",
c.ID, key, env)
}
return got, nil
}
if path, found := strings.CutPrefix(v, "file:"); found {
raw, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("site %q: setting %q: %w", c.ID, key, err)
}
got := strings.TrimRight(string(raw), "\r\n")
if got == "" {
return "", fmt.Errorf("site %q: setting %q refers to %s, which is empty", c.ID, key, path)
}
return got, nil
}
return v, nil
}
// SettingOr returns a setting or a default, with the same env: indirection.
func (c SiteConfig) SettingOr(key, def string) string {
if _, ok := c.Settings[key]; !ok {
return def
}
v, err := c.Setting(key)
if err != nil {
return def
}
return v
}
// BoolSetting reports a boolean setting, defaulting when absent or unparsable.
func (c SiteConfig) BoolSetting(key string, def bool) bool {
v := strings.ToLower(strings.TrimSpace(c.SettingOr(key, "")))
switch v {
case "true", "yes", "1":
return true
case "false", "no", "0":
return false
default:
return def
}
}
// FloatSetting reads a numeric setting. Unlike BoolSetting it reports a bad
// value rather than falling back, because a ratio silently read as its
// default changes how much hardware a site appears to have.
func (c SiteConfig) FloatSetting(key string, def float64) (float64, error) {
raw := strings.TrimSpace(c.SettingOr(key, ""))
if raw == "" {
return def, nil
}
f, err := strconv.ParseFloat(raw, 64)
if err != nil {
return 0, fmt.Errorf("site %q: setting %q must be a number, got %q", c.ID, key, raw)
}
return f, nil
}