Debian Codegraph @

examples/guest-vcpus/guest-vcpu-daemon.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """
3 This service waits for the guest agent lifecycle event and reissues
4 guest agent calls to modify the cpu count according to the metadata
5 set by guest-vcpu.py example
6 """
7
8 import libvirt
9 import threading
10 from xml.dom import minidom
11 import time
12 from argparse import ArgumentParser
13
14 uri = "qemu:///system"
15 customXMLuri = "guest-cpu.python.libvirt.org"
16 connectRetryTimeout = 5
17
18
19 class workerData:
20 def __init__(self):
21 self.doms = list()
22 self.conn = None
23 self.cond = threading.Condition()
24
25 def notify(self):
26 self.cond.acquire()
27 self.cond.notify()
28 self.cond.release()
29
30 def waitNotify(self):
31 self.cond.acquire()
32 self.cond.wait()
33 self.cond.release()
34
35 def addDomainNotify(self, dom):
36 self.doms.append(dom)
37 self.notify()
38
39 def closeConnectNotify(self):
40 conn = self.conn
41 self.conn = None
42 conn.close()
43 self.notify()
44
45 def setConnect(self, conn):
46 self.conn = conn
47
48 def hasConn(self):
49 return self.conn is not None
50
51 def hasDom(self):
52 return len(self.doms) > 0
53
54 def getDom(self):
55 return self.doms.pop()
56
57 def setDoms(self, doms):
58 self.doms = doms
59
60
61 def virEventLoopNativeRun():
62 while True:
63 libvirt.virEventRunDefaultImpl()
64
65
66 def handleAgentLifecycleEvent(conn, dom, state, reason, opaque):
67 if state == libvirt.VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_STATE_CONNECTED:
68 opaque.addDomainNotify(dom)
69
70
71 def handleConnectClose(conn, reason, opaque):
72 print('Disconnected from ' + uri)
73 opaque.closeConnectNotify()
74
75
76 def handleLibvirtLibraryError(opaque, error):
77 pass
78
79
80 def processAgentConnect(dom):
81 try:
82 cpus = dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, customXMLuri, libvirt.VIR_DOMAIN_AFFECT_LIVE)
83 doc = minidom.parseString(cpus)
84 ncpus = int(doc.getElementsByTagName('ncpus')[0].getAttribute('count'))
85 except Exception:
86 return
87
88 try:
89 dom.setVcpusFlags(ncpus, libvirt.VIR_DOMAIN_AFFECT_LIVE | libvirt.VIR_DOMAIN_VCPU_GUEST)
90 print("set vcpu count for domain " + dom.name() + " to " + str(ncpus))
91 except Exception:
92 print("failed to set vcpu count for domain " + dom.name())
93
94
95 def work():
96 data = workerData()
97
98 print("Using uri: " + uri)
99
100 while True:
101 if not data.hasConn():
102 try:
103 conn = libvirt.open(uri)
104 except libvirt.libvirtError:
105 print('Failed to connect to ' + uri + ', retry in ' + str(connectRetryTimeout)) + ' seconds'
106 time.sleep(connectRetryTimeout)
107 continue
108
109 print('Connected to ' + uri)
110
111 data.setConnect(conn)
112 conn.registerCloseCallback(handleConnectClose, data)
113 conn.setKeepAlive(5, 3)
114 conn.domainEventRegisterAny(None,
115 libvirt.VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE,
116 handleAgentLifecycleEvent,
117 data)
118
119 data.setDoms(conn.listAllDomains(libvirt.VIR_CONNECT_LIST_DOMAINS_ACTIVE))
120
121 while data.hasConn() and data.hasDom():
122 processAgentConnect(data.getDom())
123
124 data.waitNotify()
125
126
127 def main():
128 libvirt.virEventRegisterDefaultImpl()
129 libvirt.registerErrorHandler(handleLibvirtLibraryError, None)
130
131 worker = threading.Thread(target=work)
132 worker.setDaemon(True)
133 worker.start()
134
135 eventLoop = threading.Thread(target=virEventLoopNativeRun)
136 eventLoop.setDaemon(True)
137 eventLoop.start()
138
139 while True:
140 time.sleep(1)
141
142
143 if __name__ == "__main__":
144 parser = ArgumentParser(description=__doc__)
145 parser.add_argument("uri", nargs="?", default=uri)
146 args = parser.parse_args()
147
148 uri = args.uri
149
150 main()