Blame SOURCES/bz1633251-gcp-pd-move-4-fixes-and-improvements.patch

5fdbe4
From 9dedf4d4ad3a94e4ce75e0f29ffdd018e3709ae3 Mon Sep 17 00:00:00 2001
5fdbe4
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
5fdbe4
Date: Thu, 28 May 2020 11:39:20 +0200
5fdbe4
Subject: [PATCH] gcp-pd-move: fixes and improvements
5fdbe4
5fdbe4
- Fixed Python 3 encoding issue
5fdbe4
- Improved metadata
5fdbe4
- Change monitor loglevel to debug
5fdbe4
- Removed "regional" functionality that doesnt work with attachDisk()
5fdbe4
- Updated rw/ro to READ_WRITE/READ_ONLY in metadata/default value
5fdbe4
---
5fdbe4
 heartbeat/gcp-pd-move.in | 63 ++++++++++++++++++++--------------------
5fdbe4
 1 file changed, 32 insertions(+), 31 deletions(-)
5fdbe4
 mode change 100755 => 100644 heartbeat/gcp-pd-move.in
5fdbe4
5fdbe4
diff --git a/heartbeat/gcp-pd-move.in b/heartbeat/gcp-pd-move.in
5fdbe4
old mode 100755
5fdbe4
new mode 100644
5fdbe4
index 7fabc80dc..f82bd25e5
5fdbe4
--- a/heartbeat/gcp-pd-move.in
5fdbe4
+++ b/heartbeat/gcp-pd-move.in
5fdbe4
@@ -29,6 +29,7 @@ OCF_FUNCTIONS_DIR = os.environ.get("OCF_FUNCTIONS_DIR", "%s/lib/heartbeat" % os.
5fdbe4
 sys.path.append(OCF_FUNCTIONS_DIR)
5fdbe4
 
5fdbe4
 import ocf
5fdbe4
+from ocf import logger
5fdbe4
 
5fdbe4
 try:
5fdbe4
   import googleapiclient.discovery
5fdbe4
@@ -48,16 +49,16 @@ else:
5fdbe4
 CONN = None
5fdbe4
 PROJECT = None
5fdbe4
 ZONE = None
5fdbe4
-REGION = None
5fdbe4
 LIST_DISK_ATTACHED_INSTANCES = None
5fdbe4
 INSTANCE_NAME = None
5fdbe4
 
5fdbe4
 PARAMETERS = {
5fdbe4
-  'disk_name': None,
5fdbe4
-  'disk_scope': None,
5fdbe4
-  'disk_csek_file': None,
5fdbe4
-  'mode': None,
5fdbe4
-  'device_name': None,
5fdbe4
+  'disk_name': '',
5fdbe4
+  'disk_scope': 'detect',
5fdbe4
+  'disk_csek_file': '',
5fdbe4
+  'mode': "READ_WRITE",
5fdbe4
+  'device_name': '',
5fdbe4
+  'stackdriver_logging': 'no',
5fdbe4
 }
5fdbe4
 
5fdbe4
 MANDATORY_PARAMETERS = ['disk_name', 'disk_scope']
5fdbe4
@@ -80,32 +81,32 @@ correctly.
5fdbe4
 <parameter name="disk_name" unique="1" required="1">
5fdbe4
 <longdesc lang="en">The name of the GCP disk.</longdesc>
5fdbe4
 <shortdesc lang="en">Disk name</shortdesc>
5fdbe4
-<content type="string" default="" />
5fdbe4
+<content type="string" default="{}" />
5fdbe4
 </parameter>
5fdbe4
-<parameter name="disk_scope" unique="1" required="1">
5fdbe4
-<longdesc lang="en">Disk scope </longdesc>
5fdbe4
+<parameter name="disk_scope">
5fdbe4
+<longdesc lang="en">Disk scope</longdesc>
5fdbe4
 <shortdesc lang="en">Network name</shortdesc>
5fdbe4
-<content type="string" default="regional" />
5fdbe4
+<content type="string" default="{}" />
5fdbe4
 </parameter>
5fdbe4
-<parameter name="disk_csek_file" unique="1" required="0">
5fdbe4
+<parameter name="disk_csek_file">
5fdbe4
 <longdesc lang="en">Path to a Customer-Supplied Encryption Key (CSEK) key file</longdesc>
5fdbe4
 <shortdesc lang="en">Customer-Supplied Encryption Key file</shortdesc>
5fdbe4
-<content type="string" default="" />
5fdbe4
+<content type="string" default="{}" />
5fdbe4
 </parameter>
5fdbe4
-<parameter name="mode" unique="1" required="0">
5fdbe4
-<longdesc lang="en">Attachment mode (rw, ro)</longdesc>
5fdbe4
+<parameter name="mode">
5fdbe4
+<longdesc lang="en">Attachment mode (READ_WRITE, READ_ONLY)</longdesc>
5fdbe4
 <shortdesc lang="en">Attachment mode</shortdesc>
5fdbe4
-<content type="string" default="rw" />
5fdbe4
+<content type="string" default="{}" />
5fdbe4
 </parameter>
5fdbe4
-<parameter name="device_name" unique="0" required="0">
5fdbe4
+<parameter name="device_name">
5fdbe4
 <longdesc lang="en">An optional name that indicates the disk name the guest operating system will see.</longdesc>
5fdbe4
 <shortdesc lang="en">Optional device name</shortdesc>
5fdbe4
-<content type="boolean" default="" />
5fdbe4
+<content type="boolean" default="{}" />
5fdbe4
 </parameter>
5fdbe4
-<parameter name="stackdriver_logging" unique="1" required="0">
5fdbe4
+<parameter name="stackdriver_logging">
5fdbe4
 <longdesc lang="en">Use stackdriver_logging output to global resource (yes, true, enabled)</longdesc>
5fdbe4
 <shortdesc lang="en">Use stackdriver_logging</shortdesc>
5fdbe4
-<content type="string" default="no" />
5fdbe4
+<content type="string" default="{}" />
5fdbe4
 </parameter>
5fdbe4
 </parameters>
5fdbe4
 <actions>
5fdbe4
@@ -114,7 +115,9 @@ correctly.
5fdbe4
 <action name="monitor" timeout="15s" interval="10s" depth="0" />
5fdbe4
 <action name="meta-data" timeout="5s" />
5fdbe4
 </actions>
5fdbe4
-</resource-agent>'''
5fdbe4
+</resource-agent>'''.format(PARAMETERS['disk_name'], PARAMETERS['disk_scope'],
5fdbe4
+  PARAMETERS['disk_csek_file'], PARAMETERS['mode'], PARAMETERS['device_name'],
5fdbe4
+  PARAMETERS['stackdriver_logging'])
5fdbe4
 
5fdbe4
 
5fdbe4
 def get_metadata(metadata_key, params=None, timeout=None):
5fdbe4
@@ -137,7 +140,7 @@ def get_metadata(metadata_key, params=None, timeout=None):
5fdbe4
   url = '%s?%s' % (metadata_url, params)
5fdbe4
   request = urlrequest.Request(url, headers=METADATA_HEADERS)
5fdbe4
   request_opener = urlrequest.build_opener(urlrequest.ProxyHandler({}))
5fdbe4
-  return request_opener.open(request, timeout=timeout * 1.1).read()
5fdbe4
+  return request_opener.open(request, timeout=timeout * 1.1).read().decode("utf-8")
5fdbe4
 
5fdbe4
 
5fdbe4
 def populate_vars():
5fdbe4
@@ -145,11 +148,8 @@ def populate_vars():
5fdbe4
   global INSTANCE_NAME
5fdbe4
   global PROJECT
5fdbe4
   global ZONE
5fdbe4
-  global REGION
5fdbe4
   global LIST_DISK_ATTACHED_INSTANCES
5fdbe4
 
5fdbe4
-  global PARAMETERS
5fdbe4
-
5fdbe4
   # Populate global vars
5fdbe4
   try:
5fdbe4
     CONN = googleapiclient.discovery.build('compute', 'v1')
5fdbe4
@@ -158,11 +158,12 @@ def populate_vars():
5fdbe4
     sys.exit(ocf.OCF_ERR_CONFIGURED)
5fdbe4
 
5fdbe4
   for param in PARAMETERS:
5fdbe4
-    value = os.environ.get('OCF_RESKEY_%s' % param, None)
5fdbe4
+    value = os.environ.get('OCF_RESKEY_%s' % param, PARAMETERS[param])
5fdbe4
     if not value and param in MANDATORY_PARAMETERS:
5fdbe4
       logger.error('Missing %s mandatory parameter' % param)
5fdbe4
       sys.exit(ocf.OCF_ERR_CONFIGURED)
5fdbe4
-    PARAMETERS[param] = value
5fdbe4
+    elif value:
5fdbe4
+      PARAMETERS[param] = value
5fdbe4
 
5fdbe4
   try:
5fdbe4
     INSTANCE_NAME = get_metadata('instance/name')
5fdbe4
@@ -172,8 +173,10 @@ def populate_vars():
5fdbe4
     sys.exit(ocf.OCF_ERR_CONFIGURED)
5fdbe4
 
5fdbe4
   PROJECT = get_metadata('project/project-id')
5fdbe4
-  ZONE = get_metadata('instance/zone').split('/')[-1]
5fdbe4
-  REGION = ZONE[:-2]
5fdbe4
+  if PARAMETERS['disk_scope'] in ['detect', 'regional']:
5fdbe4
+    ZONE = get_metadata('instance/zone').split('/')[-1]
5fdbe4
+  else:
5fdbe4
+    ZONE = PARAMETERS['disk_scope']
5fdbe4
   LIST_DISK_ATTACHED_INSTANCES = get_disk_attached_instances(
5fdbe4
       PARAMETERS['disk_name'])
5fdbe4
 
5fdbe4
@@ -270,8 +273,6 @@ def detach_disk(instance, disk_name):
5fdbe4
 
5fdbe4
 def attach_disk(instance, disk_name):
5fdbe4
   location = 'zones/%s' % ZONE
5fdbe4
-  if PARAMETERS['disk_scope'] == 'regional':
5fdbe4
-    location = 'regions/%s' % REGION
5fdbe4
   prefix = 'https://www.googleapis.com/compute/v1'
5fdbe4
   body = {
5fdbe4
     'source': '%(prefix)s/projects/%(project)s/%(location)s/disks/%(disk)s' % {
5fdbe4
@@ -342,7 +343,7 @@ def gcp_pd_move_stop():
5fdbe4
 def gcp_pd_move_status():
5fdbe4
   fetch_data()
5fdbe4
   if is_disk_attached(INSTANCE_NAME):
5fdbe4
-    logger.info("Disk %(disk_name)s is correctly attached to %(instance)s" % {
5fdbe4
+    logger.debug("Disk %(disk_name)s is correctly attached to %(instance)s" % {
5fdbe4
         'disk_name': PARAMETERS['disk_name'],
5fdbe4
         'instance': INSTANCE_NAME,
5fdbe4
     })