54 lines
961 B
Go
54 lines
961 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
"libvirt.org/go/libvirt"
|
||
|
)
|
||
|
// GuestVM holds a handle is used to communicate to Domains
|
||
|
type GuestVM struct {
|
||
|
Name string
|
||
|
ID uint
|
||
|
BackupXML string
|
||
|
|
||
|
close chan struct{}
|
||
|
closeErr chan error
|
||
|
dom *libvirt.Domain
|
||
|
}
|
||
|
|
||
|
// GetGuest loads guest information by name and returns it
|
||
|
func GetGuest(name string, conn *libvirt.Connect) (*GuestVM, error) {
|
||
|
g := &GuestVM{
|
||
|
Name: name,
|
||
|
close: make(chan struct{}),
|
||
|
closeErr: make(chan error),
|
||
|
}
|
||
|
|
||
|
var err error
|
||
|
|
||
|
if g.dom, err = conn.LookupDomainByName(name); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if g.ID, err = g.dom.GetID(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if g.BackupXML, err = g.dom.BackupGetXMLDesc(0); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
go func() {
|
||
|
defer close(g.closeErr)
|
||
|
<-g.close
|
||
|
g.closeErr <- g.dom.Free()
|
||
|
}()
|
||
|
|
||
|
return g, nil
|
||
|
}
|
||
|
|
||
|
// Close closes an open connection
|
||
|
func (g *GuestVM) Close() error {
|
||
|
close(g.close)
|
||
|
return <-g.closeErr
|
||
|
}
|