Blame SOURCES/bz1568588-5-python-library.patch

7b144c
From 13ae97dec5754642af4d0d0edc03d9290e792e7f Mon Sep 17 00:00:00 2001
7b144c
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
7b144c
Date: Thu, 19 Jul 2018 16:12:35 +0200
7b144c
Subject: [PATCH 1/5] Add Python library
7b144c
7b144c
---
7b144c
 heartbeat/Makefile.am |   3 +-
7b144c
 heartbeat/ocf.py      | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++
7b144c
 2 files changed, 138 insertions(+), 1 deletion(-)
7b144c
 create mode 100644 heartbeat/ocf.py
7b144c
7b144c
diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am
7b144c
index d4750bf09..1333f8feb 100644
7b144c
--- a/heartbeat/Makefile.am
7b144c
+++ b/heartbeat/Makefile.am
7b144c
@@ -185,7 +185,8 @@ ocfcommon_DATA		= ocf-shellfuncs 	\
7b144c
 			  ora-common.sh		\
7b144c
 			  mysql-common.sh	\
7b144c
 			  nfsserver-redhat.sh	\
7b144c
-			  findif.sh
7b144c
+			  findif.sh		\
7b144c
+			  ocf.py
7b144c
 
7b144c
 # Legacy locations
7b144c
 hbdir			= $(sysconfdir)/ha.d
7b144c
diff --git a/heartbeat/ocf.py b/heartbeat/ocf.py
7b144c
new file mode 100644
7b144c
index 000000000..12be7a2a4
7b144c
--- /dev/null
7b144c
+++ b/heartbeat/ocf.py
7b144c
@@ -0,0 +1,136 @@
7b144c
+#
7b144c
+# Copyright (c) 2016 Red Hat, Inc, Oyvind Albrigtsen
7b144c
+#                    All Rights Reserved.
7b144c
+#
7b144c
+#
7b144c
+# This library is free software; you can redistribute it and/or
7b144c
+# modify it under the terms of the GNU Lesser General Public
7b144c
+# License as published by the Free Software Foundation; either
7b144c
+# version 2.1 of the License, or (at your option) any later version.
7b144c
+#
7b144c
+# This library is distributed in the hope that it will be useful,
7b144c
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
7b144c
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7b144c
+# Lesser General Public License for more details.
7b144c
+#
7b144c
+# You should have received a copy of the GNU Lesser General Public
7b144c
+# License along with this library; if not, write to the Free Software
7b144c
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7b144c
+# 
7b144c
+
7b144c
+import sys, os, logging, syslog
7b144c
+
7b144c
+argv=sys.argv
7b144c
+env=os.environ
7b144c
+
7b144c
+#
7b144c
+# 	Common variables for the OCF Resource Agents supplied by
7b144c
+# 	heartbeat.
7b144c
+#
7b144c
+
7b144c
+OCF_SUCCESS=0
7b144c
+OCF_ERR_GENERIC=1
7b144c
+OCF_ERR_ARGS=2
7b144c
+OCF_ERR_UNIMPLEMENTED=3
7b144c
+OCF_ERR_PERM=4
7b144c
+OCF_ERR_INSTALLED=5
7b144c
+OCF_ERR_CONFIGURED=6
7b144c
+OCF_NOT_RUNNING=7
7b144c
+
7b144c
+# Non-standard values.
7b144c
+#
7b144c
+# OCF does not include the concept of master/slave resources so we
7b144c
+#   need to extend it so we can discover a resource's complete state.
7b144c
+#
7b144c
+# OCF_RUNNING_MASTER:  
7b144c
+#    The resource is in "master" mode and fully operational
7b144c
+# OCF_FAILED_MASTER:
7b144c
+#    The resource is in "master" mode but in a failed state
7b144c
+# 
7b144c
+# The extra two values should only be used during a probe.
7b144c
+#
7b144c
+# Probes are used to discover resources that were started outside of
7b144c
+#    the CRM and/or left behind if the LRM fails.
7b144c
+# 
7b144c
+# They can be identified in RA scripts by checking for:
7b144c
+#   [ "${__OCF_ACTION}" = "monitor" -a "${OCF_RESKEY_CRM_meta_interval}" = "0" ]
7b144c
+# 
7b144c
+# Failed "slaves" should continue to use: OCF_ERR_GENERIC
7b144c
+# Fully operational "slaves" should continue to use: OCF_SUCCESS
7b144c
+#
7b144c
+OCF_RUNNING_MASTER=8
7b144c
+OCF_FAILED_MASTER=9
7b144c
+
7b144c
+
7b144c
+## Own logger handler that uses old-style syslog handler as otherwise
7b144c
+## everything is sourced from /dev/syslog
7b144c
+class SyslogLibHandler(logging.StreamHandler):
7b144c
+	"""
7b144c
+	A handler class that correctly push messages into syslog
7b144c
+	"""
7b144c
+	def emit(self, record):
7b144c
+		syslog_level = {
7b144c
+			logging.CRITICAL:syslog.LOG_CRIT,
7b144c
+			logging.ERROR:syslog.LOG_ERR,
7b144c
+			logging.WARNING:syslog.LOG_WARNING,
7b144c
+			logging.INFO:syslog.LOG_INFO,
7b144c
+			logging.DEBUG:syslog.LOG_DEBUG,
7b144c
+			logging.NOTSET:syslog.LOG_DEBUG,
7b144c
+		}[record.levelno]
7b144c
+
7b144c
+		msg = self.format(record)
7b144c
+
7b144c
+		# syslog.syslog can not have 0x00 character inside or exception
7b144c
+		# is thrown
7b144c
+		syslog.syslog(syslog_level, msg.replace("\x00","\n"))
7b144c
+		return
7b144c
+
7b144c
+
7b144c
+OCF_RESOURCE_INSTANCE = env.get("OCF_RESOURCE_INSTANCE")
7b144c
+
7b144c
+HA_DEBUG = env.get("HA_debug", 0)
7b144c
+HA_DATEFMT = env.get("HA_DATEFMT", "%b %d %T ")
7b144c
+HA_LOGFACILITY = env.get("HA_LOGFACILITY")
7b144c
+HA_LOGFILE = env.get("HA_LOGFILE")
7b144c
+HA_DEBUGLOG = env.get("HA_DEBUGLOG")
7b144c
+
7b144c
+log = logging.getLogger(os.path.basename(argv[0]))
7b144c
+log.setLevel(logging.DEBUG)
7b144c
+
7b144c
+## add logging to stderr
7b144c
+if sys.stdout.isatty():
7b144c
+	seh = logging.StreamHandler(stream=sys.stderr)
7b144c
+	if HA_DEBUG == 0:
7b144c
+		seh.setLevel(logging.WARNING)
7b144c
+	sehformatter = logging.Formatter('%(filename)s(%(OCF_RESOURCE_INSTANCE)s)[%(process)s]:\t%(asctime)s%(levelname)s: %(message)s', datefmt=HA_DATEFMT)
7b144c
+	seh.setFormatter(sehformatter)
7b144c
+	log.addHandler(seh)
7b144c
+
7b144c
+## add logging to syslog
7b144c
+if HA_LOGFACILITY:
7b144c
+	slh = SyslogLibHandler()
7b144c
+	if HA_DEBUG == 0:
7b144c
+		slh.setLevel(logging.WARNING)
7b144c
+	slhformatter = logging.Formatter('%(levelname)s: %(message)s')
7b144c
+	slh.setFormatter(slhformatter)
7b144c
+	log.addHandler(slh)
7b144c
+
7b144c
+## add logging to file
7b144c
+if HA_LOGFILE:
7b144c
+	lfh = logging.FileHandler(HA_LOGFILE)
7b144c
+	if HA_DEBUG == 0:
7b144c
+		lfh.setLevel(logging.WARNING)
7b144c
+	lfhformatter = logging.Formatter('%(filename)s(%(OCF_RESOURCE_INSTANCE)s)[%(process)s]:\t%(asctime)s%(levelname)s: %(message)s', datefmt=HA_DATEFMT)
7b144c
+	lfh.setFormatter(lfhformatter)
7b144c
+	log.addHandler(lfh)
7b144c
+
7b144c
+## add debug logging to file
7b144c
+if HA_DEBUGLOG and HA_LOGFILE != HA_DEBUGLOG:
7b144c
+	dfh = logging.FileHandler(HA_DEBUGLOG)
7b144c
+	if HA_DEBUG == 0:
7b144c
+		dfh.setLevel(logging.WARNING)
7b144c
+	dfhformatter = logging.Formatter('%(filename)s(%(OCF_RESOURCE_INSTANCE)s)[%(process)s]:\t%(asctime)s%(levelname)s: %(message)s', datefmt=HA_DATEFMT)
7b144c
+	dfh.setFormatter(dfhformatter)
7b144c
+	log.addHandler(dfh)
7b144c
+
7b144c
+logger = logging.LoggerAdapter(log, {'OCF_RESOURCE_INSTANCE': OCF_RESOURCE_INSTANCE})
7b144c
7b144c
From 2ade8dbf1f6f6d3889dd1ddbf40858edf10fbdc7 Mon Sep 17 00:00:00 2001
7b144c
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
7b144c
Date: Thu, 19 Jul 2018 16:20:39 +0200
7b144c
Subject: [PATCH 2/5] gcp-vpc-move-vip: use Python library
7b144c
7b144c
---
7b144c
 heartbeat/gcp-vpc-move-vip.in | 42 +++++++++++++++++++++---------------------
7b144c
 1 file changed, 21 insertions(+), 21 deletions(-)
7b144c
7b144c
diff --git a/heartbeat/gcp-vpc-move-vip.in b/heartbeat/gcp-vpc-move-vip.in
7b144c
index af2080502..eb5bce6a8 100755
7b144c
--- a/heartbeat/gcp-vpc-move-vip.in
7b144c
+++ b/heartbeat/gcp-vpc-move-vip.in
7b144c
@@ -22,6 +22,11 @@ import os
7b144c
 import sys
7b144c
 import time
7b144c
 
7b144c
+OCF_FUNCTIONS_DIR="%s/lib/heartbeat" % os.environ.get("OCF_ROOT")
7b144c
+sys.path.append(OCF_FUNCTIONS_DIR)
7b144c
+
7b144c
+from ocf import *
7b144c
+
7b144c
 try:
7b144c
   import googleapiclient.discovery
7b144c
 except ImportError:
7b144c
@@ -40,10 +45,6 @@ else:
7b144c
 CONN = None
7b144c
 THIS_VM = None
7b144c
 ALIAS = None
7b144c
-OCF_SUCCESS = 0
7b144c
-OCF_ERR_GENERIC = 1
7b144c
-OCF_ERR_CONFIGURED = 6
7b144c
-OCF_NOT_RUNNING = 7
7b144c
 METADATA_SERVER = 'http://metadata.google.internal/computeMetadata/v1/'
7b144c
 METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
7b144c
 METADATA = \
7b144c
@@ -206,11 +207,11 @@ def gcp_alias_start(alias):
7b144c
   # If I already have the IP, exit. If it has an alias IP that isn't the VIP,
7b144c
   # then remove it
7b144c
   if my_alias == alias:
7b144c
-    logging.info(
7b144c
+    logger.info(
7b144c
         '%s already has %s attached. No action required' % (THIS_VM, alias))
7b144c
     sys.exit(OCF_SUCCESS)
7b144c
   elif my_alias:
7b144c
-    logging.info('Removing %s from %s' % (my_alias, THIS_VM))
7b144c
+    logger.info('Removing %s from %s' % (my_alias, THIS_VM))
7b144c
     set_alias(project, my_zone, THIS_VM, '')
7b144c
 
7b144c
   # Loops through all hosts & remove the alias IP from the host that has it
7b144c
@@ -223,7 +224,7 @@ def gcp_alias_start(alias):
7b144c
     host_zone = get_zone(project, host)
7b144c
     host_alias = get_alias(project, host_zone, host)
7b144c
     if alias == host_alias:
7b144c
-      logging.info(
7b144c
+      logger.info(
7b144c
           '%s is attached to %s - Removing all alias IP addresses from %s' %
7b144c
           (alias, host, host))
7b144c
       set_alias(project, host_zone, host, '')
7b144c
@@ -237,14 +238,14 @@ def gcp_alias_start(alias):
7b144c
   # Check the IP has been added
7b144c
   my_alias = get_localhost_alias()
7b144c
   if alias == my_alias:
7b144c
-    logging.info('Finished adding %s to %s' % (alias, THIS_VM))
7b144c
+    logger.info('Finished adding %s to %s' % (alias, THIS_VM))
7b144c
   elif my_alias:
7b144c
-    logging.error(
7b144c
+    logger.error(
7b144c
         'Failed to add IP. %s has an IP attached but it isn\'t %s' %
7b144c
         (THIS_VM, alias))
7b144c
     sys.exit(OCF_ERR_GENERIC)
7b144c
   else:
7b144c
-    logging.error('Failed to add IP address %s to %s' % (alias, THIS_VM))
7b144c
+    logger.error('Failed to add IP address %s to %s' % (alias, THIS_VM))
7b144c
     sys.exit(OCF_ERR_GENERIC)
7b144c
 
7b144c
 
7b144c
@@ -254,14 +255,14 @@ def gcp_alias_stop(alias):
7b144c
   project = get_metadata('project/project-id')
7b144c
 
7b144c
   if my_alias == alias:
7b144c
-    logging.info('Removing %s from %s' % (my_alias, THIS_VM))
7b144c
+    logger.info('Removing %s from %s' % (my_alias, THIS_VM))
7b144c
     set_alias(project, my_zone, THIS_VM, '')
7b144c
 
7b144c
 
7b144c
 def gcp_alias_status(alias):
7b144c
   my_alias = get_localhost_alias()
7b144c
   if alias == my_alias:
7b144c
-    logging.info('%s has the correct IP address attached' % THIS_VM)
7b144c
+    logger.info('%s has the correct IP address attached' % THIS_VM)
7b144c
   else:
7b144c
     sys.exit(OCF_NOT_RUNNING)
7b144c
 
7b144c
@@ -275,25 +276,24 @@ def validate():
7b144c
   try:
7b144c
     CONN = googleapiclient.discovery.build('compute', 'v1')
7b144c
   except Exception as e:
7b144c
-    logging.error('Couldn\'t connect with google api: ' + str(e))
7b144c
+    logger.error('Couldn\'t connect with google api: ' + str(e))
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
 
7b144c
   try:
7b144c
     THIS_VM = get_metadata('instance/name')
7b144c
   except Exception as e:
7b144c
-    logging.error('Couldn\'t get instance name, is this running inside GCE?: ' + str(e))
7b144c
+    logger.error('Couldn\'t get instance name, is this running inside GCE?: ' + str(e))
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
 
7b144c
   ALIAS = os.environ.get('OCF_RESKEY_alias_ip')
7b144c
   if not ALIAS:
7b144c
-    logging.error('Missing alias_ip parameter')
7b144c
+    logger.error('Missing alias_ip parameter')
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
 
7b144c
 
7b144c
 def configure_logs():
7b144c
   # Prepare logging
7b144c
-  logging.basicConfig(
7b144c
-      format='gcp:alias - %(levelname)s - %(message)s', level=logging.INFO)
7b144c
+  global logger
7b144c
   logging.getLogger('googleapiclient').setLevel(logging.WARN)
7b144c
   logging_env = os.environ.get('OCF_RESKEY_stackdriver_logging')
7b144c
   if logging_env:
7b144c
@@ -307,10 +307,10 @@ def configure_logs():
7b144c
         handler.setLevel(logging.INFO)
7b144c
         formatter = logging.Formatter('gcp:alias "%(message)s"')
7b144c
         handler.setFormatter(formatter)
7b144c
-        root_logger = logging.getLogger()
7b144c
-        root_logger.addHandler(handler)
7b144c
+        log.addHandler(handler)
7b144c
+        logger = logging.LoggerAdapter(log, {'OCF_RESOURCE_INSTANCE': OCF_RESOURCE_INSTANCE})
7b144c
       except ImportError:
7b144c
-        logging.error('Couldn\'t import google.cloud.logging, '
7b144c
+        logger.error('Couldn\'t import google.cloud.logging, '
7b144c
             'disabling Stackdriver-logging support')
7b144c
 
7b144c
 
7b144c
@@ -331,7 +331,7 @@ def main():
7b144c
   elif 'status' in sys.argv[1] or 'monitor' in sys.argv[1]:
7b144c
     gcp_alias_status(ALIAS)
7b144c
   else:
7b144c
-    logging.error('no such function %s' % str(sys.argv[1]))
7b144c
+    logger.error('no such function %s' % str(sys.argv[1]))
7b144c
 
7b144c
 
7b144c
 if __name__ == "__main__":
7b144c
7b144c
From 9e9ea17c42df27d4c13fed9badba295df48437f2 Mon Sep 17 00:00:00 2001
7b144c
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
7b144c
Date: Fri, 20 Jul 2018 13:27:42 +0200
7b144c
Subject: [PATCH 3/5] gcp-vpc-move-vip: moved alias-parameters to top of
7b144c
 metadata
7b144c
7b144c
---
7b144c
 heartbeat/gcp-vpc-move-vip.in | 20 ++++++++++----------
7b144c
 1 file changed, 10 insertions(+), 10 deletions(-)
7b144c
7b144c
diff --git a/heartbeat/gcp-vpc-move-vip.in b/heartbeat/gcp-vpc-move-vip.in
7b144c
index eb5bce6a8..ba61193b6 100755
7b144c
--- a/heartbeat/gcp-vpc-move-vip.in
7b144c
+++ b/heartbeat/gcp-vpc-move-vip.in
7b144c
@@ -55,6 +55,16 @@ METADATA = \
7b144c
   <longdesc lang="en">Floating IP Address on Google Cloud Platform - Using Alias IP address functionality to attach a secondary IP address to a running instance</longdesc>
7b144c
   <shortdesc lang="en">Floating IP Address on Google Cloud Platform</shortdesc>
7b144c
   <parameters>
7b144c
+    <parameter name="alias_ip" unique="1" required="1">
7b144c
+      <longdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</longdesc>
7b144c
+      <shortdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</shortdesc>
7b144c
+      <content type="string" default="" />
7b144c
+    </parameter>
7b144c
+    <parameter name="alias_range_name" unique="1" required="0">
7b144c
+      <longdesc lang="en">Subnet name for the Alias IP</longdesc>
7b144c
+      <shortdesc lang="en">Subnet name for the Alias IP</shortdesc>
7b144c
+      <content type="string" default="" />
7b144c
+    </parameter>
7b144c
     <parameter name="hostlist" unique="1" required="0">
7b144c
       <longdesc lang="en">List of hosts in the cluster</longdesc>
7b144c
       <shortdesc lang="en">Host list</shortdesc>
7b144c
@@ -65,16 +75,6 @@ METADATA = \
7b144c
       <shortdesc lang="en">Stackdriver-logging support</shortdesc>
7b144c
       <content type="boolean" default="" />
7b144c
     </parameter>
7b144c
-    <parameter name="alias_ip" unique="1" required="1">
7b144c
-      <longdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</longdesc>
7b144c
-      <shortdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</shortdesc>
7b144c
-      <content type="string" default="" />
7b144c
-    </parameter>
7b144c
-    <parameter name="alias_range_name" unique="1" required="0">
7b144c
-      <longdesc lang="en">Subnet name for the Alias IP2</longdesc>
7b144c
-      <shortdesc lang="en">Subnet name for the Alias IP</shortdesc>
7b144c
-      <content type="string" default="" />
7b144c
-    </parameter>
7b144c
   </parameters>
7b144c
   <actions>
7b144c
     <action name="start" timeout="300" />
7b144c
7b144c
From 716d69040dba7a769efb5a60eca934fdd65585f2 Mon Sep 17 00:00:00 2001
7b144c
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
7b144c
Date: Mon, 23 Jul 2018 11:17:00 +0200
7b144c
Subject: [PATCH 4/5] gcp-vpc-move-route: use Python library
7b144c
7b144c
---
7b144c
 heartbeat/gcp-vpc-move-route.in | 58 ++++++++++++++++++++---------------------
7b144c
 1 file changed, 28 insertions(+), 30 deletions(-)
7b144c
7b144c
diff --git a/heartbeat/gcp-vpc-move-route.in b/heartbeat/gcp-vpc-move-route.in
7b144c
index 566a70f86..125289d86 100644
7b144c
--- a/heartbeat/gcp-vpc-move-route.in
7b144c
+++ b/heartbeat/gcp-vpc-move-route.in
7b144c
@@ -39,6 +39,11 @@ import os
7b144c
 import sys
7b144c
 import time
7b144c
 
7b144c
+OCF_FUNCTIONS_DIR="%s/lib/heartbeat" % os.environ.get("OCF_ROOT")
7b144c
+sys.path.append(OCF_FUNCTIONS_DIR)
7b144c
+
7b144c
+from ocf import *
7b144c
+
7b144c
 try:
7b144c
   import googleapiclient.discovery
7b144c
   import pyroute2
7b144c
@@ -55,12 +60,6 @@ else:
7b144c
   import urllib2 as urlrequest
7b144c
 
7b144c
 
7b144c
-OCF_SUCCESS = 0
7b144c
-OCF_ERR_GENERIC = 1
7b144c
-OCF_ERR_UNIMPLEMENTED = 3
7b144c
-OCF_ERR_PERM = 4
7b144c
-OCF_ERR_CONFIGURED = 6
7b144c
-OCF_NOT_RUNNING = 7
7b144c
 GCP_API_URL_PREFIX = 'https://www.googleapis.com/compute/v1'
7b144c
 METADATA_SERVER = 'http://metadata.google.internal/computeMetadata/v1/'
7b144c
 METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
7b144c
@@ -199,18 +198,18 @@ def get_metadata(metadata_key, params=None, timeout=None):
7b144c
 
7b144c
 def validate(ctx):
7b144c
   if os.geteuid() != 0:
7b144c
-    logging.error('You must run this agent as root')
7b144c
+    logger.error('You must run this agent as root')
7b144c
     sys.exit(OCF_ERR_PERM)
7b144c
 
7b144c
   try:
7b144c
     ctx.conn = googleapiclient.discovery.build('compute', 'v1')
7b144c
   except Exception as e:
7b144c
-    logging.error('Couldn\'t connect with google api: ' + str(e))
7b144c
+    logger.error('Couldn\'t connect with google api: ' + str(e))
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
 
7b144c
   ctx.ip = os.environ.get('OCF_RESKEY_ip')
7b144c
   if not ctx.ip:
7b144c
-    logging.error('Missing ip parameter')
7b144c
+    logger.error('Missing ip parameter')
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
 
7b144c
   try:
7b144c
@@ -218,7 +217,7 @@ def validate(ctx):
7b144c
     ctx.zone = get_metadata('instance/zone').split('/')[-1]
7b144c
     ctx.project = get_metadata('project/project-id')
7b144c
   except Exception as e:
7b144c
-    logging.error(
7b144c
+    logger.error(
7b144c
         'Instance information not found. Is this a GCE instance ?: %s', str(e))
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
 
7b144c
@@ -234,7 +233,7 @@ def validate(ctx):
7b144c
   atexit.register(ctx.iproute.close)
7b144c
   idxs = ctx.iproute.link_lookup(ifname=ctx.interface)
7b144c
   if not idxs:
7b144c
-    logging.error('Network interface not found')
7b144c
+    logger.error('Network interface not found')
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
   ctx.iface_idx = idxs[0]
7b144c
 
7b144c
@@ -246,7 +245,7 @@ def check_conflicting_routes(ctx):
7b144c
   response = request.execute()
7b144c
   route_list = response.get('items', None)
7b144c
   if route_list:
7b144c
-    logging.error(
7b144c
+    logger.error(
7b144c
         'Conflicting unnmanaged routes for destination %s/32 in VPC %s found : %s',
7b144c
         ctx.ip, ctx.vpc_network, str(route_list))
7b144c
     sys.exit(OCF_ERR_CONFIGURED)
7b144c
@@ -258,7 +257,7 @@ def route_release(ctx):
7b144c
 
7b144c
 
7b144c
 def ip_monitor(ctx):
7b144c
-  logging.info('IP monitor: checking local network configuration')
7b144c
+  logger.info('IP monitor: checking local network configuration')
7b144c
 
7b144c
   def address_filter(addr):
7b144c
     for attr in addr['attrs']:
7b144c
@@ -271,12 +270,12 @@ def ip_monitor(ctx):
7b144c
   route = ctx.iproute.get_addr(
7b144c
       index=ctx.iface_idx, match=address_filter)
7b144c
   if not route:
7b144c
-    logging.warn(
7b144c
+    logger.warning(
7b144c
         'The floating IP %s is not locally configured on this instance (%s)',
7b144c
         ctx.ip, ctx.instance)
7b144c
     return OCF_NOT_RUNNING
7b144c
 
7b144c
-  logging.debug(
7b144c
+  logger.debug(
7b144c
       'The floating IP %s is correctly configured on this instance (%s)',
7b144c
       ctx.ip, ctx.instance)
7b144c
   return OCF_SUCCESS
7b144c
@@ -287,7 +286,7 @@ def ip_release(ctx):
7b144c
 
7b144c
 
7b144c
 def ip_and_route_start(ctx):
7b144c
-  logging.info('Bringing up the floating IP %s', ctx.ip)
7b144c
+  logger.info('Bringing up the floating IP %s', ctx.ip)
7b144c
 
7b144c
   # Add a new entry in the routing table
7b144c
   # If the route entry exists and is pointing to another instance, take it over
7b144c
@@ -322,7 +321,7 @@ def ip_and_route_start(ctx):
7b144c
       request.execute()
7b144c
     except googleapiclient.errors.HttpError as e:
7b144c
       if e.resp.status == 404:
7b144c
-        logging.error('VPC network not found')
7b144c
+        logger.error('VPC network not found')
7b144c
         sys.exit(OCF_ERR_CONFIGURED)
7b144c
       else:
7b144c
         raise
7b144c
@@ -336,11 +335,11 @@ def ip_and_route_start(ctx):
7b144c
 
7b144c
   ctx.iproute.addr('add', index=ctx.iface_idx, address=ctx.ip, mask=32)
7b144c
   ctx.iproute.link('set', index=ctx.iface_idx, state='up')
7b144c
-  logging.info('Successfully brought up the floating IP %s', ctx.ip)
7b144c
+  logger.info('Successfully brought up the floating IP %s', ctx.ip)
7b144c
 
7b144c
 
7b144c
 def route_monitor(ctx):
7b144c
-  logging.info('GCP route monitor: checking route table')
7b144c
+  logger.info('GCP route monitor: checking route table')
7b144c
 
7b144c
   # Ensure that there is no route that we are not aware of that is also handling our IP
7b144c
   check_conflicting_routes
7b144c
@@ -360,39 +359,38 @@ def route_monitor(ctx):
7b144c
   instance_url = '%s/projects/%s/zones/%s/instances/%s' % (
7b144c
       GCP_API_URL_PREFIX, ctx.project, ctx.zone, ctx.instance)
7b144c
   if routed_to_instance != instance_url:
7b144c
-    logging.warn(
7b144c
+    logger.warning(
7b144c
         'The floating IP %s is not routed to this instance (%s) but to instance %s',
7b144c
         ctx.ip, ctx.instance, routed_to_instance.split('/')[-1])
7b144c
     return OCF_NOT_RUNNING
7b144c
 
7b144c
-  logging.debug(
7b144c
+  logger.debug(
7b144c
       'The floating IP %s is correctly routed to this instance (%s)',
7b144c
       ctx.ip, ctx.instance)
7b144c
   return OCF_SUCCESS
7b144c
 
7b144c
 
7b144c
 def ip_and_route_stop(ctx):
7b144c
-  logging.info('Bringing down the floating IP %s', ctx.ip)
7b144c
+  logger.info('Bringing down the floating IP %s', ctx.ip)
7b144c
 
7b144c
   # Delete the route entry
7b144c
   # If the route entry exists and is pointing to another instance, don't touch it
7b144c
   if route_monitor(ctx) == OCF_NOT_RUNNING:
7b144c
-    logging.info(
7b144c
+    logger.info(
7b144c
         'The floating IP %s is already not routed to this instance (%s)',
7b144c
         ctx.ip, ctx.instance)
7b144c
   else:
7b144c
     route_release(ctx)
7b144c
 
7b144c
   if ip_monitor(ctx) == OCF_NOT_RUNNING:
7b144c
-    logging.info('The floating IP %s is already down', ctx.ip)
7b144c
+    logger.info('The floating IP %s is already down', ctx.ip)
7b144c
   else:
7b144c
     ip_release(ctx)
7b144c
 
7b144c
 
7b144c
 def configure_logs(ctx):
7b144c
   # Prepare logging
7b144c
-  logging.basicConfig(
7b144c
-      format='gcp:route - %(levelname)s - %(message)s', level=logging.INFO)
7b144c
+  global logger
7b144c
   logging.getLogger('googleapiclient').setLevel(logging.WARN)
7b144c
   logging_env = os.environ.get('OCF_RESKEY_stackdriver_logging')
7b144c
   if logging_env:
7b144c
@@ -406,10 +404,10 @@ def configure_logs(ctx):
7b144c
         handler.setLevel(logging.INFO)
7b144c
         formatter = logging.Formatter('gcp:route "%(message)s"')
7b144c
         handler.setFormatter(formatter)
7b144c
-        root_logger = logging.getLogger()
7b144c
-        root_logger.addHandler(handler)
7b144c
+        log.addHandler(handler)
7b144c
+        logger = logging.LoggerAdapter(log, {'OCF_RESOURCE_INSTANCE': OCF_RESOURCE_INSTANCE})
7b144c
       except ImportError:
7b144c
-        logging.error('Couldn\'t import google.cloud.logging, '
7b144c
+        logger.error('Couldn\'t import google.cloud.logging, '
7b144c
             'disabling Stackdriver-logging support')
7b144c
 
7b144c
 
7b144c
@@ -434,7 +432,7 @@ def main():
7b144c
   else:
7b144c
     usage = 'usage: %s {start|stop|monitor|status|meta-data|validate-all}' % \
7b144c
         os.path.basename(sys.argv[0])
7b144c
-    logging.error(usage)
7b144c
+    logger.error(usage)
7b144c
     sys.exit(OCF_ERR_UNIMPLEMENTED)
7b144c
 
7b144c
 
7b144c
7b144c
From 6ec7e87693a51cbb16a1822e6d15f1dbfc11f8e6 Mon Sep 17 00:00:00 2001
7b144c
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
7b144c
Date: Mon, 23 Jul 2018 15:55:48 +0200
7b144c
Subject: [PATCH 5/5] Python: add logging.basicConfig() to support background
7b144c
 logging
7b144c
7b144c
---
7b144c
 heartbeat/ocf.py | 1 +
7b144c
 1 file changed, 1 insertion(+)
7b144c
7b144c
diff --git a/heartbeat/ocf.py b/heartbeat/ocf.py
7b144c
index 12be7a2a4..36e7ccccd 100644
7b144c
--- a/heartbeat/ocf.py
7b144c
+++ b/heartbeat/ocf.py
7b144c
@@ -94,6 +94,7 @@ def emit(self, record):
7b144c
 HA_LOGFILE = env.get("HA_LOGFILE")
7b144c
 HA_DEBUGLOG = env.get("HA_DEBUGLOG")
7b144c
 
7b144c
+logging.basicConfig()
7b144c
 log = logging.getLogger(os.path.basename(argv[0]))
7b144c
 log.setLevel(logging.DEBUG)
7b144c