|
|
b94b9d |
From e45d0ca9ccc3d5fbe94372f40bedb7559dc9530a Mon Sep 17 00:00:00 2001
|
|
|
b94b9d |
From: "feng.changf1" <feng.changf1@alibaba-inc.com>
|
|
|
b94b9d |
Date: Tue, 24 Jul 2018 15:08:45 +0800
|
|
|
b94b9d |
Subject: [PATCH] Add Aliyun vpc-move-ip agent.
|
|
|
b94b9d |
|
|
|
b94b9d |
---
|
|
|
b94b9d |
heartbeat/aliyun-vpc-move-ip | 258 +++++++++++++++++++++++++++++++++++++++++++
|
|
|
b94b9d |
1 file changed, 258 insertions(+)
|
|
|
b94b9d |
create mode 100644 heartbeat/aliyun-vpc-move-ip
|
|
|
b94b9d |
|
|
|
b94b9d |
diff --git a/heartbeat/aliyun-vpc-move-ip b/heartbeat/aliyun-vpc-move-ip
|
|
|
b94b9d |
new file mode 100644
|
|
|
b94b9d |
index 000000000..bc97822a8
|
|
|
b94b9d |
--- /dev/null
|
|
|
b94b9d |
+++ b/heartbeat/aliyun-vpc-move-ip
|
|
|
b94b9d |
@@ -0,0 +1,258 @@
|
|
|
b94b9d |
+#!/bin/bash
|
|
|
b94b9d |
+#
|
|
|
b94b9d |
+# OCF resource agent to move an IP address within a VPC in the Aliyun
|
|
|
b94b9d |
+# Based on code of Markus Guertler (GitHub AWS-VPC-move-IP)
|
|
|
b94b9d |
+# Based on code of Adam Gandelman (GitHub ec2-resource-agents/elasticip)
|
|
|
b94b9d |
+#
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+###############################################################################
|
|
|
b94b9d |
+# For testing purposes delete OCF_ROOT after testing
|
|
|
b94b9d |
+OCF_ROOT=/usr/lib/ocf/
|
|
|
b94b9d |
+#
|
|
|
b94b9d |
+# INIT
|
|
|
b94b9d |
+#: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
|
|
|
b94b9d |
+#if [ -f ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs ]; then
|
|
|
b94b9d |
+# . ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
|
|
|
b94b9d |
+#fi
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+#######################################################################
|
|
|
b94b9d |
+# Initialization:
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+: ${OCF_FUNCTIONS=${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs}
|
|
|
b94b9d |
+. ${OCF_FUNCTIONS}
|
|
|
b94b9d |
+: ${__OCF_ACTION=$1}
|
|
|
b94b9d |
+export HOME=/root
|
|
|
b94b9d |
+#######################################################################
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+USAGE="usage: $0 {start|stop|status|meta-data}";
|
|
|
b94b9d |
+###############################################################################
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+###############################################################################
|
|
|
b94b9d |
+#
|
|
|
b94b9d |
+# Functions
|
|
|
b94b9d |
+#
|
|
|
b94b9d |
+###############################################################################
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+metadata() {
|
|
|
b94b9d |
+cat <
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+<resource-agent name="vpc-move-ip">
|
|
|
b94b9d |
+<version>2.0</version>
|
|
|
b94b9d |
+<longdesc lang="en">
|
|
|
b94b9d |
+Resource Agent to move IP addresses within a VPC of the Aliyun Webservices ECS
|
|
|
b94b9d |
+by changing an entry in an specific routing table
|
|
|
b94b9d |
+</longdesc>
|
|
|
b94b9d |
+<shortdesc lang="en">Move IP within a APC of the Aliyun ECS</shortdesc>
|
|
|
b94b9d |
+<parameters>
|
|
|
b94b9d |
+<parameter name="address" required="1">
|
|
|
b94b9d |
+<longdesc lang="en">
|
|
|
b94b9d |
+VPC private IP address
|
|
|
b94b9d |
+</longdesc>
|
|
|
b94b9d |
+<shortdesc lang="en">vpc ip</shortdesc>
|
|
|
b94b9d |
+<content type="string" default="" />
|
|
|
b94b9d |
+</parameter>
|
|
|
b94b9d |
+<parameter name="routing_table" required="1">
|
|
|
b94b9d |
+<longdesc lang="en">
|
|
|
b94b9d |
+Name of the routing table, where the route for the IP address should be changed, i.e. rtb-...
|
|
|
b94b9d |
+</longdesc>
|
|
|
b94b9d |
+<shortdesc lang="en">routing table name</shortdesc>
|
|
|
b94b9d |
+<content type="string" default="" />
|
|
|
b94b9d |
+</parameter>
|
|
|
b94b9d |
+<parameter name="interface" required="1">
|
|
|
b94b9d |
+<longdesc lang="en">
|
|
|
b94b9d |
+Name of the network interfacen, i.e. eth0
|
|
|
b94b9d |
+</longdesc>
|
|
|
b94b9d |
+<shortdesc lang="en">network interface name</shortdesc>
|
|
|
b94b9d |
+<content type="string" default="eth0" />
|
|
|
b94b9d |
+</parameter>
|
|
|
b94b9d |
+<parameter name="profile" required="0">
|
|
|
b94b9d |
+<longdesc lang="en">
|
|
|
b94b9d |
+Valid Aliyun CLI profile name
|
|
|
b94b9d |
+</longdesc>
|
|
|
b94b9d |
+<shortdesc lang="en">profile name</shortdesc>
|
|
|
b94b9d |
+<content type="string" default="default" />
|
|
|
b94b9d |
+</parameter>
|
|
|
b94b9d |
+</parameters>
|
|
|
b94b9d |
+<actions>
|
|
|
b94b9d |
+<action name="start" timeout="180" />
|
|
|
b94b9d |
+<action name="stop" timeout="180" />
|
|
|
b94b9d |
+<action name="monitor" depth="0" timeout="30" interval="30" />
|
|
|
b94b9d |
+<action name="validate-all" timeout="5" />
|
|
|
b94b9d |
+<action name="meta-data" timeout="5" />
|
|
|
b94b9d |
+</actions>
|
|
|
b94b9d |
+</resource-agent>
|
|
|
b94b9d |
+END
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+debugger() {
|
|
|
b94b9d |
+ ocf_log info "DEBUG: $1"
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ecs_ip_validate() {
|
|
|
b94b9d |
+ debugger "function: validate"
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ # IP address
|
|
|
b94b9d |
+ [[ -z "$OCF_RESKEY_address" ]] && ocf_log error "IP address parameter not set $OCF_RESKEY_ADDRESS!" && exit $OCF_ERR_CONFIGURED
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ # Network Interface
|
|
|
b94b9d |
+ [[ -z "$OCF_RESKEY_interface" ]] && ocf_log error "Network interface parameter not set $OCF_RESKEY_INTERFACE!" && exit $OCF_ERR_CONFIGURED
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ # Routing Table
|
|
|
b94b9d |
+ [[ -z "$OCF_RESKEY_routing_table" ]] && ocf_log error "Routing table parameter not set $OCF_RESKEY_ROUTING_TABLE!" && exit $OCF_ERR_CONFIGURED
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ ECS_INSTANCE_ID="$(curl -s http://100.100.100.200/latest/meta-data/instance-id)"
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ if [ -z "${ECS_INSTANCE_ID}" ]; then
|
|
|
b94b9d |
+ ocf_exit_reason "Instance ID not found. Is this a ECS instance?"
|
|
|
b94b9d |
+ return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+ fi
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ return $OCF_SUCCESS
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ecs_ip_monitor() {
|
|
|
b94b9d |
+ ecs_ip_validate
|
|
|
b94b9d |
+ debugger "function: ecsip_monitor: check routing table"
|
|
|
b94b9d |
+ cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
|
|
|
b94b9d |
+ if [ -z "$ROUTE_TO_INSTANCE" ]; then
|
|
|
b94b9d |
+ ROUTE_TO_INSTANCE="<unknown>"
|
|
|
b94b9d |
+ fi
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ [[ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]] && debugger "not routed to this instance ($ECS_INSTANCE_ID) but to instance $ROUTE_TO_INSTANCE" && return $OCF_NOT_RUNNING
|
|
|
b94b9d |
+ cmd="ping -W 1 -c 1 $OCF_RESKEY_address"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ $cmd > /dev/null
|
|
|
b94b9d |
+ [[ $? -gt 0 ]] && debugger "IP $OCF_RESKEY_address not locally reachable via ping on this system" && return $OCF_NOT_RUNNING
|
|
|
b94b9d |
+ debugger "routed in VPC and locally reachable"
|
|
|
b94b9d |
+ return $OCF_SUCCESS
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ecs_ip_drop() {
|
|
|
b94b9d |
+ debugger "function: ecsip_drop"
|
|
|
b94b9d |
+ cmd="ip addr delete ${OCF_RESKEY_address}/32 dev $OCF_RESKEY_interface"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ $cmd
|
|
|
b94b9d |
+ rc=$?
|
|
|
b94b9d |
+ [[ $rc -gt 2 ]] && debugger "command failed, rc $rc" && return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+ debugger "command succeeded"
|
|
|
b94b9d |
+ return $OCF_SUCCESS
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+wait_for_deleted() {
|
|
|
b94b9d |
+ while [ ! -z "$ROUTE_TO_INSTANCE" ]; do
|
|
|
b94b9d |
+ sleep 1
|
|
|
b94b9d |
+ cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
|
|
|
b94b9d |
+ done
|
|
|
b94b9d |
+ sleep 5
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+wait_for_started() {
|
|
|
b94b9d |
+ cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ while [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; do
|
|
|
b94b9d |
+ sleep 1
|
|
|
b94b9d |
+ cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
|
|
|
b94b9d |
+ done
|
|
|
b94b9d |
+ sleep 5
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ecs_ip_get_and_configure() {
|
|
|
b94b9d |
+ debugger "function: ecsip_get_and_configure"
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ if [ $ROUTE_TO_INSTANCE != "<unknown>" ]; then
|
|
|
b94b9d |
+ # Adjusting the routing table
|
|
|
b94b9d |
+ cmd="aliyuncli vpc DeleteRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ROUTE_TO_INSTANCE --output text"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ $cmd
|
|
|
b94b9d |
+ rc=$?
|
|
|
b94b9d |
+ [[ $rc != 0 ]] && debugger "command failed, rc: $rc" && return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+ #wait_for_deleted
|
|
|
b94b9d |
+ sleep 3
|
|
|
b94b9d |
+ fi
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ cmd="aliyuncli vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ $cmd
|
|
|
b94b9d |
+ rc=$?
|
|
|
b94b9d |
+ #[[ $rc != 0 ]] && debugger "command failed, rc: $rc" && return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+ while [ $rc != 0 ]; do
|
|
|
b94b9d |
+ sleep 2
|
|
|
b94b9d |
+ cmd="aliyuncli vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
|
|
|
b94b9d |
+ debugger "executing command: $cmd"
|
|
|
b94b9d |
+ $cmd
|
|
|
b94b9d |
+ rc=$?
|
|
|
b94b9d |
+ done
|
|
|
b94b9d |
+ wait_for_started
|
|
|
b94b9d |
+ fi
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ # Reconfigure the local ip address
|
|
|
b94b9d |
+ ecs_ip_drop
|
|
|
b94b9d |
+ ip addr add "${OCF_RESKEY_address}/32" dev $OCF_RESKEY_interface
|
|
|
b94b9d |
+ rc=$?
|
|
|
b94b9d |
+ [[ $rc != 0 ]] && debugger "command failed, rc: $rc" && return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+ debugger "-success"
|
|
|
b94b9d |
+ return $OCF_SUCCESS
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ecs_ip_stop() {
|
|
|
b94b9d |
+ ocf_log info "ECS: Bringing down IP address $OCF_RESKEY_address"
|
|
|
b94b9d |
+ ecs_ip_validate
|
|
|
b94b9d |
+ ecs_ip_monitor
|
|
|
b94b9d |
+ [[ $? == $OCF_NOT_RUNNING ]] && ocf_log info "ECS: Address $OCF_RESKEY_address already down" && return $OCF_SUCCESS
|
|
|
b94b9d |
+ ecs_ip_drop
|
|
|
b94b9d |
+ [[ $? != $OCF_SUCCESS ]] && return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+ ecs_ip_monitor
|
|
|
b94b9d |
+ [[ $? == $OCF_NOT_RUNNING ]] && ocf_log info "ECS: Successfully brought down $OCF_RESKEY_address" && return $OCF_SUCCESS
|
|
|
b94b9d |
+ ocf_log error "ECS: Couldn't bring down IP address $OCF_RESKEY_address on interface $OCF_RESKEY_interface."
|
|
|
b94b9d |
+ return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+ecs_ip_start() {
|
|
|
b94b9d |
+ ocf_log info "ECS: Moving IP address $OCF_RESKEY_address to this host by adjusting routing table $OCF_RESKEY_routing_table"
|
|
|
b94b9d |
+ ecs_ip_validate
|
|
|
b94b9d |
+ ecs_ip_monitor
|
|
|
b94b9d |
+ [[ $? == $OCF_SUCCESS ]] && ocf_log info "ECS: $OCF_RESKEY_address already started" && return $OCF_SUCCESS
|
|
|
b94b9d |
+ ocf_log info "ECS: Adjusting routing table and locally configuring IP address"
|
|
|
b94b9d |
+ ecs_ip_get_and_configure
|
|
|
b94b9d |
+ [[ $? != 0 ]] && ocf_log error "Received $? from 'aliyun cli'" && return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+ return $OCF_SUCCESS
|
|
|
b94b9d |
+ ecs_ip_monitor
|
|
|
b94b9d |
+ [[ $? == $OCF_SUCCESS ]] && return $?
|
|
|
b94b9d |
+ ocf_log error "ECS: IP address couldn't be configured on this host (IP: $OCF_RESKEY_address, Interface: $OCF_RESKEY_interface)"
|
|
|
b94b9d |
+ return $OCF_ERR_GENERIC
|
|
|
b94b9d |
+}
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+###############################################################################
|
|
|
b94b9d |
+#
|
|
|
b94b9d |
+# MAIN
|
|
|
b94b9d |
+#
|
|
|
b94b9d |
+###############################################################################
|
|
|
b94b9d |
+
|
|
|
b94b9d |
+case $__OCF_ACTION in
|
|
|
b94b9d |
+ meta-data) metadata
|
|
|
b94b9d |
+ exit $OCF_SUCCESS;;
|
|
|
b94b9d |
+ monitor)
|
|
|
b94b9d |
+ ecs_ip_monitor;;
|
|
|
b94b9d |
+ stop)
|
|
|
b94b9d |
+ ecs_ip_stop;;
|
|
|
b94b9d |
+ validate-all) ecs_ip_validate;;
|
|
|
b94b9d |
+ start)
|
|
|
b94b9d |
+ ecs_ip_start;;
|
|
|
b94b9d |
+ *) exit $OCF_ERR_UNIMPLEMENTED;;
|
|
|
b94b9d |
+esac
|
|
|
b94b9d |
\ No newline at end of file
|