diff --git a/Makefile b/Makefile index ae2d873..b24ceee 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,10 @@ all: ./virtigo --version ./virtigo +xml-add: + ./virtigo --add-xml /home/nfs3/xml/*.xml + # ./virtigo --add-xml /etc/libvirt/qemu/*xml + start-all-droplets: curl --silent http://localhost:8080/start?start=git.wit.org curl --silent http://localhost:8080/start?start=go.wit.com diff --git a/addDroplet.go b/addDroplet.go new file mode 100644 index 0000000..215f4a4 --- /dev/null +++ b/addDroplet.go @@ -0,0 +1,53 @@ +// Copyright 2024 WIT.COM Inc Licensed GPL 3.0 + +package main + +import ( + "errors" + "fmt" + + "libvirt.org/go/libvirtxml" +) + +func addDroplet(domcfg *libvirtxml.Domain) (*DropletT, error) { + var found *DropletT + if domcfg == nil { + return nil, errors.New("domcfg == nil") + } +// fmt.Printf("Virt type %s\n", domcfg.Type) +// fmt.Printf("Virt name %s\n", domcfg.Name) +// fmt.Printf("Virt UUID %s\n", domcfg.UUID) +// fmt.Printf("Virt Memory %d %s\n", domcfg.Memory.Value, domcfg.Memory.Unit) + + for _, d := range me.droplets { + if d.pb.Hostname == domcfg.Name { + if d.pb.Uuid == domcfg.UUID { + fmt.Println("FOUND NAME", domcfg.Name, domcfg.UUID) + fmt.Println("CHANGED UUID", d.pb.Uuid, domcfg.UUID) + } else { + d.pb.Uuid = domcfg.UUID + me.changed = true + } + if found == nil { + found = d + } else { + fmt.Println("FOUND TWICE", d.pb.Uuid, domcfg.Name, domcfg.UUID) + return d, errors.New("Found Twice") + } + + } + if d.pb.Uuid == domcfg.UUID { + if d.pb.Hostname == domcfg.Name { + fmt.Println("FOUND UUID WITH MATCHING NAME", domcfg.Name, domcfg.UUID) + } else { + fmt.Println("FOUND UUID WITH MIS-MATCHED NAME", domcfg.Name, domcfg.UUID) + return d, errors.New("UUID with mis-matched names") + } + } + } + + // test add some ethernet devices + macs := getMacs(domcfg) + fmt.Printf("Virt mac addr:%s\n", macs) + return nil, errors.New("not found") +} diff --git a/configfiles.go b/configfiles.go index df56e83..5c2b0f6 100644 --- a/configfiles.go +++ b/configfiles.go @@ -42,13 +42,13 @@ func readConfigFile(filename string) error { pfile, err := os.ReadFile(fullname) if err != nil { log.Info("open config file :", err) - return ErrorNoFile + return err } err = me.cluster.UnmarshalJSON(pfile) if err != nil { log.Info("read json failed", err) os.Exit(-1) - return ErrorParseJSON + return err } // initialize each hypervisor diff --git a/main.go b/main.go index 48ab2c6..4456793 100644 --- a/main.go +++ b/main.go @@ -35,17 +35,40 @@ func main() { log.DaemonMode(true) } + // set defaults + me.unstable = time.Now() // initialize the grid as unstable + me.delay = 5 * time.Second // how often to poll the hypervisors + me.changed = false + cfgfile() + var ok bool = true for _, filename := range argv.Xml { log.Info("add xml file", filename) + domcfg, err := readXml(filename) + if err != nil { + log.Info("error:", filename, err) + ok = false + continue + } + d, err := addDroplet(domcfg) + if err != nil { + ok = false + } + if d == nil { + log.Info("addDroplet() returned nil") + ok = false + } + } + if me.changed { + writeConfigFile() + writeConfigFileDroplets() + os.Exit(0) + } + if !ok { + log.Info("adding xml files failed") + os.Exit(-1) } - - // initialize the grid as unstable - me.unstable = time.Now() - - // how often to poll the hypervisors - me.delay = 5 * time.Second /* log.Info("command line hypervisors:", argv.Hosts) diff --git a/structs.go b/structs.go index 0b08b25..50f2af1 100644 --- a/structs.go +++ b/structs.go @@ -28,6 +28,7 @@ type virtigoT struct { delay time.Duration // how often to poll the hypervisors killcount int unstable time.Time // the last time the cluster was incorrect + changed bool } // the stuff that is needed for a hypervisor diff --git a/xml.go b/xml.go index 1f0da87..08d9f51 100644 --- a/xml.go +++ b/xml.go @@ -71,7 +71,26 @@ func addDefaults(d *libvirtxml.Domain, filename string) { } } -func (d *DropletT) readXml(filename string) error { +func readXml(filename string) (*libvirtxml.Domain, error) { + log.Info("parse xml file:", filename) + + pfile, err := os.ReadFile(filename) + if err != nil { + log.Println("ERROR:", err) + return nil, err + } + + domcfg := &libvirtxml.Domain{} + + err = domcfg.Unmarshal(string(pfile)) + if err != nil { + log.Info("Marshal failed on file", filename, err) + return nil, ErrorParseXML + } + return domcfg, nil +} + +func (d *DropletT) mergeXml(filename string) error { log.Info("parse xml file:", filename) pfile, err := os.ReadFile(filename)