Blame SOURCES/4-gcp-vpc-move-route.patch

b4b3ce
From 0ee4c62105ee8f90a43fe0bf8a65bc9b9da2e7e0 Mon Sep 17 00:00:00 2001
b4b3ce
From: Helen Koike <helen.koike@collabora.com>
b4b3ce
Date: Wed, 18 Jul 2018 11:54:40 -0300
b4b3ce
Subject: [PATCH 1/4] gcp-vpc-move-route.in: python implementation of
b4b3ce
 gcp-vpc-move-ip.in
b4b3ce
b4b3ce
gcloud api is not reliable and it is slow, add a python version of
b4b3ce
gcp-vpc-move-ip.in
b4b3ce
---
b4b3ce
 configure.ac                    |   1 +
b4b3ce
 doc/man/Makefile.am             |   1 +
b4b3ce
 heartbeat/Makefile.am           |   1 +
b4b3ce
 heartbeat/gcp-vpc-move-route.in | 441 ++++++++++++++++++++++++++++++++++++++++
b4b3ce
 4 files changed, 444 insertions(+)
b4b3ce
 create mode 100644 heartbeat/gcp-vpc-move-route.in
b4b3ce
b4b3ce
diff --git a/configure.ac b/configure.ac
b4b3ce
index 3d8f9ca74..039b4942c 100644
b4b3ce
--- a/configure.ac
b4b3ce
+++ b/configure.ac
b4b3ce
@@ -960,6 +960,7 @@ 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/gcp-vpc-move-route], [chmod +x heartbeat/gcp-vpc-move-route])
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 e9eaf369f..3ac0569de 100644
b4b3ce
--- a/doc/man/Makefile.am
b4b3ce
+++ b/doc/man/Makefile.am
b4b3ce
@@ -115,6 +115,7 @@ man_MANS	       = ocf_heartbeat_AoEtarget.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_gcp-vpc-move-route.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 36b271956..d4750bf09 100644
b4b3ce
--- a/heartbeat/Makefile.am
b4b3ce
+++ b/heartbeat/Makefile.am
b4b3ce
@@ -112,6 +112,7 @@ ocf_SCRIPTS	     =  AoEtarget		\
b4b3ce
 			garbd			\
b4b3ce
 			gcp-vpc-move-ip		\
b4b3ce
 			gcp-vpc-move-vip	\
b4b3ce
+			gcp-vpc-move-route	\
b4b3ce
 			iSCSILogicalUnit	\
b4b3ce
 			iSCSITarget		\
b4b3ce
 			ids			\
b4b3ce
diff --git a/heartbeat/gcp-vpc-move-route.in b/heartbeat/gcp-vpc-move-route.in
b4b3ce
new file mode 100644
b4b3ce
index 000000000..5f4569baa
b4b3ce
--- /dev/null
b4b3ce
+++ b/heartbeat/gcp-vpc-move-route.in
b4b3ce
@@ -0,0 +1,441 @@
b4b3ce
+#!@PYTHON@ -tt
b4b3ce
+# - *- coding: utf- 8 - *-
b4b3ce
+#
b4b3ce
+#
b4b3ce
+# OCF resource agent to move an IP address within a VPC in GCP
b4b3ce
+#
b4b3ce
+# License: GNU General Public License (GPL)
b4b3ce
+# Copyright (c) 2018 Hervé Werner (MFG Labs)
b4b3ce
+# Copyright 2018 Google Inc.
b4b3ce
+# Based on code from Markus Guertler (aws-vpc-move-ip)
b4b3ce
+# All Rights Reserved.
b4b3ce
+#
b4b3ce
+# This program is free software; you can redistribute it and/or modify
b4b3ce
+# it under the terms of version 2 of the GNU General Public License as
b4b3ce
+# published by the Free Software Foundation.
b4b3ce
+#
b4b3ce
+# This program is distributed in the hope that it would be useful, but
b4b3ce
+# WITHOUT ANY WARRANTY; without even the implied warranty of
b4b3ce
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
b4b3ce
+#
b4b3ce
+# Further, this software is distributed without any warranty that it is
b4b3ce
+# free of the rightful claim of any third person regarding infringement
b4b3ce
+# or the like.  Any license provided herein, whether implied or
b4b3ce
+# otherwise, applies only to this software file.  Patent licenses, if
b4b3ce
+# any, provided herein do not apply to combinations of this program with
b4b3ce
+# other software, or any other product whatsoever.
b4b3ce
+#
b4b3ce
+# You should have received a copy of the GNU General Public License
b4b3ce
+# along with this program; if not, write the Free Software Foundation,
b4b3ce
+# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
b4b3ce
+#
b4b3ce
+
b4b3ce
+
b4b3ce
+#######################################################################
b4b3ce
+
b4b3ce
+import atexit
b4b3ce
+import logging
b4b3ce
+import os
b4b3ce
+import sys
b4b3ce
+import time
b4b3ce
+
b4b3ce
+try:
b4b3ce
+  import googleapiclient.discovery
b4b3ce
+  import pyroute2
b4b3ce
+except ImportError:
b4b3ce
+  pass
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
+OCF_SUCCESS = 0
b4b3ce
+OCF_ERR_GENERIC = 1
b4b3ce
+OCF_ERR_UNIMPLEMENTED = 3
b4b3ce
+OCF_ERR_PERM = 4
b4b3ce
+OCF_ERR_CONFIGURED = 6
b4b3ce
+OCF_NOT_RUNNING = 7
b4b3ce
+GCP_API_URL_PREFIX = 'https://www.googleapis.com/compute/v1'
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-route">
b4b3ce
+<version>1.0</version>
b4b3ce
+<longdesc lang="en">
b4b3ce
+Resource Agent that can move a floating IP addresse within a GCP VPC by changing an
b4b3ce
+entry in the routing table. This agent also configures the floating IP locally
b4b3ce
+on the instance OS.
b4b3ce
+Requirements :
b4b3ce
+- IP forwarding must be enabled on all instances in order to be able to
b4b3ce
+terminate the route
b4b3ce
+- The floating IP address must be choosen so that it is outside all existing
b4b3ce
+subnets in the VPC network
b4b3ce
+- IAM permissions
b4b3ce
+(see https://cloud.google.com/compute/docs/access/iam-permissions) :
b4b3ce
+1) compute.routes.delete, compute.routes.get and compute.routes.update on the
b4b3ce
+route
b4b3ce
+2) compute.networks.updatePolicy on the network (to add a new route)
b4b3ce
+3) compute.networks.get on the network (to check the VPC network existence)
b4b3ce
+4) compute.routes.list on the project (to check conflicting routes)
b4b3ce
+</longdesc>
b4b3ce
+<shortdesc lang="en">Move IP within a GCP VPC</shortdesc>
b4b3ce
+
b4b3ce
+<parameters>
b4b3ce
+
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
+
b4b3ce
+<parameter name="ip" unique="1" required="1">
b4b3ce
+<longdesc lang="en">
b4b3ce
+Floating IP address. Note that this IP must be chosen outside of all existing
b4b3ce
+subnet ranges
b4b3ce
+</longdesc>
b4b3ce
+<shortdesc lang="en">Floating IP</shortdesc>
b4b3ce
+<content type="string" />
b4b3ce
+</parameter>
b4b3ce
+
b4b3ce
+<parameter name="vpc_network" required="1">
b4b3ce
+<longdesc lang="en">
b4b3ce
+Name of the VPC network
b4b3ce
+</longdesc>
b4b3ce
+<shortdesc lang="en">VPC network</shortdesc>
b4b3ce
+<content type="string" default="${OCF_RESKEY_vpc_network_default}" />
b4b3ce
+</parameter>
b4b3ce
+
b4b3ce
+<parameter name="interface">
b4b3ce
+<longdesc lang="en">
b4b3ce
+Name of the network interface
b4b3ce
+</longdesc>
b4b3ce
+<shortdesc lang="en">Network interface name</shortdesc>
b4b3ce
+<content type="string" default="${OCF_RESKEY_interface_default}" />
b4b3ce
+</parameter>
b4b3ce
+
b4b3ce
+<parameter name="route_name" unique="1">
b4b3ce
+<longdesc lang="en">
b4b3ce
+Route name
b4b3ce
+</longdesc>
b4b3ce
+<shortdesc lang="en">Route name</shortdesc>
b4b3ce
+<content type="string" default="${OCF_RESKEY_route_name_default}" />
b4b3ce
+</parameter>
b4b3ce
+</parameters>
b4b3ce
+
b4b3ce
+<actions>
b4b3ce
+<action name="start" timeout="180s" />
b4b3ce
+<action name="stop" timeout="180s" />
b4b3ce
+<action name="monitor" depth="0" timeout="30s" interval="60s" />
b4b3ce
+<action name="validate-all" timeout="5s" />
b4b3ce
+<action name="meta-data" timeout="5s" />
b4b3ce
+</actions>
b4b3ce
+</resource-agent>
b4b3ce
+'''
b4b3ce
+
b4b3ce
+
b4b3ce
+class Context(object):
b4b3ce
+  __slots__ = 'conn', 'iface_idx', 'instance', 'instance_url', 'interface', \
b4b3ce
+      'ip', 'iproute', 'project', 'route_name', 'vpc_network', \
b4b3ce
+      'vpc_network_url', 'zone'
b4b3ce
+
b4b3ce
+
b4b3ce
+def wait_for_operation(ctx, response):
b4b3ce
+  """Blocks until operation completes.
b4b3ce
+  Code from GitHub's GoogleCloudPlatform/python-docs-samples
b4b3ce
+
b4b3ce
+  Args:
b4b3ce
+    response: dict, a request's response
b4b3ce
+  """
b4b3ce
+  def _OperationGetter(response):
b4b3ce
+    operation = response[u'name']
b4b3ce
+    if response.get(u'zone'):
b4b3ce
+      return ctx.conn.zoneOperations().get(
b4b3ce
+          project=ctx.project, zone=ctx.zone, operation=operation)
b4b3ce
+    else:
b4b3ce
+      return ctx.conn.globalOperations().get(
b4b3ce
+          project=ctx.project, operation=operation)
b4b3ce
+
b4b3ce
+  while True:
b4b3ce
+    result = _OperationGetter(response).execute()
b4b3ce
+
b4b3ce
+    if result['status'] == 'DONE':
b4b3ce
+      if 'error' in result:
b4b3ce
+        raise Exception(result['error'])
b4b3ce
+      return result
b4b3ce
+
b4b3ce
+    time.sleep(1)
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 validate(ctx):
b4b3ce
+  if os.geteuid() != 0:
b4b3ce
+    logging.error('You must run this agent as root')
b4b3ce
+    sys.exit(OCF_ERR_PERM)
b4b3ce
+
b4b3ce
+  try:
b4b3ce
+    ctx.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
+  ctx.ip = os.environ.get('OCF_RESKEY_ip')
b4b3ce
+  if not ctx.ip:
b4b3ce
+    logging.error('Missing ip parameter')
b4b3ce
+    sys.exit(OCF_ERR_CONFIGURED)
b4b3ce
+
b4b3ce
+  try:
b4b3ce
+    ctx.instance = get_metadata('instance/name')
b4b3ce
+    ctx.zone = get_metadata('instance/zone').split('/')[-1]
b4b3ce
+    ctx.project = get_metadata('project/project-id')
b4b3ce
+  except Exception as e:
b4b3ce
+    logging.error(
b4b3ce
+        'Instance information not found. Is this a GCE instance ?: %s', str(e))
b4b3ce
+    sys.exit(OCF_ERR_CONFIGURED)
b4b3ce
+
b4b3ce
+  ctx.instance_url = '%s/projects/%s/zones/%s/instances/%s' % (
b4b3ce
+      GCP_API_URL_PREFIX, ctx.project, ctx.zone, ctx.instance)
b4b3ce
+  ctx.vpc_network = os.environ.get('OCF_RESKEY_vpc_network', 'default')
b4b3ce
+  ctx.vpc_network_url = '%s/projects/%s/global/networks/%s' % (
b4b3ce
+      GCP_API_URL_PREFIX, ctx.project, ctx.vpc_network)
b4b3ce
+  ctx.interface = os.environ.get('OCF_RESKEY_interface', 'eth0')
b4b3ce
+  ctx.route_name = os.environ.get(
b4b3ce
+      'OCF_RESKEY_route_name', 'ra-%s' % os.environ['__SCRIPT_NAME'])
b4b3ce
+  ctx.iproute = pyroute2.IPRoute()
b4b3ce
+  atexit.register(ctx.iproute.close)
b4b3ce
+  idxs = ctx.iproute.link_lookup(ifname=ctx.interface)
b4b3ce
+  if not idxs:
b4b3ce
+    logging.error('Network interface not found')
b4b3ce
+    sys.exit(OCF_ERR_CONFIGURED)
b4b3ce
+  ctx.iface_idx = idxs[0]
b4b3ce
+
b4b3ce
+
b4b3ce
+def check_conflicting_routes(ctx):
b4b3ce
+  fl = '(destRange = "%s*") AND (network = "%s") AND (name != "%s")' % (
b4b3ce
+      ctx.ip, ctx.vpc_network_url, ctx.route_name)
b4b3ce
+  request = ctx.conn.routes().list(project=ctx.project, filter=fl)
b4b3ce
+  response = request.execute()
b4b3ce
+  route_list = response.get('items', None)
b4b3ce
+  if route_list:
b4b3ce
+    logging.error(
b4b3ce
+        'Conflicting unnmanaged routes for destination %s/32 in VPC %s found : %s',
b4b3ce
+        ctx.ip, ctx.vpc_network, str(route_list))
b4b3ce
+    sys.exit(OCF_ERR_CONFIGURED)
b4b3ce
+
b4b3ce
+
b4b3ce
+def route_release(ctx):
b4b3ce
+  request = ctx.conn.routes().delete(project=ctx.project, route=ctx.route_name)
b4b3ce
+  wait_for_operation(ctx, request.execute())
b4b3ce
+
b4b3ce
+
b4b3ce
+def ip_monitor(ctx):
b4b3ce
+  logging.info('IP monitor: checking local network configuration')
b4b3ce
+
b4b3ce
+  def address_filter(addr):
b4b3ce
+    for attr in addr['attrs']:
b4b3ce
+      if attr[0] == 'IFA_LOCAL':
b4b3ce
+        if attr[1] == ctx.ip:
b4b3ce
+          return True
b4b3ce
+        else:
b4b3ce
+          return False
b4b3ce
+
b4b3ce
+  route = ctx.iproute.get_addr(
b4b3ce
+      index=ctx.iface_idx, match=address_filter)
b4b3ce
+  if not route:
b4b3ce
+    logging.warn(
b4b3ce
+        'The floating IP %s is not locally configured on this instance (%s)',
b4b3ce
+        ctx.ip, ctx.instance)
b4b3ce
+    return OCF_NOT_RUNNING
b4b3ce
+
b4b3ce
+  logging.debug(
b4b3ce
+      'The floating IP %s is correctly configured on this instance (%s)',
b4b3ce
+      ctx.ip, ctx.instance)
b4b3ce
+  return OCF_SUCCESS
b4b3ce
+
b4b3ce
+
b4b3ce
+def ip_release(ctx):
b4b3ce
+  ctx.iproute.addr('del', index=ctx.iface_idx, address=ctx.ip, mask=32)
b4b3ce
+
b4b3ce
+
b4b3ce
+def ip_and_route_start(ctx):
b4b3ce
+  logging.info('Bringing up the floating IP %s', ctx.ip)
b4b3ce
+
b4b3ce
+  # Add a new entry in the routing table
b4b3ce
+  # If the route entry exists and is pointing to another instance, take it over
b4b3ce
+
b4b3ce
+  # Ensure that there is no route that we are not aware of that is also handling our IP
b4b3ce
+  check_conflicting_routes(ctx)
b4b3ce
+
b4b3ce
+  # There is no replace API, We need to first delete the existing route if any
b4b3ce
+  try:
b4b3ce
+    request = ctx.conn.routes().get(project=ctx.project, route=ctx.route_name)
b4b3ce
+    request.execute()
b4b3ce
+  # TODO: check specific exception for 404
b4b3ce
+  except googleapiclient.errors.HttpError as e:
b4b3ce
+    if e.resp.status != 404:
b4b3ce
+      raise
b4b3ce
+  else:
b4b3ce
+      route_release(ctx)
b4b3ce
+
b4b3ce
+  route_body = {
b4b3ce
+      'name': ctx.route_name,
b4b3ce
+      'network': ctx.vpc_network_url,
b4b3ce
+      'destRange': '%s/32' % ctx.ip,
b4b3ce
+      'nextHopInstance': ctx.instance_url,
b4b3ce
+  }
b4b3ce
+  try:
b4b3ce
+    request = ctx.conn.routes().insert(project=ctx.project, body=route_body)
b4b3ce
+    wait_for_operation(ctx, request.execute())
b4b3ce
+  except googleapiclient.errors.HttpError:
b4b3ce
+    try:
b4b3ce
+      request = ctx.conn.networks().get(
b4b3ce
+          project=ctx.project, network=ctx.vpc_network)
b4b3ce
+      request.execute()
b4b3ce
+    except googleapiclient.errors.HttpError as e:
b4b3ce
+      if e.resp.status == 404:
b4b3ce
+        logging.error('VPC network not found')
b4b3ce
+        sys.exit(OCF_ERR_CONFIGURED)
b4b3ce
+      else:
b4b3ce
+        raise
b4b3ce
+    else:
b4b3ce
+      raise
b4b3ce
+
b4b3ce
+  # Configure the IP address locally
b4b3ce
+  # We need to release the IP first
b4b3ce
+  if ip_monitor(ctx) == OCF_SUCCESS:
b4b3ce
+    ip_release(ctx)
b4b3ce
+
b4b3ce
+  ctx.iproute.addr('add', index=ctx.iface_idx, address=ctx.ip, mask=32)
b4b3ce
+  ctx.iproute.link('set', index=ctx.iface_idx, state='up')
b4b3ce
+  logging.info('Successfully brought up the floating IP %s', ctx.ip)
b4b3ce
+
b4b3ce
+
b4b3ce
+def route_monitor(ctx):
b4b3ce
+  logging.info('GCP route monitor: checking route table')
b4b3ce
+
b4b3ce
+  # Ensure that there is no route that we are not aware of that is also handling our IP
b4b3ce
+  check_conflicting_routes
b4b3ce
+
b4b3ce
+  try:
b4b3ce
+    request = ctx.conn.routes().get(project=ctx.project, route=ctx.route_name)
b4b3ce
+    response = request.execute()
b4b3ce
+  except googleapiclient.errors.HttpError as e:
b4b3ce
+    if 'Insufficient Permission' in e.content:
b4b3ce
+      return OCF_ERR_PERM
b4b3ce
+    elif e.resp.status == 404:
b4b3ce
+      return OCF_NOT_RUNNING
b4b3ce
+    else:
b4b3ce
+      raise
b4b3ce
+
b4b3ce
+  routed_to_instance = response.get('nextHopInstance', '<unknown>')
b4b3ce
+  instance_url = '%s/projects/%s/zones/%s/instances/%s' % (
b4b3ce
+      GCP_API_URL_PREFIX, ctx.project, ctx.zone, ctx.instance)
b4b3ce
+  if routed_to_instance != instance_url:
b4b3ce
+    logging.warn(
b4b3ce
+        'The floating IP %s is not routed to this instance (%s) but to instance %s',
b4b3ce
+        ctx.ip, ctx.instance, routed_to_instance.split('/')[-1])
b4b3ce
+    return OCF_NOT_RUNNING
b4b3ce
+
b4b3ce
+  logging.debug(
b4b3ce
+      'The floating IP %s is correctly routed to this instance (%s)',
b4b3ce
+      ctx.ip, ctx.instance)
b4b3ce
+  return OCF_SUCCESS
b4b3ce
+
b4b3ce
+
b4b3ce
+def ip_and_route_stop(ctx):
b4b3ce
+  logging.info('Bringing down the floating IP %s', ctx.ip)
b4b3ce
+
b4b3ce
+  # Delete the route entry
b4b3ce
+  # If the route entry exists and is pointing to another instance, don't touch it
b4b3ce
+  if route_monitor(ctx) == OCF_NOT_RUNNING:
b4b3ce
+    logging.info(
b4b3ce
+        'The floating IP %s is already not routed to this instance (%s)',
b4b3ce
+        ctx.ip, ctx.instance)
b4b3ce
+  else:
b4b3ce
+    route_release(ctx)
b4b3ce
+
b4b3ce
+  if ip_monitor(ctx) == OCF_NOT_RUNNING:
b4b3ce
+    logging.info('The floating IP %s is already down', ctx.ip)
b4b3ce
+  else:
b4b3ce
+    ip_release(ctx)
b4b3ce
+
b4b3ce
+
b4b3ce
+def configure_logs(ctx):
b4b3ce
+  # Prepare logging
b4b3ce
+  logging.basicConfig(
b4b3ce
+      format='gcp:route - %(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=ctx.instance)
b4b3ce
+        handler.setLevel(logging.INFO)
b4b3ce
+        formatter = logging.Formatter('gcp:route "%(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
+  if 'meta-data' in sys.argv[1]:
b4b3ce
+    print(METADATA)
b4b3ce
+    return
b4b3ce
+
b4b3ce
+  ctx = Context()
b4b3ce
+
b4b3ce
+  validate(ctx)
b4b3ce
+  if 'validate-all' in sys.argv[1]:
b4b3ce
+    return
b4b3ce
+
b4b3ce
+  configure_logs(ctx)
b4b3ce
+  if 'start' in sys.argv[1]:
b4b3ce
+    ip_and_route_start(ctx)
b4b3ce
+  elif 'stop' in sys.argv[1]:
b4b3ce
+    ip_and_route_stop(ctx)
b4b3ce
+  elif 'status' in sys.argv[1] or 'monitor' in sys.argv[1]:
b4b3ce
+    sys.exit(ip_monitor(ctx))
b4b3ce
+  else:
b4b3ce
+    usage = 'usage: $0 {start|stop|monitor|status|meta-data|validate-all}'
b4b3ce
+    logging.error(usage)
b4b3ce
+    sys.exit(OCF_ERR_UNIMPLEMENTED)
b4b3ce
+
b4b3ce
+
b4b3ce
+if __name__ == "__main__":
b4b3ce
+  main()
b4b3ce
b4b3ce
From 6590c99f462403808854114ec1031755e5ce6b36 Mon Sep 17 00:00:00 2001
b4b3ce
From: Helen Koike <helen.koike@collabora.com>
b4b3ce
Date: Thu, 19 Jul 2018 12:33:44 -0300
b4b3ce
Subject: [PATCH 2/4] gcp-vpc-move-ip.in: add deprecation message
b4b3ce
b4b3ce
---
b4b3ce
 heartbeat/gcp-vpc-move-ip.in | 2 ++
b4b3ce
 1 file changed, 2 insertions(+)
b4b3ce
b4b3ce
diff --git a/heartbeat/gcp-vpc-move-ip.in b/heartbeat/gcp-vpc-move-ip.in
b4b3ce
index 4a6c343a8..3b8d998b3 100755
b4b3ce
--- a/heartbeat/gcp-vpc-move-ip.in
b4b3ce
+++ b/heartbeat/gcp-vpc-move-ip.in
b4b3ce
@@ -348,6 +348,8 @@ ip_and_route_stop() {
b4b3ce
 #
b4b3ce
 ###############################################################################
b4b3ce
 
b4b3ce
+ocf_log warn "gcp-vpc-move-ip is deprecated, prefer to use gcp-vpc-move-route instead"
b4b3ce
+
b4b3ce
 case $__OCF_ACTION in
b4b3ce
   meta-data)  metadata
b4b3ce
               exit $OCF_SUCCESS
b4b3ce
b4b3ce
From 73608196d21068c6c2d5fb9f77e3d40179c85fee Mon Sep 17 00:00:00 2001
b4b3ce
From: Helen Koike <helen.koike@collabora.com>
b4b3ce
Date: Fri, 20 Jul 2018 08:26:17 -0300
b4b3ce
Subject: [PATCH 3/4] gcp-vpc-move-route.in: move stackdriver parameter
b4b3ce
b4b3ce
Move stackdriver parameter to the bottom of metadata list
b4b3ce
---
b4b3ce
 heartbeat/gcp-vpc-move-route.in | 12 ++++++------
b4b3ce
 1 file changed, 6 insertions(+), 6 deletions(-)
b4b3ce
b4b3ce
diff --git a/heartbeat/gcp-vpc-move-route.in b/heartbeat/gcp-vpc-move-route.in
b4b3ce
index 5f4569baa..8d5bfff36 100644
b4b3ce
--- a/heartbeat/gcp-vpc-move-route.in
b4b3ce
+++ b/heartbeat/gcp-vpc-move-route.in
b4b3ce
@@ -90,12 +90,6 @@ route
b4b3ce
 
b4b3ce
 <parameters>
b4b3ce
 
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
-
b4b3ce
 <parameter name="ip" unique="1" required="1">
b4b3ce
 <longdesc lang="en">
b4b3ce
 Floating IP address. Note that this IP must be chosen outside of all existing
b4b3ce
@@ -128,6 +122,12 @@ Route name
b4b3ce
 <shortdesc lang="en">Route name</shortdesc>
b4b3ce
 <content type="string" default="${OCF_RESKEY_route_name_default}" />
b4b3ce
 </parameter>
b4b3ce
+
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
 </parameters>
b4b3ce
 
b4b3ce
 <actions>
b4b3ce
b4b3ce
From e54565ec69f809b28337c0471ad0a9b26a64f8bf Mon Sep 17 00:00:00 2001
b4b3ce
From: Helen Koike <helen.koike@collabora.com>
b4b3ce
Date: Fri, 20 Jul 2018 08:45:53 -0300
b4b3ce
Subject: [PATCH 4/4] gcp-vpc-move-route.in: minor fixes
b4b3ce
b4b3ce
---
b4b3ce
 heartbeat/gcp-vpc-move-route.in | 13 +++++++------
b4b3ce
 1 file changed, 7 insertions(+), 6 deletions(-)
b4b3ce
b4b3ce
diff --git a/heartbeat/gcp-vpc-move-route.in b/heartbeat/gcp-vpc-move-route.in
b4b3ce
index 8d5bfff36..566a70f86 100644
b4b3ce
--- a/heartbeat/gcp-vpc-move-route.in
b4b3ce
+++ b/heartbeat/gcp-vpc-move-route.in
b4b3ce
@@ -104,7 +104,7 @@ subnet ranges
b4b3ce
 Name of the VPC network
b4b3ce
 </longdesc>
b4b3ce
 <shortdesc lang="en">VPC network</shortdesc>
b4b3ce
-<content type="string" default="${OCF_RESKEY_vpc_network_default}" />
b4b3ce
+<content type="string" default="default" />
b4b3ce
 </parameter>
b4b3ce
 
b4b3ce
 <parameter name="interface">
b4b3ce
@@ -112,7 +112,7 @@ Name of the VPC network
b4b3ce
 Name of the network interface
b4b3ce
 </longdesc>
b4b3ce
 <shortdesc lang="en">Network interface name</shortdesc>
b4b3ce
-<content type="string" default="${OCF_RESKEY_interface_default}" />
b4b3ce
+<content type="string" default="eth0" />
b4b3ce
 </parameter>
b4b3ce
 
b4b3ce
 <parameter name="route_name" unique="1">
b4b3ce
@@ -120,7 +120,7 @@ Name of the network interface
b4b3ce
 Route name
b4b3ce
 </longdesc>
b4b3ce
 <shortdesc lang="en">Route name</shortdesc>
b4b3ce
-<content type="string" default="${OCF_RESKEY_route_name_default}" />
b4b3ce
+<content type="string" default="ra-%s" />
b4b3ce
 </parameter>
b4b3ce
 
b4b3ce
 <parameter name="stackdriver_logging" unique="0" required="0">
b4b3ce
@@ -138,7 +138,7 @@ Route name
b4b3ce
 <action name="meta-data" timeout="5s" />
b4b3ce
 </actions>
b4b3ce
 </resource-agent>
b4b3ce
-'''
b4b3ce
+''' % os.path.basename(sys.argv[0])
b4b3ce
 
b4b3ce
 
b4b3ce
 class Context(object):
b4b3ce
@@ -229,7 +229,7 @@ def validate(ctx):
b4b3ce
       GCP_API_URL_PREFIX, ctx.project, ctx.vpc_network)
b4b3ce
   ctx.interface = os.environ.get('OCF_RESKEY_interface', 'eth0')
b4b3ce
   ctx.route_name = os.environ.get(
b4b3ce
-      'OCF_RESKEY_route_name', 'ra-%s' % os.environ['__SCRIPT_NAME'])
b4b3ce
+      'OCF_RESKEY_route_name', 'ra-%s' % os.path.basename(sys.argv[0]))
b4b3ce
   ctx.iproute = pyroute2.IPRoute()
b4b3ce
   atexit.register(ctx.iproute.close)
b4b3ce
   idxs = ctx.iproute.link_lookup(ifname=ctx.interface)
b4b3ce
@@ -432,7 +432,8 @@ def main():
b4b3ce
   elif 'status' in sys.argv[1] or 'monitor' in sys.argv[1]:
b4b3ce
     sys.exit(ip_monitor(ctx))
b4b3ce
   else:
b4b3ce
-    usage = 'usage: $0 {start|stop|monitor|status|meta-data|validate-all}'
b4b3ce
+    usage = 'usage: %s {start|stop|monitor|status|meta-data|validate-all}' % \
b4b3ce
+        os.path.basename(sys.argv[0])
b4b3ce
     logging.error(usage)
b4b3ce
     sys.exit(OCF_ERR_UNIMPLEMENTED)
b4b3ce