Blame SOURCES/bz1568742-1-fence_aliyun.patch

554a6e
From 418df5845d1781e18e300cf17b2de714e4ff09d1 Mon Sep 17 00:00:00 2001
554a6e
From: "feng.changf1" <feng.changf1@alibaba-inc.com>
554a6e
Date: Tue, 24 Jul 2018 15:56:50 +0800
554a6e
Subject: [PATCH 1/3] Add Aliyun fence agent.
554a6e
554a6e
---
554a6e
 agents/aliyun/fence_aliyun.py | 146 ++++++++++++++++++++++++++++++++++++++++++
554a6e
 1 file changed, 146 insertions(+)
554a6e
 create mode 100644 agents/aliyun/fence_aliyun.py
554a6e
554a6e
diff --git a/agents/aliyun/fence_aliyun.py b/agents/aliyun/fence_aliyun.py
554a6e
new file mode 100644
554a6e
index 00000000..ec7d2316
554a6e
--- /dev/null
554a6e
+++ b/agents/aliyun/fence_aliyun.py
554a6e
@@ -0,0 +1,146 @@
554a6e
+#!/usr/bin/python -tt
554a6e
+
554a6e
+import sys, re
554a6e
+import logging
554a6e
+import atexit
554a6e
+import json
554a6e
+sys.path.append("@FENCEAGENTSLIBDIR@")
554a6e
+from fencing import *
554a6e
+from fencing import fail, fail_usage, EC_TIMED_OUT, run_delay
554a6e
+
554a6e
+from aliyunsdkcore import client
554a6e
+
554a6e
+from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
554a6e
+from aliyunsdkecs.request.v20140526.StartInstanceRequest import StartInstanceRequest
554a6e
+from aliyunsdkecs.request.v20140526.StopInstanceRequest import StopInstanceRequest
554a6e
+from aliyunsdkecs.request.v20140526.RebootInstanceRequest import RebootInstanceRequest
554a6e
+
554a6e
+def _send_request(conn, request):
554a6e
+	request.set_accept_format('json')
554a6e
+	try:
554a6e
+		response_str = conn.do_action_with_exception(request)
554a6e
+		response_detail = json.loads(response_str)
554a6e
+		return response_detail
554a6e
+	except Exception as e:
554a6e
+		fail_usage("Failed: _send_request failed")
554a6e
+
554a6e
+def start_instance(conn, instance_id):
554a6e
+	request = StartInstanceRequest()
554a6e
+	request.set_InstanceId(instance_id)
554a6e
+	_send_request(conn, request)
554a6e
+
554a6e
+def stop_instance(conn, instance_id):
554a6e
+	request = StopInstanceRequest()
554a6e
+	request.set_InstanceId(instance_id)
554a6e
+	request.set_ForceStop('true')
554a6e
+	_send_request(conn, request)
554a6e
+
554a6e
+def reboot_instance(conn, instance_id):
554a6e
+	request = RebootInstanceRequest()
554a6e
+	request.set_InstanceId(instance_id)
554a6e
+	request.set_ForceStop('true')
554a6e
+	_send_request(conn, request)
554a6e
+
554a6e
+def get_status(conn, instance_id):
554a6e
+	request = DescribeInstancesRequest()
554a6e
+	request.set_InstanceIds(json.dumps([instance_id]))
554a6e
+	response = _send_request(conn, request)
554a6e
+	instance_status = None
554a6e
+	if response is not None:
554a6e
+		instance_list = response.get('Instances').get('Instance')
554a6e
+		for item in instance_list:
554a6e
+			instance_status = item.get('Status')
554a6e
+	return instance_status
554a6e
+
554a6e
+def get_nodes_list(conn, options):
554a6e
+	result = {}
554a6e
+	request = DescribeInstancesRequest()
554a6e
+	response = _send_request(conn, request)
554a6e
+	instance_status = None
554a6e
+	if response is not None:
554a6e
+		instance_list = response.get('Instances').get('Instance')
554a6e
+		for item in instance_list:
554a6e
+			instance_id = item.get('InstanceId')
554a6e
+			result[instance_id] = ("", None)
554a6e
+	return result
554a6e
+
554a6e
+def get_power_status(conn, options):
554a6e
+	state = get_status(conn, options["--plug"])
554a6e
+	if state == "Running":
554a6e
+		return "on"
554a6e
+	elif state == "Stopped":
554a6e
+		return "off"
554a6e
+	else:
554a6e
+		return "unknown"
554a6e
+
554a6e
+
554a6e
+def set_power_status(conn, options):
554a6e
+	if (options["--action"]=="off"):
554a6e
+		stop_instance(conn, options["--plug"])
554a6e
+	elif (options["--action"]=="on"):
554a6e
+		start_instance(conn, options["--plug"])
554a6e
+	elif (options["--action"]=="reboot"):
554a6e
+		reboot_instance(conn, options["--plug"])
554a6e
+
554a6e
+
554a6e
+def define_new_opts():
554a6e
+	all_opt["region"] = {
554a6e
+		"getopt" : "r:",
554a6e
+		"longopt" : "region",
554a6e
+		"help" : "-r, --region=[name]            Region, e.g. cn-hangzhou",
554a6e
+		"shortdesc" : "Region.",
554a6e
+		"required" : "0",
554a6e
+		"order" : 2
554a6e
+	}
554a6e
+	all_opt["access_key"] = {
554a6e
+		"getopt" : "a:",
554a6e
+		"longopt" : "access-key",
554a6e
+		"help" : "-a, --access-key=[name]         Access Key",
554a6e
+		"shortdesc" : "Access Key.",
554a6e
+		"required" : "0",
554a6e
+		"order" : 3
554a6e
+	}
554a6e
+	all_opt["secret_key"] = {
554a6e
+		"getopt" : "s:",
554a6e
+		"longopt" : "secret-key",
554a6e
+		"help" : "-s, --secret-key=[name]         Secret Key",
554a6e
+		"shortdesc" : "Secret Key.",
554a6e
+		"required" : "0",
554a6e
+		"order" : 4
554a6e
+	}
554a6e
+
554a6e
+# Main agent method
554a6e
+def main():
554a6e
+	conn = None
554a6e
+
554a6e
+	device_opt = ["port", "no_password", "region", "access_key", "secret_key"]
554a6e
+
554a6e
+	atexit.register(atexit_handler)
554a6e
+
554a6e
+	define_new_opts()
554a6e
+
554a6e
+	all_opt["power_timeout"]["default"] = "60"
554a6e
+
554a6e
+	options = check_input(device_opt, process_input(device_opt))
554a6e
+
554a6e
+	docs = {}
554a6e
+	docs["shortdesc"] = "Fence agent for Aliyun (Aliyun Web Services)"
554a6e
+	docs["longdesc"] = "fence_aliyun is an I/O Fencing agent for Aliyun"
554a6e
+	docs["vendorurl"] = "http://www.aliyun.com"
554a6e
+	show_docs(options, docs)
554a6e
+
554a6e
+	run_delay(options)
554a6e
+
554a6e
+	if "--region" in options and "--access-key" in options and "--secret-key" in options:  
554a6e
+		region = options["--region"]
554a6e
+		access_key = options["--access-key"]
554a6e
+		secret_key = options["--secret-key"]
554a6e
+		conn = client.AcsClient(access_key, secret_key, region)
554a6e
+
554a6e
+
554a6e
+	# Operate the fencing device
554a6e
+	result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list)
554a6e
+	sys.exit(result)
554a6e
+
554a6e
+if __name__ == "__main__":
554a6e
+	main()
554a6e
554a6e
From 28b55555abda9b6c278a7f082bb22c4f6f1e2474 Mon Sep 17 00:00:00 2001
554a6e
From: "feng.changf1" <feng.changf1@alibaba-inc.com>
554a6e
Date: Tue, 24 Jul 2018 17:18:53 +0800
554a6e
Subject: [PATCH 2/3] Add Aliyun fence agent.
554a6e
554a6e
---
554a6e
 tests/data/metadata/fence_aliyun.xml | 113 +++++++++++++++++++++++++++++++++++
554a6e
 1 file changed, 113 insertions(+)
554a6e
 create mode 100644 tests/data/metadata/fence_aliyun.xml
554a6e
554a6e
diff --git a/tests/data/metadata/fence_aliyun.xml b/tests/data/metadata/fence_aliyun.xml
554a6e
new file mode 100644
554a6e
index 00000000..1db692ee
554a6e
--- /dev/null
554a6e
+++ b/tests/data/metadata/fence_aliyun.xml
554a6e
@@ -0,0 +1,113 @@
554a6e
+
554a6e
+<resource-agent name="fence_aliyun" shortdesc="Fence agent for Aliyun (Aliyun Web Services)" >
554a6e
+<longdesc>fence_aliyun is an I/O Fencing agent for Aliyun</longdesc>
554a6e
+<vendor-url>http://www.aliyun.com</vendor-url>
554a6e
+<parameters>
554a6e
+	<parameter name="action" unique="0" required="1">
554a6e
+		<getopt mixed="-o, --action=[action]" />
554a6e
+		<content type="string" default="reboot"  />
554a6e
+		<shortdesc lang="en">Fencing action</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="plug" unique="0" required="1" obsoletes="port">
554a6e
+		<getopt mixed="-n, --plug=[id]" />
554a6e
+		<content type="string"  />
554a6e
+		<shortdesc lang="en">Physical plug number on device, UUID or identification of machine</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="port" unique="0" required="1" deprecated="1">
554a6e
+		<getopt mixed="-n, --plug=[id]" />
554a6e
+		<content type="string"  />
554a6e
+		<shortdesc lang="en">Physical plug number on device, UUID or identification of machine</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="region" unique="0" required="0">
554a6e
+		<getopt mixed="-r, --region=[name]" />
554a6e
+		<content type="string"  />
554a6e
+		<shortdesc lang="en">Region.</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="access_key" unique="0" required="0">
554a6e
+		<getopt mixed="-a, --access-key=[name]" />
554a6e
+		<content type="string"  />
554a6e
+		<shortdesc lang="en">Access Key.</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="secret_key" unique="0" required="0">
554a6e
+		<getopt mixed="-s, --secret-key=[name]" />
554a6e
+		<content type="string"  />
554a6e
+		<shortdesc lang="en">Secret Key.</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="quiet" unique="0" required="0">
554a6e
+		<getopt mixed="-q, --quiet" />
554a6e
+		<content type="boolean"  />
554a6e
+		<shortdesc lang="en">Disable logging to stderr. Does not affect --verbose or --debug-file or logging to syslog.</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="verbose" unique="0" required="0">
554a6e
+		<getopt mixed="-v, --verbose" />
554a6e
+		<content type="boolean"  />
554a6e
+		<shortdesc lang="en">Verbose mode</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="debug" unique="0" required="0" deprecated="1">
554a6e
+		<getopt mixed="-D, --debug-file=[debugfile]" />
554a6e
+		<content type="string"  />
554a6e
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="debug_file" unique="0" required="0" obsoletes="debug">
554a6e
+		<getopt mixed="-D, --debug-file=[debugfile]" />
554a6e
+		<content type="string"  />
554a6e
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="version" unique="0" required="0">
554a6e
+		<getopt mixed="-V, --version" />
554a6e
+		<content type="boolean"  />
554a6e
+		<shortdesc lang="en">Display version information and exit</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="help" unique="0" required="0">
554a6e
+		<getopt mixed="-h, --help" />
554a6e
+		<content type="boolean"  />
554a6e
+		<shortdesc lang="en">Display help and exit</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="separator" unique="0" required="0">
554a6e
+		<getopt mixed="-C, --separator=[char]" />
554a6e
+		<content type="string" default=","  />
554a6e
+		<shortdesc lang="en">Separator for CSV created by 'list' operation</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="delay" unique="0" required="0">
554a6e
+		<getopt mixed="--delay=[seconds]" />
554a6e
+		<content type="second" default="0"  />
554a6e
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="login_timeout" unique="0" required="0">
554a6e
+		<getopt mixed="--login-timeout=[seconds]" />
554a6e
+		<content type="second" default="5"  />
554a6e
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="power_timeout" unique="0" required="0">
554a6e
+		<getopt mixed="--power-timeout=[seconds]" />
554a6e
+		<content type="second" default="60"  />
554a6e
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="power_wait" unique="0" required="0">
554a6e
+		<getopt mixed="--power-wait=[seconds]" />
554a6e
+		<content type="second" default="0"  />
554a6e
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="shell_timeout" unique="0" required="0">
554a6e
+		<getopt mixed="--shell-timeout=[seconds]" />
554a6e
+		<content type="second" default="3"  />
554a6e
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
554a6e
+	</parameter>
554a6e
+	<parameter name="retry_on" unique="0" required="0">
554a6e
+		<getopt mixed="--retry-on=[attempts]" />
554a6e
+		<content type="integer" default="1"  />
554a6e
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
554a6e
+	</parameter>
554a6e
+</parameters>
554a6e
+<actions>
554a6e
+	<action name="on" automatic="0"/>
554a6e
+	<action name="off" />
554a6e
+	<action name="reboot" />
554a6e
+	<action name="status" />
554a6e
+	<action name="list" />
554a6e
+	<action name="list-status" />
554a6e
+	<action name="monitor" />
554a6e
+	<action name="metadata" />
554a6e
+	<action name="validate-all" />
554a6e
+</actions>
554a6e
+</resource-agent>
554a6e
554a6e
From 53bbd91e91c231c89ae8981238bb15d85d02207b Mon Sep 17 00:00:00 2001
554a6e
From: "feng.changf1" <feng.changf1@alibaba-inc.com>
554a6e
Date: Tue, 24 Jul 2018 17:26:45 +0800
554a6e
Subject: [PATCH 3/3] Fix CI error.
554a6e
554a6e
---
554a6e
 agents/aliyun/fence_aliyun.py | 32 +++++++++++++++++---------------
554a6e
 1 file changed, 17 insertions(+), 15 deletions(-)
554a6e
554a6e
diff --git a/agents/aliyun/fence_aliyun.py b/agents/aliyun/fence_aliyun.py
554a6e
index ec7d2316..0f24b83e 100644
554a6e
--- a/agents/aliyun/fence_aliyun.py
554a6e
+++ b/agents/aliyun/fence_aliyun.py
554a6e
@@ -1,20 +1,22 @@
554a6e
 #!/usr/bin/python -tt
554a6e
 
554a6e
-import sys, re
554a6e
-import logging
554a6e
-import atexit
554a6e
-import json
554a6e
-sys.path.append("@FENCEAGENTSLIBDIR@")
554a6e
-from fencing import *
554a6e
-from fencing import fail, fail_usage, EC_TIMED_OUT, run_delay
554a6e
-
554a6e
-from aliyunsdkcore import client
554a6e
-
554a6e
-from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
554a6e
-from aliyunsdkecs.request.v20140526.StartInstanceRequest import StartInstanceRequest
554a6e
-from aliyunsdkecs.request.v20140526.StopInstanceRequest import StopInstanceRequest
554a6e
-from aliyunsdkecs.request.v20140526.RebootInstanceRequest import RebootInstanceRequest
554a6e
-
554a6e
+try:
554a6e
+	import sys, re
554a6e
+	import logging
554a6e
+	import atexit
554a6e
+	import json
554a6e
+	sys.path.append("@FENCEAGENTSLIBDIR@")
554a6e
+	from fencing import *
554a6e
+	from fencing import fail, fail_usage, EC_TIMED_OUT, run_delay
554a6e
+
554a6e
+	from aliyunsdkcore import client
554a6e
+
554a6e
+	from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
554a6e
+	from aliyunsdkecs.request.v20140526.StartInstanceRequest import StartInstanceRequest
554a6e
+	from aliyunsdkecs.request.v20140526.StopInstanceRequest import StopInstanceRequest
554a6e
+	from aliyunsdkecs.request.v20140526.RebootInstanceRequest import RebootInstanceRequest
554a6e
+except ImportError:
554a6e
+	pass
554a6e
 def _send_request(conn, request):
554a6e
 	request.set_accept_format('json')
554a6e
 	try: