Blame SOURCES/bz1396050-fence_vmware_rest-new-fence-agent.patch

035a21
diff -uNr a/configure.ac b/configure.ac
035a21
--- a/configure.ac	2017-10-24 11:38:01.084912422 +0200
035a21
+++ b/configure.ac	2017-10-24 11:34:55.939413105 +0200
035a21
@@ -309,6 +309,7 @@
035a21
 		 fence/agents/virsh/Makefile
035a21
 		 fence/agents/vmware/Makefile
035a21
 		 fence/agents/vmware_soap/Makefile
035a21
+		 fence/agents/vmware_rest/Makefile
035a21
 		 fence/agents/wti/Makefile
035a21
 		 fence/agents/xenapi/Makefile
035a21
 		 fence/agents/hds_cb/Makefile
035a21
diff -uNr a/fence/agents/vmware_rest/fence_vmware_rest.py b/fence/agents/vmware_rest/fence_vmware_rest.py
035a21
--- a/fence/agents/vmware_rest/fence_vmware_rest.py	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/fence/agents/vmware_rest/fence_vmware_rest.py	2017-10-24 12:17:10.919982326 +0200
035a21
@@ -0,0 +1,189 @@
035a21
+#!/usr/bin/python -tt
035a21
+
035a21
+import sys
035a21
+import pycurl, io, json
035a21
+import logging
035a21
+import atexit
035a21
+sys.path.append("@FENCEAGENTSLIBDIR@")
035a21
+from fencing import *
035a21
+from fencing import fail, run_delay, EC_LOGIN_DENIED, EC_STATUS
035a21
+
035a21
+#BEGIN_VERSION_GENERATION
035a21
+RELEASE_VERSION=""
035a21
+REDHAT_COPYRIGHT=""
035a21
+BUILD_DATE=""
035a21
+#END_VERSION_GENERATION
035a21
+
035a21
+state = {"POWERED_ON": "on", 'POWERED_OFF': "off"}
035a21
+
035a21
+def get_power_status(conn, options):
035a21
+	res = send_command(conn, "vcenter/vm?filter.names={}".format(options["--plug"]))["value"]
035a21
+
035a21
+	if len(res) == 0:
035a21
+		fail(EC_STATUS)
035a21
+
035a21
+	options["id"] = res[0]["vm"]
035a21
+
035a21
+	result = res[0]["power_state"]
035a21
+
035a21
+	return state[result]
035a21
+
035a21
+def set_power_status(conn, options):
035a21
+	action = {
035a21
+		"on" : "start",
035a21
+		"off" : "stop"
035a21
+	}[options["--action"]]
035a21
+
035a21
+	send_command(conn, "vcenter/vm/{}/power/{}".format(options["id"], action), "POST")
035a21
+
035a21
+def get_list(conn, options):
035a21
+	outlets = {}
035a21
+
035a21
+	res = send_command(conn, "vcenter/vm")
035a21
+
035a21
+	for r in res["value"]:
035a21
+		outlets[r["name"]] = ("", state[r["power_state"]])
035a21
+
035a21
+	return outlets
035a21
+
035a21
+def connect(opt):
035a21
+	conn = pycurl.Curl()
035a21
+
035a21
+	## setup correct URL
035a21
+	if "--ssl" in opt or "--ssl-secure" in opt or "--ssl-insecure" in opt:
035a21
+		conn.base_url = "https:"
035a21
+	else:
035a21
+		conn.base_url = "http:"
035a21
+	if "--api-path" in opt:
035a21
+		api_path = opt["--api-path"]
035a21
+	else:
035a21
+		api_path = "/rest"
035a21
+
035a21
+	conn.base_url += "//" + opt["--ip"] + ":" + str(opt["--ipport"]) + api_path + "/"
035a21
+
035a21
+	## send command through pycurl
035a21
+	conn.setopt(pycurl.HTTPHEADER, [
035a21
+		"Accept: application/json",
035a21
+	])
035a21
+
035a21
+	conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
035a21
+	conn.setopt(pycurl.USERPWD, opt["--username"] + ":" + opt["--password"])
035a21
+
035a21
+	conn.setopt(pycurl.TIMEOUT, int(opt["--shell-timeout"]))
035a21
+	if "--ssl" in opt or "--ssl-secure" in opt:
035a21
+		conn.setopt(pycurl.SSL_VERIFYPEER, 1)
035a21
+		conn.setopt(pycurl.SSL_VERIFYHOST, 2)
035a21
+
035a21
+	if "--ssl-insecure" in opt:
035a21
+		conn.setopt(pycurl.SSL_VERIFYPEER, 0)
035a21
+		conn.setopt(pycurl.SSL_VERIFYHOST, 0)
035a21
+
035a21
+	try:
035a21
+		result = send_command(conn, "com/vmware/cis/session", "POST")
035a21
+	except Exception as e:
035a21
+		logging.debug("Failed: {}".format(e))
035a21
+		fail(EC_LOGIN_DENIED)
035a21
+
035a21
+	# set session id for later requests
035a21
+	conn.setopt(pycurl.HTTPHEADER, [
035a21
+		"Accept: application/json",
035a21
+		"vmware-api-session-id: {}".format(result["value"]),
035a21
+	])
035a21
+
035a21
+	return conn
035a21
+
035a21
+def disconnect(conn):
035a21
+	send_command(conn, "com/vmware/cis/session", "DELETE")
035a21
+	conn.close()
035a21
+
035a21
+def send_command(conn, command, method="GET"):
035a21
+	url = conn.base_url + command
035a21
+
035a21
+	conn.setopt(pycurl.URL, url.encode("ascii"))
035a21
+
035a21
+	web_buffer = io.BytesIO()
035a21
+
035a21
+	if method == "GET":
035a21
+		conn.setopt(pycurl.POST, 0)
035a21
+	if method == "POST":
035a21
+		conn.setopt(pycurl.POSTFIELDS, "")
035a21
+	if method == "DELETE":
035a21
+		conn.setopt(pycurl.CUSTOMREQUEST, "DELETE")
035a21
+
035a21
+	conn.setopt(pycurl.WRITEFUNCTION, web_buffer.write)
035a21
+
035a21
+	try:
035a21
+		conn.perform()
035a21
+	except Exception as e:
035a21
+		raise Exception(e[1])
035a21
+
035a21
+	rc = conn.getinfo(pycurl.HTTP_CODE)
035a21
+	result = web_buffer.getvalue().decode()
035a21
+
035a21
+	web_buffer.close()
035a21
+
035a21
+	if len(result) > 0:
035a21
+		result = json.loads(result)
035a21
+
035a21
+	if rc != 200:
035a21
+		raise Exception("{}: {}".format(rc, result["value"]["messages"][0]["default_message"]))
035a21
+
035a21
+	logging.debug("url: {}".format(url))
035a21
+	logging.debug("method: {}".format(method))
035a21
+	logging.debug("response code: {}".format(rc))
035a21
+	logging.debug("result: {}\n".format(result))
035a21
+
035a21
+	return result
035a21
+
035a21
+def define_new_opts():
035a21
+	all_opt["api_path"] = {
035a21
+		"getopt" : ":",
035a21
+		"longopt" : "api-path",
035a21
+		"help" : "--api-path=[path]              The path part of the API URL",
035a21
+		"default" : "/rest",
035a21
+		"required" : "0",
035a21
+		"shortdesc" : "The path part of the API URL",
035a21
+		"order" : 2}
035a21
+
035a21
+
035a21
+def main():
035a21
+	device_opt = [
035a21
+		"ipaddr",
035a21
+		"api_path",
035a21
+		"login",
035a21
+		"passwd",
035a21
+		"ssl",
035a21
+		"notls",
035a21
+		"web",
035a21
+		"port",
035a21
+	]
035a21
+
035a21
+	atexit.register(atexit_handler)
035a21
+	define_new_opts()
035a21
+
035a21
+	all_opt["shell_timeout"]["default"] = "5"
035a21
+	all_opt["power_wait"]["default"] = "1"
035a21
+
035a21
+	options = check_input(device_opt, process_input(device_opt))
035a21
+
035a21
+	docs = {}
035a21
+	docs["shortdesc"] = "Fence agent for VMware REST API"
035a21
+	docs["longdesc"] = "fence_vmware_rest is an I/O Fencing agent which can be \
035a21
+used with VMware API to fence virtual machines."
035a21
+	docs["vendorurl"] = "https://www.vmware.com"
035a21
+	show_docs(options, docs)
035a21
+
035a21
+	####
035a21
+	## Fence operations
035a21
+	####
035a21
+	run_delay(options)
035a21
+
035a21
+	conn = connect(options)
035a21
+	atexit.register(disconnect, conn)
035a21
+
035a21
+	result = fence_action(conn, options, set_power_status, get_power_status, get_list)
035a21
+
035a21
+	sys.exit(result)
035a21
+
035a21
+if __name__ == "__main__":
035a21
+	main()
035a21
diff -uNr a/fence/agents/vmware_rest/Makefile.am b/fence/agents/vmware_rest/Makefile.am
035a21
--- a/fence/agents/vmware_rest/Makefile.am	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/fence/agents/vmware_rest/Makefile.am	2017-10-24 11:32:17.369693405 +0200
035a21
@@ -0,0 +1,20 @@
035a21
+MAINTAINERCLEANFILES	= Makefile.in
035a21
+
035a21
+TARGET			= fence_vmware_rest
035a21
+
035a21
+SRC			= $(TARGET).py
035a21
+
035a21
+EXTRA_DIST		= $(SRC)
035a21
+
035a21
+sbin_SCRIPTS		= $(TARGET)
035a21
+
035a21
+man_MANS		= $(TARGET).8
035a21
+
035a21
+FENCE_TEST_ARGS         = -l test -p test -a test -n 1
035a21
+
035a21
+include $(top_srcdir)/make/fencebuild.mk
035a21
+include $(top_srcdir)/make/fenceman.mk
035a21
+include $(top_srcdir)/make/agentpycheck.mk
035a21
+
035a21
+clean-local: clean-man
035a21
+	rm -f $(TARGET)
035a21
diff -uNr a/tests/data/metadata/fence_vmware_rest.xml b/tests/data/metadata/fence_vmware_rest.xml
035a21
--- a/tests/data/metadata/fence_vmware_rest.xml	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/tests/data/metadata/fence_vmware_rest.xml	2017-10-24 11:35:43.501027721 +0200
035a21
@@ -0,0 +1,172 @@
035a21
+
035a21
+<resource-agent name="fence_vmware_rest" shortdesc="Fence agent for VMware REST API" >
035a21
+<longdesc>fence_vmware_rest is an I/O Fencing agent which can be used with VMware API to fence virtual machines.</longdesc>
035a21
+<vendor-url>https://www.vmware.com</vendor-url>
035a21
+<parameters>
035a21
+	<parameter name="ipport" unique="0" required="0">
035a21
+		<getopt mixed="-u, --ipport=[port]" />
035a21
+		<content type="integer" default="80"  />
035a21
+		<shortdesc lang="en">TCP/UDP port to use for connection with device</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="notls" unique="0" required="0">
035a21
+		<getopt mixed="-t, --notls" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Disable TLS negotiation, force SSL 3.0</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ssl_secure" unique="0" required="0">
035a21
+		<getopt mixed="--ssl-secure" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">SSL connection with verifying fence device's certificate</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="port" unique="0" required="1" deprecated="1">
035a21
+		<getopt mixed="-n, --plug=[id]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Physical plug number, name of virtual machine or UUID</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="inet6_only" unique="0" required="0">
035a21
+		<getopt mixed="-6, --inet6-only" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Forces agent to use IPv6 addresses only</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ipaddr" unique="0" required="1" deprecated="1">
035a21
+		<getopt mixed="-a, --ip=[ip]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">IP Address or Hostname</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="inet4_only" unique="0" required="0">
035a21
+		<getopt mixed="-4, --inet4-only" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Forces agent to use IPv4 addresses only</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="passwd_script" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-S, --password-script=[script]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Script to retrieve password</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="passwd" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-p, --password=[password]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ssl" unique="0" required="0">
035a21
+		<getopt mixed="-z, --ssl" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">SSL connection</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ssl_insecure" unique="0" required="0">
035a21
+		<getopt mixed="--ssl-insecure" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">SSL connection without verifying fence device's certificate</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="action" unique="0" required="1">
035a21
+		<getopt mixed="-o, --action=[action]" />
035a21
+		<content type="string" default="reboot"  />
035a21
+		<shortdesc lang="en">Fencing Action</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="login" unique="0" required="1" deprecated="1">
035a21
+		<getopt mixed="-l, --username=[name]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Login Name</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="plug" unique="0" required="1" obsoletes="port">
035a21
+		<getopt mixed="-n, --plug=[id]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Physical plug number, name of virtual machine or UUID</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="username" unique="0" required="1" obsoletes="login">
035a21
+		<getopt mixed="-l, --username=[name]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Login Name</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ip" unique="0" required="1" obsoletes="ipaddr">
035a21
+		<getopt mixed="-a, --ip=[ip]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">IP Address or Hostname</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="password" unique="0" required="0" obsoletes="passwd">
035a21
+		<getopt mixed="-p, --password=[password]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="password_script" unique="0" required="0" obsoletes="passwd_script">
035a21
+		<getopt mixed="-S, --password-script=[script]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Script to retrieve password</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="api_path" unique="0" required="0">
035a21
+		<getopt mixed="--api-path=[path]" />
035a21
+		<shortdesc lang="en">The path part of the API URL</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="verbose" unique="0" required="0">
035a21
+		<getopt mixed="-v, --verbose" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Verbose mode</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="debug" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-D, --debug-file=[debugfile]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="debug_file" unique="0" required="0" obsoletes="debug">
035a21
+		<getopt mixed="-D, --debug-file=[debugfile]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="version" unique="0" required="0">
035a21
+		<getopt mixed="-V, --version" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Display version information and exit</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="help" unique="0" required="0">
035a21
+		<getopt mixed="-h, --help" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Display help and exit</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="separator" unique="0" required="0">
035a21
+		<getopt mixed="-C, --separator=[char]" />
035a21
+		<content type="string" default=","  />
035a21
+		<shortdesc lang="en">Separator for CSV created by operation list</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="power_wait" unique="0" required="0">
035a21
+		<getopt mixed="--power-wait=[seconds]" />
035a21
+		<content type="second" default="1"  />
035a21
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="login_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--login-timeout=[seconds]" />
035a21
+		<content type="second" default="5"  />
035a21
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="power_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--power-timeout=[seconds]" />
035a21
+		<content type="second" default="20"  />
035a21
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="delay" unique="0" required="0">
035a21
+		<getopt mixed="--delay=[seconds]" />
035a21
+		<content type="second" default="0"  />
035a21
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="shell_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--shell-timeout=[seconds]" />
035a21
+		<content type="second" default="5"  />
035a21
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="retry_on" unique="0" required="0">
035a21
+		<getopt mixed="--retry-on=[attempts]" />
035a21
+		<content type="integer" default="1"  />
035a21
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
035a21
+	</parameter>
035a21
+</parameters>
035a21
+<actions>
035a21
+	<action name="on" automatic="0"/>
035a21
+	<action name="off" />
035a21
+	<action name="reboot" />
035a21
+	<action name="status" />
035a21
+	<action name="list" />
035a21
+	<action name="list-status" />
035a21
+	<action name="monitor" />
035a21
+	<action name="metadata" />
035a21
+	<action name="validate-all" />
035a21
+</actions>
035a21
+</resource-agent>