package domain import ( "context" "encoding/json" "fmt" "deevirt.fr/compute/pkg/amqp" "deevirt.fr/compute/pkg/proto" "deevirt.fr/compute/pkg/schema" "libvirt.org/go/libvirt" ) type EventsDetail map[string]string func (d *Domain) domainEventLifecycle(nodeId string, domainId string, state int64, event *libvirt.DomainEventLifecycle) { d.Logger.Sugar().Infof("%s => %s: Evènement %v", nodeId, domainId, event) switch event.Event { case libvirt.DOMAIN_EVENT_DEFINED: domStore := schema.Domain{} domData, err := d.Store.Get(fmt.Sprintf("/domain/%s", domainId)) if err != nil || len(domData) == 0 { d.Logger.Sugar().Errorf("Critique !!, la VM %s n'existe pas ou comporte une erreur importante !", domainId) } json.Unmarshal(domData, &domStore) domNode := schema.DomainNode{} domNodeJson, err := d.Store.Get(fmt.Sprintf("/domain/%s/node", domainId)) if err != nil || len(domData) == 0 { d.Logger.Sugar().Errorf("Critique !!, la VM %s n'existe pas ou comporte une erreur importante !", domainId) } json.Unmarshal(domNodeJson, &domNode) // Changement de noeud ! newdomNode := schema.DomainNode{} newdomNode.NodeId = nodeId dom2node, _ := json.Marshal(newdomNode) d.Store.Set(fmt.Sprintf("/domain/%s/node", domainId), dom2node) domAttachment := schema.DomainAttachment{} domAttachment.State = int(state) domAttachmentJson, _ := json.Marshal(domAttachment) d.Store.Set(fmt.Sprintf("/etc/libvirt/qemu/%s/%s", nodeId, domainId), domAttachmentJson) d.Store.Delete(fmt.Sprintf("/etc/libvirt/qemu/%s/%s", domNode.NodeId, domainId)) return case libvirt.DOMAIN_EVENT_STARTED: switch event.Detail { case int(libvirt.DOMAIN_EVENT_STARTED_MIGRATED): // On ne fait rien, migration en cours return } case libvirt.DOMAIN_EVENT_SUSPENDED: switch event.Detail { case int(libvirt.DOMAIN_EVENT_SUSPENDED_MIGRATED): // On ne fait rien, migration en cours return } case libvirt.DOMAIN_EVENT_RESUMED: switch event.Detail { case int(libvirt.DOMAIN_EVENT_RESUMED_MIGRATED): // On ne fait rien, migration en cours return } } // MAJ de l'état domNodeData := schema.DomainAttachment{} domNodeData.State = int(state) dom2node, _ := json.Marshal(domNodeData) d.Store.Set(fmt.Sprintf("/etc/libvirt/qemu/%s/%s", nodeId, domainId), dom2node) // AMQP - On envoi l'évènement brut desc := schema.DomainXML{} a, _ := amqp.NewAMQP() e, _ := json.Marshal(struct { Type string State int }{ Type: "DomainState", State: int(state), }) a.Publisher("vmcenter", "events."+desc.Metadata.DeevirtInstance.DeevirtCompanyID+ "."+desc.Metadata.DeevirtInstance.DeevirtDatacenterID+ "."+domainId, e) defer a.Close() } func (d *Domain) Event(ctx context.Context, req *proto.DomainEventRequest) (*proto.DomainEventResponse, error) { switch req.Type { case "DomainEventLifecycle": event := libvirt.DomainEventLifecycle{} if json.Unmarshal(req.Event, &event) == nil { d.domainEventLifecycle(req.NodeId, req.DomainId, req.State, &event) } } return &proto.DomainEventResponse{}, nil }