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

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