Blame SOURCES/fence_aliyun-1.patch

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