Blame SOURCES/aliyun-vpc-move-ip-1.patch

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