From 9dedf4d4ad3a94e4ce75e0f29ffdd018e3709ae3 Mon Sep 17 00:00:00 2001 From: Oyvind Albrigtsen 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. The name of the GCP disk. Disk name - + - -Disk scope + +Disk scope Network name - + - + Path to a Customer-Supplied Encryption Key (CSEK) key file Customer-Supplied Encryption Key file - + - -Attachment mode (rw, ro) + +Attachment mode (READ_WRITE, READ_ONLY) Attachment mode - + - + An optional name that indicates the disk name the guest operating system will see. Optional device name - + - + Use stackdriver_logging output to global resource (yes, true, enabled) Use stackdriver_logging - + @@ -114,7 +115,9 @@ correctly. -''' +'''.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, })