From ff29ba83c06c3b49bb795c24c261b2be6da33bd8 Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Fri, 1 Feb 2013 12:56:24 +0100
Subject: tcm_node: Silence output

Add option '--debug' to silence tcm_node for the standard case.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 tcm-py/tcm_node.py |   67 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 25 deletions(-)

Index: lio-utils-4.0/tcm-py/tcm_node.py
===================================================================
--- lio-utils-4.0.orig/tcm-py/tcm_node.py
+++ lio-utils-4.0/tcm-py/tcm_node.py
@@ -12,6 +12,8 @@ import tcm_iblock
 import tcm_ramdisk
 import tcm_fileio
 
+tcm_debug = 0
+
 config = ConfigParser.ConfigParser()
 config.read('/etc/target/tcm.cfg')
 try:
@@ -31,6 +33,12 @@ def tcm_err(msg):
 	print msg
 	sys.exit(1)
 
+def tcm_dbg(msg):
+	global tcm_debug
+
+	if tcm_debug == 1:
+		print msg
+
 def tcm_get_cfs_prefix(arg):
 	path = tcm_root + "/" + arg
 	return path
@@ -53,7 +61,7 @@ def tcm_add_alua_lugp(option, opt_str, v
 		test = os.system(rmdir_op)
 		tcm_err("Unable to set ID for ALUA Logical Unit Group: " + lu_gp_name)
 	else:
-		print "Successfully created ALUA Logical Unit Group: " + lu_gp_name
+		tcm_dbg("Successfully created ALUA Logical Unit Group: " + lu_gp_name)
 
 	return
 
@@ -82,7 +90,7 @@ def tcm_add_alua_tgptgp(option, opt_str,
 		tcm_err("Unable to set ID for ALUA Target Port Group: " + tg_pt_gp_name)
 	else:
 		tcm_alua_set_write_metadata(alua_cfs_path)
-		print "Successfully created ALUA Target Port Group: " + tg_pt_gp_name
+		tcm_dbg("Successfully created ALUA Target Port Group: " + tg_pt_gp_name)
 
 	return
 
@@ -145,7 +153,7 @@ def tcm_alua_process_metadata(cfs_dev_pa
 
 	p = open(alua_path, 'rU')
 	if not p:
-		print "Unable to process ALUA metadata for: " + alua_path
+		tcm_err("Unable to process ALUA metadata for: " + alua_path)
 
 	line = p.readline()
 	while line:
@@ -224,7 +232,7 @@ def tcm_add_alua_tgptgp_with_md(option,
 		os.system(rmdir_op)
 		tcm_err("Unable to set ID for ALUA Target Port Group: " + tg_pt_gp_name)
 	else:
-		print "Successfully added existing ALUA Target Port Group: " + tg_pt_gp_name + " with tg_pt_gp_id: " + tg_pt_gp_id
+		tcm_dbg("Successfully added existing ALUA Target Port Group: " + tg_pt_gp_name + " with tg_pt_gp_id: " + tg_pt_gp_id)
 
 	# Now process the ALUA metadata for this group
 	tcm_alua_process_metadata(cfs_dev_path, tg_pt_gp_name, tg_pt_gp_id)
@@ -234,7 +242,6 @@ def tcm_delhba(option, opt_str, value, p
 	hba_name = str(value)
 
 	hba_path = tcm_root + "/" + hba_name
-	print "hba_path: " + hba_path
 
 	dev_root = tcm_root + "/" + hba_name + "/"
 	for g in os.listdir(dev_root):
@@ -248,7 +255,7 @@ def tcm_delhba(option, opt_str, value, p
 	if ret:
 		tcm_err("Unable to delete TCM HBA: " + hba_path)
 	else:
-		print "Successfully released TCM HBA: " + hba_path
+		tcm_dbg("Successfully released TCM HBA: " + hba_path)
 
 	return
 
@@ -263,7 +270,7 @@ def tcm_del_alua_lugp(option, opt_str, v
 	if ret:
 		tcm_err("Unable to delete ALUA Logical Unit Group: " + lu_gp_name)
 	else:
-		print "Successfully deleted ALUA Logical Unit Group: " + lu_gp_name
+		tcm_dbg("Successfully deleted ALUA Logical Unit Group: " + lu_gp_name)
 
 	return
 
@@ -282,7 +289,7 @@ def __tcm_del_alua_tgptgp(option, opt_st
 	if ret:
 		tcm_err("Unable to delete ALUA Target Port Group: " + tg_pt_gp_name)
 	else:
-		print "Successfully deleted ALUA Target Port Group: " + tg_pt_gp_name
+		tcm_dbg("Successfully deleted ALUA Target Port Group: " + tg_pt_gp_name)
 
 	return
 
@@ -333,7 +340,7 @@ def tcm_createvirtdev(option, opt_str, v
 	if (os.path.isdir(cfs_hba_path) == False):
 		ret = os.mkdir(cfs_hba_path)
 		if not ret:
-			print "Successfully added TCM/ConfigFS HBA: " + hba_cfs
+			tcm_dbg("Successfully added TCM/ConfigFS HBA: " + hba_cfs)
 		else:
 			tcm_err("Unable to create TCM/ConfigFS HBA: " + cfs_hba_path)
 
@@ -394,7 +401,7 @@ def tcm_createvirtdev(option, opt_str, v
                         tcm_generate_uuid_for_unit_serial(cfs_path)
 			tcm_alua_check_metadata_dir(cfs_dev_path)
 		
-		print "Successfully created TCM/ConfigFS storage object: " + cfs_dev_path
+		tcm_dbg("Successfully created TCM/ConfigFS storage object: " + cfs_dev_path)
 	else:
 		os.rmdir(cfs_dev_path);
 		tcm_err("Unable to register TCM/ConfigFS storage object: " + cfs_dev_path)
@@ -461,7 +468,7 @@ def tcm_process_aptpl_metadata(option, o
 	if (os.path.isfile(aptpl_file) == False):
 		return
 
-	print "Reading APTPL metadata from: " + aptpl_file
+	tcm_dbg("Reading APTPL metadata from: " + aptpl_file)
 
 	p = open(aptpl_file, 'rU')
 	if not p:
@@ -508,7 +515,8 @@ def tcm_process_aptpl_metadata(option, o
 
 		val = cfs.write(out)
 		if val:
-			print "Failed to write PR APTPL metadata to: " + cfs_aptpl_file
+			p.close()
+			tcm_err("Failed to write PR APTPL metadata to: " + cfs_aptpl_file)
 			
 		cfs.close()
 #		print "Write out to configfs: " + out
@@ -572,8 +580,8 @@ def __tcm_freevirtdev(option, opt_str, v
 	cfs_path = cfs_unsplit.split('/')
 	hba_cfs = cfs_path[0]
 	dev_vfs_alias = cfs_path[1]
-	print " ConfigFS HBA: " + hba_cfs
-	print " ConfigFS Device Alias: " + dev_vfs_alias
+	tcm_dbg(" ConfigFS HBA: " + hba_cfs)
+	tcm_dbg(" ConfigFS Device Alias: " + dev_vfs_alias)
 
 	cfs_dev_path = tcm_get_cfs_prefix(cfs_unsplit)
 	if (os.path.isdir(cfs_dev_path) == False):
@@ -589,7 +597,7 @@ def __tcm_freevirtdev(option, opt_str, v
 
 	ret = os.rmdir(cfs_dev_path)
 	if not ret:
-		print "Successfully released TCM/ConfigFS storage object: " + cfs_dev_path
+		tcm_dbg("Successfully released TCM/ConfigFS storage object: " + cfs_dev_path)
 	else:
 		tcm_err("Failed to release ConfigFS Storage Object: " + cfs_dev_path + " ret: " + ret)
 
@@ -858,7 +866,7 @@ def tcm_set_alua_type(option, opt_str, v
 	if ret:
 		tcm_err("Unable to set ALUA access type for TG PT Group: " + tg_pt_gp_base)
 	else:
-		print "Successfully set ALUA access type for TG PT Group: " + alua_gp + " to " + new_alua_type_str
+		tcm_dbg("Successfully set ALUA access type for TG PT Group: " + alua_gp + " to " + new_alua_type_str)
 
 	return
 
@@ -880,7 +888,7 @@ def tcm_set_alua_nonop_delay(option, opt
 	if ret:
 		tcm_err("Unable to set ALUA Active/NonOptimized Delay for TG PT Group: " + tg_pt_gp_base)
 	else:
-		print "Successfully set ALUA Active/NonOptimized Delay to " + delay_msecs + " milliseconds for TG PT Group: " + tg_pt_gp_base
+		tcm_dbg("Successfully set ALUA Active/NonOptimized Delay to " + delay_msecs + " milliseconds for TG PT Group: " + tg_pt_gp_base)
 	
 	return
 
@@ -902,7 +910,7 @@ def tcm_set_alua_trans_delay(option, opt
 	if ret:
 		tcm_err("Unable to set ALUA Transition Delay for TG PT Group: " + tg_pt_gp_base)
 	else:
-		print "Successfully set ALUA Transition Delay to " + delay_msecs + " milliseconds for TG PT Group: " + tg_pt_gp_base
+		tcm_dbg("Successfully set ALUA Transition Delay to " + delay_msecs + " milliseconds for TG PT Group: " + tg_pt_gp_base)
 
 	return
 
@@ -922,7 +930,7 @@ def tcm_clear_alua_tgpt_pref(option, opt
 	if ret:
 		tcm_err("Unable to disable PREFERRED bit for TG Pt Group: " + alua_gp)
 	else:
-		print "Successfully disabled PREFERRED bit for TG Pt Group: " + alua_gp
+		tcm_dbg("Successfully disabled PREFERRED bit for TG Pt Group: " + alua_gp)
 
 	return
 
@@ -942,7 +950,7 @@ def tcm_set_alua_tgpt_pref(option, opt_s
 	if ret:
 		tcm_err("Unable to enable PREFERRED bit for TG Pt Group: " + alua_gp)
 	else:
-		print "Successfully enabled PREFERRED bit for TG Pt Group: " + alua_gp
+		tcm_dbg("Successfully enabled PREFERRED bit for TG Pt Group: " + alua_gp)
 
 	return
 
@@ -961,7 +969,7 @@ def tcm_set_alua_lugp(option, opt_str, v
 	if ret:
 		tcm_err("Unable to set ALUA Logical Unit Group: " + lu_gp_name + " for TCM storage object: " + cfs_dev_path)
 	else:
-		print "Successfully set ALUA Logical Unit Group: " + lu_gp_name + " for TCM storage object: " + cfs_dev_path
+		tcm_dbg("Successfully set ALUA Logical Unit Group: " + lu_gp_name + " for TCM storage object: " + cfs_dev_path)
 
 	return
 
@@ -979,7 +987,7 @@ def tcm_set_dev_attrib(option, opt_str,
 	if ret:
 		tcm_err("Unable to set TCM storage object attribute for:" + cfs_dev_path + "/attrib/" + attrib)
 	else:
-		print "Successfully set TCM storage object attribute: " + attrib + "=" + value + " for " + cfs_dev_path + "/attrib/" + attrib
+		tcm_dbg("Successfully set TCM storage object attribute: " + attrib + "=" + value + " for " + cfs_dev_path + "/attrib/" + attrib)
 
 	return
 
@@ -994,7 +1002,7 @@ def tcm_set_udev_path(option, opt_str, v
 	if ret:
 		tcm_err("Unable to set UDEV path for " + cfs_dev_path)
 
-	print "Set UDEV Path: " + value[1] + " for " + cfs_dev_path
+	tcm_dbg("Set UDEV Path: " + value[1] + " for " + cfs_dev_path)
 	return
 
 def tcm_set_wwn_unit_serial(option, opt_str, value, parser):
@@ -1008,7 +1016,7 @@ def tcm_set_wwn_unit_serial(option, opt_
 	if ret:
 		tcm_err("Unable to set T10 WWN Unit Serial for " + cfs_dev_path)
 
-	print "Set T10 WWN Unit Serial for " + cfs_unsplit + " to: " + value[1]
+	tcm_dbg("Set T10 WWN Unit Serial for " + cfs_unsplit + " to: " + value[1])
 	return
 
 def tcm_set_wwn_unit_serial_with_md(option, opt_str, value, parser):
@@ -1064,6 +1072,8 @@ def tcm_unload_modules(mod):
 			ret = os.system(rmmod_op)
 			if ret:
 				tcm_err("Unable to " + rmmod_op)
+                        else:
+				tcm_dbg("Unloaded " + mod)
 	return
 
 def tcm_unload(option, opt_str, value, parser):
@@ -1099,8 +1109,13 @@ def tcm_config(option, opt_str, value, p
         print("tcm_var: " + tcm_var)
         return
 
-def main():
+def tcm_enable_debug(option, opt_str, value, parser):
+	global tcm_debug
 
+	tcm_debug = 1
+	return
+
+def main():
 	parser = OptionParser()
 	parser.add_option("--addlungp", action="callback", callback=tcm_add_alua_lugp, nargs=1,
 			type="string", dest="lu_gp_name", help="Add ALUA Logical Unit Group")
@@ -1175,6 +1190,8 @@ def main():
 			help="Unload target_core_mod")
 	parser.add_option("--version", action="callback", callback=tcm_version, nargs=0,
 			help="Display target_core_mod version information")
+	parser.add_option("--debug", action="callback", callback=tcm_enable_debug, nargs=0,
+			help="Enable debugging output")
 	parser.add_option("--config", action="callback", callback=tcm_config, nargs=0,
 			help="Display target_core_mod configuration paths")
 	parser.add_option("--wwn", action="callback", callback=tcm_show_wwn_info, nargs=1,
