Blob Blame History Raw
From 9dedf4d4ad3a94e4ce75e0f29ffdd018e3709ae3 Mon Sep 17 00:00:00 2001
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
Date: Thu, 28 May 2020 11:39:20 +0200
Subject: [PATCH] gcp-pd-move: fixes and improvements

- Fixed Python 3 encoding issue
- Improved metadata
- Change monitor loglevel to debug
- Removed "regional" functionality that doesnt work with attachDisk()
- Updated rw/ro to READ_WRITE/READ_ONLY in metadata/default value
---
 heartbeat/gcp-pd-move.in | 63 ++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 31 deletions(-)
 mode change 100755 => 100644 heartbeat/gcp-pd-move.in

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