Blame SOURCES/bz1568589-2-aliyun-vpc-move-ip-fixes.patch

391384
From db3df55a6f7097e1da7d77eb361e9e7560f13353 Mon Sep 17 00:00:00 2001
391384
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
391384
Date: Tue, 24 Jul 2018 13:57:08 +0200
391384
Subject: [PATCH] aliyun-vpc-move-ip: fixes
391384
391384
---
391384
 doc/man/Makefile.am          |   1 +
391384
 heartbeat/Makefile.am        |   1 +
391384
 heartbeat/aliyun-vpc-move-ip | 336 ++++++++++++++++++++++++-------------------
391384
 3 files changed, 189 insertions(+), 149 deletions(-)
391384
 mode change 100644 => 100755 heartbeat/aliyun-vpc-move-ip
391384
391384
diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am
391384
index 3ac0569de..fc9a67161 100644
391384
--- a/doc/man/Makefile.am
391384
+++ b/doc/man/Makefile.am
391384
@@ -93,6 +93,7 @@ man_MANS	       = ocf_heartbeat_AoEtarget.7 \
391384
                           ocf_heartbeat_WinPopup.7 \
391384
                           ocf_heartbeat_Xen.7 \
391384
                           ocf_heartbeat_Xinetd.7 \
391384
+                          ocf_heartbeat_aliyun-vpc-move-ip.7 \
391384
                           ocf_heartbeat_anything.7 \
391384
                           ocf_heartbeat_apache.7 \
391384
                           ocf_heartbeat_asterisk.7 \
391384
diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am
391384
index d4750bf09..6adc6bc3c 100644
391384
--- a/heartbeat/Makefile.am
391384
+++ b/heartbeat/Makefile.am
391384
@@ -90,6 +90,7 @@ ocf_SCRIPTS	     =  AoEtarget		\
391384
 			Xen			\
391384
 			Xinetd			\
391384
 			ZFS			\
391384
+			aliyun-vpc-move-ip	\
391384
 			anything		\
391384
 			apache			\
391384
 			asterisk		\
391384
diff --git a/heartbeat/aliyun-vpc-move-ip b/heartbeat/aliyun-vpc-move-ip
391384
old mode 100644
391384
new mode 100755
391384
index bc97822a8..108feb247
391384
--- a/heartbeat/aliyun-vpc-move-ip
391384
+++ b/heartbeat/aliyun-vpc-move-ip
391384
@@ -1,30 +1,19 @@
391384
-#!/bin/bash
391384
+#!/bin/sh
391384
 #
391384
 # OCF resource agent to move an IP address within a VPC in the Aliyun
391384
 # Based on code of Markus Guertler (GitHub AWS-VPC-move-IP)
391384
 # Based on code of Adam Gandelman (GitHub ec2-resource-agents/elasticip)
391384
 #
391384
 
391384
-###############################################################################
391384
-# For testing purposes delete OCF_ROOT after testing
391384
-OCF_ROOT=/usr/lib/ocf/
391384
-#
391384
-# INIT
391384
-#: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
391384
-#if [ -f ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs ]; then
391384
-#  . ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
391384
-#fi
391384
-
391384
 #######################################################################
391384
 # Initialization:
391384
-
391384
-: ${OCF_FUNCTIONS=${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs}
391384
-. ${OCF_FUNCTIONS}
391384
-: ${__OCF_ACTION=$1}
391384
-export HOME=/root
391384
+: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
391384
+. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
391384
 #######################################################################
391384
 
391384
- 
391384
+# aliyuncli doesnt work without HOME parameter
391384
+export HOME="/root"
391384
+
391384
 USAGE="usage: $0 {start|stop|status|meta-data}";
391384
 ###############################################################################
391384
 
391384
@@ -36,8 +25,96 @@ USAGE="usage: $0 {start|stop|status|meta-data}";
391384
 ###############################################################################
391384
 
391384
 
391384
-metadata() {
391384
-cat <
391384
+
391384
+ip_get_and_configure() {
391384
+	ocf_log debug "function: ip_get_and_configure"
391384
+
391384
+	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
+
391384
+	if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then
391384
+		if [ -n "$ROUTE_TO_INSTANCE" ]; then
391384
+			ip_drop
391384
+		fi
391384
+
391384
+		cmd="aliyuncli vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
391384
+		ocf_log debug "executing command: $cmd"
391384
+		$cmd
391384
+		rc=$?
391384
+		while [ $rc -ne 0 ]; do
391384
+			sleep 1
391384
+			cmd="aliyuncli vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
391384
+			ocf_log debug "executing command: $cmd"
391384
+			$cmd
391384
+			rc=$?
391384
+		done
391384
+		wait_for_started
391384
+	fi
391384
+
391384
+
391384
+	# Reconfigure the local ip address
391384
+	ip addr add "${OCF_RESKEY_address}/32" dev $OCF_RESKEY_interface
391384
+	rc=$?
391384
+	if [ $rc -ne 0 ]; then
391384
+		ocf_log err "command failed, rc: $rc"
391384
+		return $OCF_ERR_GENERIC
391384
+	fi
391384
+
391384
+	ocf_log debug "IP added"
391384
+
391384
+	return $OCF_SUCCESS
391384
+}
391384
+
391384
+ip_drop() {
391384
+	ocf_log debug "function: ip_drop"
391384
+	cmd="ip addr delete ${OCF_RESKEY_address}/32 dev $OCF_RESKEY_interface"
391384
+	ocf_log debug "executing command: $cmd"
391384
+	$cmd
391384
+	rc=$?
391384
+	if [ $rc -ne 0 ] && [ $rc -ne 2 ]; then
391384
+		ocf_log err "command failed, rc $rc"
391384
+		return $OCF_ERR_GENERIC
391384
+	fi
391384
+
391384
+	cmd="aliyuncli vpc DeleteRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ROUTE_TO_INSTANCE --output text"
391384
+	ocf_log debug "executing command: $cmd"
391384
+	$cmd
391384
+	if [ $? -ne 0 ]; then
391384
+		ocf_log err "command failed, rc: $rc"
391384
+		return $OCF_ERR_GENERIC
391384
+	fi
391384
+	wait_for_deleted
391384
+
391384
+	ocf_log debug "IP dropped"
391384
+
391384
+	return $OCF_SUCCESS
391384
+}
391384
+
391384
+wait_for_started() {
391384
+	cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
+	ocf_log debug "executing command: $cmd"
391384
+	ROUTE_TO_INSTANCE="$($cmd | grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
+
391384
+	while [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; do
391384
+		sleep 3
391384
+		cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
+		ocf_log debug "executing command: $cmd"
391384
+		ROUTE_TO_INSTANCE="$($cmd | grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
+	done
391384
+}
391384
+
391384
+wait_for_deleted() {
391384
+	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
+
391384
+	 while [ ! -z "$ROUTE_TO_INSTANCE" ]; do
391384
+		sleep 1
391384
+		cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
+		ocf_log debug "executing command: $cmd"
391384
+		ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
+	 done
391384
+}
391384
+
391384
+ecs_ip_metadata() {
391384
+	cat <
391384
 
391384
 
391384
 <resource-agent name="vpc-move-ip">
391384
@@ -74,8 +151,8 @@ Name of the network interfacen, i.e. eth0
391384
 Valid Aliyun CLI profile name
391384
 </longdesc>
391384
 <shortdesc lang="en">profile name</shortdesc>
391384
-<content type="string" default="default" /> 
391384
-</parameter> 
391384
+<content type="string" default="default" />
391384
+</parameter>
391384
 </parameters>
391384
 <actions>
391384
 <action name="start" timeout="180" />
391384
@@ -88,171 +165,132 @@ Valid Aliyun CLI profile name
391384
 END
391384
 }
391384
 
391384
-debugger() {
391384
-	ocf_log info "DEBUG: $1"
391384
-}
391384
-
391384
 ecs_ip_validate() {
391384
-	debugger "function: validate"
391384
-	
391384
+	ocf_log debug "function: validate"
391384
+
391384
 	# IP address
391384
-	[[ -z "$OCF_RESKEY_address" ]] && ocf_log error "IP address parameter not set $OCF_RESKEY_ADDRESS!" && exit $OCF_ERR_CONFIGURED
391384
-	
391384
+	if [ -z "$OCF_RESKEY_address" ]; then
391384
+		ocf_log err "IP address parameter not set $OCF_RESKEY_ADDRESS!"
391384
+		exit $OCF_ERR_CONFIGURED
391384
+	fi
391384
+
391384
 	# Network Interface
391384
-	[[ -z "$OCF_RESKEY_interface" ]] && ocf_log error "Network interface parameter not set $OCF_RESKEY_INTERFACE!" && exit $OCF_ERR_CONFIGURED
391384
-	
391384
+	if [ -z "$OCF_RESKEY_interface" ]; then
391384
+		ocf_log err "Network interface parameter not set $OCF_RESKEY_INTERFACE!"
391384
+		exit $OCF_ERR_CONFIGURED
391384
+	fi
391384
+
391384
 	# Routing Table
391384
-	[[ -z "$OCF_RESKEY_routing_table" ]] && ocf_log error "Routing table parameter not set $OCF_RESKEY_ROUTING_TABLE!" && exit $OCF_ERR_CONFIGURED
391384
-	
391384
-	ECS_INSTANCE_ID="$(curl -s http://100.100.100.200/latest/meta-data/instance-id)"
391384
+	if [ -z "$OCF_RESKEY_routing_table" ]; then
391384
+		ocf_log err "Routing table parameter not set $OCF_RESKEY_ROUTING_TABLE!"
391384
+		exit $OCF_ERR_CONFIGURED
391384
+	fi
391384
 
391384
 	if [ -z "${ECS_INSTANCE_ID}" ]; then
391384
 		ocf_exit_reason "Instance ID not found. Is this a ECS instance?"
391384
 		return $OCF_ERR_GENERIC
391384
 	fi
391384
-	
391384
-	return $OCF_SUCCESS
391384
-}
391384
 
391384
-ecs_ip_monitor() {
391384
-	ecs_ip_validate
391384
-	debugger "function: ecsip_monitor: check routing table"
391384
-	cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
-	debugger "executing command: $cmd"
391384
-	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
-	if [ -z "$ROUTE_TO_INSTANCE" ]; then 
391384
-		ROUTE_TO_INSTANCE="<unknown>"
391384
-	fi
391384
-	
391384
-	[[ "$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 
391384
-	cmd="ping -W 1 -c 1 $OCF_RESKEY_address"
391384
-	debugger "executing command: $cmd"
391384
-	$cmd > /dev/null
391384
-	[[ $? -gt 0 ]]  && debugger "IP $OCF_RESKEY_address not locally reachable via ping on this system" && return $OCF_NOT_RUNNING
391384
-	debugger "routed in VPC and locally reachable"
391384
-	return $OCF_SUCCESS	
391384
-}
391384
-
391384
-
391384
-ecs_ip_drop() {
391384
-	debugger "function: ecsip_drop"
391384
-	cmd="ip addr delete ${OCF_RESKEY_address}/32 dev $OCF_RESKEY_interface"
391384
-	debugger "executing command: $cmd"
391384
-	$cmd
391384
-	rc=$?
391384
-	[[ $rc -gt 2 ]] && debugger "command failed, rc $rc" && return $OCF_ERR_GENERIC
391384
-	debugger "command succeeded"
391384
 	return $OCF_SUCCESS
391384
 }
391384
 
391384
-wait_for_deleted() {
391384
-  while [ ! -z "$ROUTE_TO_INSTANCE" ]; do
391384
-		sleep 1
391384
-		cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
-		debugger "executing command: $cmd"
391384
-		ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
-  done
391384
-	sleep 5
391384
-}
391384
+ecs_ip_start() {
391384
+	ocf_log info "ECS: Moving IP address $OCF_RESKEY_address to this host by adjusting routing table $OCF_RESKEY_routing_table"
391384
 
391384
-wait_for_started() {
391384
-	cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
-	debugger "executing command: $cmd"
391384
-	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
-		
391384
-  while [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; do
391384
-		sleep 1
391384
-		cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
-		debugger "executing command: $cmd"
391384
-		ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
-  done
391384
-	sleep 5
391384
-}
391384
+	ecs_ip_monitor
391384
+	if [ $? = $OCF_SUCCESS ]; then
391384
+		ocf_log info "ECS: $OCF_RESKEY_address already started"
391384
+		return $OCF_SUCCESS
391384
+	fi
391384
 
391384
-ecs_ip_get_and_configure() {
391384
-	debugger "function: ecsip_get_and_configure"
391384
-  
391384
- if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then 
391384
- 
391384
-     if [ $ROUTE_TO_INSTANCE != "<unknown>" ]; then
391384
-      # Adjusting the routing table
391384
-        cmd="aliyuncli  vpc DeleteRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ROUTE_TO_INSTANCE --output text"
391384
-        debugger "executing command: $cmd"
391384
-        $cmd
391384
-        rc=$?
391384
-        [[ $rc != 0 ]] && debugger "command failed, rc: $rc" && return $OCF_ERR_GENERIC
391384
-        #wait_for_deleted
391384
-        sleep 3
391384
-      fi
391384
-      
391384
-      cmd="aliyuncli  vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
391384
-      debugger "executing command: $cmd"
391384
-      $cmd
391384
-      rc=$?
391384
-      #[[ $rc != 0 ]] && debugger "command failed, rc: $rc" && return $OCF_ERR_GENERIC
391384
-		  while [ $rc != 0 ]; do
391384
-				sleep 2
391384
-				cmd="aliyuncli  vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
391384
-				debugger "executing command: $cmd"
391384
-				$cmd
391384
-				rc=$?
391384
-			done
391384
-      wait_for_started
391384
+	ocf_log info "ECS: Adjusting routing table and locally configuring IP address"
391384
+	ip_get_and_configure
391384
+	rc=$?
391384
+	if [ $rc -ne 0 ]; then
391384
+		ocf_log err "Received $rc from 'aliyun cli'"
391384
+		return $OCF_ERR_GENERIC
391384
 	fi
391384
-  
391384
-  
391384
-	# Reconfigure the local ip address
391384
-	ecs_ip_drop
391384
-	ip addr add "${OCF_RESKEY_address}/32" dev $OCF_RESKEY_interface
391384
+
391384
+	ecs_ip_monitor
391384
 	rc=$?
391384
-	[[ $rc != 0 ]] && debugger "command failed, rc: $rc" && return $OCF_ERR_GENERIC
391384
-	debugger "-success"
391384
+	if [ $rc -ne $OCF_SUCCESS ]; then
391384
+		ocf_log err "IP address couldn't be configured on this host (IP: $OCF_RESKEY_address, Interface: $OCF_RESKEY_interface)"
391384
+		return $rc
391384
+	fi
391384
+
391384
 	return $OCF_SUCCESS
391384
 }
391384
 
391384
 ecs_ip_stop() {
391384
 	ocf_log info "ECS: Bringing down IP address $OCF_RESKEY_address"
391384
-	ecs_ip_validate 
391384
+
391384
 	ecs_ip_monitor
391384
-	[[ $? == $OCF_NOT_RUNNING ]] && ocf_log info "ECS: Address $OCF_RESKEY_address already down" && return $OCF_SUCCESS
391384
-	ecs_ip_drop
391384
-	[[ $? != $OCF_SUCCESS ]] && return $OCF_ERR_GENERIC
391384
+	if [ $? = $OCF_NOT_RUNNING ]; then
391384
+		ocf_log info "ECS: Address $OCF_RESKEY_address already down"
391384
+		return $OCF_SUCCESS
391384
+	fi
391384
+
391384
+	ip_drop
391384
+	if [ $? -ne $OCF_SUCCESS ]; then
391384
+		ocf_log err "ECS: Couldn't drop IP address $OCF_RESKEY_address on interface $OCF_RESKEY_interface."
391384
+		return $OCF_ERR_GENERIC
391384
+	fi
391384
+
391384
 	ecs_ip_monitor
391384
-	[[ $? == $OCF_NOT_RUNNING ]] && ocf_log info "ECS: Successfully brought down $OCF_RESKEY_address" && return $OCF_SUCCESS
391384
-	ocf_log error "ECS: Couldn't bring down IP address $OCF_RESKEY_address on interface $OCF_RESKEY_interface." 
391384
+	if [ $? = $OCF_NOT_RUNNING ]; then
391384
+		ocf_log info "ECS: Successfully brought down $OCF_RESKEY_address"
391384
+		return $OCF_SUCCESS
391384
+	fi
391384
+
391384
+	ocf_log err "ECS: Couldn't bring down IP address $OCF_RESKEY_address on interface $OCF_RESKEY_interface."
391384
 	return $OCF_ERR_GENERIC
391384
 }
391384
 
391384
-ecs_ip_start() {
391384
-	ocf_log info "ECS: Moving IP address $OCF_RESKEY_address to this host by adjusting routing table $OCF_RESKEY_routing_table"
391384
-	ecs_ip_validate
391384
-	ecs_ip_monitor
391384
-	[[ $? == $OCF_SUCCESS ]] && ocf_log info "ECS: $OCF_RESKEY_address already started" && return $OCF_SUCCESS
391384
-	ocf_log info "ECS: Adjusting routing table and locally configuring IP address"
391384
-	ecs_ip_get_and_configure 
391384
-	[[ $? != 0 ]] && ocf_log error "Received $? from 'aliyun cli'" && return $OCF_ERR_GENERIC
391384
-  return $OCF_SUCCESS
391384
-	ecs_ip_monitor
391384
-	[[ $? == $OCF_SUCCESS ]] &&  return $?
391384
-	ocf_log error "ECS: IP address couldn't be configured on this host (IP: $OCF_RESKEY_address, Interface: $OCF_RESKEY_interface)"
391384
-	return $OCF_ERR_GENERIC
391384
+ecs_ip_monitor() {
391384
+	ocf_log debug "function: ecsip_monitor: check routing table"
391384
+	cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
391384
+	ocf_log debug "executing command: $cmd"
391384
+
391384
+	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
391384
+
391384
+	if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then
391384
+		ocf_log debug "not routed to this instance ($ECS_INSTANCE_ID) but to instance $ROUTE_TO_INSTANCE"
391384
+		return $OCF_NOT_RUNNING
391384
+	fi
391384
+
391384
+	cmd="ping -W 1 -c 1 $OCF_RESKEY_address"
391384
+	ocf_log debug "executing command: $cmd"
391384
+	$cmd > /dev/null
391384
+	if [ $? -ne 0 ]; then
391384
+		ocf_log debug "IP $OCF_RESKEY_address not locally reachable via ping on this system"
391384
+		return $OCF_NOT_RUNNING
391384
+	fi
391384
+	ocf_log debug "routed in VPC and locally reachable"
391384
+	return $OCF_SUCCESS
391384
 }
391384
 
391384
+
391384
 ###############################################################################
391384
 #
391384
 # MAIN
391384
 #
391384
 ###############################################################################
391384
 
391384
-case $__OCF_ACTION in 
391384
-	meta-data) metadata
391384
+case $__OCF_ACTION in
391384
+	meta-data) ecs_ip_metadata
391384
 		   exit $OCF_SUCCESS;;
391384
-	monitor)
391384
-		ecs_ip_monitor;;
391384
-	stop)
391384
-		ecs_ip_stop;;
391384
 	validate-all) ecs_ip_validate;;
391384
+esac
391384
+
391384
+ECS_INSTANCE_ID="$(curl -s http://100.100.100.200/latest/meta-data/instance-id)"
391384
+
391384
+case $__OCF_ACTION in
391384
 	start)
391384
+		ecs_ip_validate
391384
 		ecs_ip_start;;
391384
+	stop)
391384
+		ecs_ip_stop;;
391384
+	monitor)
391384
+		ecs_ip_monitor;;
391384
 	*)	exit $OCF_ERR_UNIMPLEMENTED;;
391384
-esac
391384
\ No newline at end of file
391384
+esac