libvirtpy 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. libvirtpy/__init__.py +5 -0
  2. libvirtpy/client/__init__.py +54 -0
  3. libvirtpy/client/_bases.py +326 -0
  4. libvirtpy/client/_runner.py +57 -0
  5. libvirtpy/client/aio.py +5922 -0
  6. libvirtpy/client/client.py +5695 -0
  7. libvirtpy/client/operations/__init__.py +631 -0
  8. libvirtpy/client/operations/_common.py +495 -0
  9. libvirtpy/client/operations/checkpoint.py +196 -0
  10. libvirtpy/client/operations/connection.py +93 -0
  11. libvirtpy/client/operations/connection_extra.py +256 -0
  12. libvirtpy/client/operations/domain.py +306 -0
  13. libvirtpy/client/operations/domain_agent.py +115 -0
  14. libvirtpy/client/operations/domain_block.py +368 -0
  15. libvirtpy/client/operations/domain_info.py +400 -0
  16. libvirtpy/client/operations/domain_lifecycle.py +338 -0
  17. libvirtpy/client/operations/domain_migration.py +164 -0
  18. libvirtpy/client/operations/domain_network.py +113 -0
  19. libvirtpy/client/operations/domain_tuning.py +494 -0
  20. libvirtpy/client/operations/host.py +246 -0
  21. libvirtpy/client/operations/host_interface.py +100 -0
  22. libvirtpy/client/operations/interface_extra.py +45 -0
  23. libvirtpy/client/operations/network.py +135 -0
  24. libvirtpy/client/operations/network_info.py +155 -0
  25. libvirtpy/client/operations/network_port.py +142 -0
  26. libvirtpy/client/operations/node_device.py +76 -0
  27. libvirtpy/client/operations/node_device_info.py +189 -0
  28. libvirtpy/client/operations/nwfilter.py +67 -0
  29. libvirtpy/client/operations/nwfilter_binding.py +74 -0
  30. libvirtpy/client/operations/secret.py +93 -0
  31. libvirtpy/client/operations/secret_extra.py +24 -0
  32. libvirtpy/client/operations/snapshot.py +100 -0
  33. libvirtpy/client/operations/snapshot_tree.py +114 -0
  34. libvirtpy/client/operations/storage.py +267 -0
  35. libvirtpy/client/operations/storage_info.py +188 -0
  36. libvirtpy/client/operations/streams.py +201 -0
  37. libvirtpy/client/references.py +249 -0
  38. libvirtpy/client/subscribing.py +88 -0
  39. libvirtpy/client/testing/__init__.py +24 -0
  40. libvirtpy/client/testing/_common.py +167 -0
  41. libvirtpy/client/testing/_domains.py +294 -0
  42. libvirtpy/client/testing/_objects.py +205 -0
  43. libvirtpy/client/testing/_state.py +246 -0
  44. libvirtpy/client/testing/_storage.py +171 -0
  45. libvirtpy/client/testing/async_transport.py +30 -0
  46. libvirtpy/client/testing/fake.py +81 -0
  47. libvirtpy/client/transport.py +87 -0
  48. libvirtpy/client/types.py +59 -0
  49. libvirtpy/client/wire.py +436 -0
  50. libvirtpy/client/wire_aio.py +348 -0
  51. libvirtpy/errors.py +7 -0
  52. libvirtpy/py.typed +0 -0
  53. libvirtpy/rpc/__init__.py +1 -0
  54. libvirtpy/rpc/aio.py +387 -0
  55. libvirtpy/rpc/cpumap.py +48 -0
  56. libvirtpy/rpc/errors.py +186 -0
  57. libvirtpy/rpc/events.py +259 -0
  58. libvirtpy/rpc/options.py +66 -0
  59. libvirtpy/rpc/procedure.py +27 -0
  60. libvirtpy/rpc/protocol/__init__.py +0 -0
  61. libvirtpy/rpc/protocol/procedures.py +5163 -0
  62. libvirtpy/rpc/protocol/typed_hints.py +210 -0
  63. libvirtpy/rpc/protocol/types.py +9299 -0
  64. libvirtpy/rpc/remote.py +295 -0
  65. libvirtpy/rpc/remote_aio.py +106 -0
  66. libvirtpy/rpc/sasl.py +467 -0
  67. libvirtpy/rpc/session.py +424 -0
  68. libvirtpy/rpc/sockets.py +251 -0
  69. libvirtpy/rpc/stream.py +323 -0
  70. libvirtpy/rpc/subscription.py +451 -0
  71. libvirtpy/rpc/sync.py +460 -0
  72. libvirtpy/rpc/typed_params.py +97 -0
  73. libvirtpy/rpc/uri.py +85 -0
  74. libvirtpy/rpc/xdr.py +552 -0
  75. libvirtpy/xml/__init__.py +49 -0
  76. libvirtpy/xml/_base.py +145 -0
  77. libvirtpy/xml/_shared/__init__.py +1 -0
  78. libvirtpy/xml/_shared/metadata.py +67 -0
  79. libvirtpy/xml/_shared/network.py +108 -0
  80. libvirtpy/xml/_shared/storage.py +131 -0
  81. libvirtpy/xml/_shared/sysinfo.py +83 -0
  82. libvirtpy/xml/_types.py +197 -0
  83. libvirtpy/xml/_xml_element.py +134 -0
  84. libvirtpy/xml/backup/__init__.py +25 -0
  85. libvirtpy/xml/backup/backup.py +89 -0
  86. libvirtpy/xml/backup/checkpoint.py +48 -0
  87. libvirtpy/xml/capabilities/__init__.py +43 -0
  88. libvirtpy/xml/capabilities/domain.py +436 -0
  89. libvirtpy/xml/capabilities/host.py +413 -0
  90. libvirtpy/xml/capabilities/storagepool.py +59 -0
  91. libvirtpy/xml/domain/__init__.py +493 -0
  92. libvirtpy/xml/domain/blkiotune.py +81 -0
  93. libvirtpy/xml/domain/clock.py +79 -0
  94. libvirtpy/xml/domain/cpu.py +265 -0
  95. libvirtpy/xml/domain/devices/__init__.py +495 -0
  96. libvirtpy/xml/domain/devices/address.py +152 -0
  97. libvirtpy/xml/domain/devices/audio.py +204 -0
  98. libvirtpy/xml/domain/devices/chardev.py +197 -0
  99. libvirtpy/xml/domain/devices/common.py +75 -0
  100. libvirtpy/xml/domain/devices/controller.py +262 -0
  101. libvirtpy/xml/domain/devices/devices.py +79 -0
  102. libvirtpy/xml/domain/devices/disk.py +471 -0
  103. libvirtpy/xml/domain/devices/filesystem.py +206 -0
  104. libvirtpy/xml/domain/devices/graphics.py +245 -0
  105. libvirtpy/xml/domain/devices/hostdev.py +221 -0
  106. libvirtpy/xml/domain/devices/input.py +89 -0
  107. libvirtpy/xml/domain/devices/interface.py +459 -0
  108. libvirtpy/xml/domain/devices/iommu.py +54 -0
  109. libvirtpy/xml/domain/devices/memballoon.py +42 -0
  110. libvirtpy/xml/domain/devices/memory_device.py +101 -0
  111. libvirtpy/xml/domain/devices/misc.py +169 -0
  112. libvirtpy/xml/domain/devices/nvram.py +20 -0
  113. libvirtpy/xml/domain/devices/shmem.py +46 -0
  114. libvirtpy/xml/domain/devices/sound.py +42 -0
  115. libvirtpy/xml/domain/devices/tpm.py +103 -0
  116. libvirtpy/xml/domain/devices/usb.py +86 -0
  117. libvirtpy/xml/domain/devices/video.py +101 -0
  118. libvirtpy/xml/domain/domain.py +157 -0
  119. libvirtpy/xml/domain/features.py +312 -0
  120. libvirtpy/xml/domain/iothread.py +41 -0
  121. libvirtpy/xml/domain/launch_security.py +82 -0
  122. libvirtpy/xml/domain/memory.py +149 -0
  123. libvirtpy/xml/domain/misc.py +113 -0
  124. libvirtpy/xml/domain/namespaces.py +96 -0
  125. libvirtpy/xml/domain/numa.py +140 -0
  126. libvirtpy/xml/domain/os.py +267 -0
  127. libvirtpy/xml/domain/qemu.py +102 -0
  128. libvirtpy/xml/domain/seclabel.py +29 -0
  129. libvirtpy/xml/domain/storage_source.py +466 -0
  130. libvirtpy/xml/domain/sysinfo.py +34 -0
  131. libvirtpy/xml/interface/__init__.py +17 -0
  132. libvirtpy/xml/interface/interface.py +153 -0
  133. libvirtpy/xml/network/__init__.py +101 -0
  134. libvirtpy/xml/network/network.py +311 -0
  135. libvirtpy/xml/network/port.py +84 -0
  136. libvirtpy/xml/nodedev/__init__.py +13 -0
  137. libvirtpy/xml/nodedev/nodedev.py +490 -0
  138. libvirtpy/xml/nwfilter/__init__.py +81 -0
  139. libvirtpy/xml/nwfilter/binding.py +51 -0
  140. libvirtpy/xml/nwfilter/filter.py +92 -0
  141. libvirtpy/xml/nwfilter/rules.py +534 -0
  142. libvirtpy/xml/secret/__init__.py +11 -0
  143. libvirtpy/xml/secret/secret.py +36 -0
  144. libvirtpy/xml/snapshot/__init__.py +29 -0
  145. libvirtpy/xml/snapshot/snapshot.py +136 -0
  146. libvirtpy/xml/storage/__init__.py +75 -0
  147. libvirtpy/xml/storage/pool.py +219 -0
  148. libvirtpy/xml/storage/volume.py +102 -0
  149. libvirtpy/xml/sysinfo.py +40 -0
  150. libvirtpy-0.1.0.dist-info/METADATA +109 -0
  151. libvirtpy-0.1.0.dist-info/RECORD +154 -0
  152. libvirtpy-0.1.0.dist-info/WHEEL +4 -0
  153. libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
  154. libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
@@ -0,0 +1,348 @@
1
+ """The asyncio twin of ``wire.py``: operations carried to real daemons."""
2
+
3
+ import asyncio
4
+ from collections.abc import Iterable
5
+ from enum import IntEnum
6
+ from pathlib import Path
7
+
8
+ from libvirtpy.client.subscribing import (
9
+ SubscriptionPlan,
10
+ event_lookup,
11
+ plan_subscription,
12
+ )
13
+ from libvirtpy.client.transport import Request
14
+ from libvirtpy.client.wire import PROCEDURES_BY_NAME, convert_arguments, convert_reply
15
+ from libvirtpy.rpc.aio import AsyncRpc
16
+ from libvirtpy.rpc.errors import ErrorNumber, NotSupportedError
17
+ from libvirtpy.rpc.options import ConnectionOptions
18
+ from libvirtpy.rpc.procedure import Driver
19
+ from libvirtpy.rpc.protocol import procedures
20
+ from libvirtpy.rpc.remote_aio import connect_remote_async
21
+ from libvirtpy.rpc.sasl import SaslCredentials
22
+ from libvirtpy.rpc.session import ConnectionLostError
23
+ from libvirtpy.rpc.sockets import (
24
+ REMOTE_KEY,
25
+ authentication,
26
+ open_arguments,
27
+ socket_paths,
28
+ )
29
+ from libvirtpy.rpc.stream import AsyncRemoteStream
30
+ from libvirtpy.rpc.subscription import DEFAULT_MAX_EVENTS, AsyncSubscription
31
+ from libvirtpy.rpc.uri import ConnectionTarget, parse_uri
32
+
33
+ __all__ = ["AsyncWireTransport"]
34
+
35
+
36
+ class AsyncWireTransport:
37
+ """Serves requests from real daemons, one socket per daemon, opened lazily."""
38
+
39
+ def __init__(
40
+ self,
41
+ target: ConnectionTarget,
42
+ *,
43
+ read_only: bool = False,
44
+ sasl: SaslCredentials | None = None,
45
+ options: ConnectionOptions | None = None,
46
+ ) -> None:
47
+ """Create the transport; nothing is connected until the first request.
48
+
49
+ :param target: what the URI asks for.
50
+ :param read_only: use the read-only sockets.
51
+ :param sasl: the user name and password, if the daemon asks for SASL.
52
+ :param options: the call timeout, keepalive and reconnect settings.
53
+ """
54
+ self._target = target
55
+ self._read_only = read_only
56
+ self._sasl = sasl
57
+ self._options = options
58
+
59
+ self._channels: dict[str, AsyncRpc] = {}
60
+ self._by_path: dict[str, AsyncRpc] = {}
61
+ self._opening = asyncio.Lock()
62
+ self._closed = False
63
+ self._restoring: set[asyncio.Task[None]] = set()
64
+
65
+ @classmethod
66
+ async def from_uri(
67
+ cls,
68
+ uri: str,
69
+ *,
70
+ read_only: bool = False,
71
+ sasl: SaslCredentials | None = None,
72
+ options: ConnectionOptions | None = None,
73
+ ) -> AsyncWireTransport:
74
+ """Create a transport for a libvirt URI, and check that it connects.
75
+
76
+ :param uri: e.g. ``qemu:///system``.
77
+ :param read_only: use the read-only sockets.
78
+ :param sasl: the user name and password, if the daemon asks for SASL.
79
+ :param options: the call timeout, keepalive and reconnect settings.
80
+ :returns: the transport, with the hypervisor channel already open.
81
+ :raises ConnectionError: if the daemon cannot be reached.
82
+ """
83
+ transport = cls(parse_uri(uri), read_only=read_only, sasl=sasl, options=options)
84
+ await transport._channel("hypervisor")
85
+ return transport
86
+
87
+ async def call(self, request: Request) -> object:
88
+ """Send a request to the daemon that serves it.
89
+
90
+ :param request: the call to make.
91
+ :returns: the reply, converted for the operations.
92
+ :raises NotSupportedError: if the procedure does not exist.
93
+ :raises LibvirtError: if the daemon reports an error.
94
+ """
95
+ procedure = PROCEDURES_BY_NAME.get(request.procedure)
96
+ if procedure is None:
97
+ raise NotSupportedError(
98
+ f"{request.procedure} is not a libvirt procedure",
99
+ code=ErrorNumber.NO_SUPPORT,
100
+ )
101
+
102
+ args = convert_arguments(procedure, request.arguments)
103
+ channel = await self._channel(procedure.driver)
104
+ if request.stream is not None:
105
+ stream = AsyncRemoteStream(channel, request.stream)
106
+ reply = await channel.call(procedure, args, stream=stream)
107
+ stream.reply = convert_reply(procedure, reply)
108
+ return stream
109
+ return convert_reply(procedure, await channel.call(procedure, args))
110
+
111
+ async def subscribe(
112
+ self,
113
+ source: str,
114
+ kinds: Iterable[IntEnum] | None,
115
+ target: object,
116
+ max_events: int = DEFAULT_MAX_EVENTS,
117
+ ) -> AsyncSubscription:
118
+ """Start receiving events.
119
+
120
+ :param source: ``domain``, ``network``, ``pool``, ``node_device`` or ``secret``.
121
+ :param kinds: which events; ``None`` for the ``lifecycle`` ones.
122
+ :param target: limit to one object (a reference); ``None`` for all.
123
+ :param max_events: the most events kept for a slow reader.
124
+ :returns: the subscription.
125
+ :raises ValueError: if the source or a kind is unknown.
126
+ """
127
+ plan = plan_subscription(source, kinds)
128
+ handle = (
129
+ None if target is None else await self.call(event_lookup(source, target))
130
+ )
131
+ rpc = await self._channel(plan.register.driver)
132
+
133
+ subscription = AsyncSubscription(rpc, plan.deregister, max_events)
134
+ try:
135
+ await self._register(subscription, plan, handle, rpc)
136
+ except Exception:
137
+ await subscription.close()
138
+ raise
139
+ return subscription
140
+
141
+ async def close(self) -> None:
142
+ """Close every open channel; the transport does not reconnect after this."""
143
+ self._closed = True
144
+ for task in list(self._restoring):
145
+ task.cancel()
146
+ for channel in set(self._channels.values()):
147
+ await channel.close()
148
+ self._channels.clear()
149
+ self._by_path.clear()
150
+
151
+ async def _channel(self, driver: Driver) -> AsyncRpc:
152
+ """Return the channel of a driver, connecting on first use.
153
+
154
+ A channel whose connection was lost is replaced by a new one if the
155
+ connection options ask for reconnecting; otherwise it is returned and the
156
+ call on it fails with ``ConnectionLostError``.
157
+
158
+ :param driver: the part of libvirt that serves the call.
159
+ :returns: the channel; a remote host or a monolithic ``libvirtd`` shares
160
+ one for all drivers.
161
+ :raises ConnectionLostError: if the transport was closed.
162
+ :raises ConnectionError: if the connection cannot be opened again.
163
+ """
164
+ key = REMOTE_KEY if self._target.remote else driver
165
+ async with self._opening:
166
+ if self._closed:
167
+ raise ConnectionLostError("the client is closed")
168
+
169
+ channel = self._channels.get(key)
170
+ if channel is None:
171
+ return await self._opened(driver, key)
172
+ if channel.failure is None or not self._reconnecting():
173
+ return channel
174
+
175
+ await self._forget(channel)
176
+ return await self._reopen(driver, key)
177
+
178
+ def _reconnecting(self) -> bool:
179
+ """Tell whether lost connections are opened again.
180
+
181
+ :returns: ``True`` if the options ask for it.
182
+ """
183
+ return self._options is not None and self._options.reconnects
184
+
185
+ async def _opened(self, driver: Driver, key: str) -> AsyncRpc:
186
+ """Open a channel and remember it.
187
+
188
+ :param driver: the part of libvirt that serves the call.
189
+ :param key: the channel's key.
190
+ :returns: the ready channel.
191
+ """
192
+ channel = await self._open(driver)
193
+ self._channels[key] = channel
194
+ return channel
195
+
196
+ async def _reopen(self, driver: Driver, key: str) -> AsyncRpc:
197
+ """Open a channel again after its connection was lost.
198
+
199
+ :param driver: the part of libvirt that serves the call.
200
+ :param key: the channel's key.
201
+ :returns: the new channel.
202
+ :raises ConnectionError: if every attempt failed.
203
+ """
204
+ assert self._options is not None
205
+ waits = self._options.reconnect_waits()
206
+ failure: Exception | None = None
207
+
208
+ for wait in waits:
209
+ await asyncio.sleep(wait)
210
+ try:
211
+ return await self._opened(driver, key)
212
+ except (ConnectionError, OSError) as error:
213
+ failure = error
214
+ raise ConnectionError(
215
+ f"cannot reconnect after {len(waits)} attempts: {failure}"
216
+ ) from failure
217
+
218
+ async def _forget(self, channel: AsyncRpc) -> None:
219
+ """Drop a dead channel and close what is left of it.
220
+
221
+ :param channel: the channel.
222
+ """
223
+ for table in (self._channels, self._by_path):
224
+ for key in [key for key, value in table.items() if value is channel]:
225
+ del table[key]
226
+ await channel.close()
227
+
228
+ async def _register(
229
+ self,
230
+ subscription: AsyncSubscription,
231
+ plan: SubscriptionPlan,
232
+ handle: object,
233
+ rpc: AsyncRpc,
234
+ ) -> None:
235
+ """Register the events of a subscription on a connection.
236
+
237
+ :param subscription: the subscription.
238
+ :param plan: which registrations to make.
239
+ :param handle: the object to limit to, or ``None``.
240
+ :param rpc: the connection.
241
+ """
242
+ if self._reconnecting():
243
+ rpc.when_lost(
244
+ lambda _error: self._restore_later(subscription, plan, handle)
245
+ )
246
+ else:
247
+ rpc.when_lost(subscription.lost)
248
+
249
+ for arguments in plan.arguments(handle):
250
+ await rpc.call(
251
+ plan.register,
252
+ convert_arguments(plan.register, arguments),
253
+ on_result=lambda reply: subscription.attach(reply.callback_id),
254
+ )
255
+
256
+ def _restore_later(
257
+ self, subscription: AsyncSubscription, plan: SubscriptionPlan, handle: object
258
+ ) -> None:
259
+ """Start registering a subscription again on a new connection.
260
+
261
+ :param subscription: the subscription of the lost connection.
262
+ :param plan: which registrations to make.
263
+ :param handle: the object to limit to, or ``None``.
264
+ """
265
+ task = asyncio.get_running_loop().create_task(
266
+ self._restore(subscription, plan, handle)
267
+ )
268
+ self._restoring.add(task)
269
+ task.add_done_callback(self._restoring.discard)
270
+
271
+ async def _restore(
272
+ self, subscription: AsyncSubscription, plan: SubscriptionPlan, handle: object
273
+ ) -> None:
274
+ """Register a subscription again on a new connection.
275
+
276
+ :param subscription: the subscription of the lost connection.
277
+ :param plan: which registrations to make.
278
+ :param handle: the object to limit to, or ``None``.
279
+ """
280
+ try:
281
+ rpc = await self._channel(plan.register.driver)
282
+ if subscription.rebind(rpc):
283
+ await self._register(subscription, plan, handle, rpc)
284
+ except asyncio.CancelledError:
285
+ subscription.lost(ConnectionLostError("the client is closed"))
286
+ raise
287
+ except Exception as failure: # background task: report, do not crash
288
+ subscription.lost(failure)
289
+
290
+ async def _open(self, driver: Driver) -> AsyncRpc:
291
+ """Connect, authenticate and open a channel, reusing a shared socket.
292
+
293
+ :param driver: the part of libvirt to reach.
294
+ :returns: the ready channel.
295
+ :raises ConnectionError: if no candidate socket accepts a connection.
296
+ """
297
+ path, rpc = await self._connect(driver)
298
+ key = REMOTE_KEY if path is None else str(path)
299
+ shared = self._by_path.get(key)
300
+ if shared is not None:
301
+ await rpc.close()
302
+ return shared
303
+
304
+ try:
305
+ await self._authenticate(rpc)
306
+ await rpc.call(
307
+ procedures.CONNECT_OPEN,
308
+ open_arguments(self._target, driver, path, self._read_only),
309
+ )
310
+ except BaseException:
311
+ await rpc.close() # do not leave the reader task and ssh behind
312
+ raise
313
+ self._by_path[key] = rpc
314
+ return rpc
315
+
316
+ async def _authenticate(self, rpc: AsyncRpc) -> None:
317
+ """Do the authentication the daemon asks for.
318
+
319
+ :param rpc: a channel that is not opened yet.
320
+ :raises AuthFailedError: if the daemon asks for something we cannot do.
321
+ """
322
+ offered = (await rpc.call(procedures.AUTH_LIST)).types
323
+ steps = authentication(offered, self._target, self._sasl)
324
+
325
+ try:
326
+ call = next(steps)
327
+ while True:
328
+ call = steps.send(await rpc.call(*call))
329
+ except StopIteration:
330
+ return
331
+
332
+ async def _connect(self, driver: Driver) -> tuple[Path | None, AsyncRpc]:
333
+ """Connect to the daemon of a driver, or to the remote host.
334
+
335
+ :param driver: the part of libvirt to reach.
336
+ :returns: the socket path (``None`` if remote) and the connection.
337
+ :raises ConnectionError: if nothing could be reached.
338
+ """
339
+ if self._target.remote:
340
+ return None, await connect_remote_async(self._target, self._options)
341
+
342
+ failures = []
343
+ for path in socket_paths(self._target, driver, read_only=self._read_only):
344
+ try:
345
+ return path, await AsyncRpc.connect_unix(path, self._options)
346
+ except OSError as error:
347
+ failures.append(f"{path}: {error.strerror or error}")
348
+ raise ConnectionError("no libvirt socket reachable: " + "; ".join(failures))
libvirtpy/errors.py ADDED
@@ -0,0 +1,7 @@
1
+ """Exceptions raised by libvirtpy."""
2
+
3
+ __all__ = ["LibvirtPyError"]
4
+
5
+
6
+ class LibvirtPyError(Exception):
7
+ """Base class for every error raised on purpose by libvirtpy."""
libvirtpy/py.typed ADDED
File without changes
@@ -0,0 +1 @@
1
+ """The libvirt wire protocol: XDR, sessions, transports, SASL, events, streams."""