Blame SOURCES/fence_gce-1-stackdriver-logging-default-method-cycle.patch

658b6c
From 59ae9d00060da5329d7ca538974498292bbe1d91 Mon Sep 17 00:00:00 2001
658b6c
From: Helen Koike <helen.koike@collabora.com>
658b6c
Date: Tue, 26 Jun 2018 10:18:29 -0300
658b6c
Subject: [PATCH 1/7] fence_gce: add support for stackdriver logging
658b6c
658b6c
Add --logging option to enable sending logs to google stackdriver
658b6c
---
658b6c
 agents/gce/fence_gce.py           | 65 +++++++++++++++++++++++++++++++++++++--
658b6c
 tests/data/metadata/fence_gce.xml |  5 +++
658b6c
 2 files changed, 67 insertions(+), 3 deletions(-)
658b6c
658b6c
diff --git a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
658b6c
index 3abb5207..3af5bfc8 100644
658b6c
--- a/agents/gce/fence_gce.py
658b6c
+++ b/agents/gce/fence_gce.py
658b6c
@@ -1,12 +1,19 @@
658b6c
 #!@PYTHON@ -tt
658b6c
 
658b6c
 import atexit
658b6c
+import logging
658b6c
+import platform
658b6c
 import sys
658b6c
+import time
658b6c
 sys.path.append("@FENCEAGENTSLIBDIR@")
658b6c
 
658b6c
 import googleapiclient.discovery
658b6c
 from fencing import fail_usage, run_delay, all_opt, atexit_handler, check_input, process_input, show_docs, fence_action
658b6c
 
658b6c
+
658b6c
+LOGGER = logging
658b6c
+
658b6c
+
658b6c
 def translate_status(instance_status):
658b6c
 	"Returns on | off | unknown."
658b6c
 	if instance_status == "RUNNING":
658b6c
@@ -27,6 +34,7 @@ def get_nodes_list(conn, options):
658b6c
 
658b6c
 	return result
658b6c
 
658b6c
+
658b6c
 def get_power_status(conn, options):
658b6c
 	try:
658b6c
 		instance = conn.instances().get(
658b6c
@@ -38,18 +46,37 @@ def get_power_status(conn, options):
658b6c
 		fail_usage("Failed: get_power_status: {}".format(str(err)))
658b6c
 
658b6c
 
658b6c
+def wait_for_operation(conn, project, zone, operation):
658b6c
+	while True:
658b6c
+		result = conn.zoneOperations().get(
658b6c
+			project=project,
658b6c
+			zone=zone,
658b6c
+			operation=operation['name']).execute()
658b6c
+		if result['status'] == 'DONE':
658b6c
+			if 'error' in result:
658b6c
+				raise Exception(result['error'])
658b6c
+			return
658b6c
+		time.sleep(1)
658b6c
+
658b6c
+
658b6c
 def set_power_status(conn, options):
658b6c
 	try:
658b6c
 		if options["--action"] == "off":
658b6c
-			conn.instances().stop(
658b6c
+			LOGGER.info("Issuing poweroff of %s in zone %s" % (options["--plug"], options["--zone"]))
658b6c
+			operation = conn.instances().stop(
658b6c
 					project=options["--project"],
658b6c
 					zone=options["--zone"],
658b6c
 					instance=options["--plug"]).execute()
658b6c
+			wait_for_operation(conn, options["--project"], options["--zone"], operation)
658b6c
+			LOGGER.info("Poweroff of %s in zone %s complete" % (options["--plug"], options["--zone"]))
658b6c
 		elif options["--action"] == "on":
658b6c
-			conn.instances().start(
658b6c
+			LOGGER.info("Issuing poweron of %s in zone %s" % (options["--plug"], options["--zone"]))
658b6c
+			operation = conn.instances().start(
658b6c
 					project=options["--project"],
658b6c
 					zone=options["--zone"],
658b6c
 					instance=options["--plug"]).execute()
658b6c
+			wait_for_operation(conn, options["--project"], options["--zone"], operation)
658b6c
+			LOGGER.info("Poweron of %s in zone %s complete" % (options["--plug"], options["--zone"]))
658b6c
 	except Exception as err:
658b6c
 		fail_usage("Failed: set_power_status: {}".format(str(err)))
658b6c
 
658b6c
@@ -71,11 +98,24 @@ def define_new_opts():
658b6c
 		"required" : "1",
658b6c
 		"order" : 3
658b6c
 	}
658b6c
+	all_opt["logging"] = {
658b6c
+		"getopt" : ":",
658b6c
+		"longopt" : "logging",
658b6c
+		"help" : "--logging=[bool]               Logging, true/false",
658b6c
+		"shortdesc" : "Stackdriver-logging support.",
658b6c
+		"longdesc" : "If enabled (set to true), IP failover logs will be posted to stackdriver logging.",
658b6c
+		"required" : "0",
658b6c
+		"default" : "false",
658b6c
+		"order" : 4
658b6c
+	}
658b6c
 
658b6c
 def main():
658b6c
 	conn = None
658b6c
+	global LOGGER
658b6c
+
658b6c
+	hostname = platform.node()
658b6c
 
658b6c
-	device_opt = ["port", "no_password", "zone", "project"]
658b6c
+	device_opt = ["port", "no_password", "zone", "project", "logging"]
658b6c
 
658b6c
 	atexit.register(atexit_handler)
658b6c
 
658b6c
@@ -97,6 +137,25 @@ def main():
658b6c
 
658b6c
 	run_delay(options)
658b6c
 
658b6c
+	# Prepare logging
658b6c
+	logging_env = options.get('--logging')
658b6c
+	if logging_env:
658b6c
+		logging_env = logging_env.lower()
658b6c
+		if any(x in logging_env for x in ['yes', 'true', 'enabled']):
658b6c
+			try:
658b6c
+				import google.cloud.logging.handlers
658b6c
+				client = google.cloud.logging.Client()
658b6c
+				handler = google.cloud.logging.handlers.CloudLoggingHandler(client, name=hostname)
658b6c
+				formatter = logging.Formatter('gcp:stonish "%(message)s"')
658b6c
+				LOGGER = logging.getLogger(hostname)
658b6c
+				handler.setFormatter(formatter)
658b6c
+				LOGGER.addHandler(handler)
658b6c
+				LOGGER.setLevel(logging.INFO)
658b6c
+			except ImportError:
658b6c
+				LOGGER.error('Couldn\'t import google.cloud.logging, '
658b6c
+					'disabling Stackdriver-logging support')
658b6c
+
658b6c
+	# Prepare cli
658b6c
 	try:
658b6c
 		credentials = None
658b6c
 		if tuple(googleapiclient.__version__) < tuple("1.6.0"):
658b6c
diff --git a/tests/data/metadata/fence_gce.xml b/tests/data/metadata/fence_gce.xml
658b6c
index 2a147f21..805ecc6b 100644
658b6c
--- a/tests/data/metadata/fence_gce.xml
658b6c
+++ b/tests/data/metadata/fence_gce.xml
658b6c
@@ -30,6 +30,11 @@ For instructions see: https://cloud.google.com/compute/docs/tutorials/python-gui
658b6c
 		<content type="string"  />
658b6c
 		<shortdesc lang="en">Project ID.</shortdesc>
658b6c
 	</parameter>
658b6c
+	<parameter name="logging" unique="0" required="0">
658b6c
+		<getopt mixed="--logging=[bool]" />
658b6c
+		<content type="string" default="false"  />
658b6c
+		<shortdesc lang="en">Stackdriver-logging support.</shortdesc>
658b6c
+	</parameter>
658b6c
 	<parameter name="quiet" unique="0" required="0">
658b6c
 		<getopt mixed="-q, --quiet" />
658b6c
 		<content type="boolean"  />
658b6c
658b6c
From bb34acd8b0b150599c393d56dd81a7d8185b27d3 Mon Sep 17 00:00:00 2001
658b6c
From: Helen Koike <helen.koike@collabora.com>
658b6c
Date: Tue, 26 Jun 2018 10:44:41 -0300
658b6c
Subject: [PATCH 2/7] fence_gce: set project and zone as not required
658b6c
658b6c
Try to retrieve the GCE project if the script is being executed inside a
658b6c
GCE machine if --project is not provided.
658b6c
Try to retrieve the zone automatically from GCE if --zone is not
658b6c
provided.
658b6c
---
658b6c
 agents/gce/fence_gce.py           | 63 +++++++++++++++++++++++++++++++++++++--
658b6c
 tests/data/metadata/fence_gce.xml |  4 +--
658b6c
 2 files changed, 63 insertions(+), 4 deletions(-)
658b6c
658b6c
diff --git a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
658b6c
index 3af5bfc8..e53dc5a6 100644
658b6c
--- a/agents/gce/fence_gce.py
658b6c
+++ b/agents/gce/fence_gce.py
658b6c
@@ -12,6 +12,8 @@
658b6c
 
658b6c
 
658b6c
 LOGGER = logging
658b6c
+METADATA_SERVER = 'http://metadata.google.internal/computeMetadata/v1/'
658b6c
+METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
658b6c
 
658b6c
 
658b6c
 def translate_status(instance_status):
658b6c
@@ -81,13 +83,56 @@ def set_power_status(conn, options):
658b6c
 		fail_usage("Failed: set_power_status: {}".format(str(err)))
658b6c
 
658b6c
 
658b6c
+def get_instance(conn, project, zone, instance):
658b6c
+	request = conn.instances().get(
658b6c
+			project=project, zone=zone, instance=instance)
658b6c
+	return request.execute()
658b6c
+
658b6c
+
658b6c
+def get_zone(conn, project, instance):
658b6c
+	request = conn.instances().aggregatedList(project=project)
658b6c
+	while request is not None:
658b6c
+		response = request.execute()
658b6c
+		zones = response.get('items', {})
658b6c
+		for zone in zones.values():
658b6c
+			for inst in zone.get('instances', []):
658b6c
+				if inst['name'] == instance:
658b6c
+					return inst['zone'].split("/")[-1]
658b6c
+		request = conn.instances().aggregatedList_next(
658b6c
+				previous_request=request, previous_response=response)
658b6c
+	raise Exception("Unable to find instance %s" % (instance))
658b6c
+
658b6c
+
658b6c
+def get_metadata(metadata_key, params=None, timeout=None):
658b6c
+	"""Performs a GET request with the metadata headers.
658b6c
+
658b6c
+	Args:
658b6c
+		metadata_key: string, the metadata to perform a GET request on.
658b6c
+		params: dictionary, the query parameters in the GET request.
658b6c
+		timeout: int, timeout in seconds for metadata requests.
658b6c
+
658b6c
+	Returns:
658b6c
+		HTTP response from the GET request.
658b6c
+
658b6c
+	Raises:
658b6c
+		urlerror.HTTPError: raises when the GET request fails.
658b6c
+	"""
658b6c
+	timeout = timeout or 60
658b6c
+	metadata_url = os.path.join(METADATA_SERVER, metadata_key)
658b6c
+	params = urlparse.urlencode(params or {})
658b6c
+	url = '%s?%s' % (metadata_url, params)
658b6c
+	request = urlrequest.Request(url, headers=METADATA_HEADERS)
658b6c
+	request_opener = urlrequest.build_opener(urlrequest.ProxyHandler({}))
658b6c
+	return request_opener.open(request, timeout=timeout * 1.1).read()
658b6c
+
658b6c
+
658b6c
 def define_new_opts():
658b6c
 	all_opt["zone"] = {
658b6c
 		"getopt" : ":",
658b6c
 		"longopt" : "zone",
658b6c
 		"help" : "--zone=[name]                  Zone, e.g. us-central1-b",
658b6c
 		"shortdesc" : "Zone.",
658b6c
-		"required" : "1",
658b6c
+		"required" : "0",
658b6c
 		"order" : 2
658b6c
 	}
658b6c
 	all_opt["project"] = {
658b6c
@@ -95,7 +140,7 @@ def define_new_opts():
658b6c
 		"longopt" : "project",
658b6c
 		"help" : "--project=[name]               Project ID",
658b6c
 		"shortdesc" : "Project ID.",
658b6c
-		"required" : "1",
658b6c
+		"required" : "0",
658b6c
 		"order" : 3
658b6c
 	}
658b6c
 	all_opt["logging"] = {
658b6c
@@ -109,6 +154,7 @@ def define_new_opts():
658b6c
 		"order" : 4
658b6c
 	}
658b6c
 
658b6c
+
658b6c
 def main():
658b6c
 	conn = None
658b6c
 	global LOGGER
658b6c
@@ -165,6 +211,19 @@ def main():
658b6c
 	except Exception as err:
658b6c
 		fail_usage("Failed: Create GCE compute v1 connection: {}".format(str(err)))
658b6c
 
658b6c
+	# Get project and zone
658b6c
+	if not options.get("--project"):
658b6c
+		try:
658b6c
+			options["--project"] = get_metadata('project/project-id')
658b6c
+		except Exception as err:
658b6c
+			fail_usage("Failed retrieving GCE project. Please provide --project option: {}".format(str(err)))
658b6c
+
658b6c
+	if not options.get("--zone"):
658b6c
+		try:
658b6c
+			options["--zone"] = get_zone(conn, options['--project'], options['--plug'])
658b6c
+		except Exception as err:
658b6c
+			fail_usage("Failed retrieving GCE zone. Please provide --zone option: {}".format(str(err)))
658b6c
+
658b6c
 	# Operate the fencing device
658b6c
 	result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list)
658b6c
 	sys.exit(result)
658b6c
diff --git a/tests/data/metadata/fence_gce.xml b/tests/data/metadata/fence_gce.xml
658b6c
index 805ecc6b..507b8385 100644
658b6c
--- a/tests/data/metadata/fence_gce.xml
658b6c
+++ b/tests/data/metadata/fence_gce.xml
658b6c
@@ -20,12 +20,12 @@ For instructions see: https://cloud.google.com/compute/docs/tutorials/python-gui
658b6c
 		<content type="string"  />
658b6c
 		<shortdesc lang="en">Physical plug number on device, UUID or identification of machine</shortdesc>
658b6c
 	</parameter>
658b6c
-	<parameter name="zone" unique="0" required="1">
658b6c
+	<parameter name="zone" unique="0" required="0">
658b6c
 		<getopt mixed="--zone=[name]" />
658b6c
 		<content type="string"  />
658b6c
 		<shortdesc lang="en">Zone.</shortdesc>
658b6c
 	</parameter>
658b6c
-	<parameter name="project" unique="0" required="1">
658b6c
+	<parameter name="project" unique="0" required="0">
658b6c
 		<getopt mixed="--project=[name]" />
658b6c
 		<content type="string"  />
658b6c
 		<shortdesc lang="en">Project ID.</shortdesc>
658b6c
658b6c
From 8ae1af8068d1718a861a25bf954e14392384fa55 Mon Sep 17 00:00:00 2001
658b6c
From: Helen Koike <helen.koike@collabora.com>
658b6c
Date: Wed, 4 Jul 2018 09:25:46 -0300
658b6c
Subject: [PATCH 3/7] fence_gce: add power cycle as default method
658b6c
658b6c
Add function to power cycle an instance and set cycle as the default
658b6c
method to reboot.
658b6c
---
658b6c
 agents/gce/fence_gce.py           | 21 +++++++++++++++++++--
658b6c
 tests/data/metadata/fence_gce.xml |  8 ++++++++
658b6c
 2 files changed, 27 insertions(+), 2 deletions(-)
658b6c
658b6c
diff --git a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
658b6c
index e53dc5a6..3f77dc24 100644
658b6c
--- a/agents/gce/fence_gce.py
658b6c
+++ b/agents/gce/fence_gce.py
658b6c
@@ -83,6 +83,21 @@ def set_power_status(conn, options):
658b6c
 		fail_usage("Failed: set_power_status: {}".format(str(err)))
658b6c
 
658b6c
 
658b6c
+def power_cycle(conn, options):
658b6c
+	try:
658b6c
+		LOGGER.info('Issuing reset of %s in zone %s' % (options["--plug"], options["--zone"]))
658b6c
+		operation = conn.instances().reset(
658b6c
+				project=options["--project"],
658b6c
+				zone=options["--zone"],
658b6c
+				instance=options["--plug"]).execute()
658b6c
+		wait_for_operation(conn, options["--project"], options["--zone"], operation)
658b6c
+		LOGGER.info('Reset of %s in zone %s complete' % (options["--plug"], options["--zone"]))
658b6c
+		return True
658b6c
+	except Exception as err:
658b6c
+		LOGGER.error("Failed: power_cycle: {}".format(str(err)))
658b6c
+		return False
658b6c
+
658b6c
+
658b6c
 def get_instance(conn, project, zone, instance):
658b6c
 	request = conn.instances().get(
658b6c
 			project=project, zone=zone, instance=instance)
658b6c
@@ -161,13 +176,15 @@ def main():
658b6c
 
658b6c
 	hostname = platform.node()
658b6c
 
658b6c
-	device_opt = ["port", "no_password", "zone", "project", "logging"]
658b6c
+	device_opt = ["port", "no_password", "zone", "project", "logging", "method"]
658b6c
 
658b6c
 	atexit.register(atexit_handler)
658b6c
 
658b6c
 	define_new_opts()
658b6c
 
658b6c
 	all_opt["power_timeout"]["default"] = "60"
658b6c
+	all_opt["method"]["default"] = "cycle"
658b6c
+	all_opt["method"]["help"] = "-m, --method=[method]          Method to fence (onoff|cycle) (Default: cycle)"
658b6c
 
658b6c
 	options = check_input(device_opt, process_input(device_opt))
658b6c
 
658b6c
@@ -225,7 +242,7 @@ def main():
658b6c
 			fail_usage("Failed retrieving GCE zone. Please provide --zone option: {}".format(str(err)))
658b6c
 
658b6c
 	# Operate the fencing device
658b6c
-	result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list)
658b6c
+	result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list, power_cycle)
658b6c
 	sys.exit(result)
658b6c
 
658b6c
 if __name__ == "__main__":
658b6c
diff --git a/tests/data/metadata/fence_gce.xml b/tests/data/metadata/fence_gce.xml
658b6c
index 507b8385..f522550f 100644
658b6c
--- a/tests/data/metadata/fence_gce.xml
658b6c
+++ b/tests/data/metadata/fence_gce.xml
658b6c
@@ -10,6 +10,14 @@ For instructions see: https://cloud.google.com/compute/docs/tutorials/python-gui
658b6c
 		<content type="string" default="reboot"  />
658b6c
 		<shortdesc lang="en">Fencing action</shortdesc>
658b6c
 	</parameter>
658b6c
+	<parameter name="method" unique="0" required="0">
658b6c
+		<getopt mixed="-m, --method=[method]" />
658b6c
+		<content type="select" default="cycle"  >
658b6c
+			<option value="onoff" />
658b6c
+			<option value="cycle" />
658b6c
+		</content>
658b6c
+		<shortdesc lang="en">Method to fence</shortdesc>
658b6c
+	</parameter>
658b6c
 	<parameter name="plug" unique="0" required="1" obsoletes="port">
658b6c
 		<getopt mixed="-n, --plug=[id]" />
658b6c
 		<content type="string"  />
658b6c
658b6c
From 68644764695b79a3b75826fe009ea7da675677f7 Mon Sep 17 00:00:00 2001
658b6c
From: Helen Koike <helen.koike@collabora.com>
658b6c
Date: Thu, 5 Jul 2018 11:04:32 -0300
658b6c
Subject: [PATCH 4/7] fence_gce: add missing imports to retrieve the project
658b6c
658b6c
---
658b6c
 agents/gce/fence_gce.py | 9 +++++++++
658b6c
 1 file changed, 9 insertions(+)
658b6c
658b6c
diff --git a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
658b6c
index 3f77dc24..9b7b5e55 100644
658b6c
--- a/agents/gce/fence_gce.py
658b6c
+++ b/agents/gce/fence_gce.py
658b6c
@@ -2,9 +2,18 @@
658b6c
 
658b6c
 import atexit
658b6c
 import logging
658b6c
+import os
658b6c
 import platform
658b6c
 import sys
658b6c
 import time
658b6c
+if sys.version_info >= (3, 0):
658b6c
+  # Python 3 imports.
658b6c
+  import urllib.parse as urlparse
658b6c
+  import urllib.request as urlrequest
658b6c
+else:
658b6c
+  # Python 2 imports.
658b6c
+  import urllib as urlparse
658b6c
+  import urllib2 as urlrequest
658b6c
 sys.path.append("@FENCEAGENTSLIBDIR@")
658b6c
 
658b6c
 import googleapiclient.discovery
658b6c
658b6c
From f8f3f11187341622c26e4e439dfda6a37ad660b0 Mon Sep 17 00:00:00 2001
658b6c
From: Helen Koike <helen.koike@collabora.com>
658b6c
Date: Thu, 5 Jul 2018 11:05:32 -0300
658b6c
Subject: [PATCH 5/7] fence_gce: s/--loging/--stackdriver-logging/
658b6c
658b6c
---
658b6c
 agents/gce/fence_gce.py           | 42 ++++++++++++++++++---------------------
658b6c
 tests/data/metadata/fence_gce.xml | 11 +++++++---
658b6c
 2 files changed, 27 insertions(+), 26 deletions(-)
658b6c
658b6c
diff --git a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
658b6c
index 9b7b5e55..a6befe39 100644
658b6c
--- a/agents/gce/fence_gce.py
658b6c
+++ b/agents/gce/fence_gce.py
658b6c
@@ -167,14 +167,13 @@ def define_new_opts():
658b6c
 		"required" : "0",
658b6c
 		"order" : 3
658b6c
 	}
658b6c
-	all_opt["logging"] = {
658b6c
-		"getopt" : ":",
658b6c
-		"longopt" : "logging",
658b6c
-		"help" : "--logging=[bool]               Logging, true/false",
658b6c
+	all_opt["stackdriver-logging"] = {
658b6c
+		"getopt" : "",
658b6c
+		"longopt" : "stackdriver-logging",
658b6c
+		"help" : "--stackdriver-logging		Enable Logging to Stackdriver",
658b6c
 		"shortdesc" : "Stackdriver-logging support.",
658b6c
-		"longdesc" : "If enabled (set to true), IP failover logs will be posted to stackdriver logging.",
658b6c
+		"longdesc" : "If enabled IP failover logs will be posted to stackdriver logging.",
658b6c
 		"required" : "0",
658b6c
-		"default" : "false",
658b6c
 		"order" : 4
658b6c
 	}
658b6c
 
658b6c
@@ -185,7 +184,7 @@ def main():
658b6c
 
658b6c
 	hostname = platform.node()
658b6c
 
658b6c
-	device_opt = ["port", "no_password", "zone", "project", "logging", "method"]
658b6c
+	device_opt = ["port", "no_password", "zone", "project", "stackdriver-logging", "method"]
658b6c
 
658b6c
 	atexit.register(atexit_handler)
658b6c
 
658b6c
@@ -210,22 +209,19 @@ def main():
658b6c
 	run_delay(options)
658b6c
 
658b6c
 	# Prepare logging
658b6c
-	logging_env = options.get('--logging')
658b6c
-	if logging_env:
658b6c
-		logging_env = logging_env.lower()
658b6c
-		if any(x in logging_env for x in ['yes', 'true', 'enabled']):
658b6c
-			try:
658b6c
-				import google.cloud.logging.handlers
658b6c
-				client = google.cloud.logging.Client()
658b6c
-				handler = google.cloud.logging.handlers.CloudLoggingHandler(client, name=hostname)
658b6c
-				formatter = logging.Formatter('gcp:stonish "%(message)s"')
658b6c
-				LOGGER = logging.getLogger(hostname)
658b6c
-				handler.setFormatter(formatter)
658b6c
-				LOGGER.addHandler(handler)
658b6c
-				LOGGER.setLevel(logging.INFO)
658b6c
-			except ImportError:
658b6c
-				LOGGER.error('Couldn\'t import google.cloud.logging, '
658b6c
-					'disabling Stackdriver-logging support')
658b6c
+	if options.get('--stackdriver-logging'):
658b6c
+		try:
658b6c
+			import google.cloud.logging.handlers
658b6c
+			client = google.cloud.logging.Client()
658b6c
+			handler = google.cloud.logging.handlers.CloudLoggingHandler(client, name=hostname)
658b6c
+			formatter = logging.Formatter('gcp:stonish "%(message)s"')
658b6c
+			LOGGER = logging.getLogger(hostname)
658b6c
+			handler.setFormatter(formatter)
658b6c
+			LOGGER.addHandler(handler)
658b6c
+			LOGGER.setLevel(logging.INFO)
658b6c
+		except ImportError:
658b6c
+			LOGGER.error('Couldn\'t import google.cloud.logging, '
658b6c
+				'disabling Stackdriver-logging support')
658b6c
 
658b6c
 	# Prepare cli
658b6c
 	try:
658b6c
diff --git a/tests/data/metadata/fence_gce.xml b/tests/data/metadata/fence_gce.xml
658b6c
index f522550f..79b82ebb 100644
658b6c
--- a/tests/data/metadata/fence_gce.xml
658b6c
+++ b/tests/data/metadata/fence_gce.xml
658b6c
@@ -38,9 +38,14 @@ For instructions see: https://cloud.google.com/compute/docs/tutorials/python-gui
658b6c
 		<content type="string"  />
658b6c
 		<shortdesc lang="en">Project ID.</shortdesc>
658b6c
 	</parameter>
658b6c
-	<parameter name="logging" unique="0" required="0">
658b6c
-		<getopt mixed="--logging=[bool]" />
658b6c
-		<content type="string" default="false"  />
658b6c
+	<parameter name="stackdriver-logging" unique="0" required="0" deprecated="1">
658b6c
+		<getopt mixed="--stackdriver-logging" />
658b6c
+		<content type="boolean"  />
658b6c
+		<shortdesc lang="en">Stackdriver-logging support.</shortdesc>
658b6c
+	</parameter>
658b6c
+	<parameter name="stackdriver_logging" unique="0" required="0" obsoletes="stackdriver-logging">
658b6c
+		<getopt mixed="--stackdriver-logging" />
658b6c
+		<content type="boolean"  />
658b6c
 		<shortdesc lang="en">Stackdriver-logging support.</shortdesc>
658b6c
 	</parameter>
658b6c
 	<parameter name="quiet" unique="0" required="0">
658b6c
658b6c
From 9ae0a072424fa982e1d18a2cb661628c38601c3a Mon Sep 17 00:00:00 2001
658b6c
From: Helen Koike <helen.koike@collabora.com>
658b6c
Date: Sat, 7 Jul 2018 18:42:01 -0300
658b6c
Subject: [PATCH 6/7] fence_gce: use root logger for stackdriver
658b6c
658b6c
---
658b6c
 agents/gce/fence_gce.py | 29 +++++++++++++++--------------
658b6c
 1 file changed, 15 insertions(+), 14 deletions(-)
658b6c
658b6c
diff --git a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
658b6c
index a6befe39..1d5095ae 100644
658b6c
--- a/agents/gce/fence_gce.py
658b6c
+++ b/agents/gce/fence_gce.py
658b6c
@@ -20,7 +20,6 @@
658b6c
 from fencing import fail_usage, run_delay, all_opt, atexit_handler, check_input, process_input, show_docs, fence_action
658b6c
 
658b6c
 
658b6c
-LOGGER = logging
658b6c
 METADATA_SERVER = 'http://metadata.google.internal/computeMetadata/v1/'
658b6c
 METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
658b6c
 
658b6c
@@ -73,37 +72,37 @@ def wait_for_operation(conn, project, zone, operation):
658b6c
 def set_power_status(conn, options):
658b6c
 	try:
658b6c
 		if options["--action"] == "off":
658b6c
-			LOGGER.info("Issuing poweroff of %s in zone %s" % (options["--plug"], options["--zone"]))
658b6c
+			logging.info("Issuing poweroff of %s in zone %s" % (options["--plug"], options["--zone"]))
658b6c
 			operation = conn.instances().stop(
658b6c
 					project=options["--project"],
658b6c
 					zone=options["--zone"],
658b6c
 					instance=options["--plug"]).execute()
658b6c
 			wait_for_operation(conn, options["--project"], options["--zone"], operation)
658b6c
-			LOGGER.info("Poweroff of %s in zone %s complete" % (options["--plug"], options["--zone"]))
658b6c
+			logging.info("Poweroff of %s in zone %s complete" % (options["--plug"], options["--zone"]))
658b6c
 		elif options["--action"] == "on":
658b6c
-			LOGGER.info("Issuing poweron of %s in zone %s" % (options["--plug"], options["--zone"]))
658b6c
+			logging.info("Issuing poweron of %s in zone %s" % (options["--plug"], options["--zone"]))
658b6c
 			operation = conn.instances().start(
658b6c
 					project=options["--project"],
658b6c
 					zone=options["--zone"],
658b6c
 					instance=options["--plug"]).execute()
658b6c
 			wait_for_operation(conn, options["--project"], options["--zone"], operation)
658b6c
-			LOGGER.info("Poweron of %s in zone %s complete" % (options["--plug"], options["--zone"]))
658b6c
+			logging.info("Poweron of %s in zone %s complete" % (options["--plug"], options["--zone"]))
658b6c
 	except Exception as err:
658b6c
 		fail_usage("Failed: set_power_status: {}".format(str(err)))
658b6c
 
658b6c
 
658b6c
 def power_cycle(conn, options):
658b6c
 	try:
658b6c
-		LOGGER.info('Issuing reset of %s in zone %s' % (options["--plug"], options["--zone"]))
658b6c
+		logging.info('Issuing reset of %s in zone %s' % (options["--plug"], options["--zone"]))
658b6c
 		operation = conn.instances().reset(
658b6c
 				project=options["--project"],
658b6c
 				zone=options["--zone"],
658b6c
 				instance=options["--plug"]).execute()
658b6c
 		wait_for_operation(conn, options["--project"], options["--zone"], operation)
658b6c
-		LOGGER.info('Reset of %s in zone %s complete' % (options["--plug"], options["--zone"]))
658b6c
+		logging.info('Reset of %s in zone %s complete' % (options["--plug"], options["--zone"]))
658b6c
 		return True
658b6c
 	except Exception as err:
658b6c
-		LOGGER.error("Failed: power_cycle: {}".format(str(err)))
658b6c
+		logging.error("Failed: power_cycle: {}".format(str(err)))
658b6c
 		return False
658b6c
 
658b6c
 
658b6c
@@ -180,7 +179,6 @@ def define_new_opts():
658b6c
 
658b6c
 def main():
658b6c
 	conn = None
658b6c
-	global LOGGER
658b6c
 
658b6c
 	hostname = platform.node()
658b6c
 
658b6c
@@ -209,18 +207,21 @@ def main():
658b6c
 	run_delay(options)
658b6c
 
658b6c
 	# Prepare logging
658b6c
-	if options.get('--stackdriver-logging'):
658b6c
+	if options.get('--stackdriver-logging') is not None:
658b6c
 		try:
658b6c
 			import google.cloud.logging.handlers
658b6c
 			client = google.cloud.logging.Client()
658b6c
 			handler = google.cloud.logging.handlers.CloudLoggingHandler(client, name=hostname)
658b6c
+			handler.setLevel(logging.INFO)
658b6c
 			formatter = logging.Formatter('gcp:stonish "%(message)s"')
658b6c
-			LOGGER = logging.getLogger(hostname)
658b6c
 			handler.setFormatter(formatter)
658b6c
-			LOGGER.addHandler(handler)
658b6c
-			LOGGER.setLevel(logging.INFO)
658b6c
+			root_logger = logging.getLogger()
658b6c
+			if options.get('--verbose') is None:
658b6c
+				root_logger.setLevel(logging.INFO)
658b6c
+				logging.getLogger("googleapiclient").setLevel(logging.ERROR)
658b6c
+			root_logger.addHandler(handler)
658b6c
 		except ImportError:
658b6c
-			LOGGER.error('Couldn\'t import google.cloud.logging, '
658b6c
+			logging.error('Couldn\'t import google.cloud.logging, '
658b6c
 				'disabling Stackdriver-logging support')
658b6c
 
658b6c
 	# Prepare cli
658b6c
658b6c
From a52e643708908539d6e5fdb5d36a6cea935e4481 Mon Sep 17 00:00:00 2001
658b6c
From: Helen Koike <helen.koike@collabora.com>
658b6c
Date: Wed, 11 Jul 2018 17:16:49 -0300
658b6c
Subject: [PATCH 7/7] fence_gce: minor changes in logging
658b6c
658b6c
- Remove hostname (use --plug instead).
658b6c
- Supress messages from googleapiclient and oauth2client if not error in
658b6c
non verbose mode.
658b6c
- s/stonish/stonith
658b6c
---
658b6c
 agents/gce/fence_gce.py | 13 ++++++-------
658b6c
 1 file changed, 6 insertions(+), 7 deletions(-)
658b6c
658b6c
diff --git a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
658b6c
index 1d5095ae..3eca0139 100644
658b6c
--- a/agents/gce/fence_gce.py
658b6c
+++ b/agents/gce/fence_gce.py
658b6c
@@ -3,7 +3,6 @@
658b6c
 import atexit
658b6c
 import logging
658b6c
 import os
658b6c
-import platform
658b6c
 import sys
658b6c
 import time
658b6c
 if sys.version_info >= (3, 0):
658b6c
@@ -180,8 +179,6 @@ def define_new_opts():
658b6c
 def main():
658b6c
 	conn = None
658b6c
 
658b6c
-	hostname = platform.node()
658b6c
-
658b6c
 	device_opt = ["port", "no_password", "zone", "project", "stackdriver-logging", "method"]
658b6c
 
658b6c
 	atexit.register(atexit_handler)
658b6c
@@ -207,18 +204,20 @@ def main():
658b6c
 	run_delay(options)
658b6c
 
658b6c
 	# Prepare logging
658b6c
-	if options.get('--stackdriver-logging') is not None:
658b6c
+	if options.get('--verbose') is None:
658b6c
+		logging.getLogger('googleapiclient').setLevel(logging.ERROR)
658b6c
+		logging.getLogger('oauth2client').setLevel(logging.ERROR)
658b6c
+	if options.get('--stackdriver-logging') is not None and options.get('--plug'):
658b6c
 		try:
658b6c
 			import google.cloud.logging.handlers
658b6c
 			client = google.cloud.logging.Client()
658b6c
-			handler = google.cloud.logging.handlers.CloudLoggingHandler(client, name=hostname)
658b6c
+			handler = google.cloud.logging.handlers.CloudLoggingHandler(client, name=options['--plug'])
658b6c
 			handler.setLevel(logging.INFO)
658b6c
-			formatter = logging.Formatter('gcp:stonish "%(message)s"')
658b6c
+			formatter = logging.Formatter('gcp:stonith "%(message)s"')
658b6c
 			handler.setFormatter(formatter)
658b6c
 			root_logger = logging.getLogger()
658b6c
 			if options.get('--verbose') is None:
658b6c
 				root_logger.setLevel(logging.INFO)
658b6c
-				logging.getLogger("googleapiclient").setLevel(logging.ERROR)
658b6c
 			root_logger.addHandler(handler)
658b6c
 		except ImportError:
658b6c
 			logging.error('Couldn\'t import google.cloud.logging, '