work on adding droplets from libvirt xml files
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
09b635219a
commit
35a2a37940
|
@ -9,22 +9,34 @@ import (
|
|||
"libvirt.org/go/libvirtxml"
|
||||
)
|
||||
|
||||
func addDroplet(domcfg *libvirtxml.Domain) (*DropletT, error) {
|
||||
func addDomainDroplet(domcfg *libvirtxml.Domain) (*DropletT, error) {
|
||||
if domcfg == nil {
|
||||
return nil, errors.New("domcfg == nil")
|
||||
}
|
||||
|
||||
d, _ := findDomain(domcfg)
|
||||
if d != nil {
|
||||
return d, errors.New(d.pb.Hostname + " droplet exists. need to update instead")
|
||||
}
|
||||
|
||||
// test add some ethernet devices
|
||||
macs := getMacs(domcfg)
|
||||
fmt.Printf("Virt mac addr:%s\n", macs)
|
||||
|
||||
fmt.Println("ADD FAILED", domcfg.Name, domcfg.UUID)
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
|
||||
func findDomain(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)
|
||||
if d.pb.Uuid != domcfg.UUID {
|
||||
fmt.Println("CHANGED UUID", d.pb.Uuid, domcfg.UUID)
|
||||
} else {
|
||||
d.pb.Uuid = domcfg.UUID
|
||||
me.changed = true
|
||||
}
|
||||
|
@ -37,17 +49,78 @@ func addDroplet(domcfg *libvirtxml.Domain) (*DropletT, error) {
|
|||
|
||||
}
|
||||
if d.pb.Uuid == domcfg.UUID {
|
||||
if d.pb.Hostname == domcfg.Name {
|
||||
fmt.Println("FOUND UUID WITH MATCHING NAME", domcfg.Name, domcfg.UUID)
|
||||
} else {
|
||||
if d.pb.Hostname != domcfg.Name {
|
||||
fmt.Println("FOUND UUID WITH MIS-MATCHED NAME", domcfg.Name, domcfg.UUID)
|
||||
return d, errors.New("UUID with mis-matched names")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
||||
var ok bool = true
|
||||
|
||||
if d == nil {
|
||||
return false
|
||||
}
|
||||
if domcfg == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// check memory
|
||||
if domcfg.Memory.Unit == "KiB" {
|
||||
var m int64
|
||||
m = int64(domcfg.Memory.Value * 1024)
|
||||
if d.pb.Memory != m {
|
||||
d.pb.Memory = m
|
||||
me.changed = true
|
||||
fmt.Printf("Memory changed %d, %d %s\n", d.pb.Memory, domcfg.Memory.Value, domcfg.Memory.Unit)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit)
|
||||
ok = false
|
||||
}
|
||||
|
||||
// check cpus
|
||||
if d.pb.Cpus != int64(domcfg.VCPU.Value) {
|
||||
fmt.Printf("cpus changed. VCPU = %+v\n", domcfg.VCPU)
|
||||
d.pb.Cpus = int64(domcfg.VCPU.Value)
|
||||
me.changed = true
|
||||
}
|
||||
|
||||
// check type
|
||||
if domcfg.Type != "kvm" {
|
||||
fmt.Printf("not kvm. Virt type == %s\n", domcfg.Type)
|
||||
ok = false
|
||||
}
|
||||
|
||||
// test add some ethernet devices
|
||||
macs := getMacs(domcfg)
|
||||
fmt.Printf("Virt mac addr:%s\n", macs)
|
||||
return nil, errors.New("not found")
|
||||
|
||||
// fmt.Println("UPDATE FAILED", domcfg.Name, domcfg.UUID)
|
||||
return ok
|
||||
}
|
||||
|
||||
func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
||||
if (d == nil) || (domcfg == nil) {
|
||||
return false
|
||||
}
|
||||
|
||||
// check memory
|
||||
if domcfg.Memory.Unit == "KiB" {
|
||||
var m int64
|
||||
m = int64(domcfg.Memory.Value * 1024)
|
||||
if d.pb.Memory != m {
|
||||
d.pb.Memory = m
|
||||
me.changed = true
|
||||
fmt.Printf("Memory changed %d, %d %s\n", d.pb.Memory, domcfg.Memory.Value, domcfg.Memory.Unit)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit)
|
||||
return true
|
||||
}
|
||||
|
|
13
argv.go
13
argv.go
|
@ -11,12 +11,13 @@ import "go.wit.com/log"
|
|||
var argv args
|
||||
|
||||
type args struct {
|
||||
Dir string `arg:"--dir" help:"defaults to ~/.config/virtigo/"`
|
||||
Port int `arg:"--port" default:"8080" help:"specify a different default port"`
|
||||
Hosts []string `arg:"--hosts" help:"hosts to connect to"`
|
||||
Uptime bool `arg:"--uptime" default:"true" help:"allow uptime checks for things like Kuma"`
|
||||
Daemon bool `arg:"--daemon" help:"run in daemon mode"`
|
||||
Xml []string `arg:"--add-xml" help:"add libvirt xml files"`
|
||||
Dir string `arg:"--dir" help:"defaults to ~/.config/virtigo/"`
|
||||
Port int `arg:"--port" default:"8080" help:"specify a different default port"`
|
||||
Hosts []string `arg:"--hosts" help:"hosts to connect to"`
|
||||
Uptime bool `arg:"--uptime" default:"true" help:"allow uptime checks for things like Kuma"`
|
||||
Daemon bool `arg:"--daemon" help:"run in daemon mode"`
|
||||
Xml []string `arg:"--add-xml" help:"add libvirt xml files"`
|
||||
Save bool `arg:"--save" default:"false" help:"save xml changes to the protobuf values"`
|
||||
}
|
||||
|
||||
func (a args) Description() string {
|
||||
|
|
37
main.go
37
main.go
|
@ -44,25 +44,50 @@ func main() {
|
|||
|
||||
var ok bool = true
|
||||
for _, filename := range argv.Xml {
|
||||
log.Info("add xml file", filename)
|
||||
domcfg, err := readXml(filename)
|
||||
if err != nil {
|
||||
// parsing the libvirt xml file failed
|
||||
log.Info("error:", filename, err)
|
||||
ok = false
|
||||
continue
|
||||
}
|
||||
d, err := addDroplet(domcfg)
|
||||
// see if the libvirt xml droplet is already here
|
||||
d, err := findDomain(domcfg)
|
||||
if err != nil {
|
||||
// some error. probably UUID mismatch or hostname duplication
|
||||
// this has to be fixed by hand
|
||||
ok = false
|
||||
continue
|
||||
}
|
||||
if d == nil {
|
||||
log.Info("addDroplet() returned nil")
|
||||
ok = false
|
||||
// this is a new droplet. add it to the cluster
|
||||
log.Info("Add New Droplet here", domcfg.Name)
|
||||
_, err := addDomainDroplet(domcfg)
|
||||
if err != nil {
|
||||
ok = false
|
||||
log.Info("addDomainDroplet() failed", err)
|
||||
}
|
||||
} else {
|
||||
// this droplet is already here
|
||||
if updateDroplet(d, domcfg) {
|
||||
if me.changed {
|
||||
log.Info("updateDroplet() worked. droplet changed")
|
||||
} else {
|
||||
log.Info("updateDroplet() worked. nothing changed")
|
||||
}
|
||||
} else {
|
||||
log.Info("updateDroplet() failed for", d.pb.Hostname)
|
||||
ok = false
|
||||
}
|
||||
}
|
||||
}
|
||||
if me.changed {
|
||||
writeConfigFile()
|
||||
writeConfigFileDroplets()
|
||||
if argv.Save {
|
||||
writeConfigFile()
|
||||
writeConfigFileDroplets()
|
||||
} else {
|
||||
log.Info("Not saving changes (use --save to save)")
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
if !ok {
|
||||
|
|
Loading…
Reference in New Issue