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.
- libvirtpy/__init__.py +5 -0
- libvirtpy/client/__init__.py +54 -0
- libvirtpy/client/_bases.py +326 -0
- libvirtpy/client/_runner.py +57 -0
- libvirtpy/client/aio.py +5922 -0
- libvirtpy/client/client.py +5695 -0
- libvirtpy/client/operations/__init__.py +631 -0
- libvirtpy/client/operations/_common.py +495 -0
- libvirtpy/client/operations/checkpoint.py +196 -0
- libvirtpy/client/operations/connection.py +93 -0
- libvirtpy/client/operations/connection_extra.py +256 -0
- libvirtpy/client/operations/domain.py +306 -0
- libvirtpy/client/operations/domain_agent.py +115 -0
- libvirtpy/client/operations/domain_block.py +368 -0
- libvirtpy/client/operations/domain_info.py +400 -0
- libvirtpy/client/operations/domain_lifecycle.py +338 -0
- libvirtpy/client/operations/domain_migration.py +164 -0
- libvirtpy/client/operations/domain_network.py +113 -0
- libvirtpy/client/operations/domain_tuning.py +494 -0
- libvirtpy/client/operations/host.py +246 -0
- libvirtpy/client/operations/host_interface.py +100 -0
- libvirtpy/client/operations/interface_extra.py +45 -0
- libvirtpy/client/operations/network.py +135 -0
- libvirtpy/client/operations/network_info.py +155 -0
- libvirtpy/client/operations/network_port.py +142 -0
- libvirtpy/client/operations/node_device.py +76 -0
- libvirtpy/client/operations/node_device_info.py +189 -0
- libvirtpy/client/operations/nwfilter.py +67 -0
- libvirtpy/client/operations/nwfilter_binding.py +74 -0
- libvirtpy/client/operations/secret.py +93 -0
- libvirtpy/client/operations/secret_extra.py +24 -0
- libvirtpy/client/operations/snapshot.py +100 -0
- libvirtpy/client/operations/snapshot_tree.py +114 -0
- libvirtpy/client/operations/storage.py +267 -0
- libvirtpy/client/operations/storage_info.py +188 -0
- libvirtpy/client/operations/streams.py +201 -0
- libvirtpy/client/references.py +249 -0
- libvirtpy/client/subscribing.py +88 -0
- libvirtpy/client/testing/__init__.py +24 -0
- libvirtpy/client/testing/_common.py +167 -0
- libvirtpy/client/testing/_domains.py +294 -0
- libvirtpy/client/testing/_objects.py +205 -0
- libvirtpy/client/testing/_state.py +246 -0
- libvirtpy/client/testing/_storage.py +171 -0
- libvirtpy/client/testing/async_transport.py +30 -0
- libvirtpy/client/testing/fake.py +81 -0
- libvirtpy/client/transport.py +87 -0
- libvirtpy/client/types.py +59 -0
- libvirtpy/client/wire.py +436 -0
- libvirtpy/client/wire_aio.py +348 -0
- libvirtpy/errors.py +7 -0
- libvirtpy/py.typed +0 -0
- libvirtpy/rpc/__init__.py +1 -0
- libvirtpy/rpc/aio.py +387 -0
- libvirtpy/rpc/cpumap.py +48 -0
- libvirtpy/rpc/errors.py +186 -0
- libvirtpy/rpc/events.py +259 -0
- libvirtpy/rpc/options.py +66 -0
- libvirtpy/rpc/procedure.py +27 -0
- libvirtpy/rpc/protocol/__init__.py +0 -0
- libvirtpy/rpc/protocol/procedures.py +5163 -0
- libvirtpy/rpc/protocol/typed_hints.py +210 -0
- libvirtpy/rpc/protocol/types.py +9299 -0
- libvirtpy/rpc/remote.py +295 -0
- libvirtpy/rpc/remote_aio.py +106 -0
- libvirtpy/rpc/sasl.py +467 -0
- libvirtpy/rpc/session.py +424 -0
- libvirtpy/rpc/sockets.py +251 -0
- libvirtpy/rpc/stream.py +323 -0
- libvirtpy/rpc/subscription.py +451 -0
- libvirtpy/rpc/sync.py +460 -0
- libvirtpy/rpc/typed_params.py +97 -0
- libvirtpy/rpc/uri.py +85 -0
- libvirtpy/rpc/xdr.py +552 -0
- libvirtpy/xml/__init__.py +49 -0
- libvirtpy/xml/_base.py +145 -0
- libvirtpy/xml/_shared/__init__.py +1 -0
- libvirtpy/xml/_shared/metadata.py +67 -0
- libvirtpy/xml/_shared/network.py +108 -0
- libvirtpy/xml/_shared/storage.py +131 -0
- libvirtpy/xml/_shared/sysinfo.py +83 -0
- libvirtpy/xml/_types.py +197 -0
- libvirtpy/xml/_xml_element.py +134 -0
- libvirtpy/xml/backup/__init__.py +25 -0
- libvirtpy/xml/backup/backup.py +89 -0
- libvirtpy/xml/backup/checkpoint.py +48 -0
- libvirtpy/xml/capabilities/__init__.py +43 -0
- libvirtpy/xml/capabilities/domain.py +436 -0
- libvirtpy/xml/capabilities/host.py +413 -0
- libvirtpy/xml/capabilities/storagepool.py +59 -0
- libvirtpy/xml/domain/__init__.py +493 -0
- libvirtpy/xml/domain/blkiotune.py +81 -0
- libvirtpy/xml/domain/clock.py +79 -0
- libvirtpy/xml/domain/cpu.py +265 -0
- libvirtpy/xml/domain/devices/__init__.py +495 -0
- libvirtpy/xml/domain/devices/address.py +152 -0
- libvirtpy/xml/domain/devices/audio.py +204 -0
- libvirtpy/xml/domain/devices/chardev.py +197 -0
- libvirtpy/xml/domain/devices/common.py +75 -0
- libvirtpy/xml/domain/devices/controller.py +262 -0
- libvirtpy/xml/domain/devices/devices.py +79 -0
- libvirtpy/xml/domain/devices/disk.py +471 -0
- libvirtpy/xml/domain/devices/filesystem.py +206 -0
- libvirtpy/xml/domain/devices/graphics.py +245 -0
- libvirtpy/xml/domain/devices/hostdev.py +221 -0
- libvirtpy/xml/domain/devices/input.py +89 -0
- libvirtpy/xml/domain/devices/interface.py +459 -0
- libvirtpy/xml/domain/devices/iommu.py +54 -0
- libvirtpy/xml/domain/devices/memballoon.py +42 -0
- libvirtpy/xml/domain/devices/memory_device.py +101 -0
- libvirtpy/xml/domain/devices/misc.py +169 -0
- libvirtpy/xml/domain/devices/nvram.py +20 -0
- libvirtpy/xml/domain/devices/shmem.py +46 -0
- libvirtpy/xml/domain/devices/sound.py +42 -0
- libvirtpy/xml/domain/devices/tpm.py +103 -0
- libvirtpy/xml/domain/devices/usb.py +86 -0
- libvirtpy/xml/domain/devices/video.py +101 -0
- libvirtpy/xml/domain/domain.py +157 -0
- libvirtpy/xml/domain/features.py +312 -0
- libvirtpy/xml/domain/iothread.py +41 -0
- libvirtpy/xml/domain/launch_security.py +82 -0
- libvirtpy/xml/domain/memory.py +149 -0
- libvirtpy/xml/domain/misc.py +113 -0
- libvirtpy/xml/domain/namespaces.py +96 -0
- libvirtpy/xml/domain/numa.py +140 -0
- libvirtpy/xml/domain/os.py +267 -0
- libvirtpy/xml/domain/qemu.py +102 -0
- libvirtpy/xml/domain/seclabel.py +29 -0
- libvirtpy/xml/domain/storage_source.py +466 -0
- libvirtpy/xml/domain/sysinfo.py +34 -0
- libvirtpy/xml/interface/__init__.py +17 -0
- libvirtpy/xml/interface/interface.py +153 -0
- libvirtpy/xml/network/__init__.py +101 -0
- libvirtpy/xml/network/network.py +311 -0
- libvirtpy/xml/network/port.py +84 -0
- libvirtpy/xml/nodedev/__init__.py +13 -0
- libvirtpy/xml/nodedev/nodedev.py +490 -0
- libvirtpy/xml/nwfilter/__init__.py +81 -0
- libvirtpy/xml/nwfilter/binding.py +51 -0
- libvirtpy/xml/nwfilter/filter.py +92 -0
- libvirtpy/xml/nwfilter/rules.py +534 -0
- libvirtpy/xml/secret/__init__.py +11 -0
- libvirtpy/xml/secret/secret.py +36 -0
- libvirtpy/xml/snapshot/__init__.py +29 -0
- libvirtpy/xml/snapshot/snapshot.py +136 -0
- libvirtpy/xml/storage/__init__.py +75 -0
- libvirtpy/xml/storage/pool.py +219 -0
- libvirtpy/xml/storage/volume.py +102 -0
- libvirtpy/xml/sysinfo.py +40 -0
- libvirtpy-0.1.0.dist-info/METADATA +109 -0
- libvirtpy-0.1.0.dist-info/RECORD +154 -0
- libvirtpy-0.1.0.dist-info/WHEEL +4 -0
- libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
- libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
libvirtpy/client/wire.py
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
"""A ``Transport`` that carries operations to real libvirt daemons.
|
|
2
|
+
|
|
3
|
+
``WireTransport`` turns a ``Request`` (procedure name and arguments by name)
|
|
4
|
+
into the generated argument struct, picks the daemon channel of the procedure,
|
|
5
|
+
and turns the reply struct back into what the operations expect: ``Handle``s,
|
|
6
|
+
text, numbers.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import threading
|
|
10
|
+
import time
|
|
11
|
+
import uuid
|
|
12
|
+
from collections.abc import Iterable
|
|
13
|
+
from enum import IntEnum
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from libvirtpy.client.subscribing import (
|
|
17
|
+
SubscriptionPlan,
|
|
18
|
+
event_lookup,
|
|
19
|
+
plan_subscription,
|
|
20
|
+
)
|
|
21
|
+
from libvirtpy.client.transport import Handle, Request
|
|
22
|
+
from libvirtpy.client.types import DomainInfo, DomainState
|
|
23
|
+
from libvirtpy.rpc import typed_params, xdr
|
|
24
|
+
from libvirtpy.rpc.errors import ErrorNumber, NotSupportedError
|
|
25
|
+
from libvirtpy.rpc.options import ConnectionOptions
|
|
26
|
+
from libvirtpy.rpc.procedure import Driver, Procedure
|
|
27
|
+
from libvirtpy.rpc.protocol import procedures, types
|
|
28
|
+
from libvirtpy.rpc.protocol.procedures import PROCEDURES
|
|
29
|
+
from libvirtpy.rpc.sasl import SaslCredentials
|
|
30
|
+
from libvirtpy.rpc.session import ConnectionLostError, ProtocolError
|
|
31
|
+
from libvirtpy.rpc.sockets import REMOTE_KEY, open_channel
|
|
32
|
+
from libvirtpy.rpc.stream import Mode, RemoteStream
|
|
33
|
+
from libvirtpy.rpc.subscription import DEFAULT_MAX_EVENTS, Subscription
|
|
34
|
+
from libvirtpy.rpc.sync import SyncRpc
|
|
35
|
+
from libvirtpy.rpc.uri import ConnectionTarget, parse_uri
|
|
36
|
+
|
|
37
|
+
__all__ = ["WireTransport", "convert_arguments", "convert_reply"]
|
|
38
|
+
|
|
39
|
+
PROCEDURES_BY_NAME = {procedure.name: procedure for procedure in PROCEDURES.values()}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class WireTransport:
|
|
43
|
+
"""Serves requests from real daemons, one socket per daemon, opened lazily."""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
target: ConnectionTarget,
|
|
48
|
+
*,
|
|
49
|
+
read_only: bool = False,
|
|
50
|
+
sasl: SaslCredentials | None = None,
|
|
51
|
+
options: ConnectionOptions | None = None,
|
|
52
|
+
) -> None:
|
|
53
|
+
"""Create the transport; nothing is connected until the first request.
|
|
54
|
+
|
|
55
|
+
:param target: what the URI asks for.
|
|
56
|
+
:param read_only: use the read-only sockets.
|
|
57
|
+
:param sasl: the user name and password, if the daemon asks for SASL.
|
|
58
|
+
:param options: the call timeout, keepalive and reconnect settings.
|
|
59
|
+
"""
|
|
60
|
+
self._target = target
|
|
61
|
+
self._read_only = read_only
|
|
62
|
+
self._sasl = sasl
|
|
63
|
+
self._options = options
|
|
64
|
+
|
|
65
|
+
self._channels: dict[str, SyncRpc] = {}
|
|
66
|
+
self._opening = threading.Lock()
|
|
67
|
+
self._closed = False
|
|
68
|
+
|
|
69
|
+
@classmethod
|
|
70
|
+
def from_uri(
|
|
71
|
+
cls,
|
|
72
|
+
uri: str,
|
|
73
|
+
*,
|
|
74
|
+
read_only: bool = False,
|
|
75
|
+
sasl: SaslCredentials | None = None,
|
|
76
|
+
options: ConnectionOptions | None = None,
|
|
77
|
+
) -> WireTransport:
|
|
78
|
+
"""Create a transport for a libvirt URI, and check that it connects.
|
|
79
|
+
|
|
80
|
+
:param uri: e.g. ``qemu:///system``.
|
|
81
|
+
:param read_only: use the read-only sockets.
|
|
82
|
+
:param sasl: the user name and password, if the daemon asks for SASL.
|
|
83
|
+
:param options: the call timeout, keepalive and reconnect settings.
|
|
84
|
+
:returns: the transport, with the hypervisor channel already open.
|
|
85
|
+
:raises ConnectionError: if the daemon cannot be reached.
|
|
86
|
+
"""
|
|
87
|
+
transport = cls(parse_uri(uri), read_only=read_only, sasl=sasl, options=options)
|
|
88
|
+
transport._channel("hypervisor")
|
|
89
|
+
return transport
|
|
90
|
+
|
|
91
|
+
def call(self, request: Request) -> object:
|
|
92
|
+
"""Send a request to the daemon that serves it.
|
|
93
|
+
|
|
94
|
+
:param request: the call to make.
|
|
95
|
+
:returns: the reply, converted for the operations.
|
|
96
|
+
:raises NotSupportedError: if the procedure does not exist.
|
|
97
|
+
:raises LibvirtError: if the daemon reports an error.
|
|
98
|
+
"""
|
|
99
|
+
procedure = PROCEDURES_BY_NAME.get(request.procedure)
|
|
100
|
+
if procedure is None:
|
|
101
|
+
raise NotSupportedError(
|
|
102
|
+
f"{request.procedure} is not a libvirt procedure",
|
|
103
|
+
code=ErrorNumber.NO_SUPPORT,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
args = convert_arguments(procedure, request.arguments)
|
|
107
|
+
rpc = self._channel(procedure.driver)
|
|
108
|
+
if request.stream is not None:
|
|
109
|
+
return self._open_stream(rpc, procedure, args, request.stream)
|
|
110
|
+
return convert_reply(procedure, rpc.call(procedure, args))
|
|
111
|
+
|
|
112
|
+
def _open_stream(
|
|
113
|
+
self, rpc: SyncRpc, procedure: Procedure[Any, Any], args: Any, mode: Mode
|
|
114
|
+
) -> RemoteStream:
|
|
115
|
+
"""Call a procedure that opens a data stream.
|
|
116
|
+
|
|
117
|
+
:param rpc: the connection to the daemon that serves it.
|
|
118
|
+
:param procedure: the procedure.
|
|
119
|
+
:param args: its arguments struct.
|
|
120
|
+
:param mode: which way the data flows.
|
|
121
|
+
:returns: the open stream; ``reply`` has what the call returned.
|
|
122
|
+
"""
|
|
123
|
+
stream = RemoteStream(rpc, mode)
|
|
124
|
+
reply = rpc.call(procedure, args, stream=stream)
|
|
125
|
+
stream.reply = convert_reply(procedure, reply)
|
|
126
|
+
return stream
|
|
127
|
+
|
|
128
|
+
def subscribe(
|
|
129
|
+
self,
|
|
130
|
+
source: str,
|
|
131
|
+
kinds: Iterable[IntEnum] | None,
|
|
132
|
+
target: object,
|
|
133
|
+
max_events: int = DEFAULT_MAX_EVENTS,
|
|
134
|
+
) -> Subscription:
|
|
135
|
+
"""Start receiving events.
|
|
136
|
+
|
|
137
|
+
:param source: ``domain``, ``network``, ``pool``, ``node_device`` or ``secret``.
|
|
138
|
+
:param kinds: which events; ``None`` for the ``lifecycle`` ones.
|
|
139
|
+
:param target: limit to one object (a reference); ``None`` for all.
|
|
140
|
+
:param max_events: the most events kept for a slow reader.
|
|
141
|
+
:returns: the subscription.
|
|
142
|
+
:raises ValueError: if the source or a kind is unknown.
|
|
143
|
+
"""
|
|
144
|
+
plan = plan_subscription(source, kinds)
|
|
145
|
+
handle = None if target is None else self.call(event_lookup(source, target))
|
|
146
|
+
rpc = self._channel(plan.register.driver)
|
|
147
|
+
|
|
148
|
+
subscription = Subscription(rpc, plan.deregister, max_events)
|
|
149
|
+
try:
|
|
150
|
+
self._register(subscription, plan, handle, rpc)
|
|
151
|
+
except Exception:
|
|
152
|
+
subscription.close()
|
|
153
|
+
raise
|
|
154
|
+
return subscription
|
|
155
|
+
|
|
156
|
+
def close(self) -> None:
|
|
157
|
+
"""Close every open channel; the transport does not reconnect after this."""
|
|
158
|
+
self._closed = True
|
|
159
|
+
for channel in set(self._channels.values()):
|
|
160
|
+
channel.close()
|
|
161
|
+
self._channels.clear()
|
|
162
|
+
|
|
163
|
+
def _channel(self, driver: Driver) -> SyncRpc:
|
|
164
|
+
"""Return the channel of a driver, connecting on first use.
|
|
165
|
+
|
|
166
|
+
A channel whose connection was lost is replaced by a new one if the
|
|
167
|
+
connection options ask for reconnecting; otherwise it is returned and the
|
|
168
|
+
call on it fails with ``ConnectionLostError``.
|
|
169
|
+
|
|
170
|
+
:param driver: the part of libvirt that serves the call.
|
|
171
|
+
:returns: the channel; a monolithic ``libvirtd`` shares one for all.
|
|
172
|
+
:raises ConnectionLostError: if the transport was closed.
|
|
173
|
+
:raises ConnectionError: if the connection cannot be opened again.
|
|
174
|
+
"""
|
|
175
|
+
key = REMOTE_KEY if self._target.remote else driver
|
|
176
|
+
with self._opening:
|
|
177
|
+
if self._closed:
|
|
178
|
+
raise ConnectionLostError("the client is closed")
|
|
179
|
+
|
|
180
|
+
channel = self._channels.get(key)
|
|
181
|
+
if channel is None:
|
|
182
|
+
return self._open(driver, key)
|
|
183
|
+
if channel.failure is None or not self._reconnecting():
|
|
184
|
+
return channel
|
|
185
|
+
|
|
186
|
+
self._forget(channel)
|
|
187
|
+
return self._reopen(driver, key)
|
|
188
|
+
|
|
189
|
+
def _reconnecting(self) -> bool:
|
|
190
|
+
"""Tell whether lost connections are opened again.
|
|
191
|
+
|
|
192
|
+
:returns: ``True`` if the options ask for it.
|
|
193
|
+
"""
|
|
194
|
+
return self._options is not None and self._options.reconnects
|
|
195
|
+
|
|
196
|
+
def _reopen(self, driver: Driver, key: str) -> SyncRpc:
|
|
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
|
+
time.sleep(wait)
|
|
210
|
+
try:
|
|
211
|
+
return self._open(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
|
+
def _forget(self, channel: SyncRpc) -> None:
|
|
219
|
+
"""Drop a dead channel and close what is left of it.
|
|
220
|
+
|
|
221
|
+
:param channel: the channel.
|
|
222
|
+
"""
|
|
223
|
+
for key in [key for key, value in self._channels.items() if value is channel]:
|
|
224
|
+
del self._channels[key]
|
|
225
|
+
channel.close()
|
|
226
|
+
|
|
227
|
+
def _register(
|
|
228
|
+
self,
|
|
229
|
+
subscription: Subscription,
|
|
230
|
+
plan: SubscriptionPlan,
|
|
231
|
+
handle: object,
|
|
232
|
+
rpc: SyncRpc,
|
|
233
|
+
) -> None:
|
|
234
|
+
"""Register the events of a subscription on a connection.
|
|
235
|
+
|
|
236
|
+
:param subscription: the subscription.
|
|
237
|
+
:param plan: which registrations to make.
|
|
238
|
+
:param handle: the object to limit to, or ``None``.
|
|
239
|
+
:param rpc: the connection.
|
|
240
|
+
"""
|
|
241
|
+
if self._reconnecting():
|
|
242
|
+
rpc.when_lost(
|
|
243
|
+
lambda _error: threading.Thread(
|
|
244
|
+
target=self._restore,
|
|
245
|
+
args=(subscription, plan, handle),
|
|
246
|
+
daemon=True,
|
|
247
|
+
).start()
|
|
248
|
+
)
|
|
249
|
+
else:
|
|
250
|
+
rpc.when_lost(subscription.lost)
|
|
251
|
+
|
|
252
|
+
for arguments in plan.arguments(handle):
|
|
253
|
+
rpc.call(
|
|
254
|
+
plan.register,
|
|
255
|
+
convert_arguments(plan.register, arguments),
|
|
256
|
+
on_result=lambda reply: subscription.attach(reply.callback_id),
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
def _restore(
|
|
260
|
+
self, subscription: Subscription, plan: SubscriptionPlan, handle: object
|
|
261
|
+
) -> None:
|
|
262
|
+
"""Register a subscription again on a new connection (own thread).
|
|
263
|
+
|
|
264
|
+
:param subscription: the subscription of the lost connection.
|
|
265
|
+
:param plan: which registrations to make.
|
|
266
|
+
:param handle: the object to limit to, or ``None``.
|
|
267
|
+
"""
|
|
268
|
+
try:
|
|
269
|
+
rpc = self._channel(plan.register.driver)
|
|
270
|
+
if subscription.rebind(rpc):
|
|
271
|
+
self._register(subscription, plan, handle, rpc)
|
|
272
|
+
except Exception as failure: # background thread: report, do not crash
|
|
273
|
+
subscription.lost(failure)
|
|
274
|
+
|
|
275
|
+
def _open(self, driver: Driver, key: str) -> SyncRpc:
|
|
276
|
+
"""Connect, authenticate and open a channel, reusing a shared socket.
|
|
277
|
+
|
|
278
|
+
:param driver: the part of libvirt that serves the call.
|
|
279
|
+
:param key: the channel's key.
|
|
280
|
+
:returns: the ready channel.
|
|
281
|
+
"""
|
|
282
|
+
path, rpc = open_channel(
|
|
283
|
+
self._target,
|
|
284
|
+
driver,
|
|
285
|
+
read_only=self._read_only,
|
|
286
|
+
sasl=self._sasl,
|
|
287
|
+
options=self._options,
|
|
288
|
+
)
|
|
289
|
+
shared = self._channels.get(f"path:{path}")
|
|
290
|
+
if shared is not None:
|
|
291
|
+
rpc.close()
|
|
292
|
+
self._channels[key] = shared
|
|
293
|
+
return shared
|
|
294
|
+
|
|
295
|
+
self._channels[f"path:{path}"] = rpc
|
|
296
|
+
self._channels[key] = rpc
|
|
297
|
+
return rpc
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def convert_arguments(procedure: Procedure[Any, Any], arguments: Any) -> Any:
|
|
301
|
+
"""Build the argument struct of a procedure from named values.
|
|
302
|
+
|
|
303
|
+
:param procedure: the procedure to call.
|
|
304
|
+
:param arguments: the values by argument name.
|
|
305
|
+
:returns: the argument struct, or ``None`` if the procedure has none.
|
|
306
|
+
:raises ProtocolError: if a name is missing or unknown.
|
|
307
|
+
"""
|
|
308
|
+
if procedure.args is None:
|
|
309
|
+
return None
|
|
310
|
+
|
|
311
|
+
names = [name for name, _ in procedure.args.xdr_fields]
|
|
312
|
+
if sorted(arguments) != sorted(names):
|
|
313
|
+
raise ProtocolError(f"{procedure.name} takes {names}, got {sorted(arguments)}")
|
|
314
|
+
|
|
315
|
+
values = {
|
|
316
|
+
name: _to_wire(xdr_type, arguments[name])
|
|
317
|
+
for name, xdr_type in procedure.args.xdr_fields
|
|
318
|
+
}
|
|
319
|
+
return procedure.args(**values)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def convert_reply(procedure: Procedure[Any, Any], reply: Any) -> Any:
|
|
323
|
+
"""Turn a reply struct into the value the operations expect.
|
|
324
|
+
|
|
325
|
+
:param procedure: the procedure that was called.
|
|
326
|
+
:param reply: its return struct, or ``None``.
|
|
327
|
+
:returns: ``Handle``s, text, numbers, bytes, typed-parameter dicts,
|
|
328
|
+
``DomainInfo``, lists of these, or for a reply of several fields the
|
|
329
|
+
generated return struct itself.
|
|
330
|
+
"""
|
|
331
|
+
if reply is None:
|
|
332
|
+
return None
|
|
333
|
+
if procedure is procedures.DOMAIN_GET_INFO:
|
|
334
|
+
return _domain_info(reply)
|
|
335
|
+
if procedure is procedures.DOMAIN_GET_STATE:
|
|
336
|
+
return int(reply.state)
|
|
337
|
+
|
|
338
|
+
fields = dict(procedure.ret.xdr_fields) if procedure.ret else {}
|
|
339
|
+
names = [name for name in fields if name != "ret" or len(fields) != 2]
|
|
340
|
+
if len(names) != 1:
|
|
341
|
+
return reply
|
|
342
|
+
return _from_wire(getattr(reply, names[0]), fields[names[0]])
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _to_wire(xdr_type: Any, value: Any) -> Any:
|
|
346
|
+
"""Convert one argument to what its XDR type holds.
|
|
347
|
+
|
|
348
|
+
:param xdr_type: the descriptor of the field.
|
|
349
|
+
:param value: the value the operation gave.
|
|
350
|
+
:returns: the value for the struct.
|
|
351
|
+
:raises ProtocolError: if a handle does not come from a daemon.
|
|
352
|
+
"""
|
|
353
|
+
if isinstance(value, Handle):
|
|
354
|
+
return _handle_to_wire(xdr_type, value)
|
|
355
|
+
if isinstance(value, dict) and _is_typed_params(xdr_type):
|
|
356
|
+
return typed_params.to_wire(value)
|
|
357
|
+
if isinstance(xdr_type, xdr.FixedOpaque) and isinstance(value, str):
|
|
358
|
+
return uuid.UUID(value).bytes
|
|
359
|
+
return value
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def _handle_to_wire(xdr_type: Any, handle: Handle) -> Any:
|
|
363
|
+
"""Convert an object handle to the struct (or name) a field holds.
|
|
364
|
+
|
|
365
|
+
:param xdr_type: the descriptor of the field.
|
|
366
|
+
:param handle: the object.
|
|
367
|
+
:returns: the daemon's own struct for the object, or its name for a text field.
|
|
368
|
+
:raises ProtocolError: if the handle was not returned by a daemon.
|
|
369
|
+
"""
|
|
370
|
+
if isinstance(xdr_type, xdr.String):
|
|
371
|
+
return handle.name
|
|
372
|
+
if handle.native is None:
|
|
373
|
+
raise ProtocolError(f"{handle!r} does not come from a daemon")
|
|
374
|
+
return handle.native
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def _is_typed_params(xdr_type: Any) -> bool:
|
|
378
|
+
"""Tell whether a descriptor is a list of typed parameters.
|
|
379
|
+
|
|
380
|
+
:param xdr_type: the descriptor of a field.
|
|
381
|
+
:returns: ``True`` for ``remote_typed_param<>``.
|
|
382
|
+
"""
|
|
383
|
+
item = getattr(xdr_type, "item", None)
|
|
384
|
+
return isinstance(item, xdr.StructType) and item.cls is types.TypedParam
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def _from_wire(value: Any, xdr_type: Any = None) -> Any:
|
|
388
|
+
"""Convert a returned value: object structs become handles, parameters dicts.
|
|
389
|
+
|
|
390
|
+
:param value: a field of a return struct.
|
|
391
|
+
:param xdr_type: the descriptor of the field, if known.
|
|
392
|
+
:returns: the value for the operations.
|
|
393
|
+
"""
|
|
394
|
+
if _is_typed_params(xdr_type):
|
|
395
|
+
return typed_params.from_wire(value)
|
|
396
|
+
if isinstance(value, list):
|
|
397
|
+
return [_from_wire(item) for item in value]
|
|
398
|
+
if type(value).__name__.startswith("Nonnull"):
|
|
399
|
+
return _handle_from_struct(value)
|
|
400
|
+
return value
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def _handle_from_struct(struct: Any) -> Handle:
|
|
404
|
+
"""Describe a daemon's object struct as a ``Handle``.
|
|
405
|
+
|
|
406
|
+
:param struct: a ``Nonnull*`` struct.
|
|
407
|
+
:returns: the handle, which keeps the struct for later requests.
|
|
408
|
+
"""
|
|
409
|
+
uuid_bytes = getattr(struct, "uuid", None)
|
|
410
|
+
uuid_text = str(uuid.UUID(bytes=uuid_bytes)) if uuid_bytes else None
|
|
411
|
+
domain_id = getattr(struct, "id", None)
|
|
412
|
+
owner = getattr(struct, "pool", None) or getattr(
|
|
413
|
+
getattr(struct, "dom", None), "name", None
|
|
414
|
+
)
|
|
415
|
+
return Handle(
|
|
416
|
+
name=getattr(struct, "name", None) or uuid_text or "",
|
|
417
|
+
uuid=uuid_text,
|
|
418
|
+
id=domain_id if domain_id is not None and domain_id >= 0 else None,
|
|
419
|
+
parent=owner,
|
|
420
|
+
native=struct,
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _domain_info(reply: types.DomainGetInfoRet) -> DomainInfo:
|
|
425
|
+
"""Convert ``remote_domain_get_info_ret``.
|
|
426
|
+
|
|
427
|
+
:param reply: the return struct.
|
|
428
|
+
:returns: the information.
|
|
429
|
+
"""
|
|
430
|
+
return DomainInfo(
|
|
431
|
+
state=DomainState(reply.state),
|
|
432
|
+
max_memory_kib=reply.max_mem,
|
|
433
|
+
memory_kib=reply.memory,
|
|
434
|
+
vcpus=reply.nr_virt_cpu,
|
|
435
|
+
cpu_time_ns=reply.cpu_time,
|
|
436
|
+
)
|