Files
Provisioning/internal/pxe/supervisor.go
T
josh df78f881bb
build-and-push / test (push) Successful in 36s
build-and-push / build-and-push (push) Successful in 1m14s
Remove dhcp-ignore filter to debug proxy DHCP non-response
dnsmasq sees PXE requests but never responds. Remove the known-host
filter to determine if tag matching is the issue or if the problem
is elsewhere in the proxy DHCP flow.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-13 22:35:29 -04:00

207 lines
4.4 KiB
Go

package pxe
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"provisioning/internal/model"
)
type SupervisorConfig struct {
Enabled bool
Interface string
Subnet string
RuntimeDir string
TFTPRoot string
DnsmasqBin string
PublicURL string
}
type Supervisor struct {
cfg SupervisorConfig
cmd *exec.Cmd
cancel context.CancelFunc
}
func NewSupervisor(cfg SupervisorConfig) *Supervisor {
return &Supervisor{cfg: cfg}
}
func (s *Supervisor) Start(ctx context.Context, hosts []model.Host) error {
if !s.cfg.Enabled {
log.Printf("pxe: dnsmasq disabled")
return nil
}
if err := os.MkdirAll(s.cfg.RuntimeDir, 0o755); err != nil {
return fmt.Errorf("pxe: create runtime dir: %w", err)
}
if err := os.MkdirAll(s.cfg.TFTPRoot, 0o755); err != nil {
return fmt.Errorf("pxe: create tftp root: %w", err)
}
s.seedIPXE()
if err := s.writeConfig(hosts); err != nil {
return err
}
return s.startProcess(ctx)
}
func (s *Supervisor) Reload(hosts []model.Host) error {
if !s.cfg.Enabled {
return nil
}
if err := s.writeConfig(hosts); err != nil {
return err
}
if s.cmd != nil && s.cmd.Process != nil {
return signalReload(s.cmd.Process)
}
return nil
}
func (s *Supervisor) Shutdown() error {
if s.cancel != nil {
s.cancel()
}
if s.cmd != nil && s.cmd.Process != nil {
return s.cmd.Process.Kill()
}
return nil
}
func (s *Supervisor) writeConfig(hosts []model.Host) error {
confPath := filepath.Join(s.cfg.RuntimeDir, "dnsmasq.conf")
hostsPath := filepath.Join(s.cfg.RuntimeDir, "dhcp-hostsfile")
var lines []string
for _, h := range hosts {
lines = append(lines, h.MAC+",set:known")
}
if err := os.WriteFile(hostsPath, []byte(strings.Join(lines, "\n")+"\n"), 0o644); err != nil {
return fmt.Errorf("pxe: write dhcp-hostsfile: %w", err)
}
subnet := s.cfg.Subnet
if i := strings.Index(subnet, "/"); i != -1 {
subnet = subnet[:i]
}
conf := dnsmasqConf{
Interface: s.cfg.Interface,
Subnet: subnet,
TFTPRoot: s.cfg.TFTPRoot,
HostsFile: hostsPath,
PublicURL: s.cfg.PublicURL,
RuntimeDir: s.cfg.RuntimeDir,
}
f, err := os.Create(confPath)
if err != nil {
return fmt.Errorf("pxe: create dnsmasq.conf: %w", err)
}
defer f.Close()
if err := dnsmasqTmpl.Execute(f, conf); err != nil {
return fmt.Errorf("pxe: render dnsmasq.conf: %w", err)
}
return nil
}
func (s *Supervisor) startProcess(ctx context.Context) error {
confPath := filepath.Join(s.cfg.RuntimeDir, "dnsmasq.conf")
procCtx, cancel := context.WithCancel(ctx)
s.cancel = cancel
s.cmd = exec.CommandContext(procCtx, s.cfg.DnsmasqBin, "--keep-in-foreground", "--conf-file="+confPath)
s.cmd.Stdout = os.Stdout
s.cmd.Stderr = os.Stderr
if err := s.cmd.Start(); err != nil {
cancel()
return fmt.Errorf("pxe: start dnsmasq: %w", err)
}
go func() {
if err := s.cmd.Wait(); err != nil {
select {
case <-procCtx.Done():
default:
log.Printf("pxe: dnsmasq exited: %v", err)
}
}
}()
log.Printf("pxe: dnsmasq started (pid %d)", s.cmd.Process.Pid)
return nil
}
var ipxeFiles = []struct{ src, dst string }{
{"/usr/lib/ipxe/undionly.kpxe", "undionly.kpxe"},
{"/usr/lib/ipxe/ipxe.efi", "ipxe.efi"},
}
func (s *Supervisor) seedIPXE() {
for _, f := range ipxeFiles {
dst := filepath.Join(s.cfg.TFTPRoot, f.dst)
if _, err := os.Stat(dst); err == nil {
continue
}
if err := copyFile(f.src, dst); err != nil {
log.Printf("pxe: copy %s → %s: %v", f.src, dst, err)
} else {
log.Printf("pxe: seeded %s", f.dst)
}
}
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
type dnsmasqConf struct {
Interface string
Subnet string
TFTPRoot string
HostsFile string
PublicURL string
RuntimeDir string
}
var dnsmasqTmpl = template.Must(template.New("dnsmasq.conf").Parse(`
port=0
interface={{.Interface}}
bind-interfaces
dhcp-range={{.Subnet}},proxy
dhcp-hostsfile={{.HostsFile}}
enable-tftp
tftp-root={{.TFTPRoot}}
# Legacy BIOS
dhcp-match=set:bios,option:client-arch,0
dhcp-boot=tag:bios,undionly.kpxe
# UEFI
dhcp-match=set:efi64,option:client-arch,7
dhcp-boot=tag:efi64,ipxe.efi
# iPXE user-class: chain to HTTP script
dhcp-match=set:ipxe,option:user-class,iPXE
dhcp-boot=tag:ipxe,{{.PublicURL}}/ipxe/${mac:hexhyp}
log-dhcp
log-facility={{.RuntimeDir}}/dnsmasq.log
`))