Blob Blame History Raw
From 4ef3bd1bc0e7bb3fcd3c3b2a7ce20e49e37d3332 Mon Sep 17 00:00:00 2001
From: Marek 'marx' Grac <mgrac@redhat.com>
Date: Mon, 22 Jun 2015 16:49:27 +0200
Subject: [PATCH] fence_zvmip: Add --missing-as-off and improve usage of system
 resources

Resolves: rhbz#1188750
---
 fence/agents/lib/fencing.py.py      |  5 ++++
 fence/agents/zvm/fence_zvmip.py     | 49 +++++++++++++++++++++++++------------
 tests/data/metadata/fence_zvmip.xml |  5 ++++
 3 files changed, 44 insertions(+), 15 deletions(-)

diff --git a/fence/agents/lib/fencing.py.py b/fence/agents/lib/fencing.py.py
index 29b3a94..db620e8 100644
--- a/fence/agents/lib/fencing.py.py
+++ b/fence/agents/lib/fencing.py.py
@@ -902,7 +902,12 @@ def fence_action(tn, options, set_power_fn, get_power_fn, get_outlet_list=None,
 			return
 		elif (options["--action"] == "list") or \
 				((options["--action"] == "monitor") and 1 == options["device_opt"].count("port")):
+			options["--original-action"] = options["--action"]
+			options["--action"] = "list"
 			outlets = get_outlet_list(tn, options)
+			options["--action"] = options["--original-action"]
+			del options["--original-action"]
+
 			## keys can be numbers (port numbers) or strings (names of VM)
 			for outlet_id in outlets.keys():
 				(alias, status) = outlets[outlet_id]
diff --git a/fence/agents/zvm/fence_zvmip.py b/fence/agents/zvm/fence_zvmip.py
index 63f7fa7..3fe6f24 100644
--- a/fence/agents/zvm/fence_zvmip.py
+++ b/fence/agents/zvm/fence_zvmip.py
@@ -62,24 +62,38 @@ def prepare_smapi_command(options, smapi_function, additional_args):
 def get_power_status(conn, options):
 	del conn
 
-	# '*' = list all active images
+	if options.get("--original-action", None) == "monitor":
+		(return_code, reason_code, images_active) = \
+			get_list_of_images(options, "Check_Authentication", None)
+		logging.debug("Check_Authenticate (%d,%d)", return_code, reason_code)
+		if return_code == 0:
+			return {}
+		else:
+			fail(EC_LOGIN_DENIED)
+
+	if options["--action"] == "list":
+		# '*' = list all active images
+		options["--plug"] = "*"
+
 	(return_code, reason_code, images_active) = \
-			get_list_of_images(options, "Image_Status_Query", "*")
+			get_list_of_images(options, "Image_Status_Query", options["--plug"])
 	logging.debug("Image_Status_Query results are (%d,%d)", return_code, reason_code)
-	(return_code, reason_code, images_defined) = \
+
+	if not options["--action"] == "list":
+		if (return_code == 0) and (reason_code == 0):
+			return "on"
+		elif (return_code == 0) and (reason_code == 12):
+			# We are running always with --missing-as-off because we can not check if image
+			# is defined or not (look at rhbz#1188750)
+			return "off"
+		else:
+			return "unknown"
+	else:
+		(return_code, reason_code, images_defined) = \
 			get_list_of_images(options, "Image_Name_Query_DM", options["--username"])
-	logging.debug("Image_Name_Query_DM results are (%d,%d)", return_code, reason_code)
+		logging.debug("Image_Name_Query_DM results are (%d,%d)", return_code, reason_code)
 
-	if ["list", "monitor"].count(options["--action"]) == 1:
 		return dict([(i, ("", "on" if i in images_active else "off")) for i in images_defined])
-	else:
-		status = "error"
-		if options["--plug"].upper() in images_defined:
-			if options["--plug"].upper() in images_active:
-				status = "on"
-			else:
-				status = "off"
-		return status
 
 def set_power_status(conn, options):
 	conn = open_socket(options)
@@ -101,7 +115,11 @@ def set_power_status(conn, options):
 def get_list_of_images(options, command, data_as_plug):
 	conn = open_socket(options)
 
-	packet = prepare_smapi_command(options, command, [data_as_plug])
+	if data_as_plug is None:
+		packet = prepare_smapi_command(options, command, [])
+	else:
+		packet = prepare_smapi_command(options, command, [data_as_plug])
+
 	conn.send(packet)
 
 	request_id = struct.unpack("!i", conn.recv(INT4))[0]
@@ -133,12 +151,13 @@ def get_list_of_images(options, command, data_as_plug):
 	return (return_code, reason_code, images)
 
 def main():
-	device_opt = ["ipaddr", "login", "passwd", "port", "method"]
+	device_opt = ["ipaddr", "login", "passwd", "port", "method", "missing_as_off"]
 
 	atexit.register(atexit_handler)
 
 	all_opt["ipport"]["default"] = "44444"
 	all_opt["shell_timeout"]["default"] = "5.0"
+	all_opt["missing_as_off"]["default"] = "1"
 	options = check_input(device_opt, process_input(device_opt))
 
 	if len(options.get("--plug", "")) > 8:
diff --git a/tests/data/metadata/fence_zvmip.xml b/tests/data/metadata/fence_zvmip.xml
index 03220cb..993dfb3 100644
--- a/tests/data/metadata/fence_zvmip.xml
+++ b/tests/data/metadata/fence_zvmip.xml
@@ -120,6 +120,11 @@ Where XXXXXXX is the name of the virtual machine used in the authuser field of t
 		<content type="string" default="5.0"  />
 		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
 	</parameter>
+	<parameter name="missing_as_off" unique="0" required="0">
+		<getopt mixed="--missing-as-off" />
+		<content type="boolean" default="1"  />
+		<shortdesc lang="en">Missing port returns OFF instead of failure</shortdesc>
+	</parameter>
 	<parameter name="retry_on" unique="0" required="0">
 		<getopt mixed="--retry-on=[attempts]" />
 		<content type="string" default="1"  />
-- 
1.9.3