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

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