[Cluster-devel] [PATCH 3/3] fence agents: Transfer SNMP fence agents to long-opts + transfer script

Marek 'marx' Grac <[email protected]>
Newsgroups com.redhat.cluster-devel
Message-ID <[email protected]>
---
 fence/agents/apc_snmp/fence_apc_snmp.py         |   24 ++++++++--------
 fence/agents/cisco_mds/fence_cisco_mds.py       |    6 ++--
 fence/agents/ibmblade/fence_ibmblade.py         |    4 +-
 fence/agents/ifmib/fence_ifmib.py               |    6 ++--
 fence/agents/intelmodular/fence_intelmodular.py |    4 +-
 fence/agents/ipdu/fence_ipdu.py                 |   10 +++---
 fence/agents/lib/fencing_snmp.py.py             |   33 ++++++++++++-----------
 fence/agents/lib/transfer.py                    |   16 +++++++++++
 8 files changed, 60 insertions(+), 43 deletions(-)
 create mode 100755 fence/agents/lib/transfer.py

diff --git a/fence/agents/apc_snmp/fence_apc_snmp.py b/fence/agents/apc_snmp/fence_apc_snmp.py
index 6f95f37..8a97ae4 100644
--- a/fence/agents/apc_snmp/fence_apc_snmp.py
+++ b/fence/agents/apc_snmp/fence_apc_snmp.py
@@ -94,16 +94,16 @@ def apc_resolv_port_id(conn, options):
 		apc_set_device(conn, options)
 
 	# Now we resolv port_id/switch_id
-	if ((options["-n"].isdigit()) and ((not device.has_switches) or (options["-s"].isdigit()))):
-		port_id = int(options["-n"])
+	if ((options["--plug"].isdigit()) and ((not device.has_switches) or (options["--switch"].isdigit()))):
+		port_id = int(options["--plug"])
 
 		if (device.has_switches):
-			switch_id = int(options["-s"])
+			switch_id = int(options["--switch"])
 	else:
 		table = conn.walk(device.outlet_table_oid, 30)
 
 		for x in table:
-			if (x[1].strip('"') == options["-n"]):
+			if (x[1].strip('"') == options["--plug"]):
 				t = x[0].split('.')
 				if (device.has_switches):
 					port_id = int(t[len(t)-1])
@@ -112,7 +112,7 @@ def apc_resolv_port_id(conn, options):
 					port_id = int(t[len(t)-1])
 
 	if (port_id == None):
-		fail_usage("Can't find port with name %s!"%(options["-n"]))
+		fail_usage("Can't find port with name %s!"%(options["--plug"]))
 
 def get_power_status(conn, options):
 	if (port_id == None):
@@ -129,7 +129,7 @@ def set_power_status(conn, options):
 
 	oid = ((device.has_switches) and device.control_oid%(switch_id, port_id) or device.control_oid%(port_id))
 
-	conn.set(oid, (options["-o"]=="on" and device.turn_on or device.turn_off))
+	conn.set(oid, (options["--action"]=="on" and device.turn_on or device.turn_off))
 
 
 def get_outlets_status(conn, options):
@@ -172,14 +172,14 @@ def main():
 	options = check_input(device_opt, process_input(device_opt))
 
         ## Support for -n [switch]:[plug] notation that was used before
-	if ((options.has_key("-n")) and (-1 != options["-n"].find(":"))):
-		(switch, plug) = options["-n"].split(":", 1)
+	if ((options.has_key("--plug")) and (-1 != options["--plug"].find(":"))):
+		(switch, plug) = options["--plug"].split(":", 1)
 		if ((switch.isdigit()) and (plug.isdigit())):
-			options["-s"] = switch
-			options["-n"] = plug
+			options["--switch"] = switch
+			options["--plug"] = plug
 
-	if (not (options.has_key("-s"))):
-		options["-s"] = "1"
+	if (not (options.has_key("--switch"))):
+		options["--switch"] = "1"
 
 	docs = { }
 	docs["shortdesc"] = "Fence agent for APC over SNMP"
diff --git a/fence/agents/cisco_mds/fence_cisco_mds.py b/fence/agents/cisco_mds/fence_cisco_mds.py
index 8ebce8a..cb45d73 100644
--- a/fence/agents/cisco_mds/fence_cisco_mds.py
+++ b/fence/agents/cisco_mds/fence_cisco_mds.py
@@ -47,7 +47,7 @@ def get_power_status(conn, options):
 	return (status=="1" and "on" or "off")
 
 def set_power_status(conn, options):
-	conn.set(PORT_OID, (options["-o"]=="on" and 1 or 2))
+	conn.set(PORT_OID, (options["--action"]=="on" and 1 or 2))
 
 # Convert array of format [[key1, value1], [key2, value2], ... [keyN, valueN]] to dict, where key is
 # in format a.b.c.d...z and returned dict has key only z
@@ -96,8 +96,8 @@ which can be used with any Cisco MDS 9000 series with SNMP enabled device."
 	docs["vendorurl"] = "http://www.cisco.com"
 	show_docs(options, docs)
 
-	if (not (options["-o"] in ["list","monitor"])):
-		PORT_OID = cisco_port2oid(options["-n"])
+	if (not (options["--action"] in ["list","monitor"])):
+		PORT_OID = cisco_port2oid(options["--plug"])
 
 	# Operate the fencing device
 	result = fence_action(FencingSnmp(options), options, set_power_status, get_power_status, get_outlets_status)
diff --git a/fence/agents/ibmblade/fence_ibmblade.py b/fence/agents/ibmblade/fence_ibmblade.py
index ceb747d..759bfc4 100644
--- a/fence/agents/ibmblade/fence_ibmblade.py
+++ b/fence/agents/ibmblade/fence_ibmblade.py
@@ -27,11 +27,11 @@ STATUS_SET_ON = 1
 ### FUNCTIONS ###
 
 def get_power_status(conn, options):
-	(_, status) = conn.get("%s.%s"% (STATUSES_OID, options["-n"]))
+	(_, status) = conn.get("%s.%s"% (STATUSES_OID, options["--plug"]))
 	return (status == str(STATUS_UP) and "on" or "off")
 
 def set_power_status(conn, options):
-	conn.set("%s.%s"%(CONTROL_OID, options["-n"]), (options["-o"]=="on" and STATUS_SET_ON or STATUS_SET_OFF))
+	conn.set("%s.%s"%(CONTROL_OID, options["--plug"]), (options["--action"]=="on" and STATUS_SET_ON or STATUS_SET_OFF))
 
 def get_outlets_status(conn, _):
 	result = {}
diff --git a/fence/agents/ifmib/fence_ifmib.py b/fence/agents/ifmib/fence_ifmib.py
index 170134e..9a17257 100644
--- a/fence/agents/ifmib/fence_ifmib.py
+++ b/fence/agents/ifmib/fence_ifmib.py
@@ -59,7 +59,7 @@ def get_power_status(conn, options):
 	global port_num
 
 	if (port_num==None):
-		port_num = port2index(conn, options["-n"])
+		port_num = port2index(conn, options["--plug"])
 
 	(oid, status) = conn.get("%s.%d"%(STATUSES_OID, port_num))
 	return (status==str(STATUS_UP) and "on" or "off")
@@ -68,9 +68,9 @@ def set_power_status(conn, options):
 	global port_num
 
 	if (port_num==None):
-		port_num = port2index(conn, options["-n"])
+		port_num = port2index(conn, options["--plug"])
 
-	conn.set("%s.%d"%(STATUSES_OID, port_num), (options["-o"]=="on" and STATUS_UP or STATUS_DOWN))
+	conn.set("%s.%d"%(STATUSES_OID, port_num), (options["--action"]=="on" and STATUS_UP or STATUS_DOWN))
 
 # Convert array of format [[key1, value1], [key2, value2], ... [keyN, valueN]] to dict, where key is
 # in format a.b.c.d...z and returned dict has key only z
diff --git a/fence/agents/intelmodular/fence_intelmodular.py b/fence/agents/intelmodular/fence_intelmodular.py
index 3fd9822..a590ced 100644
--- a/fence/agents/intelmodular/fence_intelmodular.py
+++ b/fence/agents/intelmodular/fence_intelmodular.py
@@ -38,11 +38,11 @@ STATUS_SET_OFF = 3
 ### FUNCTIONS ###
 
 def get_power_status(conn, options):
-	(oid, status) = conn.get("%s.%s"% (STATUSES_OID, options["-n"]))
+	(oid, status) = conn.get("%s.%s"% (STATUSES_OID, options["--plug"]))
 	return (status==str(STATUS_UP) and "on" or "off")
 
 def set_power_status(conn, options):
-	conn.set("%s.%s"%(STATUSES_OID, options["-n"]), (options["-o"]=="on" and STATUS_SET_ON or STATUS_SET_OFF))
+	conn.set("%s.%s"%(STATUSES_OID, options["--plug"]), (options["--action"]=="on" and STATUS_SET_ON or STATUS_SET_OFF))
 
 def get_outlets_status(conn, options):
 	result = {}
diff --git a/fence/agents/ipdu/fence_ipdu.py b/fence/agents/ipdu/fence_ipdu.py
index da7f192..6c93b53 100644
--- a/fence/agents/ipdu/fence_ipdu.py
+++ b/fence/agents/ipdu/fence_ipdu.py
@@ -66,8 +66,8 @@ def ipdu_resolv_port_id(conn, options):
 		ipdu_set_device(conn, options)
 
 	# Now we resolv port_id/switch_id
-	if ((options["-n"].isdigit()) and ((not device.has_switches) or (options["-s"].isdigit()))):
-		port_id = int(options["-n"])
+	if ((options["--plug"].isdigit()) and ((not device.has_switches) or (options["-s"].isdigit()))):
+		port_id = int(options["--plug"])
 
 		if (device.has_switches):
 			switch_id = int(options["-s"])
@@ -75,7 +75,7 @@ def ipdu_resolv_port_id(conn, options):
 		table = conn.walk(device.outlet_table_oid, 30)
 
 		for x in table:
-			if (x[1].strip('"')==options["-n"]):
+			if (x[1].strip('"')==options["--plug"]):
 				t = x[0].split('.')
 				if (device.has_switches):
 					port_id = int(t[len(t)-1])
@@ -84,7 +84,7 @@ def ipdu_resolv_port_id(conn, options):
 					port_id = int(t[len(t)-1])
 
 	if (port_id==None):
-		fail_usage("Can't find port with name %s!"%(options["-n"]))
+		fail_usage("Can't find port with name %s!"%(options["--plug"]))
 
 def get_power_status(conn, options):
 	if (port_id==None):
@@ -101,7 +101,7 @@ def set_power_status(conn, options):
 
 	oid = ((device.has_switches) and device.control_oid%(switch_id, port_id) or device.control_oid%(port_id))
 
-	conn.set(oid,(options["-o"]=="on" and device.turn_on or device.turn_off))
+	conn.set(oid,(options["--action"]=="on" and device.turn_on or device.turn_off))
 
 
 def get_outlets_status(conn, options):
diff --git a/fence/agents/lib/fencing_snmp.py.py b/fence/agents/lib/fencing_snmp.py.py
index e4c4636..89f8ece 100644
--- a/fence/agents/lib/fencing_snmp.py.py
+++ b/fence/agents/lib/fencing_snmp.py.py
@@ -31,8 +31,8 @@ class FencingSnmp:
 
 	def complete_missed_params(self):
 		mapping = [
-			[['P','p','!E'],'self.options["-E"]="authPriv"'],
-			[['!d','c','!l','!P','!p'],'self.options["-d"]="2c"']
+			[['snmp-priv-passwd','password','!snmp-sec-level'],'self.options["--snmp-sec-level"]="authPriv"'],
+			[['!snmp-version','community','!username','!snmp-priv-passwd','!password'],'self.options["--snmp-version"]="2c"']
 			]
 
 		for val in mapping:
@@ -41,11 +41,11 @@ class FencingSnmp:
 			res = True
 
 			for item in e:
-				if ((item[0]=='!') and (self.options.has_key("-"+item[1]))):
+				if ((item[0]=='!') and (self.options.has_key("--"+item[1:]))):
 					res = False
 					break
 
-				if ((item[0]!='!') and (not self.options.has_key("-"+item[0]))):
+				if ((item[0]!='!') and (not self.options.has_key("--"+item[0:]))):
 					res = False
 					break
 
@@ -58,37 +58,38 @@ class FencingSnmp:
 		self.complete_missed_params()
 
 		#mapping from our option to snmpcmd option
-		mapping = (('d', 'v'),('c', 'c'))
+		mapping = (('snmp-version', 'v'),('community', 'c'))
 
 		for item in mapping:
-			if (self.options.has_key("-"+item[0])):
-				cmd += " -%s '%s'"% (item[1], self.quote_for_run(self.options["-" + item[0]]))
+			if (self.options.has_key("--" + item[0])):
+				cmd += " -%s '%s'"% (item[1], self.quote_for_run(self.options["--" + item[0]]))
 
 		# Some options make sense only for v3 (and for v1/2c can cause "problems")
-		if (self.options.has_key("-d")) and (self.options["-d"] == "3"):
+		if (self.options.has_key("--snmp-version")) and (self.options["--snmp-version"] == "3"):
 			# Mapping from our options to snmpcmd options for v3
-			mapping_v3 = (('b','a'),('E','l'),('B','x'),('P','X'),('p','A'),('l','u'))
+			mapping_v3 = (('snmp-auth-prot','a'), ('snmp-sec-level','l'), ('snmp-priv-prot','x'), \
+				('snmp-priv-passwd','X'),('password','A'),('username','u'))
 			for item in mapping_v3:
-				if (self.options.has_key("-"+item[0])):
-					cmd += " -%s '%s'"% (item[1], self.quote_for_run(self.options["-" + item[0]]))
+				if (self.options.has_key("--"+item[0])):
+					cmd += " -%s '%s'"% (item[1], self.quote_for_run(self.options["--" + item[1]]))
 
 		force_ipvx = ""
 
-		if (self.options.has_key("-6")):
+		if (self.options.has_key("--inet6-only")):
 			force_ipvx = "udp6:"
 
-		if (self.options.has_key("-4")):
+		if (self.options.has_key("--inet4-only")):
 			force_ipvx = "udp:"
 
-		cmd += " '%s%s%s'"% (force_ipvx, self.quote_for_run(self.options["-a"]),
-				self.options.has_key("-u") and self.quote_for_run(":" + str (self.options["-u"])) or "")
+		cmd += " '%s%s%s'"% (force_ipvx, self.quote_for_run(self.options["--ip"]),
+				self.options.has_key("--udpport") and self.quote_for_run(":" + str (self.options["--udpport"])) or "")
 		return cmd
 
 	def run_command(self, command, additional_timemout=0):
 		try:
 			self.log_command(command)
 
-			(res_output, res_code) = pexpect.run(command, int(self.options["-Y"]) + int(self.options["-y"]) + additional_timemout, True)
+			(res_output, res_code) = pexpect.run(command, int(self.options["--shell-timeout"]) + int(self.options["--login-timeout"]) + additional_timemout, True)
 
 			if (res_code==None):
 				fail(EC_TIMED_OUT)
diff --git a/fence/agents/lib/transfer.py b/fence/agents/lib/transfer.py
new file mode 100755
index 0000000..ddf2486
--- /dev/null
+++ b/fence/agents/lib/transfer.py
@@ -0,0 +1,16 @@
+#!/usr/bin/python
+
+from fencing import *
+
+def main():
+	for key in all_opt.keys():
+		if all_opt[key].has_key("getopt") and all_opt[key].has_key("longopt"):
+			print "s/options\[\"-" + all_opt[key]["getopt"].rstrip(":") + "\"\]/options[\"--" + \
+				all_opt[key]["longopt"] + "\"]/g"
+			print "s/options.has_key(\"-" + all_opt[key]["getopt"].rstrip(":") + "\")/options.has_key(" + \
+				"\"--" + all_opt[key]["longopt"] + "\")/g"
+
+
+
+if __name__ == "__main__":
+	main()
\ No newline at end of file
-- 
1.7.7.6
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.