Blame SOURCES/bz1214359-1-fence_compute.patch

02611b
From 563c93016b0581c2f6beea1f3f18e76e25491895 Mon Sep 17 00:00:00 2001
02611b
From: Marek 'marx' Grac <mgrac@redhat.com>
02611b
Date: Fri, 5 Jun 2015 18:03:00 +0200
02611b
Subject: [PATCH 1/4] fence_compute: Fence agent for Nova compute machines
02611b
02611b
Author: Andrew Beekhof
02611b
---
02611b
 configure.ac                          |   2 +
02611b
 fence/agents/compute/Makefile.am      |  17 +++
02611b
 fence/agents/compute/fence_compute.py | 218 ++++++++++++++++++++++++++++++++++
02611b
 make/fencebuild.mk                    |   1 +
02611b
 tests/data/metadata/fence_compute.xml | 121 +++++++++++++++++++
02611b
 5 files changed, 359 insertions(+)
02611b
 create mode 100644 fence/agents/compute/Makefile.am
02611b
 create mode 100644 fence/agents/compute/fence_compute.py
02611b
 create mode 100644 tests/data/metadata/fence_compute.xml
02611b
02611b
diff --git a/configure.ac b/configure.ac
02611b
index b603878..9d996d3 100644
02611b
--- a/configure.ac
02611b
+++ b/configure.ac
02611b
@@ -167,6 +167,7 @@ AC_PATH_PROG([COROSYNC_CMAPCTL_PATH], [corosync-cmapctl], [/usr/sbin/corosync-cm
02611b
 AC_PATH_PROG([SG_PERSIST_PATH], [sg_persist], [/usr/bin/sg_persist])
02611b
 AC_PATH_PROG([SG_TURS_PATH], [sg_turs], [/usr/bin/sg_turs])
02611b
 AC_PATH_PROG([VGS_PATH], [vgs], [/usr/sbin/vgs])
02611b
+AC_PATH_PROG([NOVA_PATH], [nova], [/usr/bin/nova])
02611b
 ## do subst
02611b
 
02611b
 AC_SUBST([DEFAULT_CONFIG_DIR])
02611b
@@ -265,6 +266,7 @@ AC_CONFIG_FILES([Makefile
02611b
 		 fence/agents/brocade/Makefile
02611b
 		 fence/agents/cisco_mds/Makefile
02611b
 		 fence/agents/cisco_ucs/Makefile
02611b
+		 fence/agents/compute/Makefile
02611b
 		 fence/agents/docker/Makefile
02611b
 		 fence/agents/drac/Makefile
02611b
 		 fence/agents/drac5/Makefile
02611b
diff --git a/fence/agents/compute/Makefile.am b/fence/agents/compute/Makefile.am
02611b
new file mode 100644
02611b
index 0000000..ab21272
02611b
--- /dev/null
02611b
+++ b/fence/agents/compute/Makefile.am
02611b
@@ -0,0 +1,17 @@
02611b
+MAINTAINERCLEANFILES	= Makefile.in
02611b
+
02611b
+TARGET			= fence_compute
02611b
+
02611b
+SRC			= $(TARGET).py
02611b
+
02611b
+EXTRA_DIST		= $(SRC)
02611b
+
02611b
+sbin_SCRIPTS		= $(TARGET)
02611b
+
02611b
+man_MANS		= $(TARGET).8
02611b
+
02611b
+FENCE_TEST_ARGS		= -l test -p test -n 1
02611b
+
02611b
+include $(top_srcdir)/make/fencebuild.mk
02611b
+include $(top_srcdir)/make/fenceman.mk
02611b
+include $(top_srcdir)/make/agentpycheck.mk
02611b
diff --git a/fence/agents/compute/fence_compute.py b/fence/agents/compute/fence_compute.py
02611b
new file mode 100644
02611b
index 0000000..2b37de7
02611b
--- /dev/null
02611b
+++ b/fence/agents/compute/fence_compute.py
02611b
@@ -0,0 +1,218 @@
02611b
+#!/usr/bin/python -tt
02611b
+
02611b
+import sys
02611b
+import time
02611b
+import atexit
02611b
+import logging
02611b
+
02611b
+sys.path.append("@FENCEAGENTSLIBDIR@")
02611b
+from fencing import *
02611b
+from fencing import fail_usage, is_executable, run_command, run_delay
02611b
+from novaclient import client as nova_client
02611b
+
02611b
+#BEGIN_VERSION_GENERATION
02611b
+RELEASE_VERSION="4.0.11"
02611b
+BUILD_DATE="(built Wed Nov 12 06:33:38 EST 2014)"
02611b
+REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
02611b
+#END_VERSION_GENERATION
02611b
+
02611b
+override_status = ""
02611b
+nova = None
02611b
+
02611b
+def get_power_status(_, options):
02611b
+	global override_status
02611b
+
02611b
+	status = "unknown"
02611b
+	logging.debug("get action: " + options["--action"])
02611b
+
02611b
+	if len(override_status):
02611b
+		logging.debug("Pretending we're " + override_status)
02611b
+		return override_status
02611b
+
02611b
+	if nova:
02611b
+		try:
02611b
+			services = nova.services.list(host=options["--plug"])
02611b
+		except Exception, e:
02611b
+			fail_usage(str(e))
02611b
+
02611b
+		for service in services:
02611b
+			if service.binary == "nova-compute":
02611b
+				if service.state == "up":
02611b
+					status = "on"
02611b
+				elif service.state == "down":
02611b
+					status = "down"
02611b
+				else:
02611b
+					logging.debug("Unknown status detected from nova: " + service.state)
02611b
+				break
02611b
+	return status
02611b
+
02611b
+# NOTE(sbauza); We mimic the host-evacuate module since it's only a contrib
02611b
+# module which is not stable
02611b
+def _server_evacuate(server, on_shared_storage):
02611b
+	success = True
02611b
+	error_message = ""
02611b
+	try:
02611b
+		nova.servers.evacuate(server=server['uuid'], on_shared_storage=on_shared_storage)
02611b
+	except Exception as e:
02611b
+		success = False
02611b
+		error_message = "Error while evacuating instance: %s" % e
02611b
+
02611b
+	return {
02611b
+		"server_uuid": server['uuid'],
02611b
+		"evacuate_accepted": success,
02611b
+		"error_message": error_message,
02611b
+		}
02611b
+
02611b
+def _host_evacuate(host, on_shared_storage):
02611b
+	hypervisors = nova.hypervisors.search(host, servers=True)
02611b
+	response = []
02611b
+	for hyper in hypervisors:
02611b
+		if hasattr(hyper, 'servers'):
02611b
+			for server in hyper.servers:
02611b
+				response.append(_server_evacuate(server, on_shared_storage))
02611b
+
02611b
+def set_power_status(_, options):
02611b
+	global override_status
02611b
+
02611b
+	override_status = ""
02611b
+	logging.debug("set action: " + options["--action"])
02611b
+
02611b
+	if not nova:
02611b
+		return
02611b
+
02611b
+	if options["--action"] == "on":
02611b
+		if get_power_status(_, options) == "on":
02611b
+			nova.services.enable(options["--plug"], 'nova-compute')
02611b
+		else:
02611b
+			# Pretend we're 'on' so that the fencing library doesn't loop forever waiting for the node to boot
02611b
+			override_status = "on"
02611b
+		return
02611b
+
02611b
+	# need to wait for nova to update its internal status or we
02611b
+	# cannot call host-evacuate
02611b
+	while get_power_status(_, options) != "off":
02611b
+		# Loop forever if need be.
02611b
+		#
02611b
+		# Some callers (such as Pacemaker) will have a timer
02611b
+		# running and kill us if necessary
02611b
+		logging.debug("Waiting for nova to update it's internal state")
02611b
+		time.sleep(1)
02611b
+
02611b
+	if "--no-shared-storage" not in options:
02611b
+		# If the admin sets this when they DO have shared
02611b
+		# storage in use, then they get what they asked for
02611b
+		on_shared_storage = True
02611b
+	else:
02611b
+		on_shared_storage = False
02611b
+
02611b
+	_host_evacuate(options["--plug"], on_shared_storage)
02611b
+	return
02611b
+
02611b
+def get_plugs_list(_, options):
02611b
+	result = {}
02611b
+
02611b
+	if nova:
02611b
+		hypervisors = nova.hypervisors.list()
02611b
+		for hypervisor in hypervisors:
02611b
+			longhost = hypervisor.hypervisor_hostname
02611b
+			if options["--action"] == "list" and options["--domain"] != "":
02611b
+				shorthost = longhost.replace("." + options["--domain"],
02611b
+                                                 "")
02611b
+				result[shorthost] = ("", None)
02611b
+			else:
02611b
+				result[longhost] = ("", None)
02611b
+	return result
02611b
+
02611b
+
02611b
+def define_new_opts():
02611b
+	all_opt["tenant-name"] = {
02611b
+		"getopt" : "t:",
02611b
+		"longopt" : "tenant-name",
02611b
+		"help" : "-t, --tenant-name=[tenant]     Keystone Admin Tenant",
02611b
+		"required" : "0",
02611b
+		"shortdesc" : "Keystone Admin Tenant",
02611b
+		"default" : "",
02611b
+		"order": 1,
02611b
+	}
02611b
+	all_opt["auth-url"] = {
02611b
+		"getopt" : "k:",
02611b
+		"longopt" : "auth-url",
02611b
+		"help" : "-k, --auth-url=[tenant]        Keystone Admin Auth URL",
02611b
+		"required" : "0",
02611b
+		"shortdesc" : "Keystone Admin Auth URL",
02611b
+		"default" : "",
02611b
+		"order": 1,
02611b
+	}
02611b
+	all_opt["novatool-path"] = {
02611b
+		"getopt" : "i:",
02611b
+		"longopt" : "novatool-path",
02611b
+		"help" : "-i, --novatool-path=[path]     Path to nova binary",
02611b
+		"required" : "0",
02611b
+		"shortdesc" : "Path to nova binary",
02611b
+		"default" : "@NOVA_PATH@",
02611b
+		"order": 6,
02611b
+	}
02611b
+	all_opt["domain"] = {
02611b
+		"getopt" : "d:",
02611b
+		"longopt" : "domain",
02611b
+		"help" : "-d, --domain=[string]          DNS domain in which hosts live, useful when the cluster uses short names and nova uses FQDN",
02611b
+		"required" : "0",
02611b
+		"shortdesc" : "DNS domain in which hosts live",
02611b
+		"default" : "",
02611b
+		"order": 5,
02611b
+	}
02611b
+	all_opt["no-shared-storage"] = {
02611b
+		"getopt" : "",
02611b
+		"longopt" : "no-shared-storage",
02611b
+		"help" : "--no-shared-storage            Disable functionality for shared storage",
02611b
+		"required" : "0",
02611b
+		"shortdesc" : "Disable functionality for dealing with shared storage",
02611b
+		"default" : "False",
02611b
+		"order": 5,
02611b
+	}
02611b
+
02611b
+def main():
02611b
+	global override_status
02611b
+	global nova
02611b
+	atexit.register(atexit_handler)
02611b
+
02611b
+	device_opt = ["login", "passwd", "tenant-name", "auth-url",
02611b
+		"novatool-path", "no_login", "no_password", "port", "domain", "no-shared-storage"]
02611b
+	define_new_opts()
02611b
+	all_opt["shell_timeout"]["default"] = "180"
02611b
+
02611b
+	options = check_input(device_opt, process_input(device_opt))
02611b
+
02611b
+	docs = {}
02611b
+	docs["shortdesc"] = "Fence agent for nova compute nodes"
02611b
+	docs["longdesc"] = "fence_nova_host is a Nova fencing notification agent"
02611b
+	docs["vendorurl"] = ""
02611b
+
02611b
+	show_docs(options, docs)
02611b
+
02611b
+	run_delay(options)
02611b
+
02611b
+	# The first argument is the Nova client version
02611b
+	nova = nova_client.Client('2',
02611b
+		options["--username"],
02611b
+		options["--password"],
02611b
+		options["--tenant-name"],
02611b
+		options["--auth-url"])
02611b
+
02611b
+	if options["--action"] in ["off", "reboot"]:
02611b
+		# Pretend we're 'on' so that the fencing library will always call set_power_status(off)
02611b
+		override_status = "on"
02611b
+
02611b
+	if options["--action"] == "on":
02611b
+		# Pretend we're 'off' so that the fencing library will always call set_power_status(on)
02611b
+		override_status = "off"
02611b
+
02611b
+	# Potentially we should make this a pacemaker feature
02611b
+	if options["--action"] != "list" and options["--domain"] != "" and options.has_key("--plug"):
02611b
+		options["--plug"] = options["--plug"]+"."+options["--domain"]
02611b
+
02611b
+	result = fence_action(None, options, set_power_status, get_power_status, get_plugs_list, None)
02611b
+	sys.exit(result)
02611b
+
02611b
+if __name__ == "__main__":
02611b
+	main()
02611b
diff --git a/make/fencebuild.mk b/make/fencebuild.mk
02611b
index 1c4be6b..b59c069 100644
02611b
--- a/make/fencebuild.mk
02611b
+++ b/make/fencebuild.mk
02611b
@@ -16,6 +16,7 @@ $(TARGET): $(SRC)
02611b
 		-e 's#@''SG_PERSIST_PATH@#${SG_PERSIST_PATH}#g' \
02611b
 		-e 's#@''SG_TURS_PATH@#${SG_TURS_PATH}#g' \
02611b
 		-e 's#@''VGS_PATH@#${VGS_PATH}#g' \
02611b
+		-e 's#@''NOVA_PATH@#${NOVA_PATH}#g' \
02611b
 	> $@
02611b
 
02611b
 	if [ 0 -eq `echo "$(SRC)" | grep fence_ &> /dev/null; echo $$?` ]; then \
02611b
diff --git a/tests/data/metadata/fence_compute.xml b/tests/data/metadata/fence_compute.xml
02611b
new file mode 100644
02611b
index 0000000..ff7c06c
02611b
--- /dev/null
02611b
+++ b/tests/data/metadata/fence_compute.xml
02611b
@@ -0,0 +1,121 @@
02611b
+
02611b
+<resource-agent name="fence_compute" shortdesc="Fence agent for nova compute nodes" >
02611b
+<longdesc>fence_nova_host is a Nova fencing notification agent</longdesc>
02611b
+<vendor-url></vendor-url>
02611b
+<parameters>
02611b
+	<parameter name="port" unique="0" required="1">
02611b
+		<getopt mixed="-n, --plug=[id]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">Physical plug number, name of virtual machine or UUID</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="passwd_script" unique="0" required="0">
02611b
+		<getopt mixed="-S, --password-script=[script]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">Script to retrieve password</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="auth-url" unique="0" required="0">
02611b
+		<getopt mixed="-k, --auth-url=[tenant]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">Keystone Admin Auth URL</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="passwd" unique="0" required="0">
02611b
+		<getopt mixed="-p, --password=[password]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="tenant-name" unique="0" required="0">
02611b
+		<getopt mixed="-t, --tenant-name=[tenant]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">Keystone Admin Tenant</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="action" unique="0" required="1">
02611b
+		<getopt mixed="-o, --action=[action]" />
02611b
+		<content type="string" default="reboot"  />
02611b
+		<shortdesc lang="en">Fencing Action</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="login" unique="0" required="0">
02611b
+		<getopt mixed="-l, --username=[name]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">Login Name</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="domain" unique="0" required="0">
02611b
+		<getopt mixed="-d, --domain=[string]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">DNS domain in which hosts live</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="no-shared-storage" unique="0" required="0">
02611b
+		<getopt mixed="--no-shared-storage" />
02611b
+		<content type="boolean" default="False"  />
02611b
+		<shortdesc lang="en">Disable functionality for dealing with shared storage</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="novatool-path" unique="0" required="0">
02611b
+		<getopt mixed="-i, --novatool-path=[path]" />
02611b
+		<content type="string" default="/usr/bin/nova"  />
02611b
+		<shortdesc lang="en">Path to nova binary</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="verbose" unique="0" required="0">
02611b
+		<getopt mixed="-v, --verbose" />
02611b
+		<content type="boolean"  />
02611b
+		<shortdesc lang="en">Verbose mode</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="debug" unique="0" required="0">
02611b
+		<getopt mixed="-D, --debug-file=[debugfile]" />
02611b
+		<content type="string"  />
02611b
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="version" unique="0" required="0">
02611b
+		<getopt mixed="-V, --version" />
02611b
+		<content type="boolean"  />
02611b
+		<shortdesc lang="en">Display version information and exit</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="help" unique="0" required="0">
02611b
+		<getopt mixed="-h, --help" />
02611b
+		<content type="boolean"  />
02611b
+		<shortdesc lang="en">Display help and exit</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="separator" unique="0" required="0">
02611b
+		<getopt mixed="-C, --separator=[char]" />
02611b
+		<content type="string" default=","  />
02611b
+		<shortdesc lang="en">Separator for CSV created by operation list</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="power_wait" unique="0" required="0">
02611b
+		<getopt mixed="--power-wait=[seconds]" />
02611b
+		<content type="string" default="0"  />
02611b
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="login_timeout" unique="0" required="0">
02611b
+		<getopt mixed="--login-timeout=[seconds]" />
02611b
+		<content type="string" default="5"  />
02611b
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="power_timeout" unique="0" required="0">
02611b
+		<getopt mixed="--power-timeout=[seconds]" />
02611b
+		<content type="string" default="20"  />
02611b
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="delay" unique="0" required="0">
02611b
+		<getopt mixed="--delay=[seconds]" />
02611b
+		<content type="string" default="0"  />
02611b
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="shell_timeout" unique="0" required="0">
02611b
+		<getopt mixed="--shell-timeout=[seconds]" />
02611b
+		<content type="string" default="180"  />
02611b
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
02611b
+	</parameter>
02611b
+	<parameter name="retry_on" unique="0" required="0">
02611b
+		<getopt mixed="--retry-on=[attempts]" />
02611b
+		<content type="string" default="1"  />
02611b
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
02611b
+	</parameter>
02611b
+</parameters>
02611b
+<actions>
02611b
+	<action name="on" automatic="0"/>
02611b
+	<action name="off" />
02611b
+	<action name="reboot" />
02611b
+	<action name="status" />
02611b
+	<action name="list" />
02611b
+	<action name="monitor" />
02611b
+	<action name="metadata" />
02611b
+</actions>
02611b
+</resource-agent>
02611b
-- 
02611b
1.9.3
02611b