Blame SOURCES/bz1283084-fence_compute.patch

e4ffb1
From e80e142092c53102a46886e9748b8e25465ce4f6 Mon Sep 17 00:00:00 2001
e4ffb1
From: Marek 'marx' Grac <mgrac@redhat.com>
e4ffb1
Date: Wed, 20 Jan 2016 11:32:21 +0100
e4ffb1
Subject: [PATCH] fence_compute: Sync with master branch
e4ffb1
e4ffb1
---
e4ffb1
 fence/agents/compute/fence_compute.py | 180 ++++++++++++++++++++++++++--------
e4ffb1
 tests/data/metadata/fence_compute.xml |  16 +--
e4ffb1
 2 files changed, 150 insertions(+), 46 deletions(-)
e4ffb1
e4ffb1
diff --git a/fence/agents/compute/fence_compute.py b/fence/agents/compute/fence_compute.py
e4ffb1
index 82d9c46..d9fe54a 100644
e4ffb1
--- a/fence/agents/compute/fence_compute.py
e4ffb1
+++ b/fence/agents/compute/fence_compute.py
e4ffb1
@@ -19,6 +19,9 @@ REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
e4ffb1
 override_status = ""
e4ffb1
 nova = None
e4ffb1
 
e4ffb1
+EVACUABLE_TAG = "evacuable"
e4ffb1
+TRUE_TAGS = ['true']
e4ffb1
+
e4ffb1
 def get_power_status(_, options):
e4ffb1
 	global override_status
e4ffb1
 
e4ffb1
@@ -32,8 +35,8 @@ def get_power_status(_, options):
e4ffb1
 	if nova:
e4ffb1
 		try:
e4ffb1
 			services = nova.services.list(host=options["--plug"])
e4ffb1
-
e4ffb1
 			for service in services:
e4ffb1
+				logging.debug("Status of %s is %s" % (service.binary, service.state))
e4ffb1
 				if service.binary == "nova-compute":
e4ffb1
 					if service.state == "up":
e4ffb1
 						status = "on"
e4ffb1
@@ -49,31 +52,91 @@ def get_power_status(_, options):
e4ffb1
 # NOTE(sbauza); We mimic the host-evacuate module since it's only a contrib
e4ffb1
 # module which is not stable
e4ffb1
 def _server_evacuate(server, on_shared_storage):
e4ffb1
-	success = True
e4ffb1
+	success = False
e4ffb1
 	error_message = ""
e4ffb1
 	try:
e4ffb1
-		nova.servers.evacuate(server=server['uuid'], on_shared_storage=on_shared_storage)
e4ffb1
+		logging.debug("Resurrecting instance: %s" % server)
e4ffb1
+		(response, dictionary) = nova.servers.evacuate(server=server, on_shared_storage=on_shared_storage)
e4ffb1
+
e4ffb1
+		if response == None:
e4ffb1
+			error_message = "No response while evacuating instance"
e4ffb1
+		elif response.status_code == 200:
e4ffb1
+			success = True
e4ffb1
+			error_message = response.reason
e4ffb1
+		else:
e4ffb1
+			error_message = response.reason
e4ffb1
+
e4ffb1
 	except Exception as e:
e4ffb1
-		success = False
e4ffb1
 		error_message = "Error while evacuating instance: %s" % e
e4ffb1
 
e4ffb1
 	return {
e4ffb1
-		"server_uuid": server['uuid'],
e4ffb1
-		"evacuate_accepted": success,
e4ffb1
-		"error_message": error_message,
e4ffb1
+		"uuid": server,
e4ffb1
+		"accepted": success,
e4ffb1
+		"reason": error_message,
e4ffb1
 		}
e4ffb1
 
e4ffb1
-def _host_evacuate(host, on_shared_storage):
e4ffb1
-	hypervisors = nova.hypervisors.search(host, servers=True)
e4ffb1
-	response = []
e4ffb1
-	for hyper in hypervisors:
e4ffb1
-		if hasattr(hyper, 'servers'):
e4ffb1
-			for server in hyper.servers:
e4ffb1
-				response.append(_server_evacuate(server, on_shared_storage))
e4ffb1
+def _is_server_evacuable(server, evac_flavors, evac_images):
e4ffb1
+	if server.flavor.get('id') in evac_flavors:
e4ffb1
+		return True
e4ffb1
+	if server.image.get('id') in evac_images:
e4ffb1
+		return True
e4ffb1
+	return False
e4ffb1
+
e4ffb1
+def _get_evacuable_flavors():
e4ffb1
+	result = []
e4ffb1
+	flavors = nova.flavors.list()
e4ffb1
+	# Since the detailed view for all flavors doesn't provide the extra specs,
e4ffb1
+	# we need to call each of the flavor to get them.
e4ffb1
+	for flavor in flavors:
e4ffb1
+		if flavor.get_keys().get(EVACUABLE_TAG).strip().lower() in TRUE_TAGS:
e4ffb1
+			result.append(flavor.id)
e4ffb1
+	return result
e4ffb1
+
e4ffb1
+def _get_evacuable_images():
e4ffb1
+	result = []
e4ffb1
+	images = nova.images.list(detailed=True)
e4ffb1
+	for image in images:
e4ffb1
+		if hasattr(image, 'metadata'):
e4ffb1
+			if image.metadata.get(EVACUABLE_TAG).strip.lower() in TRUE_TAGS:
e4ffb1
+				result.append(image.id)
e4ffb1
+	return result
e4ffb1
+
e4ffb1
+def _host_evacuate(options):
e4ffb1
+	result = True
e4ffb1
+	servers = nova.servers.list(search_opts={'host': options["--plug"]})
e4ffb1
+	if options["--instance-filtering"] == "False":
e4ffb1
+		evacuables = servers
e4ffb1
+	else:
e4ffb1
+		flavors = _get_evacuable_flavors()
e4ffb1
+		images = _get_evacuable_images()
e4ffb1
+		# Identify all evacuable servers
e4ffb1
+		evacuables = [server for server in servers
e4ffb1
+				if _is_server_evacuable(server, flavors, images)]
e4ffb1
+
e4ffb1
+	if options["--no-shared-storage"] != "False":
e4ffb1
+		on_shared_storage = False
e4ffb1
+	else:
e4ffb1
+		on_shared_storage = True
e4ffb1
+
e4ffb1
+	for server in evacuables:
e4ffb1
+		if hasattr(server, 'id'):
e4ffb1
+			response = _server_evacuate(server.id, on_shared_storage)
e4ffb1
+			if response["accepted"]:
e4ffb1
+				logging.debug("Evacuated %s from %s: %s" %
e4ffb1
+					      (response["uuid"], options["--plug"], response["reason"]))
e4ffb1
+			else:
e4ffb1
+				logging.error("Evacuation of %s on %s failed: %s" %
e4ffb1
+					      (response["uuid"], options["--plug"], response["reason"]))
e4ffb1
+				result = False
e4ffb1
+		else:
e4ffb1
+			logging.error("Could not evacuate instance: %s" % server.to_dict())
e4ffb1
+			# Should a malformed instance result in a failed evacuation?
e4ffb1
+			# result = False
e4ffb1
+	return result
e4ffb1
 
e4ffb1
 def set_attrd_status(host, status, options):
e4ffb1
 	logging.debug("Setting fencing status for %s to %s" % (host, status))
e4ffb1
-	run_command(options, "attrd_updater -p -n evacute -Q -N %s -v %s" % (host, status))
e4ffb1
+	run_command(options, "attrd_updater -p -n evacuate -Q -N %s -U %s" % (host, status))
e4ffb1
 
e4ffb1
 def set_power_status(_, options):
e4ffb1
 	global override_status
e4ffb1
@@ -86,28 +149,53 @@ def set_power_status(_, options):
e4ffb1
 
e4ffb1
 	if options["--action"] == "on":
e4ffb1
 		if get_power_status(_, options) == "on":
e4ffb1
+			# Forcing the service back up in case it was disabled
e4ffb1
 			nova.services.enable(options["--plug"], 'nova-compute')
e4ffb1
+			try:
e4ffb1
+				# Forcing the host back up
e4ffb1
+				nova.services.force_down(
e4ffb1
+					options["--plug"], "nova-compute", force_down=False)
e4ffb1
+			except Exception as e:
e4ffb1
+				# In theory, if foce_down=False fails, that's for the exact
e4ffb1
+				# same possible reasons that below with force_down=True
e4ffb1
+				# eg. either an incompatible version or an old client.
e4ffb1
+				# Since it's about forcing back to a default value, there is
e4ffb1
+				# no real worries to just consider it's still okay even if the
e4ffb1
+				# command failed
e4ffb1
+				logging.info("Exception from attempt to force "
e4ffb1
+					      "host back up via nova API: "
e4ffb1
+					      "%s: %s" % (e.__class__.__name__, e))
e4ffb1
 		else:
e4ffb1
 			# Pretend we're 'on' so that the fencing library doesn't loop forever waiting for the node to boot
e4ffb1
 			override_status = "on"
e4ffb1
 		return
e4ffb1
 
e4ffb1
-	# need to wait for nova to update its internal status or we
e4ffb1
-	# cannot call host-evacuate
e4ffb1
-	while get_power_status(_, options) != "off":
e4ffb1
-		# Loop forever if need be.
e4ffb1
-		#
e4ffb1
-		# Some callers (such as Pacemaker) will have a timer
e4ffb1
-		# running and kill us if necessary
e4ffb1
-		logging.debug("Waiting for nova to update it's internal state")
e4ffb1
-		time.sleep(1)
e4ffb1
-
e4ffb1
-	if options["--no-shared-storage"] != "False":
e4ffb1
-		on_shared_storage = False
e4ffb1
-	else:
e4ffb1
-		on_shared_storage = True
e4ffb1
+	try:
e4ffb1
+		nova.services.force_down(
e4ffb1
+			options["--plug"], "nova-compute", force_down=True)
e4ffb1
+	except Exception as e:
e4ffb1
+		# Something went wrong when we tried to force the host down.
e4ffb1
+		# That could come from either an incompatible API version
e4ffb1
+		# eg. UnsupportedVersion or VersionNotFoundForAPIMethod
e4ffb1
+		# or because novaclient is old and doesn't include force_down yet
e4ffb1
+		# eg. AttributeError
e4ffb1
+		# In that case, fallbacking to wait for Nova to catch the right state.
e4ffb1
+
e4ffb1
+		logging.error("Exception from attempt to force host down via nova API: "
e4ffb1
+			      "%s: %s" % (e.__class__.__name__, e))
e4ffb1
+		# need to wait for nova to update its internal status or we
e4ffb1
+		# cannot call host-evacuate
e4ffb1
+		while get_power_status(_, options) != "off":
e4ffb1
+			# Loop forever if need be.
e4ffb1
+			#
e4ffb1
+			# Some callers (such as Pacemaker) will have a timer
e4ffb1
+			# running and kill us if necessary
e4ffb1
+			logging.debug("Waiting for nova to update it's internal state for %s" % options["--plug"])
e4ffb1
+			time.sleep(1)
e4ffb1
+
e4ffb1
+	if not _host_evacuate(options):
e4ffb1
+		sys.exit(1)
e4ffb1
 
e4ffb1
-	_host_evacuate(options["--plug"], on_shared_storage)
e4ffb1
 	return
e4ffb1
 
e4ffb1
 def get_plugs_list(_, options):
e4ffb1
@@ -117,9 +205,9 @@ def get_plugs_list(_, options):
e4ffb1
 		hypervisors = nova.hypervisors.list()
e4ffb1
 		for hypervisor in hypervisors:
e4ffb1
 			longhost = hypervisor.hypervisor_hostname
e4ffb1
-			if options["--action"] == "list" and options["--domain"] != "":
e4ffb1
-				shorthost = longhost.replace("." + options["--domain"],
e4ffb1
-                                                 "")
e4ffb1
+			if options["--domain"] != "":
e4ffb1
+				shorthost = longhost.replace("." + options["--domain"], "")
e4ffb1
+				result[longhost] = ("", None)
e4ffb1
 				result[shorthost] = ("", None)
e4ffb1
 			else:
e4ffb1
 				result[longhost] = ("", None)
e4ffb1
@@ -164,7 +252,7 @@ def define_new_opts():
e4ffb1
 		"order": 5,
e4ffb1
 	}
e4ffb1
 	all_opt["record-only"] = {
e4ffb1
-		"getopt" : "",
e4ffb1
+		"getopt" : "r:",
e4ffb1
 		"longopt" : "record-only",
e4ffb1
 		"help" : "--record-only                  Record the target as needing evacuation but as yet do not intiate it",
e4ffb1
 		"required" : "0",
e4ffb1
@@ -172,6 +260,15 @@ def define_new_opts():
e4ffb1
 		"default" : "False",
e4ffb1
 		"order": 5,
e4ffb1
 	}
e4ffb1
+	all_opt["instance-filtering"] = {
e4ffb1
+		"getopt" : "",
e4ffb1
+		"longopt" : "instance-filtering",
e4ffb1
+		"help" : "--instance-filtering           Only evacuate instances create from images and flavors with evacuable=true",
e4ffb1
+		"required" : "0",
e4ffb1
+		"shortdesc" : "Only evacuate flagged instances",
e4ffb1
+		"default" : "False",
e4ffb1
+		"order": 5,
e4ffb1
+	}
e4ffb1
 	all_opt["no-shared-storage"] = {
e4ffb1
 		"getopt" : "",
e4ffb1
 		"longopt" : "no-shared-storage",
e4ffb1
@@ -187,17 +284,17 @@ def main():
e4ffb1
 	global nova
e4ffb1
 	atexit.register(atexit_handler)
e4ffb1
 
e4ffb1
-	device_opt = ["login", "passwd", "tenant-name", "auth-url",
e4ffb1
+	device_opt = ["login", "passwd", "tenant-name", "auth-url", "fabric_fencing", "on_target",
e4ffb1
 		"no_login", "no_password", "port", "domain", "no-shared-storage", "endpoint-type",
e4ffb1
-		"record-only"]
e4ffb1
+		"record-only", "instance-filtering"]
e4ffb1
 	define_new_opts()
e4ffb1
 	all_opt["shell_timeout"]["default"] = "180"
e4ffb1
 
e4ffb1
 	options = check_input(device_opt, process_input(device_opt))
e4ffb1
 
e4ffb1
 	docs = {}
e4ffb1
-	docs["shortdesc"] = "Fence agent for nova compute nodes"
e4ffb1
-	docs["longdesc"] = "fence_nova_host is a Nova fencing notification agent"
e4ffb1
+	docs["shortdesc"] = "Fence agent for the automatic resurrection of OpenStack compute instances"
e4ffb1
+	docs["longdesc"] = "Used to tell Nova that compute nodes are down and to reschedule flagged instances"
e4ffb1
 	docs["vendorurl"] = ""
e4ffb1
 
e4ffb1
 	show_docs(options, docs)
e4ffb1
@@ -213,7 +310,10 @@ def main():
e4ffb1
 	if options["--action"] != "list" and options["--domain"] != "" and options.has_key("--plug"):
e4ffb1
 		options["--plug"] = options["--plug"] + "." + options["--domain"]
e4ffb1
 
e4ffb1
-	if options["--record-only"] != "False":
e4ffb1
+	if options["--record-only"] in [ "2", "Disabled", "disabled" ]:
e4ffb1
+		sys.exit(0)
e4ffb1
+
e4ffb1
+	elif options["--record-only"] in [ "1", "True", "true", "Yes", "yes"]:
e4ffb1
 		if options["--action"] == "on":
e4ffb1
 			set_attrd_status(options["--plug"], "no", options)
e4ffb1
 			sys.exit(0)
e4ffb1
@@ -222,7 +322,7 @@ def main():
e4ffb1
 			set_attrd_status(options["--plug"], "yes", options)
e4ffb1
 			sys.exit(0)
e4ffb1
 
e4ffb1
-		elif options["--action"] in ["status", "monitor"]:
e4ffb1
+		elif options["--action"] in ["monitor", "status"]:
e4ffb1
 			sys.exit(0)
e4ffb1
 
e4ffb1
 	# The first argument is the Nova client version
e4ffb1
diff --git a/tests/data/metadata/fence_compute.xml b/tests/data/metadata/fence_compute.xml
e4ffb1
index 846a861..98bed4e 100644
e4ffb1
--- a/tests/data/metadata/fence_compute.xml
e4ffb1
+++ b/tests/data/metadata/fence_compute.xml
e4ffb1
@@ -1,6 +1,6 @@
e4ffb1
 
e4ffb1
-<resource-agent name="fence_compute" shortdesc="Fence agent for nova compute nodes" >
e4ffb1
-<longdesc>fence_nova_host is a Nova fencing notification agent</longdesc>
e4ffb1
+<resource-agent name="fence_compute" shortdesc="Fence agent for the automatic resurrection of OpenStack compute instances" >
e4ffb1
+<longdesc>Used to tell Nova that compute nodes are down and to reschedule flagged instances</longdesc>
e4ffb1
 <vendor-url></vendor-url>
e4ffb1
 <parameters>
e4ffb1
 	<parameter name="port" unique="0" required="1">
e4ffb1
@@ -35,7 +35,7 @@
e4ffb1
 	</parameter>
e4ffb1
 	<parameter name="action" unique="0" required="1">
e4ffb1
 		<getopt mixed="-o, --action=[action]" />
e4ffb1
-		<content type="string" default="reboot"  />
e4ffb1
+		<content type="string" default="off"  />
e4ffb1
 		<shortdesc lang="en">Fencing Action</shortdesc>
e4ffb1
 	</parameter>
e4ffb1
 	<parameter name="login" unique="0" required="0">
e4ffb1
@@ -48,6 +48,11 @@
e4ffb1
 		<content type="string"  />
e4ffb1
 		<shortdesc lang="en">DNS domain in which hosts live</shortdesc>
e4ffb1
 	</parameter>
e4ffb1
+	<parameter name="instance-filtering" unique="0" required="0">
e4ffb1
+		<getopt mixed="--instance-filtering" />
e4ffb1
+		<content type="boolean" default="False"  />
e4ffb1
+		<shortdesc lang="en">Only evacuate flagged instances</shortdesc>
e4ffb1
+	</parameter>
e4ffb1
 	<parameter name="no-shared-storage" unique="0" required="0">
e4ffb1
 		<getopt mixed="--no-shared-storage" />
e4ffb1
 		<content type="boolean" default="False"  />
e4ffb1
@@ -55,7 +60,7 @@
e4ffb1
 	</parameter>
e4ffb1
 	<parameter name="record-only" unique="0" required="0">
e4ffb1
 		<getopt mixed="--record-only" />
e4ffb1
-		<content type="boolean" default="False"  />
e4ffb1
+		<content type="string" default="False"  />
e4ffb1
 		<shortdesc lang="en">Only record the target as needing evacuation</shortdesc>
e4ffb1
 	</parameter>
e4ffb1
 	<parameter name="verbose" unique="0" required="0">
e4ffb1
@@ -115,9 +120,8 @@
e4ffb1
 	</parameter>
e4ffb1
 </parameters>
e4ffb1
 <actions>
e4ffb1
-	<action name="on" automatic="0"/>
e4ffb1
+	<action name="on" on_target="1" automatic="1"/>
e4ffb1
 	<action name="off" />
e4ffb1
-	<action name="reboot" />
e4ffb1
 	<action name="status" />
e4ffb1
 	<action name="list" />
e4ffb1
 	<action name="list-status" />
e4ffb1
-- 
e4ffb1
2.4.3
e4ffb1