cmd/dhcp reçoit quatre paramètres en clair — interface, state, socket, conf — sur le modèle de dnsmasq. Il ne compose aucun chemin, ne découpe aucun nom composite et ignore le netns dans lequel il tourne : seul le wrapper en a besoin, pour y entrer. La boucle de lecture UDP est écrite à la main plutôt que confiée à server4.Serve, qui lance une goroutine par datagramme sans borne et ne pose aucun recover. Elle traite en ligne, réutilise un tampon de 1500 octets et pose un recover par datagramme. NewIPv4UDPConn est conservé pour le SO_BROADCAST et le bind à l'interface. Une réponse destinée à un client sans adresse part en broadcast. dhcp.run_dir n'est pas une clé de configuration : le wrapper code /run/two/dhcp en dur et le Go utilise dhcpapi.DefaultRunDir, comme dhcp.DefaultConfDir pour dnsmasq. Un test lit le script et vérifie que les deux s'accordent. Le défaut dhcp.backend reste dnsmasq : un agent.yml de 0.1.0 se comporte comme avant. deploy.sh et le pipeline publient le binaire, l'unit et le script. La boucle est testée sur une vraie socket UDP en loopback. Neuf mutations, dont une qui a révélé que le recover n'était couvert par rien. Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
package configuration
|
|
|
|
import (
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
Database struct {
|
|
Path string `mapstructure:"path"`
|
|
} `mapstructure:"database"`
|
|
Api struct {
|
|
Address string `mapstructure:"address"`
|
|
Port int `mapstructure:"port"`
|
|
} `mapstructure:"api"`
|
|
Prometheus struct {
|
|
Address string `mapstructure:"address"`
|
|
Port int `mapstructure:"port"`
|
|
} `mapstructure:"prometheus"`
|
|
Worker struct {
|
|
Count int `mapstructure:"count"`
|
|
BufferSize int `mapstructure:"buffer_size"`
|
|
} `mapstructure:"worker"`
|
|
Dispatcher struct {
|
|
TimeoutSeconds int `mapstructure:"timeout_seconds"`
|
|
PollSeconds int `mapstructure:"poll_seconds"`
|
|
} `mapstructure:"dispatcher"`
|
|
Logger struct {
|
|
Level string `mapstructure:"level"`
|
|
Debug bool `mapstructure:"debug"`
|
|
} `mapstructure:"logger"`
|
|
Metadata struct {
|
|
RunDir string `mapstructure:"run_dir"`
|
|
} `mapstructure:"metadata"`
|
|
DHCP struct {
|
|
Backend string `mapstructure:"backend"`
|
|
} `mapstructure:"dhcp"`
|
|
Admin struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
Address string `mapstructure:"address"`
|
|
Port int `mapstructure:"port"`
|
|
} `mapstructure:"admin"`
|
|
QEMU struct {
|
|
OVMFCodePath string `mapstructure:"ovmf_code_path"`
|
|
OVMFVarsTemplate string `mapstructure:"ovmf_vars_template"`
|
|
UEFIVarsDir string `mapstructure:"uefi_vars_dir"`
|
|
SerialDir string `mapstructure:"serial_dir"`
|
|
MonitorDir string `mapstructure:"monitor_dir"`
|
|
QMPDir string `mapstructure:"qmp_dir"`
|
|
} `mapstructure:"qemu"`
|
|
Watchdog struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
IntervalSeconds int `mapstructure:"interval_seconds"`
|
|
} `mapstructure:"watchdog"`
|
|
DefaultInterface string `mapstructure:"default_interface"`
|
|
Interfaces map[string]string `mapstructure:"interfaces"`
|
|
}
|
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
v := viper.New()
|
|
v.SetConfigFile(path)
|
|
v.SetConfigType("yaml")
|
|
|
|
v.SetDefault("database.path", "/var/lib/two/data/")
|
|
v.SetDefault("api.address", "")
|
|
v.SetDefault("api.port", 8080)
|
|
v.SetDefault("prometheus.address", "")
|
|
v.SetDefault("prometheus.port", 9090)
|
|
v.SetDefault("worker.count", 4)
|
|
v.SetDefault("worker.buffer_size", 100)
|
|
v.SetDefault("dispatcher.timeout_seconds", 300)
|
|
v.SetDefault("dispatcher.poll_seconds", 2)
|
|
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
|
v.SetDefault("dhcp.backend", BackendDnsmasq)
|
|
v.SetDefault("qemu.ovmf_code_path", "/usr/share/OVMF/OVMF_CODE.fd")
|
|
v.SetDefault("qemu.ovmf_vars_template", "/usr/share/OVMF/OVMF_VARS.fd")
|
|
v.SetDefault("qemu.uefi_vars_dir", "/run/two/vms/uefi")
|
|
v.SetDefault("qemu.serial_dir", "/run/two/vms/serial")
|
|
v.SetDefault("qemu.monitor_dir", "/run/two/vms/monitor")
|
|
v.SetDefault("qemu.qmp_dir", "/run/two/vms/qmp")
|
|
v.SetDefault("watchdog.enabled", false)
|
|
v.SetDefault("watchdog.interval_seconds", 60)
|
|
v.SetDefault("admin.enabled", false)
|
|
v.SetDefault("admin.address", "127.0.0.1")
|
|
v.SetDefault("admin.port", 9091)
|
|
v.SetDefault("default_interface", "br-000000")
|
|
v.SetDefault("logger.level", "info")
|
|
v.SetDefault("logger.debug", false)
|
|
|
|
v.ReadInConfig()
|
|
|
|
var cfg Config
|
|
if err := v.Unmarshal(&cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|