checkpoint: make python example accept multiple devices and timeout

Add a timeout parameter and allow passing multiple interfaces to make
the script more useful for testing purposes.
This commit is contained in:
Beniamino Galvani 2016-09-07 17:47:23 +02:00
parent 8b9ea0b7c6
commit 1ab616b59c

View File

@ -20,36 +20,48 @@
import dbus, sys
# This example takes a device interface name as a parameter and tells
# NetworkManager to disconnect that device, closing down any network
# connection it may have
if len(sys.argv) != 2:
raise Exception("Usage: %s <interface>" % sys.argv[0])
bus = dbus.SystemBus()
# This example takes a list of device interface names as a parameter
# and tells NetworkManager to create a checkpoint on those devices. It
# is then possible to restore or destroy the checkpoint.
# Get a proxy for the base NetworkManager object
bus = dbus.SystemBus()
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
allDevs = manager.GetDevices()
dpath = None
def Usage():
print "Usage: %s <ROLLBACK-INTERVAL> [INTERFACE]..." % sys.argv[0]
sys.exit(1)
# Find the device
devices = manager.GetDevices()
for d in devices:
dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")
iface = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
if iface == sys.argv[1]:
dpath = d
break
def GetDevicePath(ifname):
for dev in allDevs:
dev_proxy = bus.get_object("org.freedesktop.NetworkManager", dev)
prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")
interface = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
if interface == ifname:
return dev
return
if not dpath or not len(dpath):
raise Exception("NetworkManager knows nothing about %s" % sys.argv[1])
if len(sys.argv) < 2:
Usage()
checkpoint = manager.CheckpointCreate([ dpath ],
0, # no rollback
try:
interval = int(sys.argv[1])
except ValueError:
Usage()
devList = []
for arg in sys.argv[2:]:
path = GetDevicePath(arg)
if path == None:
raise Exception("NetworkManager knows nothing about %s" % arg)
else:
devList.append(path)
checkpoint = manager.CheckpointCreate(devList,
interval,
1); # DESTROY_ALL
choice = raw_input('Do you want to rollback [y/n]? ').lower()