Blame SOURCES/bz1451776-1-fence_aws-new-fence-agent.patch

035a21
diff -uNr a/configure.ac b/configure.ac
035a21
--- a/configure.ac	2017-10-05 10:21:12.966801280 +0200
035a21
+++ b/configure.ac	2017-10-05 10:22:01.993319558 +0200
035a21
@@ -267,6 +267,7 @@
035a21
 		 fence/agents/apc_snmp/Makefile
035a21
 		 fence/agents/amt/Makefile
035a21
 		 fence/agents/amt_ws/Makefile
035a21
+		 fence/agents/aws/Makefile
035a21
 		 fence/agents/bladecenter/Makefile
035a21
 		 fence/agents/brocade/Makefile
035a21
 		 fence/agents/cisco_mds/Makefile
035a21
diff -uNr a/fence/agents/aws/fence_aws.py b/fence/agents/aws/fence_aws.py
035a21
--- a/fence/agents/aws/fence_aws.py	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/fence/agents/aws/fence_aws.py	2017-08-29 12:01:39.187348926 +0200
035a21
@@ -0,0 +1,139 @@
035a21
+#!/usr/bin/python -tt
035a21
+
035a21
+import sys, re
035a21
+import logging
035a21
+import atexit
035a21
+sys.path.append("/usr/share/fence")
035a21
+from fencing import *
035a21
+from fencing import fail, fail_usage, EC_TIMED_OUT, run_delay
035a21
+
035a21
+try:
035a21
+	import boto3
035a21
+	from botocore.exceptions import ClientError, EndpointConnectionError, NoRegionError
035a21
+except:
035a21
+	pass
035a21
+
035a21
+#BEGIN_VERSION_GENERATION
035a21
+RELEASE_VERSION="Fence agent for AWS (Amazon Web Services)"
035a21
+REDHAT_COPYRIGHT=""
035a21
+BUILD_DATE=""
035a21
+#END_VERSION_GENERATION
035a21
+
035a21
+def get_nodes_list(conn, options):
035a21
+	result = {}
035a21
+	try:
035a21
+		for instance in conn.instances.all():
035a21
+			result[instance.id] = ("", None)
035a21
+	except ClientError:
035a21
+		fail_usage("Failed: Incorrect Access Key or Secret Key.")
035a21
+	except EndpointConnectionError:
035a21
+		fail_usage("Failed: Incorrect Region.")
035a21
+
035a21
+	return result
035a21
+
035a21
+def get_power_status(conn, options):
035a21
+	try:
035a21
+		instance = conn.instances.filter(Filters=[{"Name": "instance-id", "Values": [options["--plug"]]}])
035a21
+		state = list(instance)[0].state["Name"]
035a21
+		if state == "running":
035a21
+			return "on"
035a21
+		elif state == "stopped":
035a21
+			return "off"
035a21
+		else:
035a21
+			return "unknown"
035a21
+
035a21
+	except ClientError:
035a21
+		fail_usage("Failed: Incorrect Access Key or Secret Key.")
035a21
+	except EndpointConnectionError:
035a21
+		fail_usage("Failed: Incorrect Region.")
035a21
+	except IndexError:
035a21
+		return "fail"
035a21
+
035a21
+def set_power_status(conn, options):
035a21
+	if (options["--action"]=="off"):
035a21
+		conn.instances.filter(InstanceIds=[options["--plug"]]).stop(Force=True)
035a21
+	elif (options["--action"]=="on"):
035a21
+		conn.instances.filter(InstanceIds=[options["--plug"]]).start()
035a21
+
035a21
+
035a21
+def define_new_opts():
035a21
+	all_opt["region"] = {
035a21
+		"getopt" : "r:",
035a21
+		"longopt" : "region",
035a21
+		"help" : "-r, --region=[name]            Region, e.g. us-east-1",
035a21
+		"shortdesc" : "Region.",
035a21
+		"required" : "0",
035a21
+		"order" : 2
035a21
+	}
035a21
+	all_opt["access_key"] = {
035a21
+		"getopt" : "a:",
035a21
+		"longopt" : "access-key",
035a21
+		"help" : "-a, --access-key=[name]         Access Key",
035a21
+		"shortdesc" : "Access Key.",
035a21
+		"required" : "0",
035a21
+		"order" : 3
035a21
+	}
035a21
+	all_opt["secret_key"] = {
035a21
+		"getopt" : "s:",
035a21
+		"longopt" : "secret-key",
035a21
+		"help" : "-s, --secret-key=[name]         Secret Key",
035a21
+		"shortdesc" : "Secret Key.",
035a21
+		"required" : "0",
035a21
+		"order" : 4
035a21
+	}
035a21
+
035a21
+# Main agent method
035a21
+def main():
035a21
+	conn = None
035a21
+
035a21
+	device_opt = ["port", "no_password", "region", "access_key", "secret_key"]
035a21
+
035a21
+	atexit.register(atexit_handler)
035a21
+
035a21
+	define_new_opts()
035a21
+
035a21
+	all_opt["power_timeout"]["default"] = "60"
035a21
+
035a21
+	options = check_input(device_opt, process_input(device_opt))
035a21
+
035a21
+	docs = {}
035a21
+	docs["shortdesc"] = "Fence agent for AWS (Amazon Web Services)"
035a21
+	docs["longdesc"] = "fence_aws is an I/O Fencing agent for AWS (Amazon Web\
035a21
+Services). It uses the boto3 library to connect to AWS.\
035a21
+\n.P\n\
035a21
+boto3 can be configured with AWS CLI or by creating ~/.aws/credentials.\n\
035a21
+For instructions see: https://boto3.readthedocs.io/en/latest/guide/quickstart.html#configuration"
035a21
+	docs["vendorurl"] = "http://www.amazon.com"
035a21
+	show_docs(options, docs)
035a21
+
035a21
+	run_delay(options)
035a21
+
035a21
+	if "--region" in options and "--access-key" in options and "--secret-key" in options:  
035a21
+		region = options["--region"]
035a21
+		access_key = options["--access-key"]
035a21
+		secret_key = options["--secret-key"]
035a21
+		try:
035a21
+			conn = boto3.resource('ec2', region_name=region,
035a21
+					      aws_access_key_id=access_key,
035a21
+					      aws_secret_access_key=secret_key)
035a21
+		except NameError:
035a21
+			fail_usage("Failed: boto3 Python library not available")
035a21
+		except:
035a21
+			fail_usage("Failed: Unable to connect to AWS. Check your configuration.")
035a21
+	else:
035a21
+		# If setup with "aws configure" or manually in
035a21
+		# ~/.aws/credentials
035a21
+		try:
035a21
+			conn = boto3.resource('ec2')
035a21
+		except NameError:
035a21
+			fail_usage("Failed: boto3 Python library not available")
035a21
+		except:
035a21
+			# If any of region/access/secret are missing
035a21
+			fail_usage("Failed: Unable to connect to AWS. Check your configuration.")
035a21
+
035a21
+	# Operate the fencing device
035a21
+	result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list)
035a21
+	sys.exit(result)
035a21
+
035a21
+if __name__ == "__main__":
035a21
+	main()
035a21
diff -uNr a/fence/agents/aws/Makefile.am b/fence/agents/aws/Makefile.am
035a21
--- a/fence/agents/aws/Makefile.am	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/fence/agents/aws/Makefile.am	2017-08-29 10:57:41.315547575 +0200
035a21
@@ -0,0 +1,17 @@
035a21
+MAINTAINERCLEANFILES	= Makefile.in
035a21
+
035a21
+TARGET			= fence_aws
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		= -r test -a test -s 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
diff -uNr a/tests/data/metadata/fence_aws.xml b/tests/data/metadata/fence_aws.xml
035a21
--- a/tests/data/metadata/fence_aws.xml	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/tests/data/metadata/fence_aws.xml	2017-08-29 10:52:48.250543883 +0200
035a21
@@ -0,0 +1,111 @@
035a21
+
035a21
+<resource-agent name="fence_aws" shortdesc="Fence agent for AWS (Amazon Web Services)" >
035a21
+<longdesc>fence_aws is an I/O Fencing agent for AWS (Amazon WebServices). It uses the boto3 library to connect to AWS.
035a21
+.P
035a21
+boto3 can be configured with AWS CLI or by creating ~/.aws/credentials.
035a21
+For instructions see: https://boto3.readthedocs.io/en/latest/guide/quickstart.html#configuration</longdesc>
035a21
+<vendor-url>http://www.amazon.com</vendor-url>
035a21
+<parameters>
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="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="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="region" unique="0" required="0">
035a21
+		<getopt mixed="-r, --region=[name]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Region.</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="access_key" unique="0" required="0">
035a21
+		<getopt mixed="-a, --access-key=[name]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Access Key.</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="secret_key" unique="0" required="0">
035a21
+		<getopt mixed="-s, --secret-key=[name]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Secret Key.</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_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--power-timeout=[seconds]" />
035a21
+		<content type="second" default="60"  />
035a21
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="power_wait" unique="0" required="0">
035a21
+		<getopt mixed="--power-wait=[seconds]" />
035a21
+		<content type="second" default="0"  />
035a21
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="shell_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--shell-timeout=[seconds]" />
035a21
+		<content type="second" default="3"  />
035a21
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</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="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="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>