From 2ab607246e19b79b11c19fe6ca17d4015966a0ef Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 31 Oct 2024 06:46:16 -0500 Subject: [PATCH] change name to virtigolib Signed-off-by: Jeff Carr --- addDroplet.go | 27 +++++++++++---------------- disks.go | 2 +- dumpNonStandardXML.go | 2 +- importXML.go | 42 ++++++++++++++++++++++++++++++++++++++++++ libvirtxml.go | 2 +- spice.go | 2 +- start.go | 2 +- 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 importXML.go diff --git a/addDroplet.go b/addDroplet.go index 04795da..382ee6e 100644 --- a/addDroplet.go +++ b/addDroplet.go @@ -1,6 +1,6 @@ // Copyright 2024 WIT.COM Inc Licensed GPL 3.0 -package virtigoxml +package virtigolib import ( "encoding/xml" @@ -27,7 +27,7 @@ func AddDomainDroplet(cluster *pb.Cluster, domcfg *libvirtxml.Domain) (*pb.Dropl if d == nil { d = cluster.AddDroplet(domcfg.UUID, domcfg.Name, 2, 2*1024*1024) d.StartState = pb.DropletState_OFF - d.CurrentState = pb.DropletState_UNKNOWN + d.Current.State = pb.DropletState_UNKNOWN // if the domcfg doesn't have a uuid, make a new one here if d.Uuid == "" { @@ -36,7 +36,7 @@ func AddDomainDroplet(cluster *pb.Cluster, domcfg *libvirtxml.Domain) (*pb.Dropl } } - alle, err = updateDroplet(cluster, d, domcfg) + alle, err = updateDroplet(d, domcfg) if err != nil { log.Info("updateDroplet() failed for", d.Hostname) return d, alle, errors.New("update failed for " + domcfg.Name) @@ -85,7 +85,7 @@ func findDomain(c *pb.Cluster, domcfg *libvirtxml.Domain) (*pb.Droplet, error) { return found, nil } -func updateDroplet(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error) { +func updateDroplet(d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error) { var alle []*pb.Event if d == nil { @@ -108,10 +108,10 @@ func updateDroplet(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain if (domcfg.OS != nil) && (domcfg.OS.Type != nil) { // OS Type: &{Arch:x86_64 Machine:pc-i440fx-5.2 Type:hvm} t := domcfg.OS.Type - if d.QemuArch != t.Arch { - e := d.NewChangeEvent("Droplet.QemuArch", d.QemuArch, t.Arch) + if d.PreferredArch != t.Arch { + e := d.NewChangeEvent("Droplet.QemuArch", d.PreferredArch, t.Arch) alle = append(alle, e) - d.QemuArch = t.Arch + d.PreferredArch = t.Arch } if d.QemuMachine != t.Machine { e := d.NewChangeEvent("Droplet.QemuMachine", d.QemuMachine, t.Machine) @@ -156,7 +156,7 @@ func updateDroplet(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain return alle, errors.New("not kvm") } - nete, err := updateNetwork(cluster, d, domcfg) + nete, err := updateNetwork(d, domcfg) if err != nil { log.Info("updateNetwork() failed", err) return alle, err @@ -166,7 +166,7 @@ func updateDroplet(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain alle = append(alle, e) } - nete, err = updateDisk(cluster, d, domcfg) + nete, err = updateDisk(d, domcfg) if err != nil { return alle, err } @@ -180,11 +180,6 @@ func updateDroplet(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain return alle, nil } log.Info("libvirt xml import worked. droplet changed", domcfg.Name) - - // append each change event - for _, e := range alle { - cluster.E.Events = append(cluster.E.Events, e) - } return alle, nil } @@ -219,7 +214,7 @@ func updateMemory(d *pb.Droplet, domcfg *libvirtxml.Domain) (*pb.Event, error) { return e, nil } -func updateNetwork(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error) { +func updateNetwork(d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error) { var allEvents []*pb.Event if (d == nil) || (domcfg == nil) { return nil, errors.New("domcfg == nil") @@ -343,7 +338,7 @@ func updateNetwork(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain */ // returns false if something went wrong -func updateDisk(cluster *pb.Cluster, d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error) { +func updateDisk(d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error) { var alle []*pb.Event if (d == nil) || (domcfg == nil) { diff --git a/disks.go b/disks.go index d122e8c..5956907 100644 --- a/disks.go +++ b/disks.go @@ -1,4 +1,4 @@ -package virtigoxml +package virtigolib /* makes a droplet hard disk record diff --git a/dumpNonStandardXML.go b/dumpNonStandardXML.go index 2b2bdb9..e92d44b 100644 --- a/dumpNonStandardXML.go +++ b/dumpNonStandardXML.go @@ -8,7 +8,7 @@ // there might be something interesting in a VM // 'standard' here means what I think is standard -package virtigoxml +package virtigolib import ( "encoding/xml" diff --git a/importXML.go b/importXML.go new file mode 100644 index 0000000..e93102b --- /dev/null +++ b/importXML.go @@ -0,0 +1,42 @@ +// Copyright 2024 WIT.COM Inc Licensed GPL 3.0 + +package virtigolib + +import ( + "errors" + "fmt" + + pb "go.wit.com/lib/protobuf/virtbuf" + "go.wit.com/log" + "libvirt.org/go/libvirtxml" +) + +// import a libvirt xml domain +func ImportXML(domcfg *libvirtxml.Domain) (*pb.Droplet, []*pb.Event, error) { + var alle []*pb.Event + var d *pb.Droplet + if domcfg == nil { + return nil, alle, errors.New("domcfg == nil") + } + + d = new(pb.Droplet) + d.Hostname = domcfg.Name + d.Uuid = domcfg.UUID + d.StartState = pb.DropletState_OFF + d.Current.State = pb.DropletState_UNKNOWN + + alle, err := updateDroplet(d, domcfg) + if err != nil { + log.Info("updateDroplet() failed for", d.Hostname) + return d, alle, errors.New("update failed for " + domcfg.Name) + } + log.Info("added new droplet", domcfg.Name, domcfg.UUID) + s, err := DumpNonStandardXML(domcfg) + if err != nil { + reason := s + "\n" + reason = fmt.Sprintln("DumpNonStandardXML() on", domcfg.Name, "failed for", err) + log.Info(reason) + return d, alle, errors.New(reason) + } + return d, alle, nil +} diff --git a/libvirtxml.go b/libvirtxml.go index 6633b6f..5576710 100644 --- a/libvirtxml.go +++ b/libvirtxml.go @@ -1,6 +1,6 @@ // Copyright 2024 WIT.COM Inc Licensed GPL 3.0 -package virtigoxml +package virtigolib import ( "encoding/xml" diff --git a/spice.go b/spice.go index b5334b3..8443539 100644 --- a/spice.go +++ b/spice.go @@ -1,6 +1,6 @@ // Copyright 2024 WIT.COM Inc Licensed GPL 3.0 -package virtigoxml +package virtigolib import ( "errors" diff --git a/start.go b/start.go index ab24473..98a5aa7 100644 --- a/start.go +++ b/start.go @@ -1,6 +1,6 @@ // Copyright 2024 WIT.COM Inc Licensed GPL 3.0 -package virtigoxml +package virtigolib import ( "errors"