Blame SOURCES/3-gcp-vpc-move-vip.patch

b94b9d
From 92da4155d881e9ac2dce3a51c6953817349d164a Mon Sep 17 00:00:00 2001
b94b9d
From: Helen Koike <helen.koike@collabora.com>
b94b9d
Date: Mon, 25 Jun 2018 11:03:51 -0300
b94b9d
Subject: [PATCH 1/4] gcp-vpc-move-vip.in: manage ip alias
b94b9d
b94b9d
Add a resource agent to manage ip alias in the cluster.
b94b9d
b94b9d
start:
b94b9d
	Check if any machine in hostlist has the alias_ip assigned and
b94b9d
	disassociate it.
b94b9d
	Assign alias_ip to the current machine.
b94b9d
b94b9d
stop:
b94b9d
	Disassociate the alias_ip from the current machine.
b94b9d
b94b9d
status/monitor:
b94b9d
	Check if alias_ip is assigned with the current machine.
b94b9d
b94b9d
---
b94b9d
b94b9d
This is a port to the following bash script to python:
b94b9d
https://storage.googleapis.com/sapdeploy/pacemaker-gcp/alias
b94b9d
b94b9d
The problem with the bash script is the use of gcloud whose command line
b94b9d
API is not stable.
b94b9d
b94b9d
ocf-tester.in results:
b94b9d
b94b9d
	> sudo ./tools/ocf-tester.in -o alias_ip='10.128.1.0/32' -o stackdriver_logging=yes -n gcp-vpc-move-vip.in heartbeat/gcp-vpc-move-vip.in
b94b9d
	Beginning tests for heartbeat/gcp-vpc-move-vip.in...
b94b9d
	./tools/ocf-tester.in: line 226: cd: @datadir@/resource-agents: No such file or directory
b94b9d
	close failed in file object destructor:
b94b9d
	sys.excepthook is missing
b94b9d
	lost sys.stderr
b94b9d
	* rc=1: Your agent produces meta-data which does not conform to ra-api-1.dtd
b94b9d
	Tests failed: heartbeat/gcp-vpc-move-vip.in failed 1 tests
b94b9d
b94b9d
The only test faillig is the meta-data, but all the agents that I tried
b94b9d
also fails on this. If this is a concern, could you please point me out
b94b9d
to a test which succeeds so I can check what I am doing differently?
b94b9d
b94b9d
This commit can also be viewed at:
b94b9d
	https://github.com/collabora-gce/resource-agents/tree/alias
b94b9d
b94b9d
Thanks
b94b9d
---
b94b9d
 configure.ac                  |   1 +
b94b9d
 doc/man/Makefile.am           |   1 +
b94b9d
 heartbeat/Makefile.am         |   1 +
b94b9d
 heartbeat/gcp-vpc-move-vip.in | 299 ++++++++++++++++++++++++++++++++++++++++++
b94b9d
 4 files changed, 302 insertions(+)
b94b9d
 create mode 100755 heartbeat/gcp-vpc-move-vip.in
b94b9d
b94b9d
diff --git a/configure.ac b/configure.ac
b94b9d
index bdf057d33..3d8f9ca74 100644
b94b9d
--- a/configure.ac
b94b9d
+++ b/configure.ac
b94b9d
@@ -959,6 +959,7 @@ AC_CONFIG_FILES([heartbeat/dnsupdate], [chmod +x heartbeat/dnsupdate])
b94b9d
 AC_CONFIG_FILES([heartbeat/eDir88], [chmod +x heartbeat/eDir88])
b94b9d
 AC_CONFIG_FILES([heartbeat/fio], [chmod +x heartbeat/fio])
b94b9d
 AC_CONFIG_FILES([heartbeat/gcp-vpc-move-ip], [chmod +x heartbeat/gcp-vpc-move-ip])
b94b9d
+AC_CONFIG_FILES([heartbeat/gcp-vpc-move-vip], [chmod +x heartbeat/gcp-vpc-move-vip])
b94b9d
 AC_CONFIG_FILES([heartbeat/iSCSILogicalUnit], [chmod +x heartbeat/iSCSILogicalUnit])
b94b9d
 AC_CONFIG_FILES([heartbeat/iSCSITarget], [chmod +x heartbeat/iSCSITarget])
b94b9d
 AC_CONFIG_FILES([heartbeat/jira], [chmod +x heartbeat/jira])
b94b9d
diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am
b94b9d
index c59126d13..e9eaf369f 100644
b94b9d
--- a/doc/man/Makefile.am
b94b9d
+++ b/doc/man/Makefile.am
b94b9d
@@ -114,6 +114,7 @@ man_MANS	       = ocf_heartbeat_AoEtarget.7 \
b94b9d
                           ocf_heartbeat_galera.7 \
b94b9d
                           ocf_heartbeat_garbd.7 \
b94b9d
                           ocf_heartbeat_gcp-vpc-move-ip.7 \
b94b9d
+                          ocf_heartbeat_gcp-vpc-move-vip.7 \
b94b9d
                           ocf_heartbeat_iSCSILogicalUnit.7 \
b94b9d
                           ocf_heartbeat_iSCSITarget.7 \
b94b9d
                           ocf_heartbeat_iface-bridge.7 \
b94b9d
diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am
b94b9d
index 4f5059e27..36b271956 100644
b94b9d
--- a/heartbeat/Makefile.am
b94b9d
+++ b/heartbeat/Makefile.am
b94b9d
@@ -111,6 +111,7 @@ ocf_SCRIPTS	     =  AoEtarget		\
b94b9d
 			galera			\
b94b9d
 			garbd			\
b94b9d
 			gcp-vpc-move-ip		\
b94b9d
+			gcp-vpc-move-vip	\
b94b9d
 			iSCSILogicalUnit	\
b94b9d
 			iSCSITarget		\
b94b9d
 			ids			\
b94b9d
diff --git a/heartbeat/gcp-vpc-move-vip.in b/heartbeat/gcp-vpc-move-vip.in
b94b9d
new file mode 100755
b94b9d
index 000000000..4954e11df
b94b9d
--- /dev/null
b94b9d
+++ b/heartbeat/gcp-vpc-move-vip.in
b94b9d
@@ -0,0 +1,299 @@
b94b9d
+#!/usr/bin/env python
b94b9d
+# ---------------------------------------------------------------------
b94b9d
+# Copyright 2016 Google Inc.
b94b9d
+#
b94b9d
+# Licensed under the Apache License, Version 2.0 (the "License");
b94b9d
+# you may not use this file except in compliance with the License.
b94b9d
+# You may obtain a copy of the License at
b94b9d
+#
b94b9d
+# http://www.apache.org/licenses/LICENSE-2.0
b94b9d
+# Unless required by applicable law or agreed to in writing, software
b94b9d
+# distributed under the License is distributed on an "AS IS" BASIS,
b94b9d
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b94b9d
+# See the License for the specific language governing permissions and
b94b9d
+# limitations under the License.
b94b9d
+# ---------------------------------------------------------------------
b94b9d
+# Description:	Google Cloud Platform - Floating IP Address (Alias)
b94b9d
+# ---------------------------------------------------------------------
b94b9d
+
b94b9d
+import json
b94b9d
+import logging
b94b9d
+import os
b94b9d
+import sys
b94b9d
+import time
b94b9d
+
b94b9d
+import googleapiclient.discovery
b94b9d
+
b94b9d
+if sys.version_info >= (3, 0):
b94b9d
+  # Python 3 imports.
b94b9d
+  import urllib.parse as urlparse
b94b9d
+  import urllib.request as urlrequest
b94b9d
+else:
b94b9d
+  # Python 2 imports.
b94b9d
+  import urllib as urlparse
b94b9d
+  import urllib2 as urlrequest
b94b9d
+
b94b9d
+
b94b9d
+CONN = None
b94b9d
+THIS_VM = None
b94b9d
+OCF_SUCCESS = 0
b94b9d
+OCF_ERR_GENERIC = 1
b94b9d
+OCF_ERR_CONFIGURED = 6
b94b9d
+OCF_NOT_RUNNING = 7
b94b9d
+METADATA_SERVER = 'http://metadata.google.internal/computeMetadata/v1/'
b94b9d
+METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
b94b9d
+METADATA = \
b94b9d
+'''
b94b9d
+
b94b9d
+<resource-agent name="gcp-vpc-move-vip">
b94b9d
+  <version>1.0</version>
b94b9d
+  <longdesc lang="en">Floating IP Address on Google Cloud Platform - Using Alias IP address functionality to attach a secondary IP address to a running instance</longdesc>
b94b9d
+  <shortdesc lang="en">Floating IP Address on Google Cloud Platform</shortdesc>
b94b9d
+  <parameters>
b94b9d
+    <parameter name="hostlist" unique="1" required="1">
b94b9d
+      <longdesc lang="en">List of hosts in the cluster</longdesc>
b94b9d
+      <shortdesc lang="en">Host list</shortdesc>
b94b9d
+      <content type="string" default="" />
b94b9d
+    </parameter>
b94b9d
+    <parameter name="stackdriver-logging" unique="0" required="0">
b94b9d
+      <longdesc lang="en">If enabled (set to true), IP failover logs will be posted to stackdriver logging</longdesc>
b94b9d
+      <shortdesc lang="en">Stackdriver-logging support</shortdesc>
b94b9d
+      <content type="boolean" default="" />
b94b9d
+    </parameter>
b94b9d
+    <parameter name="alias_ip" unique="1" required="1">
b94b9d
+      <longdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</longdesc>
b94b9d
+      <shortdesc lang="en">IP Address to be added including CIDR. E.g 192.168.0.1/32</shortdesc>
b94b9d
+      <content type="string" default="" />
b94b9d
+    </parameter>
b94b9d
+    <parameter name="alias_range_name" unique="1" required="0">
b94b9d
+      <longdesc lang="en">Subnet name for the Alias IP2</longdesc>
b94b9d
+      <shortdesc lang="en">Subnet name for the Alias IP</shortdesc>
b94b9d
+      <content type="string" default="" />
b94b9d
+    </parameter>
b94b9d
+  </parameters>
b94b9d
+  <actions>
b94b9d
+    <action name="start" timeout="300" />
b94b9d
+    <action name="stop" timeout="15" />
b94b9d
+    <action name="monitor" timeout="15" interval="60" depth="0" />
b94b9d
+    <action name="meta-data" timeout="15" />
b94b9d
+  </actions>
b94b9d
+</resource-agent>'''
b94b9d
+
b94b9d
+
b94b9d
+def get_metadata(metadata_key, params=None, timeout=None):
b94b9d
+  """Performs a GET request with the metadata headers.
b94b9d
+
b94b9d
+  Args:
b94b9d
+    metadata_key: string, the metadata to perform a GET request on.
b94b9d
+    params: dictionary, the query parameters in the GET request.
b94b9d
+    timeout: int, timeout in seconds for metadata requests.
b94b9d
+
b94b9d
+  Returns:
b94b9d
+    HTTP response from the GET request.
b94b9d
+
b94b9d
+  Raises:
b94b9d
+    urlerror.HTTPError: raises when the GET request fails.
b94b9d
+  """
b94b9d
+  timeout = timeout or 60
b94b9d
+  metadata_url = os.path.join(METADATA_SERVER, metadata_key)
b94b9d
+  params = urlparse.urlencode(params or {})
b94b9d
+  url = '%s?%s' % (metadata_url, params)
b94b9d
+  request = urlrequest.Request(url, headers=METADATA_HEADERS)
b94b9d
+  request_opener = urlrequest.build_opener(urlrequest.ProxyHandler({}))
b94b9d
+  return request_opener.open(request, timeout=timeout * 1.1).read()
b94b9d
+
b94b9d
+
b94b9d
+def get_instance(project, zone, instance):
b94b9d
+  request = CONN.instances().get(
b94b9d
+      project=project, zone=zone, instance=instance)
b94b9d
+  return request.execute()
b94b9d
+
b94b9d
+
b94b9d
+def get_network_ifaces(project, zone, instance):
b94b9d
+  return get_instance(project, zone, instance)['networkInterfaces']
b94b9d
+
b94b9d
+
b94b9d
+def wait_for_operation(project, zone, operation):
b94b9d
+  while True:
b94b9d
+    result = CONN.zoneOperations().get(
b94b9d
+        project=project,
b94b9d
+        zone=zone,
b94b9d
+        operation=operation['name']).execute()
b94b9d
+
b94b9d
+    if result['status'] == 'DONE':
b94b9d
+      if 'error' in result:
b94b9d
+        raise Exception(result['error'])
b94b9d
+      return
b94b9d
+    time.sleep(1)
b94b9d
+
b94b9d
+
b94b9d
+def set_alias(project, zone, instance, alias, alias_range_name=None):
b94b9d
+  fingerprint = get_network_ifaces(project, zone, instance)[0]['fingerprint']
b94b9d
+  body = {
b94b9d
+      'aliasIpRanges': [],
b94b9d
+      'fingerprint': fingerprint
b94b9d
+  }
b94b9d
+  if alias:
b94b9d
+    obj = {'ipCidrRange': alias}
b94b9d
+    if alias_range_name:
b94b9d
+      obj['subnetworkRangeName'] = alias_range_name
b94b9d
+    body['aliasIpRanges'].append(obj)
b94b9d
+
b94b9d
+  request = CONN.instances().updateNetworkInterface(
b94b9d
+      instance=instance, networkInterface='nic0', project=project, zone=zone,
b94b9d
+      body=body)
b94b9d
+  operation = request.execute()
b94b9d
+  wait_for_operation(project, zone, operation)
b94b9d
+
b94b9d
+
b94b9d
+def get_alias(project, zone, instance):
b94b9d
+  iface = get_network_ifaces(project, zone, instance)
b94b9d
+  try:
b94b9d
+    return iface[0]['aliasIpRanges'][0]['ipCidrRange']
b94b9d
+  except KeyError:
b94b9d
+    return ''
b94b9d
+
b94b9d
+
b94b9d
+def get_localhost_alias():
b94b9d
+  net_iface = get_metadata('instance/network-interfaces', {'recursive': True})
b94b9d
+  net_iface = json.loads(net_iface.decode('utf-8'))
b94b9d
+  try:
b94b9d
+    return net_iface[0]['ipAliases'][0]
b94b9d
+  except (KeyError, IndexError):
b94b9d
+    return ''
b94b9d
+
b94b9d
+
b94b9d
+def get_zone(project, instance):
b94b9d
+  request = CONN.instances().aggregatedList(project=project)
b94b9d
+  while request is not None:
b94b9d
+    response = request.execute()
b94b9d
+    zones = response.get('items', {})
b94b9d
+    for zone in zones.values():
b94b9d
+      for inst in zone.get('instances', []):
b94b9d
+        if inst['name'] == instance:
b94b9d
+          return inst['zone'].split("/")[-1]
b94b9d
+    request = CONN.instances().aggregatedList_next(
b94b9d
+        previous_request=request, previous_response=response)
b94b9d
+  raise Exception("Unable to find instance %s" % (instance))
b94b9d
+
b94b9d
+
b94b9d
+def gcp_alias_start(alias):
b94b9d
+  if not alias:
b94b9d
+    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
+  my_alias = get_localhost_alias()
b94b9d
+  my_zone = get_metadata('instance/zone').split('/')[-1]
b94b9d
+  project = get_metadata('project/project-id')
b94b9d
+
b94b9d
+  # If I already have the IP, exit. If it has an alias IP that isn't the VIP,
b94b9d
+  # then remove it
b94b9d
+  if my_alias == alias:
b94b9d
+    logging.info(
b94b9d
+        '%s already has %s attached. No action required' % (THIS_VM, alias))
b94b9d
+    sys.exit(OCF_SUCCESS)
b94b9d
+  elif my_alias:
b94b9d
+    logging.info('Removing %s from %s' % (my_alias, THIS_VM))
b94b9d
+    set_alias(project, my_zone, THIS_VM, '')
b94b9d
+
b94b9d
+  # Loops through all hosts & remove the alias IP from the host that has it
b94b9d
+  hostlist = os.environ.get('OCF_RESKEY_hostlist', '')
b94b9d
+  hostlist.replace(THIS_VM, '')
b94b9d
+  for host in hostlist.split():
b94b9d
+    host_zone = get_zone(project, host)
b94b9d
+    host_alias = get_alias(project, host_zone, host)
b94b9d
+    if alias == host_alias:
b94b9d
+      logging.info(
b94b9d
+          '%s is attached to %s - Removing all alias IP addresses from %s' %
b94b9d
+          (alias, host, host))
b94b9d
+      set_alias(project, host_zone, host, '')
b94b9d
+      break
b94b9d
+
b94b9d
+  # add alias IP to localhost
b94b9d
+  set_alias(
b94b9d
+      project, my_zone, THIS_VM, alias,
b94b9d
+      os.environ.get('OCF_RESKEY_alias_range_name'))
b94b9d
+
b94b9d
+  # Check the IP has been added
b94b9d
+  my_alias = get_localhost_alias()
b94b9d
+  if alias == my_alias:
b94b9d
+    logging.info('Finished adding %s to %s' % (alias, THIS_VM))
b94b9d
+  elif my_alias:
b94b9d
+    logging.error(
b94b9d
+        'Failed to add IP. %s has an IP attached but it isn\'t %s' %
b94b9d
+        (THIS_VM, alias))
b94b9d
+    sys.exit(OCF_ERR_GENERIC)
b94b9d
+  else:
b94b9d
+    logging.error('Failed to add IP address %s to %s' % (alias, THIS_VM))
b94b9d
+    sys.exit(OCF_ERR_GENERIC)
b94b9d
+
b94b9d
+
b94b9d
+def gcp_alias_stop(alias):
b94b9d
+  if not alias:
b94b9d
+    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
+  my_alias = get_localhost_alias()
b94b9d
+  my_zone = get_metadata('instance/zone').split('/')[-1]
b94b9d
+  project = get_metadata('project/project-id')
b94b9d
+
b94b9d
+  if my_alias == alias:
b94b9d
+    logging.info('Removing %s from %s' % (my_alias, THIS_VM))
b94b9d
+    set_alias(project, my_zone, THIS_VM, '')
b94b9d
+
b94b9d
+
b94b9d
+def gcp_alias_status(alias):
b94b9d
+  if not alias:
b94b9d
+    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
+  my_alias = get_localhost_alias()
b94b9d
+  if alias == my_alias:
b94b9d
+    logging.info('%s has the correct IP address attached' % THIS_VM)
b94b9d
+  else:
b94b9d
+    sys.exit(OCF_NOT_RUNNING)
b94b9d
+
b94b9d
+
b94b9d
+def configure():
b94b9d
+  global CONN
b94b9d
+  global THIS_VM
b94b9d
+
b94b9d
+  # Populate global vars
b94b9d
+  CONN = googleapiclient.discovery.build('compute', 'v1')
b94b9d
+  THIS_VM = get_metadata('instance/name')
b94b9d
+
b94b9d
+  # Prepare logging
b94b9d
+  logging.basicConfig(
b94b9d
+      format='gcp:alias - %(levelname)s - %(message)s', level=logging.INFO)
b94b9d
+  logging.getLogger('googleapiclient').setLevel(logging.WARN)
b94b9d
+  logging_env = os.environ.get('OCF_RESKEY_stackdriver_logging')
b94b9d
+  if logging_env:
b94b9d
+    logging_env = logging_env.lower()
b94b9d
+    if any(x in logging_env for x in ['yes', 'true', 'enabled']):
b94b9d
+      try:
b94b9d
+        import google.cloud.logging.handlers
b94b9d
+        client = google.cloud.logging.Client()
b94b9d
+        handler = google.cloud.logging.handlers.CloudLoggingHandler(
b94b9d
+            client, name=THIS_VM)
b94b9d
+        handler.setLevel(logging.INFO)
b94b9d
+        formatter = logging.Formatter('gcp:alias "%(message)s"')
b94b9d
+        handler.setFormatter(formatter)
b94b9d
+        root_logger = logging.getLogger()
b94b9d
+        root_logger.addHandler(handler)
b94b9d
+      except ImportError:
b94b9d
+        logging.error('Couldn\'t import google.cloud.logging, '
b94b9d
+            'disabling Stackdriver-logging support')
b94b9d
+
b94b9d
+
b94b9d
+def main():
b94b9d
+  configure()
b94b9d
+
b94b9d
+  alias = os.environ.get('OCF_RESKEY_alias_ip')
b94b9d
+  if 'start' in sys.argv[1]:
b94b9d
+    gcp_alias_start(alias)
b94b9d
+  elif 'stop' in sys.argv[1]:
b94b9d
+    gcp_alias_stop(alias)
b94b9d
+  elif 'status' in sys.argv[1] or 'monitor' in sys.argv[1]:
b94b9d
+    gcp_alias_status(alias)
b94b9d
+  elif 'meta-data' in sys.argv[1]:
b94b9d
+    print(METADATA)
b94b9d
+  else:
b94b9d
+    logging.error('gcp:alias - no such function %s' % str(sys.argv[1]))
b94b9d
+
b94b9d
+
b94b9d
+if __name__ == "__main__":
b94b9d
+  main()
b94b9d
b94b9d
From 0e6ba4894a748664ac1d8ff5b9e8c271f0b04d93 Mon Sep 17 00:00:00 2001
b94b9d
From: Helen Koike <helen.koike@collabora.com>
b94b9d
Date: Thu, 12 Jul 2018 09:01:22 -0300
b94b9d
Subject: [PATCH 2/4] gcp-vpc-move-vip.in: minor fixes
b94b9d
b94b9d
- Get hostlist from the project if the parameter is not given
b94b9d
- Verify if alias is present out of each action function
b94b9d
- Don't call configure if 'meta-data' action is given
b94b9d
---
b94b9d
 heartbeat/gcp-vpc-move-vip.in | 40 ++++++++++++++++++++++++++++------------
b94b9d
 1 file changed, 28 insertions(+), 12 deletions(-)
b94b9d
b94b9d
diff --git a/heartbeat/gcp-vpc-move-vip.in b/heartbeat/gcp-vpc-move-vip.in
b94b9d
index 4954e11df..f3d117bda 100755
b94b9d
--- a/heartbeat/gcp-vpc-move-vip.in
b94b9d
+++ b/heartbeat/gcp-vpc-move-vip.in
b94b9d
@@ -50,7 +50,7 @@ METADATA = \
b94b9d
   <longdesc lang="en">Floating IP Address on Google Cloud Platform - Using Alias IP address functionality to attach a secondary IP address to a running instance</longdesc>
b94b9d
   <shortdesc lang="en">Floating IP Address on Google Cloud Platform</shortdesc>
b94b9d
   <parameters>
b94b9d
-    <parameter name="hostlist" unique="1" required="1">
b94b9d
+    <parameter name="hostlist" unique="1" required="0">
b94b9d
       <longdesc lang="en">List of hosts in the cluster</longdesc>
b94b9d
       <shortdesc lang="en">Host list</shortdesc>
b94b9d
       <content type="string" default="" />
b94b9d
@@ -177,9 +177,22 @@ def get_zone(project, instance):
b94b9d
   raise Exception("Unable to find instance %s" % (instance))
b94b9d
 
b94b9d
 
b94b9d
+def get_instances_list(project, exclude):
b94b9d
+  hostlist = []
b94b9d
+  request = CONN.instances().aggregatedList(project=project)
b94b9d
+  while request is not None:
b94b9d
+    response = request.execute()
b94b9d
+    zones = response.get('items', {})
b94b9d
+    for zone in zones.values():
b94b9d
+      for inst in zone.get('instances', []):
b94b9d
+        if inst['name'] != exclude:
b94b9d
+          hostlist.append(inst['name'])
b94b9d
+    request = CONN.instances().aggregatedList_next(
b94b9d
+        previous_request=request, previous_response=response)
b94b9d
+  return hostlist
b94b9d
+
b94b9d
+
b94b9d
 def gcp_alias_start(alias):
b94b9d
-  if not alias:
b94b9d
-    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
   my_alias = get_localhost_alias()
b94b9d
   my_zone = get_metadata('instance/zone').split('/')[-1]
b94b9d
   project = get_metadata('project/project-id')
b94b9d
@@ -196,8 +209,11 @@ def gcp_alias_start(alias):
b94b9d
 
b94b9d
   # Loops through all hosts & remove the alias IP from the host that has it
b94b9d
   hostlist = os.environ.get('OCF_RESKEY_hostlist', '')
b94b9d
-  hostlist.replace(THIS_VM, '')
b94b9d
-  for host in hostlist.split():
b94b9d
+  if hostlist:
b94b9d
+    hostlist.replace(THIS_VM, '').split()
b94b9d
+  else:
b94b9d
+    hostlist = get_instances_list(project, THIS_VM)
b94b9d
+  for host in hostlist:
b94b9d
     host_zone = get_zone(project, host)
b94b9d
     host_alias = get_alias(project, host_zone, host)
b94b9d
     if alias == host_alias:
b94b9d
@@ -227,8 +243,6 @@ def gcp_alias_start(alias):
b94b9d
 
b94b9d
 
b94b9d
 def gcp_alias_stop(alias):
b94b9d
-  if not alias:
b94b9d
-    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
   my_alias = get_localhost_alias()
b94b9d
   my_zone = get_metadata('instance/zone').split('/')[-1]
b94b9d
   project = get_metadata('project/project-id')
b94b9d
@@ -239,8 +253,6 @@ def gcp_alias_stop(alias):
b94b9d
 
b94b9d
 
b94b9d
 def gcp_alias_status(alias):
b94b9d
-  if not alias:
b94b9d
-    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
   my_alias = get_localhost_alias()
b94b9d
   if alias == my_alias:
b94b9d
     logging.info('%s has the correct IP address attached' % THIS_VM)
b94b9d
@@ -280,17 +292,21 @@ def configure():
b94b9d
 
b94b9d
 
b94b9d
 def main():
b94b9d
-  configure()
b94b9d
+  if 'meta-data' in sys.argv[1]:
b94b9d
+    print(METADATA)
b94b9d
+    return
b94b9d
 
b94b9d
   alias = os.environ.get('OCF_RESKEY_alias_ip')
b94b9d
+  if not alias:
b94b9d
+    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
+
b94b9d
+  configure()
b94b9d
   if 'start' in sys.argv[1]:
b94b9d
     gcp_alias_start(alias)
b94b9d
   elif 'stop' in sys.argv[1]:
b94b9d
     gcp_alias_stop(alias)
b94b9d
   elif 'status' in sys.argv[1] or 'monitor' in sys.argv[1]:
b94b9d
     gcp_alias_status(alias)
b94b9d
-  elif 'meta-data' in sys.argv[1]:
b94b9d
-    print(METADATA)
b94b9d
   else:
b94b9d
     logging.error('gcp:alias - no such function %s' % str(sys.argv[1]))
b94b9d
 
b94b9d
b94b9d
From 1f50c4bc80f23f561a8630c12076707366525899 Mon Sep 17 00:00:00 2001
b94b9d
From: Helen Koike <helen.koike@collabora.com>
b94b9d
Date: Thu, 12 Jul 2018 13:02:16 -0300
b94b9d
Subject: [PATCH 3/4] gcp-vcp-move-vip.in: implement validate-all
b94b9d
b94b9d
Also fix some return errors
b94b9d
---
b94b9d
 heartbeat/gcp-vpc-move-vip.in | 47 +++++++++++++++++++++++++++++++------------
b94b9d
 1 file changed, 34 insertions(+), 13 deletions(-)
b94b9d
b94b9d
diff --git a/heartbeat/gcp-vpc-move-vip.in b/heartbeat/gcp-vpc-move-vip.in
b94b9d
index f3d117bda..a90c2de8d 100755
b94b9d
--- a/heartbeat/gcp-vpc-move-vip.in
b94b9d
+++ b/heartbeat/gcp-vpc-move-vip.in
b94b9d
@@ -22,7 +22,10 @@ import os
b94b9d
 import sys
b94b9d
 import time
b94b9d
 
b94b9d
-import googleapiclient.discovery
b94b9d
+try:
b94b9d
+  import googleapiclient.discovery
b94b9d
+except ImportError:
b94b9d
+  pass
b94b9d
 
b94b9d
 if sys.version_info >= (3, 0):
b94b9d
   # Python 3 imports.
b94b9d
@@ -36,6 +39,7 @@ else:
b94b9d
 
b94b9d
 CONN = None
b94b9d
 THIS_VM = None
b94b9d
+ALIAS = None
b94b9d
 OCF_SUCCESS = 0
b94b9d
 OCF_ERR_GENERIC = 1
b94b9d
 OCF_ERR_CONFIGURED = 6
b94b9d
@@ -210,7 +214,7 @@ def gcp_alias_start(alias):
b94b9d
   # Loops through all hosts & remove the alias IP from the host that has it
b94b9d
   hostlist = os.environ.get('OCF_RESKEY_hostlist', '')
b94b9d
   if hostlist:
b94b9d
-    hostlist.replace(THIS_VM, '').split()
b94b9d
+    hostlist = hostlist.replace(THIS_VM, '').split()
b94b9d
   else:
b94b9d
     hostlist = get_instances_list(project, THIS_VM)
b94b9d
   for host in hostlist:
b94b9d
@@ -260,14 +264,31 @@ def gcp_alias_status(alias):
b94b9d
     sys.exit(OCF_NOT_RUNNING)
b94b9d
 
b94b9d
 
b94b9d
-def configure():
b94b9d
+def validate():
b94b9d
+  global ALIAS
b94b9d
   global CONN
b94b9d
   global THIS_VM
b94b9d
 
b94b9d
   # Populate global vars
b94b9d
-  CONN = googleapiclient.discovery.build('compute', 'v1')
b94b9d
-  THIS_VM = get_metadata('instance/name')
b94b9d
+  try:
b94b9d
+    CONN = googleapiclient.discovery.build('compute', 'v1')
b94b9d
+  except Exception as e:
b94b9d
+    logging.error('Couldn\'t connect with google api: ' + str(e))
b94b9d
+    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
+
b94b9d
+  try:
b94b9d
+    THIS_VM = get_metadata('instance/name')
b94b9d
+  except Exception as e:
b94b9d
+    logging.error('Couldn\'t get instance name, is this running inside GCE?: ' + str(e))
b94b9d
+    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
 
b94b9d
+  ALIAS = os.environ.get('OCF_RESKEY_alias_ip')
b94b9d
+  if not ALIAS:
b94b9d
+    logging.error('Missing alias_ip parameter')
b94b9d
+    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
+
b94b9d
+
b94b9d
+def configure_logs():
b94b9d
   # Prepare logging
b94b9d
   logging.basicConfig(
b94b9d
       format='gcp:alias - %(levelname)s - %(message)s', level=logging.INFO)
b94b9d
@@ -296,19 +317,19 @@ def main():
b94b9d
     print(METADATA)
b94b9d
     return
b94b9d
 
b94b9d
-  alias = os.environ.get('OCF_RESKEY_alias_ip')
b94b9d
-  if not alias:
b94b9d
-    sys.exit(OCF_ERR_CONFIGURED)
b94b9d
+  validate()
b94b9d
+  if 'validate-all' in sys.argv[1]:
b94b9d
+    return
b94b9d
 
b94b9d
-  configure()
b94b9d
+  configure_logs()
b94b9d
   if 'start' in sys.argv[1]:
b94b9d
-    gcp_alias_start(alias)
b94b9d
+    gcp_alias_start(ALIAS)
b94b9d
   elif 'stop' in sys.argv[1]:
b94b9d
-    gcp_alias_stop(alias)
b94b9d
+    gcp_alias_stop(ALIAS)
b94b9d
   elif 'status' in sys.argv[1] or 'monitor' in sys.argv[1]:
b94b9d
-    gcp_alias_status(alias)
b94b9d
+    gcp_alias_status(ALIAS)
b94b9d
   else:
b94b9d
-    logging.error('gcp:alias - no such function %s' % str(sys.argv[1]))
b94b9d
+    logging.error('no such function %s' % str(sys.argv[1]))
b94b9d
 
b94b9d
 
b94b9d
 if __name__ == "__main__":
b94b9d
b94b9d
From f11cb236bb348ebee74e962d0ded1cb2fc97bd5f Mon Sep 17 00:00:00 2001
b94b9d
From: Helen Koike <helen.koike@collabora.com>
b94b9d
Date: Fri, 13 Jul 2018 08:01:02 -0300
b94b9d
Subject: [PATCH 4/4] gcp-vpc-move-vip.in: minor fixes
b94b9d
b94b9d
---
b94b9d
 heartbeat/gcp-vpc-move-vip.in | 5 +++--
b94b9d
 1 file changed, 3 insertions(+), 2 deletions(-)
b94b9d
b94b9d
diff --git a/heartbeat/gcp-vpc-move-vip.in b/heartbeat/gcp-vpc-move-vip.in
b94b9d
index a90c2de8d..9fc87242f 100755
b94b9d
--- a/heartbeat/gcp-vpc-move-vip.in
b94b9d
+++ b/heartbeat/gcp-vpc-move-vip.in
b94b9d
@@ -1,4 +1,4 @@
b94b9d
-#!/usr/bin/env python
b94b9d
+#!@PYTHON@ -tt
b94b9d
 # ---------------------------------------------------------------------
b94b9d
 # Copyright 2016 Google Inc.
b94b9d
 #
b94b9d
@@ -59,7 +59,7 @@ METADATA = \
b94b9d
       <shortdesc lang="en">Host list</shortdesc>
b94b9d
       <content type="string" default="" />
b94b9d
     </parameter>
b94b9d
-    <parameter name="stackdriver-logging" unique="0" required="0">
b94b9d
+    <parameter name="stackdriver_logging" unique="0" required="0">
b94b9d
       <longdesc lang="en">If enabled (set to true), IP failover logs will be posted to stackdriver logging</longdesc>
b94b9d
       <shortdesc lang="en">Stackdriver-logging support</shortdesc>
b94b9d
       <content type="boolean" default="" />
b94b9d
@@ -80,6 +80,7 @@ METADATA = \
b94b9d
     <action name="stop" timeout="15" />
b94b9d
     <action name="monitor" timeout="15" interval="60" depth="0" />
b94b9d
     <action name="meta-data" timeout="15" />
b94b9d
+    <action name="validate-all" timeout="15" />
b94b9d
   </actions>
b94b9d
 </resource-agent>'''
b94b9d