[ovs-dev] [PATCH] dump-vif-details: Safeguard 'finally' code

Reid Price reid at nicira.com
Fri Oct 30 19:39:14 UTC 2009


This makes several minor streamlining changes to dump-vif-details,
and moves the try statement in dump_vif_info to exclude session
initialization, so that finally will not obscure the original exception
with a new exception related to the session variable when logins fail.
---
 .../usr_share_vswitch_scripts_dump-vif-details     |   33 +++++++++----------
 1 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/xenserver/usr_share_vswitch_scripts_dump-vif-details b/xenserver/usr_share_vswitch_scripts_dump-vif-details
index b0ceb40..7ce8bf7 100755
--- a/xenserver/usr_share_vswitch_scripts_dump-vif-details
+++ b/xenserver/usr_share_vswitch_scripts_dump-vif-details
@@ -25,22 +25,20 @@ def get_vif_ref(domid, devid):
 
 # Query XAPI for the information we need using the vif's opaque reference
 def dump_vif_info(domid, devid, vif_ref):
+	vif_info = []
+	session = XenAPI.xapi_local()
+	session.xenapi.login_with_password("root", "")
 	try: 
-		session = XenAPI.xapi_local()
-		session.xenapi.login_with_password("root", "")
 		vif_rec = session.xenapi.VIF.get_record(vif_ref)
 		net_rec = session.xenapi.network.get_record(vif_rec["network"])
-		vm_rec = session.xenapi.VM.get_record(vif_rec["VM"])
+		vm_uuid = session.xenapi.VM.get_uuid(vif_rec["VM"])
 
 		# Data to allow vNetManager to associate VIFs with xapi data
-		sys.stdout.write('--add=port.vif%s.%s.net-uuid=%s ' 
-				% (domid, devid, net_rec["uuid"]))
-		sys.stdout.write('--add=port.vif%s.%s.vif-mac=%s ' 
-				% (domid, devid, vif_rec["MAC"]))
-		sys.stdout.write('--add=port.vif%s.%s.vif-uuid=%s ' 
-				% (domid, devid, vif_rec["uuid"]))
-		sys.stdout.write('--add=port.vif%s.%s.vm-uuid=%s ' 
-				% (domid, devid, vm_rec["uuid"]))
+		add_port = '--add=port.vif%s.%s' % (domid, devid)
+		vif_info.append('%s.net-uuid=%s' % (add_port, net_rec["uuid"]))
+		vif_info.append('%s.vif-mac=%s' % (add_port, vif_rec["MAC"]))
+		vif_info.append('%s.vif-uuid=%s' % (add_port, vif_rec["uuid"]))
+		vif_info.append('%s.vm-uuid=%s' % (add_port, vm_uuid))
 
 		# vNetManager needs to know the network UUID(s) associated with
 		# each datapath.  Normally interface-reconfigure adds them, but
@@ -52,17 +50,18 @@ def dump_vif_info(domid, devid, vif_ref):
 		# There may still be a brief delay between the initial
 		# ovs-vswitchd connection to vNetManager and setting this
 		# configuration variable, but vNetManager can tolerate that.
-		if len(net_rec['PIFs']) == 0:
+		if not net_rec['PIFs']:
 			key = 'bridge.%s.xs-network-uuids' % net_rec['bridge']
 			value = net_rec['uuid']
-			sys.stdout.write('--del-match=%s=* ' % key)
-			sys.stdout.write('--add=%s=%s ' % (key, value))
+			vif_info.append('--del-match=%s=*' % key)
+			vif_info.append('--add=%s=%s' % (key, value))
 	finally:
 		session.xenapi.session.logout()
+	print ' '.join(vif_info)
 	
 if __name__ == '__main__':
-	if (len(sys.argv) != 3):
-		sys.stderr.write("ERROR: %s <domid> <devid>\n")
+	if len(sys.argv) != 3:
+		sys.stderr.write("ERROR: %s <domid> <devid>\n" % sys.argv[0])
 		sys.exit(1)
 
 	domid = sys.argv[1]
@@ -71,7 +70,7 @@ if __name__ == '__main__':
 	vif_ref = get_vif_ref(domid, devid)
 	if not vif_ref:
 		sys.stderr.write("ERROR: Could not find interface vif%s.%s\n" 
-				% (domid, devid))
+		                 % (domid, devid))
 		sys.exit(1)
 
 	dump_vif_info(domid, devid, vif_ref)
-- 
1.5.4.3





More information about the dev mailing list