From a345a813d887b02d9c6eaea9d4645cf5c30f0573 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 31 Oct 2024 06:50:17 -0500 Subject: [PATCH] rename to virtigolib Signed-off-by: Jeff Carr --- http.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- hypervisior.go | 32 -------------------------------- main.go | 7 +++---- start.go | 8 ++++---- structs.go | 3 +++ 5 files changed, 59 insertions(+), 41 deletions(-) diff --git a/http.go b/http.go index 8463d4a..4685980 100644 --- a/http.go +++ b/http.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io/ioutil" "net/http" @@ -8,8 +9,10 @@ import ( "strings" "go.wit.com/lib/gui/shell" + "go.wit.com/lib/virtigolib" pb "go.wit.com/lib/protobuf/virtbuf" "go.wit.com/log" + "libvirt.org/go/libvirtxml" ) // remove '?' part and trailing '/' @@ -53,11 +56,37 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if tmp == "/vms" { - s := pollHypervisor(hv) + s := pollHypervisor(me.hv) fmt.Fprint(w, s) return } + if tmp == "/import" { + domname := r.URL.Query().Get("import") + fmt.Fprint(w, "import domain:", domname) + + xmldoc, err := virshDumpXML(w, r, domname) + + domcfg := &libvirtxml.Domain{} + err = domcfg.Unmarshal(xmldoc) + if err != nil { + fmt.Fprintln(w, "domain.Unmarshal XML failed") + fmt.Fprintln(w, "error =", err) + return + } + // importDomain(w, r, domcfg) + // importDomain(w http.ResponseWriter, r *http.Request, dom *libvirtxml.Domain) { + d, _, err := virtigolib.ImportXML(domcfg) + if err != nil { + fmt.Fprintln(w, "ImportXML failed for", domname, err) + return + } + fmt.Fprintln(w, "ImportXML worked for", domname) + fmt.Fprintln(w, "should send the protobuf to virtigo here", domname) + d.Current.FullXml = xmldoc + return + } + if tmp == "/cluster" { log.Info("/cluster jcarr actually doing START") fmt.Fprintln(w, "/cluster jcarr actually doing START") @@ -201,3 +230,22 @@ func startHTTP() { log.Println("Error starting server:", err) } } + +func virshDumpXML(w http.ResponseWriter, r *http.Request, name string) (string, error) { + cmd := []string{"virsh", "dumpxml", "--security-info", name} + + fmt.Fprintln(w, "virsh dumpxml", name) + log.Warn("cmd :", cmd) + fmt.Fprintln(w, "cmd: ", cmd) + err, ok, output := shell.RunCmd("/home/", cmd) + shell.Run(cmd) + if !ok { + fmt.Fprintln(w, "START FAILED", me.Hostname) + fmt.Fprintln(w, "error =", err) + result := fmt.Sprintln("virsh dumpxml failed:", err) + return output, errors.New(result) + } + fmt.Fprintln(w, "START OK", me.Hostname) + fmt.Fprintln(w, output) + return output, nil +} diff --git a/hypervisior.go b/hypervisior.go index 2085e98..11f99cf 100644 --- a/hypervisior.go +++ b/hypervisior.go @@ -76,35 +76,3 @@ func pollHypervisor(hv *hypervisor.Hypervisor) string { } return out } - -/* -func displayBlockDevices(domain *qemu.Domain) { - // var []blks string - blockDevices, err := domain.BlockDevices() - if err != nil { - log.Fatalf("Error getting blockDevices: %v\n", blockDevices) - } - fmt.Printf("\n[ BlockDevices ]\n") - fmt.Printf("========================================================================\n") - fmt.Printf("%20s %8s %30s\n", "Device", "Driver", "File") - fmt.Printf("========================================================================\n") - for _, blockDevice := range blockDevices { - fmt.Printf("%20s %8s %30s\n", - blockDevice.Device, blockDevice.Inserted.Driver, blockDevice.Inserted.File) - } -} - -func displayPCIDevices(domain *qemu.Domain) { - pciDevices, err := domain.PCIDevices() - if err != nil { - log.Fatalf("Error getting PCIDevices: %v\n", pciDevices) - } - fmt.Printf("\n[ PCIDevices ]\n") - fmt.Printf("======================================\n") - fmt.Printf("%10s %20s\n", "[ID]", "[Description]") - fmt.Printf("======================================\n") - for _, pciDevice := range pciDevices { - fmt.Printf("[%10s] [%20s]\n", pciDevice.QdevID, pciDevice.ClassInfo.Desc) - } -} -*/ diff --git a/main.go b/main.go index 0c4f04a..3ef9aab 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,6 @@ import ( ) var Version string -var hv *hypervisor.Hypervisor //go:embed resources/* var resources embed.FS @@ -46,15 +45,15 @@ func main() { // fmt.Printf("\nConnecting to %s://%s\n", *network, *address) newConn := func() (net.Conn, error) { + // return net.DialTimeout("tcp", "hyper02:16514", *timeout) // return net.DialTimeout(*network, *address, *timeout) return net.DialTimeout("unix", "/var/run/libvirt/libvirt-sock", 2*time.Second) - // return net.DialTimeout("tcp", "farm02:16514", *timeout) } driver := hypervisor.NewRPCDriver(newConn) - hv = hypervisor.New(driver) + me.hv = hypervisor.New(driver) - pollHypervisor(hv) + pollHypervisor(me.hv) startHTTP() } diff --git a/start.go b/start.go index 02f5fef..19f83ce 100644 --- a/start.go +++ b/start.go @@ -6,7 +6,7 @@ import ( "errors" pb "go.wit.com/lib/protobuf/virtbuf" - "go.wit.com/lib/virtigoxml" + "go.wit.com/lib/virtigolib" "go.wit.com/log" "libvirt.org/go/libvirtxml" ) @@ -25,13 +25,13 @@ func newStart(start string, d *pb.Droplet) error { log.Info("spice port was not set. spice disabled") } else { mergeXML(domcfg, "spice") - if err := virtigoxml.SetSpicePort(d, domcfg); err != nil { + if err := virtigolib.SetSpicePort(d, domcfg); err != nil { return err } } mergeXML(domcfg, "qcow") - return virtigoxml.GenerateDropletXml(me.dirs, d, domcfg, start) + return virtigolib.GenerateDropletXml(me.dirs, d, domcfg, start) } func mergeXML(domcfg *libvirtxml.Domain, filename string) error { @@ -42,5 +42,5 @@ func mergeXML(domcfg *libvirtxml.Domain, filename string) error { return err } - return virtigoxml.AddDefaultXml(domcfg, string(pfile)) + return virtigolib.AddDefaultXml(domcfg, string(pfile)) } diff --git a/structs.go b/structs.go index b209c9b..8c8caed 100644 --- a/structs.go +++ b/structs.go @@ -1,9 +1,12 @@ package main +import "github.com/digitalocean/go-qemu/hypervisor" + var me *Virtigod // this app's variables type Virtigod struct { Hostname string dirs []string + hv *hypervisor.Hypervisor }