Blame SOURCES/bz1173178-1-rewrite_fence_zvmip.patch

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