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,246 @@
1
+ """Operations on the host itself: CPUs, memory, huge pages, suspend."""
2
+
3
+ from libvirtpy.client.operations._common import (
4
+ as_int,
5
+ as_list,
6
+ as_struct,
7
+ parameter_count,
8
+ parameters_of,
9
+ )
10
+ from libvirtpy.client.transport import Operation, Request
11
+ from libvirtpy.rpc.protocol import types
12
+ from libvirtpy.rpc.typed_params import TypedValue, from_wire, to_wire
13
+
14
+ __all__ = [
15
+ "allocate_host_pages",
16
+ "get_host_cells_free_memory",
17
+ "get_host_cpu_map",
18
+ "get_host_cpu_stats",
19
+ "get_host_free_memory",
20
+ "get_host_free_pages",
21
+ "get_host_info",
22
+ "get_host_memory_parameters",
23
+ "get_host_memory_stats",
24
+ "get_host_security_model",
25
+ "get_host_sev_info",
26
+ "set_host_memory_parameters",
27
+ "suspend_host_for_duration",
28
+ ]
29
+
30
+
31
+ def get_host_info() -> Operation[types.NodeGetInfoRet]:
32
+ """Return the hardware summary of the host.
33
+
34
+ :returns: ``model`` (CPU model as character codes), ``memory`` (KiB), ``cpus``,
35
+ ``mhz``, ``nodes``, ``sockets``, ``cores`` and ``threads``.
36
+ """
37
+ return as_struct((yield Request("node_get_info")), types.NodeGetInfoRet)
38
+
39
+
40
+ def get_host_free_memory() -> Operation[int]:
41
+ """Return the free memory of the host.
42
+
43
+ :returns: the free memory in bytes.
44
+ """
45
+ reply = yield Request("node_get_free_memory")
46
+ return as_int(reply)
47
+
48
+
49
+ def get_host_cells_free_memory(
50
+ start_cell: int = 0, max_cells: int = 64
51
+ ) -> Operation[list[int]]:
52
+ """Return the free memory of each NUMA cell of the host.
53
+
54
+ :param start_cell: the first cell.
55
+ :param max_cells: how many cells to ask for at most.
56
+ :returns: the free memory of each cell, in bytes.
57
+ """
58
+ reply = yield Request(
59
+ "node_get_cells_free_memory", {"start_cell": start_cell, "maxcells": max_cells}
60
+ )
61
+ return [as_int(item) for item in as_list(reply)]
62
+
63
+
64
+ def get_host_cpu_stats(cpu: int = -1, *, flags: int = 0) -> Operation[dict[str, int]]:
65
+ """Return the CPU time statistics of the host.
66
+
67
+ :param cpu: the host CPU number; -1 for all together.
68
+ :param flags: reserved, 0.
69
+ :returns: ``kernel``, ``user``, ``idle``, ``iowait``, ``intr`` in nanoseconds.
70
+ """
71
+ first = yield Request(
72
+ "node_get_cpu_stats", {"cpu_num": cpu, "nparams": 0, "flags": flags}
73
+ )
74
+ reply = yield Request(
75
+ "node_get_cpu_stats",
76
+ {"cpu_num": cpu, "nparams": parameter_count(first), "flags": flags},
77
+ )
78
+ return _name_value_pairs(reply)
79
+
80
+
81
+ def get_host_memory_stats(
82
+ cell: int = -1, *, flags: int = 0
83
+ ) -> Operation[dict[str, int]]:
84
+ """Return the memory statistics of the host.
85
+
86
+ :param cell: the NUMA cell; -1 for the whole host.
87
+ :param flags: reserved, 0.
88
+ :returns: ``total``, ``free``, ``buffers``, ``cached`` in KiB.
89
+ """
90
+ first = yield Request(
91
+ "node_get_memory_stats", {"nparams": 0, "cell_num": cell, "flags": flags}
92
+ )
93
+ reply = yield Request(
94
+ "node_get_memory_stats",
95
+ {"nparams": parameter_count(first), "cell_num": cell, "flags": flags},
96
+ )
97
+ return _name_value_pairs(reply)
98
+
99
+
100
+ def get_host_memory_parameters(*, flags: int = 0) -> Operation[dict[str, TypedValue]]:
101
+ """Return the memory sharing (KSM) settings of the host.
102
+
103
+ :param flags: reserved, 0.
104
+ :returns: ``shm_pages_to_scan``, ``shm_sleep_millisecs``, ...
105
+ """
106
+ first = yield Request("node_get_memory_parameters", {"nparams": 0, "flags": flags})
107
+ reply = yield Request(
108
+ "node_get_memory_parameters",
109
+ {"nparams": parameter_count(first), "flags": flags},
110
+ )
111
+ return from_wire(parameters_of(reply))
112
+
113
+
114
+ def set_host_memory_parameters(
115
+ parameters: dict[str, TypedValue], *, flags: int = 0
116
+ ) -> Operation[None]:
117
+ """Change the memory sharing (KSM) settings of the host.
118
+
119
+ :param parameters: e.g. ``{"shm_sleep_millisecs": 20}``.
120
+ :param flags: reserved, 0.
121
+ """
122
+ yield Request(
123
+ "node_set_memory_parameters", {"params": to_wire(parameters), "flags": flags}
124
+ )
125
+
126
+
127
+ def get_host_cpu_map(*, flags: int = 0) -> Operation[types.NodeGetCpuMapRet]:
128
+ """Return which host CPUs exist and which are online.
129
+
130
+ :param flags: reserved, 0.
131
+ :returns: ``cpumap`` (a bit mask of online CPUs), ``online`` (their count) and
132
+ the total in ``ret``.
133
+ """
134
+ reply = yield Request(
135
+ "node_get_cpu_map", {"need_map": 1, "need_online": 1, "flags": flags}
136
+ )
137
+ return as_struct(reply, types.NodeGetCpuMapRet)
138
+
139
+
140
+ def get_host_security_model() -> Operation[types.NodeGetSecurityModelRet]:
141
+ """Return the security driver of the host.
142
+
143
+ :returns: ``model`` and ``doi`` as character codes.
144
+ """
145
+ reply = yield Request("node_get_security_model")
146
+ return as_struct(reply, types.NodeGetSecurityModelRet)
147
+
148
+
149
+ def get_host_free_pages(
150
+ page_sizes: list[int], start_cell: int = 0, cell_count: int = 1, *, flags: int = 0
151
+ ) -> Operation[list[int]]:
152
+ """Return how many pages of each size are free.
153
+
154
+ :param page_sizes: the page sizes in KiB (4, 2048, 1048576...).
155
+ :param start_cell: the first NUMA cell.
156
+ :param cell_count: how many cells.
157
+ :param flags: reserved, 0.
158
+ :returns: the free page counts, for each cell and size.
159
+ """
160
+ reply = yield Request(
161
+ "node_get_free_pages",
162
+ {
163
+ "pages": page_sizes,
164
+ "start_cell": start_cell,
165
+ "cell_count": cell_count,
166
+ "flags": flags,
167
+ },
168
+ )
169
+ return [as_int(item) for item in as_list(reply)]
170
+
171
+
172
+ def allocate_host_pages(
173
+ page_sizes: list[int],
174
+ page_counts: list[int],
175
+ start_cell: int = 0,
176
+ cell_count: int = 1,
177
+ *,
178
+ flags: int = 0,
179
+ ) -> Operation[int]:
180
+ """Reserve huge pages on the host.
181
+
182
+ :param page_sizes: the page sizes in KiB.
183
+ :param page_counts: how many pages of each size.
184
+ :param start_cell: the first NUMA cell.
185
+ :param cell_count: how many cells.
186
+ :param flags: ``virNodeAllocPagesFlags``: 1 add, 2 set.
187
+ :returns: how many pages were allocated.
188
+ """
189
+ reply = yield Request(
190
+ "node_alloc_pages",
191
+ {
192
+ "page_sizes": page_sizes,
193
+ "page_counts": page_counts,
194
+ "start_cell": start_cell,
195
+ "cell_count": cell_count,
196
+ "flags": flags,
197
+ },
198
+ )
199
+ return as_int(reply)
200
+
201
+
202
+ def get_host_sev_info(*, flags: int = 0) -> Operation[dict[str, TypedValue]]:
203
+ """Return the AMD SEV capabilities of the host.
204
+
205
+ :param flags: reserved, 0.
206
+ :returns: ``cert-chain``, ``cbitpos``, ``reduced-phys-bits``...
207
+ """
208
+ first = yield Request("node_get_sev_info", {"nparams": 0, "flags": flags})
209
+ reply = yield Request(
210
+ "node_get_sev_info", {"nparams": parameter_count(first), "flags": flags}
211
+ )
212
+ return from_wire(parameters_of(reply))
213
+
214
+
215
+ def suspend_host_for_duration(
216
+ target: int, duration: int, *, flags: int = 0
217
+ ) -> Operation[None]:
218
+ """Suspend the whole host for a time.
219
+
220
+ :param target: ``virNodeSuspendTarget``: 0 memory, 1 disk, 2 hybrid.
221
+ :param duration: seconds until it wakes up.
222
+ :param flags: reserved, 0.
223
+ """
224
+ yield Request(
225
+ "node_suspend_for_duration",
226
+ {"target": target, "duration": duration, "flags": flags},
227
+ )
228
+
229
+
230
+ def _name_value_pairs(reply: object) -> dict[str, int]:
231
+ """Make a dict from the ``(field, value)`` list of a host statistics reply.
232
+
233
+ :param reply: a return struct with a ``params`` list of ``field``/``value``.
234
+ :returns: the statistics by name.
235
+ :raises TypeError: if the reply is not of that shape.
236
+ """
237
+ items = getattr(reply, "params", None)
238
+ if not isinstance(items, list):
239
+ raise TypeError(f"expected a reply with params, got {reply!r}")
240
+
241
+ statistics = {}
242
+ for item in items:
243
+ if not isinstance(item, types.NodeGetCpuStats | types.NodeGetMemoryStats):
244
+ raise TypeError(f"expected a statistic, got {item!r}")
245
+ statistics[item.field] = item.value
246
+ return statistics
@@ -0,0 +1,100 @@
1
+ """Operations on the network interfaces of the host."""
2
+
3
+ from libvirtpy.client.operations._common import (
4
+ ACTIVE,
5
+ INACTIVE,
6
+ as_xml,
7
+ fetch_model,
8
+ fetch_models,
9
+ many_handles,
10
+ one_handle,
11
+ )
12
+ from libvirtpy.client.references import (
13
+ HostInterfaceRef,
14
+ host_interface_lookup,
15
+ )
16
+ from libvirtpy.client.transport import Operation, Request
17
+ from libvirtpy.xml.interface import HostInterface
18
+
19
+ __all__ = [
20
+ "define_host_interface",
21
+ "destroy_host_interface",
22
+ "list_host_interfaces",
23
+ "lookup_host_interface",
24
+ "start_host_interface",
25
+ "undefine_host_interface",
26
+ ]
27
+
28
+
29
+ def lookup_host_interface(interface: HostInterfaceRef) -> Operation[HostInterface]:
30
+ """Return a network interface of the host.
31
+
32
+ :param interface: a device name or MAC address, or a ``HostInterface`` model.
33
+ :returns: the interface.
34
+ :raises NoInterfaceError: if there is no such interface.
35
+ """
36
+ handle = yield from one_handle(host_interface_lookup(interface))
37
+ return (yield from fetch_model("interface_get_xml_desc", handle, HostInterface))
38
+
39
+
40
+ def list_host_interfaces(
41
+ *, active: bool | None = None
42
+ ) -> Operation[list[HostInterface]]:
43
+ """Return the network interfaces of the host.
44
+
45
+ :param active: ``True`` for active interfaces only, ``False`` for the others,
46
+ ``None`` for all.
47
+ :returns: the interfaces.
48
+ """
49
+ flags = {True: ACTIVE, False: INACTIVE, None: 0}[active]
50
+ handles = yield from many_handles(
51
+ Request("connect_list_all_interfaces", {"need_results": 1, "flags": flags})
52
+ )
53
+ return (yield from fetch_models("interface_get_xml_desc", handles, HostInterface))
54
+
55
+
56
+ def define_host_interface(
57
+ interface: HostInterface | str, *, flags: int = 0
58
+ ) -> Operation[HostInterface]:
59
+ """Define (or redefine) a host network interface.
60
+
61
+ :param interface: the configuration, as a model or XML text.
62
+ :param flags: reserved, ``0``.
63
+ :returns: the stored configuration.
64
+ """
65
+ xml = as_xml(interface)
66
+ handle = yield from one_handle(
67
+ Request("interface_define_xml", {"xml": xml, "flags": flags})
68
+ )
69
+ return (yield from fetch_model("interface_get_xml_desc", handle, HostInterface))
70
+
71
+
72
+ def undefine_host_interface(interface: HostInterfaceRef) -> Operation[None]:
73
+ """Remove the definition of a host network interface.
74
+
75
+ :param interface: the interface.
76
+ """
77
+ handle = yield from one_handle(host_interface_lookup(interface))
78
+ yield Request("interface_undefine", {"iface": handle})
79
+
80
+
81
+ def start_host_interface(interface: HostInterfaceRef) -> Operation[HostInterface]:
82
+ """Bring a host network interface up.
83
+
84
+ :param interface: the interface.
85
+ :returns: the interface, now active.
86
+ :raises OperationInvalidError: if it is already active.
87
+ """
88
+ handle = yield from one_handle(host_interface_lookup(interface))
89
+ yield Request("interface_create", {"iface": handle, "flags": 0})
90
+ return (yield from fetch_model("interface_get_xml_desc", handle, HostInterface))
91
+
92
+
93
+ def destroy_host_interface(interface: HostInterfaceRef) -> Operation[None]:
94
+ """Bring a host network interface down.
95
+
96
+ :param interface: the interface.
97
+ :raises OperationInvalidError: if it is not active.
98
+ """
99
+ handle = yield from one_handle(host_interface_lookup(interface))
100
+ yield Request("interface_destroy", {"iface": handle, "flags": 0})
@@ -0,0 +1,45 @@
1
+ """Operations on host network interfaces beyond the basic ones."""
2
+
3
+ from libvirtpy.client.operations._common import as_bool, on_host_interface
4
+ from libvirtpy.client.references import HostInterfaceRef
5
+ from libvirtpy.client.transport import Operation, Request
6
+
7
+ __all__ = [
8
+ "begin_host_interface_changes",
9
+ "commit_host_interface_changes",
10
+ "is_host_interface_active",
11
+ "rollback_host_interface_changes",
12
+ ]
13
+
14
+
15
+ def is_host_interface_active(interface: HostInterfaceRef) -> Operation[bool]:
16
+ """Tell whether a host network interface is up.
17
+
18
+ :param interface: the interface, by name or MAC address.
19
+ :returns: ``True`` if it is active.
20
+ """
21
+ return as_bool((yield from on_host_interface(interface, "interface_is_active")))
22
+
23
+
24
+ def begin_host_interface_changes(*, flags: int = 0) -> Operation[None]:
25
+ """Start a transaction of host network changes (they can be rolled back).
26
+
27
+ :param flags: reserved, 0.
28
+ """
29
+ yield Request("interface_change_begin", {"flags": flags})
30
+
31
+
32
+ def commit_host_interface_changes(*, flags: int = 0) -> Operation[None]:
33
+ """Keep the host network changes made since ``begin_host_interface_changes``.
34
+
35
+ :param flags: reserved, 0.
36
+ """
37
+ yield Request("interface_change_commit", {"flags": flags})
38
+
39
+
40
+ def rollback_host_interface_changes(*, flags: int = 0) -> Operation[None]:
41
+ """Undo the host network changes made since ``begin_host_interface_changes``.
42
+
43
+ :param flags: reserved, 0.
44
+ """
45
+ yield Request("interface_change_rollback", {"flags": flags})
@@ -0,0 +1,135 @@
1
+ """Operations on virtual networks."""
2
+
3
+ from libvirtpy.client.operations._common import (
4
+ ACTIVE,
5
+ INACTIVE,
6
+ as_int,
7
+ as_xml,
8
+ fetch_model,
9
+ fetch_models,
10
+ many_handles,
11
+ one_handle,
12
+ )
13
+ from libvirtpy.client.references import (
14
+ NetworkRef,
15
+ network_lookup,
16
+ )
17
+ from libvirtpy.client.transport import Operation, Request
18
+ from libvirtpy.xml.network import Network
19
+
20
+ __all__ = [
21
+ "create_network",
22
+ "define_network",
23
+ "destroy_network",
24
+ "get_network_autostart",
25
+ "list_networks",
26
+ "lookup_network",
27
+ "set_network_autostart",
28
+ "start_network",
29
+ "undefine_network",
30
+ ]
31
+
32
+
33
+ def lookup_network(network: NetworkRef) -> Operation[Network]:
34
+ """Return the configuration of a virtual network.
35
+
36
+ :param network: a name, UUID or ``Network`` model.
37
+ :returns: the network.
38
+ :raises NoNetworkError: if there is no such network.
39
+ """
40
+ handle = yield from one_handle(network_lookup(network))
41
+ return (yield from fetch_model("network_get_xml_desc", handle, Network))
42
+
43
+
44
+ def list_networks(*, active: bool | None = None) -> Operation[list[Network]]:
45
+ """Return the virtual networks.
46
+
47
+ :param active: ``True`` for active networks only, ``False`` for the others,
48
+ ``None`` for all.
49
+ :returns: the networks.
50
+ """
51
+ flags = {True: ACTIVE, False: INACTIVE, None: 0}[active]
52
+ handles = yield from many_handles(
53
+ Request("connect_list_all_networks", {"need_results": 1, "flags": flags})
54
+ )
55
+ return (yield from fetch_models("network_get_xml_desc", handles, Network))
56
+
57
+
58
+ def define_network(network: Network | str, *, flags: int = 0) -> Operation[Network]:
59
+ """Define (or redefine) a persistent network without starting it.
60
+
61
+ :param network: the configuration, as a model or XML text.
62
+ :param flags: ``virNetworkDefineFlags``.
63
+ :returns: the stored configuration, with what libvirt filled in.
64
+ """
65
+ xml = as_xml(network)
66
+ handle = yield from one_handle(
67
+ Request("network_define_xml_flags", {"xml": xml, "flags": flags})
68
+ )
69
+ return (yield from fetch_model("network_get_xml_desc", handle, Network))
70
+
71
+
72
+ def create_network(network: Network | str, *, flags: int = 0) -> Operation[Network]:
73
+ """Start a transient network: it disappears when it stops.
74
+
75
+ :param network: the configuration, as a model or XML text.
76
+ :param flags: ``virNetworkCreateFlags``.
77
+ :returns: the running network.
78
+ """
79
+ xml = as_xml(network)
80
+ handle = yield from one_handle(
81
+ Request("network_create_xml_flags", {"xml": xml, "flags": flags})
82
+ )
83
+ return (yield from fetch_model("network_get_xml_desc", handle, Network))
84
+
85
+
86
+ def undefine_network(network: NetworkRef) -> Operation[None]:
87
+ """Remove a persistent network's definition.
88
+
89
+ :param network: the network.
90
+ :raises OperationInvalidError: if the network is active.
91
+ """
92
+ handle = yield from one_handle(network_lookup(network))
93
+ yield Request("network_undefine", {"net": handle})
94
+
95
+
96
+ def start_network(network: NetworkRef) -> Operation[Network]:
97
+ """Start a defined network.
98
+
99
+ :param network: the network.
100
+ :returns: the network, now active.
101
+ :raises OperationInvalidError: if the network is already active.
102
+ """
103
+ handle = yield from one_handle(network_lookup(network))
104
+ yield Request("network_create", {"net": handle})
105
+ return (yield from fetch_model("network_get_xml_desc", handle, Network))
106
+
107
+
108
+ def destroy_network(network: NetworkRef) -> Operation[None]:
109
+ """Stop a network.
110
+
111
+ :param network: the network.
112
+ :raises OperationInvalidError: if the network is not active.
113
+ """
114
+ handle = yield from one_handle(network_lookup(network))
115
+ yield Request("network_destroy", {"net": handle})
116
+
117
+
118
+ def get_network_autostart(network: NetworkRef) -> Operation[bool]:
119
+ """Tell whether a network starts with the host.
120
+
121
+ :param network: the network.
122
+ :returns: ``True`` if it starts at boot.
123
+ """
124
+ handle = yield from one_handle(network_lookup(network))
125
+ return bool(as_int((yield Request("network_get_autostart", {"net": handle}))))
126
+
127
+
128
+ def set_network_autostart(network: NetworkRef, enabled: bool) -> Operation[None]:
129
+ """Make a network start (or not) with the host.
130
+
131
+ :param network: the network.
132
+ :param enabled: ``True`` to start it at boot.
133
+ """
134
+ handle = yield from one_handle(network_lookup(network))
135
+ yield Request("network_set_autostart", {"net": handle, "autostart": int(enabled)})
@@ -0,0 +1,155 @@
1
+ """Operations that read or change parts of a virtual network."""
2
+
3
+ from libvirtpy.client.operations._common import (
4
+ as_bool,
5
+ as_list,
6
+ as_struct,
7
+ as_text,
8
+ on_network,
9
+ on_object,
10
+ )
11
+ from libvirtpy.client.references import NetworkRef, network_lookup
12
+ from libvirtpy.client.transport import Operation
13
+ from libvirtpy.rpc.protocol import types
14
+
15
+ __all__ = [
16
+ "get_network_bridge_name",
17
+ "get_network_metadata",
18
+ "is_network_active",
19
+ "is_network_persistent",
20
+ "list_network_dhcp_leases",
21
+ "set_network_metadata",
22
+ "update_network",
23
+ ]
24
+
25
+
26
+ def get_network_bridge_name(network: NetworkRef) -> Operation[str]:
27
+ """Return the name of the bridge device of a network.
28
+
29
+ :param network: the network.
30
+ :returns: e.g. ``virbr0``.
31
+ """
32
+ return as_text((yield from on_network(network, "network_get_bridge_name")))
33
+
34
+
35
+ def is_network_active(network: NetworkRef) -> Operation[bool]:
36
+ """Tell whether a network is started.
37
+
38
+ :param network: the network.
39
+ :returns: ``True`` if it is active.
40
+ """
41
+ return as_bool((yield from on_network(network, "network_is_active")))
42
+
43
+
44
+ def is_network_persistent(network: NetworkRef) -> Operation[bool]:
45
+ """Tell whether a network has a stored configuration.
46
+
47
+ :param network: the network.
48
+ :returns: ``True`` if persistent, ``False`` if transient.
49
+ """
50
+ return as_bool((yield from on_network(network, "network_is_persistent")))
51
+
52
+
53
+ def update_network(
54
+ network: NetworkRef,
55
+ command: int,
56
+ section: int,
57
+ xml: str,
58
+ *,
59
+ parent_index: int = -1,
60
+ flags: int = 0,
61
+ ) -> Operation[None]:
62
+ """Change part of a network's configuration, without redefining it.
63
+
64
+ :param network: the network.
65
+ :param command: ``virNetworkUpdateCommand``: 1 modify, 2 delete, 3 add last,
66
+ 4 add first.
67
+ :param section: ``virNetworkUpdateSection``: 1 bridge, 2 domain, 3 ip,
68
+ 4 ip dhcp host, 5 ip dhcp range, 6 forward, ...
69
+ :param xml: the XML of the element to add, modify or delete.
70
+ :param parent_index: which parent element, or -1 for any.
71
+ :param flags: ``virNetworkUpdateFlags``: live, config.
72
+ """
73
+ yield from on_network(
74
+ network,
75
+ "network_update",
76
+ command=command,
77
+ section=section,
78
+ parent_index=parent_index,
79
+ xml=xml,
80
+ flags=flags,
81
+ )
82
+
83
+
84
+ def list_network_dhcp_leases(
85
+ network: NetworkRef, mac: str | None = None, *, flags: int = 0
86
+ ) -> Operation[list[types.NetworkDhcpLease]]:
87
+ """List the DHCP leases a network has given out.
88
+
89
+ :param network: the network.
90
+ :param mac: only the leases of this MAC address; ``None`` for all.
91
+ :param flags: reserved, 0.
92
+ :returns: each lease's ``ipaddr``, ``mac``, ``hostname``, ``expirytime``...
93
+ """
94
+ reply = yield from on_network(
95
+ network, "network_get_dhcp_leases", mac=mac, need_results=1, flags=flags
96
+ )
97
+ return [as_struct(item, types.NetworkDhcpLease) for item in as_list(reply)]
98
+
99
+
100
+ def get_network_metadata(
101
+ network: NetworkRef,
102
+ metadata_type: int = 0,
103
+ namespace: str | None = None,
104
+ *,
105
+ flags: int = 0,
106
+ ) -> Operation[str]:
107
+ """Read the metadata of a network.
108
+
109
+ :param network: the network.
110
+ :param metadata_type: ``virNetworkMetadataType``: 0 description, 1 title,
111
+ 2 an element (then give ``namespace``).
112
+ :param namespace: the XML namespace URI of an element; ``None`` otherwise.
113
+ :param flags: ``virNetworkUpdateFlags``: live, config.
114
+ :returns: the text or the XML of the element.
115
+ """
116
+ reply = yield from on_object(
117
+ network_lookup(network),
118
+ "network",
119
+ "network_get_metadata",
120
+ {"type": metadata_type, "uri": namespace, "flags": flags},
121
+ )
122
+ return as_text(reply)
123
+
124
+
125
+ def set_network_metadata(
126
+ network: NetworkRef,
127
+ metadata_type: int,
128
+ value: str | None,
129
+ *,
130
+ key: str | None = None,
131
+ namespace: str | None = None,
132
+ flags: int = 0,
133
+ ) -> Operation[None]:
134
+ """Set the metadata of a network.
135
+
136
+ :param network: the network.
137
+ :param metadata_type: ``virNetworkMetadataType``: 0 description, 1 title,
138
+ 2 an element (then give ``key`` and ``namespace``).
139
+ :param value: the text or the XML of the element; ``None`` removes it.
140
+ :param key: the namespace prefix of an element.
141
+ :param namespace: the XML namespace URI of an element.
142
+ :param flags: ``virNetworkUpdateFlags``: live, config.
143
+ """
144
+ yield from on_object(
145
+ network_lookup(network),
146
+ "network",
147
+ "network_set_metadata",
148
+ {
149
+ "type": metadata_type,
150
+ "metadata": value,
151
+ "key": key,
152
+ "uri": namespace,
153
+ "flags": flags,
154
+ },
155
+ )