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