compute/cmd/qemu/events/server.go

75 lines
1.6 KiB
Go

package events
import (
"encoding/hex"
"fmt"
"log"
"os"
"github.com/denisbrodbeck/machineid"
"gopkg.in/ini.v1"
"libvirt.org/go/libvirt"
)
func Config() (*ini.File, error) {
return ini.Load("/etc/deevirt/config.ini")
}
var ClusterID = func() string {
config, _ := Config()
return config.Section("").Key("id").String()
}
var MachineID = func() string {
id, err := machineid.ID()
if err != nil {
log.Fatal(err)
}
u, _ := hex.DecodeString(id)
return fmt.Sprintf("%x-%x-%x-%x-%x\n", u[:4], u[4:6], u[6:8], u[8:10], u[10:])
}
func agentLifecycle(c *libvirt.Connect, d *libvirt.Domain, event *libvirt.DomainEventAgentLifecycle) {
println(event.State)
println(event.Reason)
}
func graphics(c *libvirt.Connect, d *libvirt.Domain, event *libvirt.DomainEventGraphics) {
println(event.String())
}
func reboot(c *libvirt.Connect, d *libvirt.Domain) {
}
func watchdog(c *libvirt.Connect, d *libvirt.Domain, event *libvirt.DomainEventWatchdog) {
println(event.String())
}
func Server() {
err := libvirt.EventRegisterDefaultImpl()
if err != nil {
log.Fatalf("Échec d'EventRegisterDefaultImpl: %v", err)
os.Exit(0)
}
conn, err := libvirt.NewConnect("qemu:///system")
if err != nil {
log.Println("Connexion Error")
}
defer conn.Close()
conn.SetKeepAlive(5, 3)
conn.DomainEventAgentLifecycleRegister(nil, agentLifecycle)
conn.DomainEventGraphicsRegister(nil, graphics)
conn.DomainEventLifecycleRegister(nil, Lifecyle)
conn.DomainEventRebootRegister(nil, reboot)
conn.DomainEventWatchdogRegister(nil, watchdog)
for {
libvirt.EventRunDefaultImpl()
}
}