Blame SOURCES/bz1173178-1-rewrite_fence_zvmip.patch

e4ffb1
From f5401fc8dad9a39da0a279b7d55149dc0ebcba26 Mon Sep 17 00:00:00 2001
e4ffb1
From: Marek 'marx' Grac <mgrac@redhat.com>
e4ffb1
Date: Mon, 5 Jan 2015 10:15:36 +0100
e4ffb1
Subject: [PATCH] fence_zvmip: Port fence agent to fencing library
e4ffb1
e4ffb1
This rewrite adds a correct result codes, support for 'list' action and
e4ffb1
autogenerated manual page. The original code is still part of codebase as
e4ffb1
new agent requires additional testing.
e4ffb1
---
e4ffb1
 fence/agents/lib/fencing.py.py      |   4 +
e4ffb1
 fence/agents/zvm/Makefile.am        |  26 ++----
e4ffb1
 fence/agents/zvm/fence_zvmip.8      |  92 -------------------
e4ffb1
 fence/agents/zvm/fence_zvmip.py     | 172 ++++++++++++++++++++++++++++++++++++
e4ffb1
 5 files changed, 310 insertions(+), 149 deletions(-)
e4ffb1
 delete mode 100644 fence/agents/zvm/fence_zvmip.8
e4ffb1
 create mode 100644 fence/agents/zvm/fence_zvmip.py
e4ffb1
e4ffb1
diff --git a/fence/agents/lib/fencing.py.py b/fence/agents/lib/fencing.py.py
e4ffb1
index a8ea448..83bbfcd 100644
e4ffb1
--- a/fence/agents/lib/fencing.py.py
e4ffb1
+++ b/fence/agents/lib/fencing.py.py
e4ffb1
@@ -7,6 +7,7 @@ import subprocess
e4ffb1
 import threading
e4ffb1
 import shlex
e4ffb1
 import exceptions
e4ffb1
+import socket
e4ffb1
 import __main__
e4ffb1
 
e4ffb1
 ## do not add code here.
e4ffb1
@@ -805,6 +806,9 @@ def fence_action(connection, options, set_power_fn, get_power_fn, get_outlet_lis
e4ffb1
 	except pycurl.error, ex:
e4ffb1
 		logging.error("%s\n", str(ex))
e4ffb1
 		fail(EC_TIMED_OUT)
e4ffb1
+	except socket.timeout, ex:
e4ffb1
+		logging.error("%s\n", str(ex))
e4ffb1
+		fail(EC_TIMED_OUT)
e4ffb1
 
e4ffb1
 	return result
e4ffb1
 
e4ffb1
diff --git a/fence/agents/zvm/Makefile.am b/fence/agents/zvm/Makefile.am
e4ffb1
index 62eb862..a29754d 100644
e4ffb1
--- a/fence/agents/zvm/Makefile.am
e4ffb1
+++ b/fence/agents/zvm/Makefile.am
e4ffb1
@@ -2,26 +2,16 @@ MAINTAINERCLEANFILES	= Makefile.in
e4ffb1
 
e4ffb1
 TARGET			= fence_zvmip
e4ffb1
 
e4ffb1
-sbin_PROGRAMS		= fence_zvm fence_zvmip
e4ffb1
+SRC			= $(TARGET).py
e4ffb1
 
e4ffb1
-noinst_HEADERS		= fence_zvm.h
e4ffb1
+EXTRA_DIST		= $(SRC)
e4ffb1
 
e4ffb1
-fence_zvm_SOURCES	= fence_zvm.c
e4ffb1
-fence_zvm_CFLAGS	= -D_GNU_SOURCE
e4ffb1
+sbin_SCRIPTS		= $(TARGET)
e4ffb1
 
e4ffb1
-fence_zvmip_SOURCES	= fence_zvmip.c
e4ffb1
-fence_zvmip_CFLAGS	= -D_GNU_SOURCE
e4ffb1
+man_MANS		= $(TARGET).8
e4ffb1
 
e4ffb1
-dist_man_MANS		= fence_zvm.8 fence_zvmip.8
e4ffb1
+FENCE_TEST_ARGS         = -l test -p test -a test -n 1
e4ffb1
 
e4ffb1
-#include $(top_srcdir)/make/fencemanc.mk
e4ffb1
-
e4ffb1
-clean-local:
e4ffb1
-	rm -f $(sbin_PROGRAMS)
e4ffb1
-
e4ffb1
-FENCE_TEST_ARGS		= -n test -a test -p test -u test
e4ffb1
-
e4ffb1
-include $(top_srcdir)/make/agentccheck.mk
e4ffb1
-
e4ffb1
-# we do not test fence_zvm because it can be compiled only on specific architecture
e4ffb1
-check: xml-check.fence_zvmip delay-check.fence_zvmip
e4ffb1
\ No newline at end of file
e4ffb1
+include $(top_srcdir)/make/fencebuild.mk
e4ffb1
+include $(top_srcdir)/make/fenceman.mk
e4ffb1
+include $(top_srcdir)/make/agentpycheck.mk
e4ffb1
diff --git a/fence/agents/zvm/fence_zvmip.8 b/fence/agents/zvm/fence_zvmip.8
e4ffb1
deleted file mode 100644
e4ffb1
index 6b01425..0000000
e4ffb1
--- a/fence/agents/zvm/fence_zvmip.8
e4ffb1
+++ /dev/null
e4ffb1
@@ -1,92 +0,0 @@
e4ffb1
-.TH fence_zvmip 8
e4ffb1
-
e4ffb1
-.SH NAME
e4ffb1
-fence_zvmip - Power Fencing agent for GFS on System z z/VM Clusters using IP interface to SMAPI
e4ffb1
-
e4ffb1
-.SH SYNOPSIS
e4ffb1
-.B
e4ffb1
-fence_zvmip
e4ffb1
-[\fIOPTION\fR]...
e4ffb1
-
e4ffb1
-.SH DESCRIPTION
e4ffb1
-fence_zvmip is a Power Fencing agent used on a GFS virtual machine in a System z z/VM cluster.
e4ffb1
-It uses the TCP/IP SMAPI interface to recycle an active image.
e4ffb1
-
e4ffb1
-fence_zvmip accepts options on the command line as well as from stdin.
e4ffb1
-fence_node sends the options through stdin when it execs the agent.
e4ffb1
-fence_zvmip can be run by itself with command line options which is useful
e4ffb1
-for testing.
e4ffb1
-
e4ffb1
-Vendor URL: http://www.sinenomine.net
e4ffb1
-
e4ffb1
-.SH OPTIONS
e4ffb1
-.TP
e4ffb1
-\fB-o --action\fP
e4ffb1
-Fencing action: "off" - deactivate virtual machine; "on" - activate virtual machine; "metadata" - display device metadata" - describe fence agent parameters; "status" - state of virtual machine
e4ffb1
-.TP
e4ffb1
-\fB--delay\fP \fIseconds\fP
e4ffb1
-Time to delay fencing action in seconds
e4ffb1
-.TP
e4ffb1
-\fB-n --plug\fP \fItarget\fP
e4ffb1
-Name of target virtual machine to fence
e4ffb1
-.TP
e4ffb1
-\fB-h --help\fP
e4ffb1
-Print out a help message describing available options, then exit.
e4ffb1
-.TP
e4ffb1
-\fB-a --ip\fP \fIsmapi Server\fP
e4ffb1
-Host name or IP address of SMAPI server
e4ffb1
-.TP
e4ffb1
-\fB-u --username\fP \fISMAPI authorized user\fP
e4ffb1
-Name of an authorized SMAPI user
e4ffb1
-.TP
e4ffb1
-\fB-p --password\fP \fISMAPI authorized user's password\fP
e4ffb1
-Password of the authorized SMAPI user
e4ffb1
-.TP
e4ffb1
-\fB-t --timeout\fP \fIRecycle timeout\fP
e4ffb1
-Amount of \fIgrace\fP time to give the virtual machine to shutdown cleanly before being
e4ffb1
-forcibly terminated. Currently this is ignored.
e4ffb1
-.TP
e4ffb1
-\fB-h --help\fP
e4ffb1
-Display usage information
e4ffb1
-
e4ffb1
-.SH STDIN PARAMETERS
e4ffb1
-.TP
e4ffb1
-\fIagent = < param >\fP
e4ffb1
-This option is used by fence_node(8) and is ignored by fence_zvmip.
e4ffb1
-.TP
e4ffb1
-\fIaction = < action >\fP
e4ffb1
-Fencing action: "off" - deactivate virtual machine; "on" - activate virtual machine; "metadata" - display device metadata" - describe fence agent parameters; "status" - state of virtual machine
e4ffb1
-.TP
e4ffb1
-\fIplug = < plug >\fP
e4ffb1
-Name of virtual machine to recycle.
e4ffb1
-.TP
e4ffb1
-\fIipaddr = < server host name or IP address >\fP
e4ffb1
-Host name or IP address of SMAPI server
e4ffb1
-.TP
e4ffb1
-\fIlogin = < SMAPI authorized user >\fP
e4ffb1
-Name of an authorized SMAPI user
e4ffb1
-.TP
e4ffb1
-\fIpasswd = < SMAPI authorized user's password >\fP
e4ffb1
-Password of the authorized SMAPI user
e4ffb1
-.TP
e4ffb1
-\fItimeout = < shutdown timeout >\fP
e4ffb1
-Amount of \fIgrace\fP time to give the virtual machine to shutdown cleanly before being
e4ffb1
-forcibly terminated. Currently this is ignored.
e4ffb1
-
e4ffb1
-.SH SEE ALSO
e4ffb1
-fence(8), fenced(8), fence_node(8)
e4ffb1
-
e4ffb1
-.SH NOTES
e4ffb1
-To use this agent the z/VM SMAPI service needs to be configured to allow the virtual
e4ffb1
-machine running this agent to connect to it and issue the image_recycle operation.
e4ffb1
-This involves updating the VSMWORK1 AUTHLIST VMSYS:VSMWORK1. file. The entry should look
e4ffb1
-something similar to this:
e4ffb1
-
e4ffb1
-.nf
e4ffb1
-Column 1                   Column 66                Column 131
e4ffb1
-|                          |                        |
e4ffb1
-V                          V                        V
e4ffb1
-XXXXXXXX                   ALL                      IMAGE_OPERATIONS
e4ffb1
-.fi
e4ffb1
-
e4ffb1
-Where XXXXXXX is the name of the virtual machine used in the authuser field of the request. 
e4ffb1
diff --git a/fence/agents/zvm/fence_zvmip.py b/fence/agents/zvm/fence_zvmip.py
e4ffb1
new file mode 100644
e4ffb1
index 0000000..63f7fa7
e4ffb1
--- /dev/null
e4ffb1
+++ b/fence/agents/zvm/fence_zvmip.py
e4ffb1
@@ -0,0 +1,172 @@
e4ffb1
+#!/usr/bin/python -tt
e4ffb1
+
e4ffb1
+import sys
e4ffb1
+import atexit
e4ffb1
+import socket
e4ffb1
+import struct
e4ffb1
+import logging
e4ffb1
+sys.path.append("@FENCEAGENTSLIBDIR@")
e4ffb1
+from fencing import *
e4ffb1
+from fencing import fail, fail_usage, run_delay, EC_LOGIN_DENIED, EC_TIMED_OUT
e4ffb1
+
e4ffb1
+#BEGIN_VERSION_GENERATION
e4ffb1
+RELEASE_VERSION=""
e4ffb1
+REDHAT_COPYRIGHT=""
e4ffb1
+BUILD_DATE=""
e4ffb1
+#END_VERSION_GENERATION
e4ffb1
+
e4ffb1
+INT4 = 4
e4ffb1
+
e4ffb1
+def open_socket(options):
e4ffb1
+	try:
e4ffb1
+		if "--inet6-only" in options:
e4ffb1
+			protocol = socket.AF_INET6
e4ffb1
+		elif "--inet4-only" in options:
e4ffb1
+			protocol = socket.AF_INET
e4ffb1
+		else:
e4ffb1
+			protocol = 0
e4ffb1
+		(_, _, _, _, addr) = socket.getaddrinfo( \
e4ffb1
+				options["--ip"], options["--ipport"], protocol,
e4ffb1
+				0, socket.IPPROTO_TCP, socket.AI_PASSIVE
e4ffb1
+				)[0]
e4ffb1
+	except socket.gaierror:
e4ffb1
+		fail(EC_LOGIN_DENIED)
e4ffb1
+
e4ffb1
+	conn = socket.socket()
e4ffb1
+	conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
e4ffb1
+	conn.settimeout(float(options["--shell-timeout"]))
e4ffb1
+	try:
e4ffb1
+		conn.connect(addr)
e4ffb1
+	except socket.error:
e4ffb1
+		fail(EC_LOGIN_DENIED)
e4ffb1
+
e4ffb1
+	return conn
e4ffb1
+
e4ffb1
+def smapi_pack_string(string):
e4ffb1
+	return struct.pack("!i%ds" % (len(string)), len(string), string)
e4ffb1
+
e4ffb1
+def prepare_smapi_command(options, smapi_function, additional_args):
e4ffb1
+	packet_size = 3*INT4 + len(smapi_function) + len(options["--username"]) + len(options["--password"])
e4ffb1
+	for arg in additional_args:
e4ffb1
+		packet_size += INT4 + len(arg)
e4ffb1
+
e4ffb1
+	command = struct.pack("!i", packet_size)
e4ffb1
+	command += smapi_pack_string(smapi_function)
e4ffb1
+	command += smapi_pack_string(options["--username"])
e4ffb1
+	command += smapi_pack_string(options["--password"])
e4ffb1
+	for arg in additional_args:
e4ffb1
+		command += smapi_pack_string(arg)
e4ffb1
+
e4ffb1
+	return command
e4ffb1
+
e4ffb1
+def get_power_status(conn, options):
e4ffb1
+	del conn
e4ffb1
+
e4ffb1
+	# '*' = list all active images
e4ffb1
+	(return_code, reason_code, images_active) = \
e4ffb1
+			get_list_of_images(options, "Image_Status_Query", "*")
e4ffb1
+	logging.debug("Image_Status_Query results are (%d,%d)", return_code, reason_code)
e4ffb1
+	(return_code, reason_code, images_defined) = \
e4ffb1
+			get_list_of_images(options, "Image_Name_Query_DM", options["--username"])
e4ffb1
+	logging.debug("Image_Name_Query_DM results are (%d,%d)", return_code, reason_code)
e4ffb1
+
e4ffb1
+	if ["list", "monitor"].count(options["--action"]) == 1:
e4ffb1
+		return dict([(i, ("", "on" if i in images_active else "off")) for i in images_defined])
e4ffb1
+	else:
e4ffb1
+		status = "error"
e4ffb1
+		if options["--plug"].upper() in images_defined:
e4ffb1
+			if options["--plug"].upper() in images_active:
e4ffb1
+				status = "on"
e4ffb1
+			else:
e4ffb1
+				status = "off"
e4ffb1
+		return status
e4ffb1
+
e4ffb1
+def set_power_status(conn, options):
e4ffb1
+	conn = open_socket(options)
e4ffb1
+
e4ffb1
+	packet = None
e4ffb1
+	if options["--action"] == "on":
e4ffb1
+		packet = prepare_smapi_command(options, "Image_Activate", [options["--plug"]])
e4ffb1
+	elif options["--action"] == "off":
e4ffb1
+		packet = prepare_smapi_command(options, "Image_Deactivate", [options["--plug"], "IMMED"])
e4ffb1
+	conn.send(packet)
e4ffb1
+
e4ffb1
+	request_id = struct.unpack("!i", conn.recv(INT4))[0]
e4ffb1
+	(output_len, request_id, return_code, reason_code) = struct.unpack("!iiii", conn.recv(INT4 * 4))
e4ffb1
+	logging.debug("Image_(De)Activate results are (%d,%d)", return_code, reason_code)
e4ffb1
+
e4ffb1
+	conn.close()
e4ffb1
+	return
e4ffb1
+
e4ffb1
+def get_list_of_images(options, command, data_as_plug):
e4ffb1
+	conn = open_socket(options)
e4ffb1
+
e4ffb1
+	packet = prepare_smapi_command(options, command, [data_as_plug])
e4ffb1
+	conn.send(packet)
e4ffb1
+
e4ffb1
+	request_id = struct.unpack("!i", conn.recv(INT4))[0]
e4ffb1
+	(output_len, request_id, return_code, reason_code) = struct.unpack("!iiii", conn.recv(INT4 * 4))
e4ffb1
+	images = set()
e4ffb1
+
e4ffb1
+	if output_len > 3*INT4:
e4ffb1
+		array_len = struct.unpack("!i", conn.recv(INT4))[0]
e4ffb1
+		data = ""
e4ffb1
+
e4ffb1
+		while True:
e4ffb1
+			read_data = conn.recv(1024, socket.MSG_WAITALL)
e4ffb1
+			data += read_data
e4ffb1
+			if array_len == len(data):
e4ffb1
+				break
e4ffb1
+			elif not read_data:
e4ffb1
+				logging.error("Failed: Not enough data read from socket")
e4ffb1
+				fail(EC_TIMED_OUT)
e4ffb1
+
e4ffb1
+		parsed_len = 0
e4ffb1
+		while parsed_len < array_len:
e4ffb1
+			string_len = struct.unpack("!i", data[parsed_len:parsed_len+INT4])[0]
e4ffb1
+			parsed_len += INT4
e4ffb1
+			image_name = struct.unpack("!%ds" % (string_len), data[parsed_len:parsed_len+string_len])[0]
e4ffb1
+			parsed_len += string_len
e4ffb1
+			images.add(image_name)
e4ffb1
+
e4ffb1
+	conn.close()
e4ffb1
+	return (return_code, reason_code, images)
e4ffb1
+
e4ffb1
+def main():
e4ffb1
+	device_opt = ["ipaddr", "login", "passwd", "port", "method"]
e4ffb1
+
e4ffb1
+	atexit.register(atexit_handler)
e4ffb1
+
e4ffb1
+	all_opt["ipport"]["default"] = "44444"
e4ffb1
+	all_opt["shell_timeout"]["default"] = "5.0"
e4ffb1
+	options = check_input(device_opt, process_input(device_opt))
e4ffb1
+
e4ffb1
+	if len(options.get("--plug", "")) > 8:
e4ffb1
+		fail_usage("Failed: Name of image can not be longer than 8 characters")
e4ffb1
+
e4ffb1
+	docs = {}
e4ffb1
+	docs["shortdesc"] = "Fence agent for use with z/VM Virtual Machines"
e4ffb1
+	docs["longdesc"] = """The fence_zvm agent is intended to be used with with z/VM SMAPI service via TCP/IP
e4ffb1
+
e4ffb1
+To  use this agent the z/VM SMAPI service needs to be configured to allow the virtual machine running this agent to connect to it and issue
e4ffb1
+the image_recycle operation.  This involves updating the VSMWORK1 AUTHLIST VMSYS:VSMWORK1. file. The entry should look something similar to
e4ffb1
+this:
e4ffb1
+
e4ffb1
+Column 1                   Column 66                Column 131
e4ffb1
+
e4ffb1
+   |                          |                        |
e4ffb1
+   V                          V                        V
e4ffb1
+
e4ffb1
+XXXXXXXX                      ALL                      IMAGE_OPERATIONS
e4ffb1
+
e4ffb1
+Where XXXXXXX is the name of the virtual machine used in the authuser field of the request.
e4ffb1
+"""
e4ffb1
+	docs["vendorurl"] = "http://www.ibm.com"
e4ffb1
+	show_docs(options, docs)
e4ffb1
+
e4ffb1
+	run_delay(options)
e4ffb1
+	result = fence_action(None, options, set_power_status, get_power_status, get_power_status)
e4ffb1
+	sys.exit(result)
e4ffb1
+
e4ffb1
+if __name__ == "__main__":
e4ffb1
+	main()
e4ffb1
-- 
e4ffb1
1.9.3
e4ffb1