compiles and runs. logic wrong

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-23 02:56:55 -05:00
parent 3f2cbcb57f
commit 09b635219a
6 changed files with 109 additions and 9 deletions

View File

@ -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

53
addDroplet.go Normal file
View File

@ -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")
}

View File

@ -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

35
main.go
View File

@ -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)

View File

@ -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

21
xml.go
View File

@ -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)