mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-07-17 00:59:36 +03:00
examples: Replace sys.exit() with exit()
No need to import sys. Signed-off-by: Philipp Hahn <hahn@univention.de>
This commit is contained in:
committed by
Philipp Hahn
parent
f496dc55ee
commit
b801ff31fa
@ -4,7 +4,6 @@ Print leases info for a given virtual network
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
import time
|
import time
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
@ -18,18 +17,18 @@ try:
|
|||||||
conn = libvirt.open(args.uri)
|
conn = libvirt.open(args.uri)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print("Unable to open connection to libvirt")
|
print("Unable to open connection to libvirt")
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
net = conn.networkLookupByName(args.network)
|
net = conn.networkLookupByName(args.network)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print("Network %s not found" % args.network)
|
print("Network %s not found" % args.network)
|
||||||
sys.exit(0)
|
exit(0)
|
||||||
|
|
||||||
leases = net.DHCPLeases()
|
leases = net.DHCPLeases()
|
||||||
if not leases:
|
if not leases:
|
||||||
print("Failed to get leases for %s" % net.name())
|
print("Failed to get leases for %s" % net.name())
|
||||||
sys.exit(0)
|
exit(0)
|
||||||
|
|
||||||
def toIPAddrType(addrType: int) -> str:
|
def toIPAddrType(addrType: int) -> str:
|
||||||
if addrType == libvirt.VIR_IP_ADDR_TYPE_IPV4:
|
if addrType == libvirt.VIR_IP_ADDR_TYPE_IPV4:
|
||||||
|
@ -4,7 +4,6 @@ Print information about the domain DOMAIN
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
import libxml2
|
import libxml2
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -37,14 +36,14 @@ try:
|
|||||||
conn = libvirt.openReadOnly(None)
|
conn = libvirt.openReadOnly(None)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print('Failed to open connection to the hypervisor')
|
print('Failed to open connection to the hypervisor')
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dom = conn.lookupByName(args.domain)
|
dom = conn.lookupByName(args.domain)
|
||||||
# Annoyiingly, libvirt prints its own error message here
|
# Annoyiingly, libvirt prints its own error message here
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print("Domain %s is not running" % args.domain)
|
print("Domain %s is not running" % args.domain)
|
||||||
sys.exit(0)
|
exit(0)
|
||||||
|
|
||||||
info = dom.info()
|
info = dom.info()
|
||||||
print_section("Domain info")
|
print_section("Domain info")
|
||||||
|
@ -4,7 +4,6 @@ Print domain interfaces along with their MAC and IP addresses
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
IPTYPE = {
|
IPTYPE = {
|
||||||
@ -17,7 +16,7 @@ def print_dom_ifaces(dom: libvirt.virDomain) -> None:
|
|||||||
ifaces = dom.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE)
|
ifaces = dom.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE)
|
||||||
if ifaces is None:
|
if ifaces is None:
|
||||||
print("Failed to get domain interfaces")
|
print("Failed to get domain interfaces")
|
||||||
sys.exit(0)
|
exit(0)
|
||||||
|
|
||||||
print(" {0:10} {1:20} {2:12} {3}".format(
|
print(" {0:10} {1:20} {2:12} {3}".format(
|
||||||
"Interface", "MAC address", "Protocol", "Address"))
|
"Interface", "MAC address", "Protocol", "Address"))
|
||||||
@ -50,7 +49,7 @@ if __name__ == "__main__":
|
|||||||
dom = conn.lookupByName(args.domain)
|
dom = conn.lookupByName(args.domain)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print("Domain %s not found" % args.domain)
|
print("Domain %s not found" % args.domain)
|
||||||
sys.exit(0)
|
exit(0)
|
||||||
|
|
||||||
print_dom_ifaces(dom)
|
print_dom_ifaces(dom)
|
||||||
conn.close()
|
conn.close()
|
||||||
|
@ -5,7 +5,6 @@ It is assumed that all files in DIR are images of domU's previously created with
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
@ -20,7 +19,7 @@ try:
|
|||||||
conn = libvirt.open(None)
|
conn = libvirt.open(None)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print('Failed to open connection to the hypervisor')
|
print('Failed to open connection to the hypervisor')
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
for img in imgs:
|
for img in imgs:
|
||||||
file = os.path.join(args.dir, img)
|
file = os.path.join(args.dir, img)
|
||||||
|
@ -5,7 +5,6 @@ DIR must exist and be writable by this process.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
@ -18,7 +17,7 @@ try:
|
|||||||
conn = libvirt.open(None)
|
conn = libvirt.open(None)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print('Failed to open connection to the hypervisor')
|
print('Failed to open connection to the hypervisor')
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
doms = conn.listDomainsID()
|
doms = conn.listDomainsID()
|
||||||
for id in doms:
|
for id in doms:
|
||||||
|
@ -5,7 +5,6 @@ If the domain is not running, create it.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
import libxml2
|
import libxml2
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
@ -33,7 +32,7 @@ try:
|
|||||||
conn = libvirt.open(None)
|
conn = libvirt.open(None)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print('Failed to open connection to the hypervisor')
|
print('Failed to open connection to the hypervisor')
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dom = conn.lookupByName(name)
|
dom = conn.lookupByName(name)
|
||||||
@ -42,6 +41,6 @@ except libvirt.libvirtError:
|
|||||||
dom = conn.createLinux(xmldesc, 0)
|
dom = conn.createLinux(xmldesc, 0)
|
||||||
if dom is None:
|
if dom is None:
|
||||||
print("failed")
|
print("failed")
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
else:
|
else:
|
||||||
print("done")
|
print("done")
|
||||||
|
@ -5,7 +5,6 @@ List active domains of an ESX host and print some info.
|
|||||||
# also demonstrates how to use the libvirt.openAuth() method
|
# also demonstrates how to use the libvirt.openAuth() method
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
import libxml2
|
import libxml2
|
||||||
import getpass
|
import getpass
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
@ -96,7 +95,7 @@ try:
|
|||||||
conn = libvirt.openAuth(uri, auth, 0)
|
conn = libvirt.openAuth(uri, auth, 0)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print("Failed to open connection to %s" % args.hostname)
|
print("Failed to open connection to %s" % args.hostname)
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
state_names = { libvirt.VIR_DOMAIN_RUNNING : "running",
|
state_names = { libvirt.VIR_DOMAIN_RUNNING : "running",
|
||||||
libvirt.VIR_DOMAIN_BLOCKED : "idle",
|
libvirt.VIR_DOMAIN_BLOCKED : "idle",
|
||||||
|
@ -5,7 +5,6 @@ used by guest-vcpu-daemon.py example
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
customXMLuri = "guest-cpu.python.libvirt.org"
|
customXMLuri = "guest-cpu.python.libvirt.org"
|
||||||
@ -28,14 +27,14 @@ if flags == 0 or args.config:
|
|||||||
|
|
||||||
if confvcpus < args.count:
|
if confvcpus < args.count:
|
||||||
print("Persistent domain configuration has only " + str(confvcpus) + " vcpus configured")
|
print("Persistent domain configuration has only " + str(confvcpus) + " vcpus configured")
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
if flags == 0 or args.live:
|
if flags == 0 or args.live:
|
||||||
livevcpus = dom.vcpusFlags(libvirt.VIR_DOMAIN_AFFECT_LIVE)
|
livevcpus = dom.vcpusFlags(libvirt.VIR_DOMAIN_AFFECT_LIVE)
|
||||||
|
|
||||||
if livevcpus < args.count:
|
if livevcpus < args.count:
|
||||||
print("Live domain configuration has only " + str(livevcpus) + " vcpus configured")
|
print("Live domain configuration has only " + str(livevcpus) + " vcpus configured")
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
if flags == 0 or args.live:
|
if flags == 0 or args.live:
|
||||||
dom.setVcpusFlags(args.count, libvirt.VIR_DOMAIN_AFFECT_LIVE | libvirt.VIR_DOMAIN_VCPU_GUEST)
|
dom.setVcpusFlags(args.count, libvirt.VIR_DOMAIN_AFFECT_LIVE | libvirt.VIR_DOMAIN_VCPU_GUEST)
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
# Michal Privoznik <mprivozn@redhat.com>
|
# Michal Privoznik <mprivozn@redhat.com>
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
import libxml2
|
import libxml2
|
||||||
from typing import Any, Dict # noqa F401
|
from typing import Any, Dict # noqa F401
|
||||||
@ -22,13 +21,13 @@ try:
|
|||||||
conn = libvirt.openReadOnly(None)
|
conn = libvirt.openReadOnly(None)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print("Failed to connect to the hypervisor")
|
print("Failed to connect to the hypervisor")
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
capsXML = conn.getCapabilities()
|
capsXML = conn.getCapabilities()
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print("Failed to request capabilities")
|
print("Failed to request capabilities")
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
caps = minidom.parseString(capsXML)
|
caps = minidom.parseString(capsXML)
|
||||||
cells = caps.getElementsByTagName("cells")[0]
|
cells = caps.getElementsByTagName("cells")[0]
|
||||||
|
@ -7,20 +7,19 @@
|
|||||||
# Peter Krempa <pkrempa@redhat.com>
|
# Peter Krempa <pkrempa@redhat.com>
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conn = libvirt.openReadOnly(None)
|
conn = libvirt.openReadOnly(None)
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print('Failed to connect to the hypervisor')
|
print('Failed to connect to the hypervisor')
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
capsXML = conn.getCapabilities()
|
capsXML = conn.getCapabilities()
|
||||||
except libvirt.libvirtError:
|
except libvirt.libvirtError:
|
||||||
print('Failed to request capabilities')
|
print('Failed to request capabilities')
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
caps = minidom.parseString(capsXML)
|
caps = minidom.parseString(capsXML)
|
||||||
host = caps.getElementsByTagName('host')[0]
|
host = caps.getElementsByTagName('host')[0]
|
||||||
|
Reference in New Issue
Block a user