Blame SOURCES/bz1781357-fence_aws-improve-logging-and-metadata-usage-text.patch

66332a
From 7ac16fb281fa8cfc51a31f672014c614c81aec82 Mon Sep 17 00:00:00 2001
66332a
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
66332a
Date: Fri, 31 Jan 2020 15:53:47 +0100
66332a
Subject: [PATCH] fence_aws: improve logging and metadata/usage text
66332a
66332a
---
66332a
 agents/aws/fence_aws.py           | 36 +++++++++++++++++++------------
66332a
 tests/data/metadata/fence_aws.xml |  6 +++---
66332a
 2 files changed, 25 insertions(+), 17 deletions(-)
66332a
66332a
diff --git a/agents/aws/fence_aws.py b/agents/aws/fence_aws.py
66332a
index 647b66fc..74321e8e 100644
66332a
--- a/agents/aws/fence_aws.py
66332a
+++ b/agents/aws/fence_aws.py
66332a
@@ -5,7 +5,7 @@
66332a
 import atexit
66332a
 sys.path.append("@FENCEAGENTSLIBDIR@")
66332a
 from fencing import *
66332a
-from fencing import fail, fail_usage, EC_TIMED_OUT, run_delay
66332a
+from fencing import fail, fail_usage, run_delay, EC_STATUS
66332a
 
66332a
 import boto3
66332a
 from botocore.exceptions import ClientError, EndpointConnectionError, NoRegionError
66332a
@@ -19,6 +19,8 @@ def get_nodes_list(conn, options):
66332a
 		fail_usage("Failed: Incorrect Access Key or Secret Key.")
66332a
 	except EndpointConnectionError:
66332a
 		fail_usage("Failed: Incorrect Region.")
66332a
+	except Exception as e:
66332a
+		logging.error("Failed to get node list: %s", e)
66332a
 
66332a
 	return result
66332a
 
66332a
@@ -38,20 +40,26 @@ def get_power_status(conn, options):
66332a
 	except EndpointConnectionError:
66332a
 		fail_usage("Failed: Incorrect Region.")
66332a
 	except IndexError:
66332a
-		return "fail"
66332a
+		fail(EC_STATUS)
66332a
+	except Exception as e:
66332a
+		logging.error("Failed to get power status: %s", e)
66332a
+		fail(EC_STATUS)
66332a
 
66332a
 def set_power_status(conn, options):
66332a
-	if (options["--action"]=="off"):
66332a
-		conn.instances.filter(InstanceIds=[options["--plug"]]).stop(Force=True)
66332a
-	elif (options["--action"]=="on"):
66332a
-		conn.instances.filter(InstanceIds=[options["--plug"]]).start()
66332a
-
66332a
+	try:
66332a
+		if (options["--action"]=="off"):
66332a
+			conn.instances.filter(InstanceIds=[options["--plug"]]).stop(Force=True)
66332a
+		elif (options["--action"]=="on"):
66332a
+			conn.instances.filter(InstanceIds=[options["--plug"]]).start()
66332a
+	except Exception as e:
66332a
+		logging.error("Failed to power %s %s: %s", \
66332a
+				options["--action"], options["--plug"], e)
66332a
 
66332a
 def define_new_opts():
66332a
 	all_opt["region"] = {
66332a
 		"getopt" : "r:",
66332a
 		"longopt" : "region",
66332a
-		"help" : "-r, --region=[name]            Region, e.g. us-east-1",
66332a
+		"help" : "-r, --region=[region]           Region, e.g. us-east-1",
66332a
 		"shortdesc" : "Region.",
66332a
 		"required" : "0",
66332a
 		"order" : 2
66332a
@@ -59,7 +67,7 @@ def define_new_opts():
66332a
 	all_opt["access_key"] = {
66332a
 		"getopt" : "a:",
66332a
 		"longopt" : "access-key",
66332a
-		"help" : "-a, --access-key=[name]         Access Key",
66332a
+		"help" : "-a, --access-key=[key]         Access Key",
66332a
 		"shortdesc" : "Access Key.",
66332a
 		"required" : "0",
66332a
 		"order" : 3
66332a
@@ -67,7 +75,7 @@ def define_new_opts():
66332a
 	all_opt["secret_key"] = {
66332a
 		"getopt" : "s:",
66332a
 		"longopt" : "secret-key",
66332a
-		"help" : "-s, --secret-key=[name]         Secret Key",
66332a
+		"help" : "-s, --secret-key=[key]         Secret Key",
66332a
 		"shortdesc" : "Secret Key.",
66332a
 		"required" : "0",
66332a
 		"order" : 4
66332a
@@ -107,16 +115,16 @@ def main():
66332a
 			conn = boto3.resource('ec2', region_name=region,
66332a
 					      aws_access_key_id=access_key,
66332a
 					      aws_secret_access_key=secret_key)
66332a
-		except:
66332a
-			fail_usage("Failed: Unable to connect to AWS. Check your configuration.")
66332a
+		except Exception as e:
66332a
+			fail_usage("Failed: Unable to connect to AWS: " + str(e))
66332a
 	else:
66332a
 		# If setup with "aws configure" or manually in
66332a
 		# ~/.aws/credentials
66332a
 		try:
66332a
 			conn = boto3.resource('ec2')
66332a
-		except:
66332a
+		except Exception as e:
66332a
 			# If any of region/access/secret are missing
66332a
-			fail_usage("Failed: Unable to connect to AWS. Check your configuration.")
66332a
+			fail_usage("Failed: Unable to connect to AWS: " + str(e))
66332a
 
66332a
 	# Operate the fencing device
66332a
 	result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list)
66332a
diff --git a/tests/data/metadata/fence_aws.xml b/tests/data/metadata/fence_aws.xml
66332a
index 4dea4418..5e5d5d99 100644
66332a
--- a/tests/data/metadata/fence_aws.xml
66332a
+++ b/tests/data/metadata/fence_aws.xml
66332a
@@ -22,17 +22,17 @@ For instructions see: https://boto3.readthedocs.io/en/latest/guide/quickstart.ht
66332a
 		<shortdesc lang="en">Physical plug number on device, UUID or identification of machine</shortdesc>
66332a
 	</parameter>
66332a
 	<parameter name="region" unique="0" required="0">
66332a
-		<getopt mixed="-r, --region=[name]" />
66332a
+		<getopt mixed="-r, --region=[region]" />
66332a
 		<content type="string"  />
66332a
 		<shortdesc lang="en">Region.</shortdesc>
66332a
 	</parameter>
66332a
 	<parameter name="access_key" unique="0" required="0">
66332a
-		<getopt mixed="-a, --access-key=[name]" />
66332a
+		<getopt mixed="-a, --access-key=[key]" />
66332a
 		<content type="string"  />
66332a
 		<shortdesc lang="en">Access Key.</shortdesc>
66332a
 	</parameter>
66332a
 	<parameter name="secret_key" unique="0" required="0">
66332a
-		<getopt mixed="-s, --secret-key=[name]" />
66332a
+		<getopt mixed="-s, --secret-key=[key]" />
66332a
 		<content type="string"  />
66332a
 		<shortdesc lang="en">Secret Key.</shortdesc>
66332a
 	</parameter>