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

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