Blame SOURCES/bz1633249-gcp-pd-move-1.patch

5c52dd
From dedf420b8aa7e7e64fa56eeda2d7aeb5b2a5fcd9 Mon Sep 17 00:00:00 2001
5c52dd
From: Gustavo Serra Scalet <gustavo.scalet@collabora.com>
5c52dd
Date: Mon, 17 Sep 2018 12:29:51 -0300
5c52dd
Subject: [PATCH] Add gcp-pd-move python script
5c52dd
5c52dd
---
5c52dd
 configure.ac             |   1 +
5c52dd
 doc/man/Makefile.am      |   1 +
5c52dd
 heartbeat/Makefile.am    |   1 +
5c52dd
 heartbeat/gcp-pd-move.in | 370 +++++++++++++++++++++++++++++++++++++++
5c52dd
 4 files changed, 373 insertions(+)
5c52dd
 create mode 100755 heartbeat/gcp-pd-move.in
5c52dd
5c52dd
diff --git a/configure.ac b/configure.ac
5c52dd
index 10f5314da..b7ffb99f3 100644
5c52dd
--- a/configure.ac
5c52dd
+++ b/configure.ac
5c52dd
@@ -958,6 +958,7 @@ AC_CONFIG_FILES([heartbeat/conntrackd], [chmod +x heartbeat/conntrackd])
5c52dd
 AC_CONFIG_FILES([heartbeat/dnsupdate], [chmod +x heartbeat/dnsupdate])
5c52dd
 AC_CONFIG_FILES([heartbeat/eDir88], [chmod +x heartbeat/eDir88])
5c52dd
 AC_CONFIG_FILES([heartbeat/fio], [chmod +x heartbeat/fio])
5c52dd
+AC_CONFIG_FILES([heartbeat/gcp-pd-move], [chmod +x heartbeat/gcp-pd-move])
5c52dd
 AC_CONFIG_FILES([heartbeat/gcp-vpc-move-ip], [chmod +x heartbeat/gcp-vpc-move-ip])
5c52dd
 AC_CONFIG_FILES([heartbeat/gcp-vpc-move-vip], [chmod +x heartbeat/gcp-vpc-move-vip])
5c52dd
 AC_CONFIG_FILES([heartbeat/gcp-vpc-move-route], [chmod +x heartbeat/gcp-vpc-move-route])
5c52dd
diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am
5c52dd
index 0bef88740..0235c9af6 100644
5c52dd
--- a/doc/man/Makefile.am
5c52dd
+++ b/doc/man/Makefile.am
5c52dd
@@ -115,6 +115,7 @@ man_MANS	       = ocf_heartbeat_AoEtarget.7 \
5c52dd
                           ocf_heartbeat_fio.7 \
5c52dd
                           ocf_heartbeat_galera.7 \
5c52dd
                           ocf_heartbeat_garbd.7 \
5c52dd
+                          ocf_heartbeat_gcp-pd-move.7 \
5c52dd
                           ocf_heartbeat_gcp-vpc-move-ip.7 \
5c52dd
                           ocf_heartbeat_gcp-vpc-move-vip.7 \
5c52dd
                           ocf_heartbeat_gcp-vpc-move-route.7 \
5c52dd
diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am
5c52dd
index 993bff042..843186c98 100644
5c52dd
--- a/heartbeat/Makefile.am
5c52dd
+++ b/heartbeat/Makefile.am
5c52dd
@@ -111,6 +111,7 @@ ocf_SCRIPTS	     =  AoEtarget		\
5c52dd
 			fio			\
5c52dd
 			galera			\
5c52dd
 			garbd			\
5c52dd
+			gcp-pd-move		\
5c52dd
 			gcp-vpc-move-ip		\
5c52dd
 			gcp-vpc-move-vip	\
5c52dd
 			gcp-vpc-move-route	\
5c52dd
diff --git a/heartbeat/gcp-pd-move.in b/heartbeat/gcp-pd-move.in
5c52dd
new file mode 100755
5c52dd
index 000000000..f9f6c3163
5c52dd
--- /dev/null
5c52dd
+++ b/heartbeat/gcp-pd-move.in
5c52dd
@@ -0,0 +1,370 @@
5c52dd
+#!@PYTHON@ -tt
5c52dd
+# - *- coding: utf- 8 - *-
5c52dd
+#
5c52dd
+# ---------------------------------------------------------------------
5c52dd
+# Copyright 2018 Google Inc.
5c52dd
+#
5c52dd
+# Licensed under the Apache License, Version 2.0 (the "License");
5c52dd
+# you may not use this file except in compliance with the License.
5c52dd
+# You may obtain a copy of the License at
5c52dd
+#
5c52dd
+# http://www.apache.org/licenses/LICENSE-2.0
5c52dd
+# Unless required by applicable law or agreed to in writing, software
5c52dd
+# distributed under the License is distributed on an "AS IS" BASIS,
5c52dd
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5c52dd
+# See the License for the specific language governing permissions and
5c52dd
+# limitations under the License.
5c52dd
+# ---------------------------------------------------------------------
5c52dd
+# Description:	Google Cloud Platform - Disk attach
5c52dd
+# ---------------------------------------------------------------------
5c52dd
+
5c52dd
+import json
5c52dd
+import logging
5c52dd
+import os
5c52dd
+import re
5c52dd
+import sys
5c52dd
+import time
5c52dd
+
5c52dd
+OCF_FUNCTIONS_DIR = "%s/lib/heartbeat" % os.environ.get("OCF_ROOT")
5c52dd
+sys.path.append(OCF_FUNCTIONS_DIR)
5c52dd
+
5c52dd
+import ocf
5c52dd
+
5c52dd
+try:
5c52dd
+  import googleapiclient.discovery
5c52dd
+except ImportError:
5c52dd
+  pass
5c52dd
+
5c52dd
+if sys.version_info >= (3, 0):
5c52dd
+  # Python 3 imports.
5c52dd
+  import urllib.parse as urlparse
5c52dd
+  import urllib.request as urlrequest
5c52dd
+else:
5c52dd
+  # Python 2 imports.
5c52dd
+  import urllib as urlparse
5c52dd
+  import urllib2 as urlrequest
5c52dd
+
5c52dd
+
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
+}
5c52dd
+
5c52dd
+MANDATORY_PARAMETERS = ['disk_name', 'disk_scope']
5c52dd
+
5c52dd
+METADATA_SERVER = 'http://metadata.google.internal/computeMetadata/v1/'
5c52dd
+METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
5c52dd
+METADATA = '''
5c52dd
+
5c52dd
+<resource-agent name="gcp-pd-move">
5c52dd
+<version>1.0</version>
5c52dd
+<longdesc lang="en">
5c52dd
+Resource Agent that can attach or detach a regional/zonal disk on current GCP
5c52dd
+instance.
5c52dd
+Requirements :
5c52dd
+- Disk has to be properly created as regional/zonal in order to be used
5c52dd
+correctly.
5c52dd
+</longdesc>
5c52dd
+<shortdesc lang="en">Attach/Detach a persistent disk on current GCP instance</shortdesc>
5c52dd
+<parameters>
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
+</parameter>
5c52dd
+<parameter name="disk_scope" unique="1" required="1">
5c52dd
+<longdesc lang="en">Disk scope </longdesc>
5c52dd
+<shortdesc lang="en">Network name</shortdesc>
5c52dd
+<content type="string" default="regional" />
5c52dd
+</parameter>
5c52dd
+<parameter name="disk_csek_file" unique="1" required="0">
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
+</parameter>
5c52dd
+<parameter name="mode" unique="1" required="0">
5c52dd
+<longdesc lang="en">Attachment mode (rw, ro)</longdesc>
5c52dd
+<shortdesc lang="en">Attachment mode</shortdesc>
5c52dd
+<content type="string" default="rw" />
5c52dd
+</parameter>
5c52dd
+<parameter name="device_name" unique="0" required="0">
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
+</parameter>
5c52dd
+</parameters>
5c52dd
+<actions>
5c52dd
+<action name="start" timeout="300s" />
5c52dd
+<action name="stop" timeout="15s" />
5c52dd
+<action name="monitor" timeout="15s" interval="10s" depth="0" />
5c52dd
+<action name="meta-data" timeout="5s" />
5c52dd
+</actions>
5c52dd
+</resource-agent>'''
5c52dd
+
5c52dd
+
5c52dd
+def get_metadata(metadata_key, params=None, timeout=None):
5c52dd
+  """Performs a GET request with the metadata headers.
5c52dd
+
5c52dd
+  Args:
5c52dd
+    metadata_key: string, the metadata to perform a GET request on.
5c52dd
+    params: dictionary, the query parameters in the GET request.
5c52dd
+    timeout: int, timeout in seconds for metadata requests.
5c52dd
+
5c52dd
+  Returns:
5c52dd
+    HTTP response from the GET request.
5c52dd
+
5c52dd
+  Raises:
5c52dd
+    urlerror.HTTPError: raises when the GET request fails.
5c52dd
+  """
5c52dd
+  timeout = timeout or 60
5c52dd
+  metadata_url = os.path.join(METADATA_SERVER, metadata_key)
5c52dd
+  params = urlparse.urlencode(params or {})
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
+
5c52dd
+
5c52dd
+def populate_vars():
5c52dd
+  global CONN
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
+  except Exception as e:
5c52dd
+    logger.error('Couldn\'t connect with google api: ' + str(e))
5c52dd
+    sys.exit(ocf.OCF_ERR_CONFIGURED)
5c52dd
+
5c52dd
+  for param in PARAMETERS:
5c52dd
+    value = os.environ.get('OCF_RESKEY_%s' % param, None)
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
+
5c52dd
+  try:
5c52dd
+    INSTANCE_NAME = get_metadata('instance/name')
5c52dd
+  except Exception as e:
5c52dd
+    logger.error(
5c52dd
+        'Couldn\'t get instance name, is this running inside GCE?: ' + str(e))
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
+  LIST_DISK_ATTACHED_INSTANCES = get_disk_attached_instances(
5c52dd
+      PARAMETERS['disk_name'])
5c52dd
+
5c52dd
+
5c52dd
+def configure_logs():
5c52dd
+  # Prepare logging
5c52dd
+  global logger
5c52dd
+  logging.getLogger('googleapiclient').setLevel(logging.WARN)
5c52dd
+  logging_env = os.environ.get('OCF_RESKEY_stackdriver_logging')
5c52dd
+  if logging_env:
5c52dd
+    logging_env = logging_env.lower()
5c52dd
+    if any(x in logging_env for x in ['yes', 'true', 'enabled']):
5c52dd
+      try:
5c52dd
+        import google.cloud.logging.handlers
5c52dd
+        client = google.cloud.logging.Client()
5c52dd
+        handler = google.cloud.logging.handlers.CloudLoggingHandler(
5c52dd
+            client, name=INSTANCE_NAME)
5c52dd
+        handler.setLevel(logging.INFO)
5c52dd
+        formatter = logging.Formatter('gcp:alias "%(message)s"')
5c52dd
+        handler.setFormatter(formatter)
5c52dd
+        ocf.log.addHandler(handler)
5c52dd
+        logger = logging.LoggerAdapter(
5c52dd
+            ocf.log, {'OCF_RESOURCE_INSTANCE': ocf.OCF_RESOURCE_INSTANCE})
5c52dd
+      except ImportError:
5c52dd
+        logger.error('Couldn\'t import google.cloud.logging, '
5c52dd
+            'disabling Stackdriver-logging support')
5c52dd
+
5c52dd
+
5c52dd
+def wait_for_operation(operation):
5c52dd
+  while True:
5c52dd
+    result = CONN.zoneOperations().get(
5c52dd
+        project=PROJECT,
5c52dd
+        zone=ZONE,
5c52dd
+        operation=operation['name']).execute()
5c52dd
+
5c52dd
+    if result['status'] == 'DONE':
5c52dd
+      if 'error' in result:
5c52dd
+        raise Exception(result['error'])
5c52dd
+      return
5c52dd
+    time.sleep(1)
5c52dd
+
5c52dd
+
5c52dd
+def get_disk_attached_instances(disk):
5c52dd
+  def get_users_list():
5c52dd
+    fl = 'name="%s"' % disk
5c52dd
+    request = CONN.disks().aggregatedList(project=PROJECT, filter=fl)
5c52dd
+    while request is not None:
5c52dd
+      response = request.execute()
5c52dd
+      locations = response.get('items', {})
5c52dd
+      for location in locations.values():
5c52dd
+        for d in location.get('disks', []):
5c52dd
+          if d['name'] == disk:
5c52dd
+            return d.get('users', [])
5c52dd
+      request = CONN.instances().aggregatedList_next(
5c52dd
+          previous_request=request, previous_response=response)
5c52dd
+    raise Exception("Unable to find disk %s" % disk)
5c52dd
+
5c52dd
+  def get_only_instance_name(user):
5c52dd
+    return re.sub('.*/instances/', '', user)
5c52dd
+
5c52dd
+  return map(get_only_instance_name, get_users_list())
5c52dd
+
5c52dd
+
5c52dd
+def is_disk_attached(instance):
5c52dd
+  return instance in LIST_DISK_ATTACHED_INSTANCES
5c52dd
+
5c52dd
+
5c52dd
+def detach_disk(instance, disk_name):
5c52dd
+  # Python API misses disk-scope argument.
5c52dd
+
5c52dd
+  # Detaching a disk is only possible by using deviceName, which is retrieved
5c52dd
+  # as a disk parameter when listing the instance information
5c52dd
+  request = CONN.instances().get(
5c52dd
+      project=PROJECT, zone=ZONE, instance=instance)
5c52dd
+  response = request.execute()
5c52dd
+
5c52dd
+  device_name = None
5c52dd
+  for disk in response['disks']:
5c52dd
+    if disk_name in disk['source']:
5c52dd
+      device_name = disk['deviceName']
5c52dd
+      break
5c52dd
+
5c52dd
+  if not device_name:
5c52dd
+    logger.error("Didn't find %(d)s deviceName attached to %(i)s" % {
5c52dd
+        'd': disk_name,
5c52dd
+        'i': instance,
5c52dd
+    })
5c52dd
+    return
5c52dd
+
5c52dd
+  request = CONN.instances().detachDisk(
5c52dd
+      project=PROJECT, zone=ZONE, instance=instance, deviceName=device_name)
5c52dd
+  wait_for_operation(request.execute())
5c52dd
+
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
+        'prefix': prefix,
5c52dd
+        'project': PROJECT,
5c52dd
+        'location': location,
5c52dd
+        'disk': disk_name,
5c52dd
+    },
5c52dd
+  }
5c52dd
+
5c52dd
+  # Customer-Supplied Encryption Key (CSEK)
5c52dd
+  if PARAMETERS['disk_csek_file']:
5c52dd
+    with open(PARAMETERS['disk_csek_file']) as csek_file:
5c52dd
+      body['diskEncryptionKey'] = {
5c52dd
+          'rawKey': csek_file.read(),
5c52dd
+      }
5c52dd
+
5c52dd
+  if PARAMETERS['device_name']:
5c52dd
+    body['deviceName'] = PARAMETERS['device_name']
5c52dd
+
5c52dd
+  if PARAMETERS['mode']:
5c52dd
+    body['mode'] = PARAMETERS['mode']
5c52dd
+
5c52dd
+  force_attach = None
5c52dd
+  if PARAMETERS['disk_scope'] == 'regional':
5c52dd
+    # Python API misses disk-scope argument.
5c52dd
+    force_attach = True
5c52dd
+  else:
5c52dd
+    # If this disk is attached to some instance, detach it first.
5c52dd
+    for other_instance in LIST_DISK_ATTACHED_INSTANCES:
5c52dd
+      logger.info("Detaching disk %(disk_name)s from other instance %(i)s" % {
5c52dd
+          'disk_name': PARAMETERS['disk_name'],
5c52dd
+          'i': other_instance,
5c52dd
+      })
5c52dd
+      detach_disk(other_instance, PARAMETERS['disk_name'])
5c52dd
+
5c52dd
+  request = CONN.instances().attachDisk(
5c52dd
+      project=PROJECT, zone=ZONE, instance=instance, body=body,
5c52dd
+      forceAttach=force_attach)
5c52dd
+  wait_for_operation(request.execute())
5c52dd
+
5c52dd
+
5c52dd
+def fetch_data():
5c52dd
+  configure_logs()
5c52dd
+  populate_vars()
5c52dd
+
5c52dd
+
5c52dd
+def gcp_pd_move_start():
5c52dd
+  fetch_data()
5c52dd
+  if not is_disk_attached(INSTANCE_NAME):
5c52dd
+    logger.info("Attaching disk %(disk_name)s to %(instance)s" % {
5c52dd
+        'disk_name': PARAMETERS['disk_name'],
5c52dd
+        'instance': INSTANCE_NAME,
5c52dd
+    })
5c52dd
+    attach_disk(INSTANCE_NAME, PARAMETERS['disk_name'])
5c52dd
+
5c52dd
+
5c52dd
+def gcp_pd_move_stop():
5c52dd
+  fetch_data()
5c52dd
+  if is_disk_attached(INSTANCE_NAME):
5c52dd
+    logger.info("Detaching disk %(disk_name)s to %(instance)s" % {
5c52dd
+        'disk_name': PARAMETERS['disk_name'],
5c52dd
+        'instance': INSTANCE_NAME,
5c52dd
+    })
5c52dd
+    detach_disk(INSTANCE_NAME, PARAMETERS['disk_name'])
5c52dd
+
5c52dd
+
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
+        'disk_name': PARAMETERS['disk_name'],
5c52dd
+        'instance': INSTANCE_NAME,
5c52dd
+    })
5c52dd
+  else:
5c52dd
+    sys.exit(ocf.OCF_NOT_RUNNING)
5c52dd
+
5c52dd
+
5c52dd
+def main():
5c52dd
+  if len(sys.argv) < 2:
5c52dd
+    logger.error('Missing argument')
5c52dd
+    return
5c52dd
+
5c52dd
+  command = sys.argv[1]
5c52dd
+  if 'meta-data' in command:
5c52dd
+    print(METADATA)
5c52dd
+    return
5c52dd
+
5c52dd
+  if command in 'start':
5c52dd
+    gcp_pd_move_start()
5c52dd
+  elif command in 'stop':
5c52dd
+    gcp_pd_move_stop()
5c52dd
+  elif command in ('monitor', 'status'):
5c52dd
+    gcp_pd_move_status()
5c52dd
+  else:
5c52dd
+    configure_logs()
5c52dd
+    logger.error('no such function %s' % str(command))
5c52dd
+
5c52dd
+
5c52dd
+if __name__ == "__main__":
5c52dd
+  main()