From 030af1bcfb7dad880a9fdf4286432fc36d1bf251 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 25 Oct 2024 19:16:44 -0500 Subject: [PATCH] check for duplicate disk names Signed-off-by: Jeff Carr --- Makefile | 2 +- addDroplet.go | 1 - argv.go | 15 +++++----- libvirtxml.go | 5 +++- main.go | 38 ++++++++++++++++---------- structs.go | 1 + validate.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 113 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 53b9eec..a3541c8 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ all: ./virtigo --help xml-add: - ./virtigo --libvirt *.xml + ./virtigo --libvirt ~/libvirt/*.xml --xml-ignore-disk start-all-droplets: curl --silent http://localhost:8080/start?start=git.wit.org diff --git a/addDroplet.go b/addDroplet.go index d572ea8..9027526 100644 --- a/addDroplet.go +++ b/addDroplet.go @@ -180,7 +180,6 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) ([]*pb.Event, error) return alle, errors.New("updateDisk() failed") } - if alle == nil { log.Info("libvirt xml import worked. nothing changed", domcfg.Name) return alle, nil diff --git a/argv.go b/argv.go index 530c7a0..f610d18 100644 --- a/argv.go +++ b/argv.go @@ -11,13 +11,14 @@ import "go.wit.com/log" var argv args type args struct { - Xml []string `arg:"--libvirt" help:"import qemu xml files: --libvirt /etc/libvirt/qemu/*.xml"` - IgnoreCpu bool `arg:"--xml-ignore-cpu" default:"true" help:"ignore non-standard libvirt xml cpus"` - IgnoreBr bool `arg:"--xml-ignore-net" default:"true" help:"ignore network bridge name changes"` - Save bool `arg:"--save" default:"false" help:"save protobuf config after import"` - Config string `arg:"env:VIRTIGO_HOME" help:"defaults to ~/.config/virtigo/"` - Port int `arg:"--port" default:"8080" help:"allow droplet events via http"` - Daemon bool `arg:"--daemon" help:"run in daemon mode"` + Xml []string `arg:"--libvirt" help:"import qemu xml files: --libvirt /etc/libvirt/qemu/*.xml"` + IgnoreCpu bool `arg:"--xml-ignore-cpu" default:"true" help:"ignore non-standard libvirt xml cpus"` + IgnoreBr bool `arg:"--xml-ignore-net" default:"true" help:"ignore network bridge name changes"` + IgnDisk bool `arg:"--xml-ignore-disk" default:"false" help:"ignore duplicate disk names"` + Save bool `arg:"--save" default:"false" help:"save protobuf config after import"` + Config string `arg:"env:VIRTIGO_HOME" help:"defaults to ~/.config/virtigo/"` + Port int `arg:"--port" default:"8080" help:"allow droplet events via http"` + Daemon bool `arg:"--daemon" help:"run in daemon mode"` } // Uptime bool `arg:"--uptime" default:"true" help:"allow uptime checks for things like Kuma"` diff --git a/libvirtxml.go b/libvirtxml.go index b40c3a1..38a90a1 100644 --- a/libvirtxml.go +++ b/libvirtxml.go @@ -376,7 +376,10 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) (string, error) { // this is probably something about what kind of OS you might be running // todo: get this directly from the disk image if domcfg.Metadata != nil { - fmt.Printf("Not saving Domain.Metadata: %+v\n", domcfg.Metadata) + var s string + s = domcfg.Metadata.XML + log.Info("Not saving Domain.Metadata.XML:", s) + log.Info("todo: get this from disk image") domcfg.Metadata = nil } diff --git a/main.go b/main.go index a93af29..ae4babe 100644 --- a/main.go +++ b/main.go @@ -53,10 +53,17 @@ func main() { os.Exit(-1) } + var newEvents []*pb.Event + // sanity check the droplets checkDroplets(false) + newe := checkDiskFilenames() + for _, e := range newe { + newEvents = append(newEvents, e) + } + checkUniqueFilenames() + - var newEvents []*pb.Event for _, filename := range argv.Xml { domcfg, err := readXml(filename) if err != nil { @@ -81,21 +88,22 @@ func main() { newEvents = append(newEvents, e) } } + for i, e := range newEvents { + log.Info(i, "Event:", e.Droplet, e.FieldName, "orig:", e.OrigVal, "new:", e.NewVal) + me.changed = true + } + if me.changed { + if argv.Save { + writeConfigFile() + writeConfigFileDroplets() + log.Info("XML changes saved in protobuf config") + os.Exit(0) + } else { + log.Info("Not saving changes (use --save to save)") + os.Exit(0) + } + } if len(argv.Xml) != 0 { - for i, e := range newEvents { - log.Info(i, "Event:", e.Droplet, e.FieldName, "orig:", e.OrigVal, "new:", e.NewVal) - } - if me.changed { - if argv.Save { - writeConfigFile() - writeConfigFileDroplets() - log.Info("XML changes saved in protobuf config") - os.Exit(0) - } else { - log.Info("Not saving changes (use --save to save)") - os.Exit(0) - } - } log.Info("No XML changes found") os.Exit(0) } diff --git a/structs.go b/structs.go index 35e36f0..45f303e 100644 --- a/structs.go +++ b/structs.go @@ -30,6 +30,7 @@ type virtigoT struct { killcount int unstable time.Time // the last time the cluster was incorrect changed bool + dirs []string // all the paths too search for a qcow image } // the stuff that is needed for a hypervisor diff --git a/validate.go b/validate.go index 2faaf38..2f7de9b 100644 --- a/validate.go +++ b/validate.go @@ -15,9 +15,11 @@ package main import ( "os" + "path/filepath" "github.com/google/uuid" + pb "go.wit.com/lib/protobuf/virtbuf" "go.wit.com/log" ) @@ -34,6 +36,80 @@ func checkUniqueMac(mac string) bool { return true } +func addClusterFilepath(dir string) { + var found bool = false + for _, d := range me.dirs { + if d == dir { + // found dir + found = true + break + } + } + if !found { + me.dirs = append(me.dirs, dir) + } +} + +func checkUniqueFilenames() bool { + var ok bool = true + var disks map[string]string + disks = make(map[string]string) + + for _, d := range me.cluster.Droplets { + for _, disk := range d.Disks { + filename := disk.Filename + if _, ok := disks[filename]; ok { + /* + if argv.IgnDisk { + log.Info("ignore dup disk", filename, disks[filename], d.Hostname) + } else { + } + */ + log.Info("file", filename, "on droplet", disks[filename]) + log.Info("file", filename, "on droplet", d.Hostname) + log.Info("duplicate disk names (--xml-ignore-disk to ignore)") + ok = false + } + disks[filename] = d.Hostname + } + } + if ok { + log.Println("validated okay: no duplicate disk images") + } + return ok +} + +func checkDiskFilenames() []*pb.Event { + var alle []*pb.Event + + for _, d := range me.cluster.Droplets { + for _, disk := range d.Disks { + filename := disk.Filename + filebase := filepath.Base(filename) + dir := filepath.Dir(filename) + if disk.Filename != filebase { + // update filename + e := NewChangeEvent(d, "Disk.Filename", disk.Filename, filebase) + alle = append(alle, e) + disk.Filename = filebase + } + if dir == "." { + continue + } + if dir == "" { + continue + } + if disk.Filepath != dir { + // update filename + e := NewChangeEvent(d, "Disk.Filepath", disk.Filepath, dir) + alle = append(alle, e) + disk.Filepath = dir + } + } + } + return alle +} + func checkDroplets(dump bool) bool { // uuid map to check for duplicates var umap map[string]string