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