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

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