package validators import ( "fmt" "os" "os/exec" "strings" "jagacloud/node-agent/pkg/compute/libvirt" "jagacloud/node-agent/pkg/config" "jagacloud/node-agent/pkg/containers/lxc" ) // CheckBridge returns nil if the bridge exists on the host. func CheckBridge(name string) error { if name == "" { return fmt.Errorf("bridge name required") } if os.Getenv("JAGACLOUD_SKIP_BRIDGE_CHECK") == "1" { return nil } if err := exec.Command("bash", "-lc", fmt.Sprintf("ip link show %s", name)).Run(); err != nil { return fmt.Errorf("bridge %s not found", name) } return nil } // CheckBridgeSet validates all bridges in NIC specs. func CheckBridgeSet(nics []libvirt.NICSpec) error { for _, nic := range nics { if err := CheckBridge(nic.Bridge); err != nil { return err } } return nil } // CheckBridgeSetCT validates bridges for LXC NICs. func CheckBridgeSetCT(nics []lxc.NICSpec) error { for _, nic := range nics { if err := CheckBridge(nic.Bridge); err != nil { return err } } return nil } // CheckStoragePool is a stub; implement LVM/ZFS/dir verification as needed. func CheckStoragePool(name string) error { if name == "" { return fmt.Errorf("storage pool name required") } // TODO: query configured pools return nil } // CheckStoragePoolsVM ensures all disks reference known pools from config. func CheckStoragePoolsVM(disks []libvirt.DiskSpec, cfg config.Config) error { for _, d := range disks { if d.Pool == "" { continue } if !poolExists(cfg, d.Pool) { return fmt.Errorf("storage pool %s not configured", d.Pool) } } return nil } // CheckStoragePoolsCT ensures rootfs pool exists. func CheckStoragePoolsCT(spec lxc.Spec, cfg config.Config) error { if spec.RootfsPool != "" && !poolExists(cfg, spec.RootfsPool) { return fmt.Errorf("storage pool %s not configured", spec.RootfsPool) } return nil } // CheckStoragePoolsRuntime queries libvirt to see if pool exists; fallback to config. func CheckStoragePoolsRuntime(pools []string, cfg config.Config) error { if len(pools) == 0 { return nil } for _, p := range pools { if p == "" { continue } if poolExists(cfg, p) { continue } // Try libvirt cmd := exec.Command("bash", "-lc", "virsh -q pool-list --name") out, err := cmd.Output() if err != nil { return fmt.Errorf("pool %s not found and virsh failed: %v", p, err) } found := false for _, line := range strings.Split(string(out), "\n") { if strings.TrimSpace(line) == p { found = true break } } if !found { return fmt.Errorf("storage pool %s not found", p) } } return nil } func poolExists(cfg config.Config, name string) bool { for _, p := range cfg.StoragePools { if p.Name == name { return true } } return false }