Blame SOURCES/bz1843999-aliyun-vpc-move-ip-log-output-when-failing.patch

d42a06
--- a/heartbeat/aliyun-vpc-move-ip	2020-06-09 13:41:35.308379032 +0200
d42a06
+++ b/heartbeat/aliyun-vpc-move-ip	2020-06-05 10:48:45.555132686 +0200
d42a06
@@ -9,12 +9,46 @@
d42a06
 # Initialization:
d42a06
 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
d42a06
 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
d42a06
+
d42a06
+# Parameter defaults
d42a06
+
d42a06
+OCF_RESKEY_address_default=""
d42a06
+OCF_RESKEY_routing_table_default=""
d42a06
+OCF_RESKEY_interface_default="eth0"
d42a06
+OCF_RESKEY_profile_default="default"
d42a06
+OCF_RESKEY_endpoint_default="vpc.aliyuncs.com"
d42a06
+OCF_RESKEY_aliyuncli_default="detect"
d42a06
+
d42a06
+
d42a06
+: ${OCF_RESKEY_address=${OCF_RESKEY_address_default}}
d42a06
+: ${OCF_RESKEY_routing_table=${OCF_RESKEY_routing_table_default}}
d42a06
+: ${OCF_RESKEY_interface=${OCF_RESKEY_interface_default}}
d42a06
+: ${OCF_RESKEY_profile=${OCF_RESKEY_profile_default}}
d42a06
+: ${OCF_RESKEY_endpoint=${OCF_RESKEY_endpoint_default}}
d42a06
+: ${OCF_RESKEY_aliyuncli=${OCF_RESKEY_aliyuncli_default}}
d42a06
+
d42a06
 #######################################################################
d42a06
 
d42a06
-# aliyuncli doesnt work without HOME parameter
d42a06
+# aliyun cli doesnt work without HOME parameter
d42a06
 export HOME="/root"
d42a06
 
d42a06
 USAGE="usage: $0 {start|stop|status|meta-data}";
d42a06
+
d42a06
+if [ "${OCF_RESKEY_aliyuncli}" = "detect" ]; then
d42a06
+	OCF_RESKEY_aliyuncli="$(which aliyuncli 2> /dev/null || which aliyun 2> /dev/null)"
d42a06
+fi
d42a06
+
d42a06
+if [[ "${OCF_RESKEY_aliyuncli##*/}" == 'aliyuncli' ]]; then
d42a06
+	OUTPUT="text"
d42a06
+	EXECUTING='{ print $3 }'
d42a06
+	IFS_=" "
d42a06
+	ENDPOINT=""
d42a06
+elif [[ "${OCF_RESKEY_aliyuncli##*/}" == 'aliyun' ]]; then
d42a06
+	OUTPUT="table cols=InstanceId,DestinationCidrBlock rows=RouteTables.RouteTable[].RouteEntrys.RouteEntry[]"
d42a06
+	EXECUTING='{ gsub (" ", "", $0); print $1 }'
d42a06
+	IFS_="|"
d42a06
+	ENDPOINT="--endpoint $OCF_RESKEY_endpoint"
d42a06
+fi
d42a06
 ###############################################################################
d42a06
 
d42a06
 
d42a06
@@ -24,27 +58,61 @@
d42a06
 #
d42a06
 ###############################################################################
d42a06
 
d42a06
+request_create_route_entry() {
d42a06
+	cmd="${OCF_RESKEY_aliyuncli} vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance ${ENDPOINT}"
d42a06
+	ocf_log debug "executing command: $cmd"
d42a06
+	res=$($cmd  2>&1)
d42a06
+	rc=$?
d42a06
+	if [[ $rc -eq 0 ]]
d42a06
+	then
d42a06
+		ocf_log debug "result: $res; rc: $rc"
d42a06
+	else
d42a06
+		ocf_log err "result: $res; cmd: $cmd; rc: $rc"
d42a06
+	fi
d42a06
+	return $rc
d42a06
+}
d42a06
+
d42a06
+request_delete_route_entry() {
d42a06
+	cmd="${OCF_RESKEY_aliyuncli} vpc DeleteRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ROUTE_TO_INSTANCE ${ENDPOINT}"
d42a06
+	ocf_log debug "executing command: $cmd"
d42a06
+	res=$($cmd)
d42a06
+	rc=$?
d42a06
+	if [[ $rc -eq 0 ]]
d42a06
+	then
d42a06
+		ocf_log debug "result: $res; rc: $rc"
d42a06
+	else
d42a06
+		ocf_log err "result: $res; cmd: $cmd; rc: $rc"
d42a06
+	fi
d42a06
+	return $rc
d42a06
+}
d42a06
 
d42a06
+request_describe_route_tables() {
d42a06
+	cmd="${OCF_RESKEY_aliyuncli} vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output ${OUTPUT} ${ENDPOINT}"
d42a06
+	ocf_log debug "executing command: $cmd"
d42a06
+	res=$($cmd)
d42a06
+	rc=$?
d42a06
+	if [[ $rc -eq 0 ]]
d42a06
+	then
d42a06
+		ROUTE_TO_INSTANCE=$(echo "$res" |grep "\s${OCF_RESKEY_address}/" | awk -F "${IFS_}" "${EXECUTING}")
d42a06
+		ocf_log debug "ROUTE_TO_INSTANCE: $ROUTE_TO_INSTANCE"
d42a06
+	else
d42a06
+		ocf_log err "result: $res; cmd: $cmd; rc: $rc"
d42a06
+	fi
d42a06
+}
d42a06
 
d42a06
 ip_get_and_configure() {
d42a06
 	ocf_log debug "function: ip_get_and_configure"
d42a06
 
d42a06
-	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
d42a06
-
d42a06
+	request_describe_route_tables
d42a06
 	if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then
d42a06
 		if [ -n "$ROUTE_TO_INSTANCE" ]; then
d42a06
 			ip_drop
d42a06
 		fi
d42a06
-
d42a06
-		cmd="aliyuncli vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
d42a06
-		ocf_log debug "executing command: $cmd"
d42a06
-		$cmd
d42a06
+		request_create_route_entry
d42a06
 		rc=$?
d42a06
 		while [ $rc -ne 0 ]; do
d42a06
 			sleep 1
d42a06
-			cmd="aliyuncli vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance --output text"
d42a06
-			ocf_log debug "executing command: $cmd"
d42a06
-			$cmd
d42a06
+			request_create_route_entry
d42a06
 			rc=$?
d42a06
 		done
d42a06
 		wait_for_started
d42a06
@@ -68,17 +136,15 @@
d42a06
 	ocf_log debug "function: ip_drop"
d42a06
 	cmd="ip addr delete ${OCF_RESKEY_address}/32 dev $OCF_RESKEY_interface"
d42a06
 	ocf_log debug "executing command: $cmd"
d42a06
-	$cmd
d42a06
+	res=$($cmd)
d42a06
 	rc=$?
d42a06
 	if [ $rc -ne 0 ] && [ $rc -ne 2 ]; then
d42a06
-		ocf_log err "command failed, rc $rc"
d42a06
+		ocf_log err "command failed, rc: $rc; cmd: $cmd; result: $res"
d42a06
 		return $OCF_ERR_GENERIC
d42a06
 	fi
d42a06
-
d42a06
-	cmd="aliyuncli vpc DeleteRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ROUTE_TO_INSTANCE --output text"
d42a06
-	ocf_log debug "executing command: $cmd"
d42a06
-	$cmd
d42a06
-	if [ $? -ne 0 ]; then
d42a06
+	request_delete_route_entry
d42a06
+	rc=$?
d42a06
+	if [ $rc -ne 0 ]; then
d42a06
 		ocf_log err "command failed, rc: $rc"
d42a06
 		return $OCF_ERR_GENERIC
d42a06
 	fi
d42a06
@@ -90,26 +156,18 @@
d42a06
 }
d42a06
 
d42a06
 wait_for_started() {
d42a06
-	cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
d42a06
-	ocf_log debug "executing command: $cmd"
d42a06
-	ROUTE_TO_INSTANCE="$($cmd | grep $OCF_RESKEY_address | awk '{ print $3 }')"
d42a06
-
d42a06
+	request_describe_route_tables
d42a06
 	while [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; do
d42a06
 		sleep 3
d42a06
-		cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
d42a06
-		ocf_log debug "executing command: $cmd"
d42a06
-		ROUTE_TO_INSTANCE="$($cmd | grep $OCF_RESKEY_address | awk '{ print $3 }')"
d42a06
+		request_describe_route_tables
d42a06
 	done
d42a06
 }
d42a06
 
d42a06
 wait_for_deleted() {
d42a06
-	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
d42a06
-
d42a06
+	request_describe_route_tables
d42a06
 	 while [ ! -z "$ROUTE_TO_INSTANCE" ]; do
d42a06
 		sleep 1
d42a06
-		cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
d42a06
-		ocf_log debug "executing command: $cmd"
d42a06
-		ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
d42a06
+		request_describe_route_tables
d42a06
 	 done
d42a06
 }
d42a06
 
d42a06
@@ -124,38 +182,58 @@
d42a06
 by changing an entry in an specific routing table
d42a06
 </longdesc>
d42a06
 <shortdesc lang="en">Move IP within a VPC of the Aliyun ECS</shortdesc>
d42a06
+
d42a06
 <parameters>
d42a06
+<parameter name="aliyuncli" required="0">
d42a06
+<longdesc lang="en">
d42a06
+Path to command line tools for Aliyun
d42a06
+</longdesc>
d42a06
+<shortdesc lang="en">Path to Aliyun CLI tools</shortdesc>
d42a06
+<content type="string" default="${OCF_RESKEY_aliyuncli_default}" />
d42a06
+</parameter>
d42a06
+
d42a06
 <parameter name="address" required="1">
d42a06
 <longdesc lang="en">
d42a06
 VPC private IP address
d42a06
 </longdesc>
d42a06
 <shortdesc lang="en">vpc ip</shortdesc>
d42a06
-<content type="string" default="" />
d42a06
+<content type="string" default="${OCF_RESKEY_address_default}" />
d42a06
 </parameter>
d42a06
+
d42a06
 <parameter name="routing_table" required="1">
d42a06
 <longdesc lang="en">
d42a06
 Name of the routing table, where the route for the IP address should be changed, i.e. vtb-...
d42a06
 </longdesc>
d42a06
 <shortdesc lang="en">routing table name</shortdesc>
d42a06
-<content type="string" default="" />
d42a06
+<content type="string" default="${OCF_RESKEY_routing_table_default}" />
d42a06
 </parameter>
d42a06
+
d42a06
 <parameter name="interface" required="1">
d42a06
 <longdesc lang="en">
d42a06
 Name of the network interface, i.e. eth0
d42a06
 </longdesc>
d42a06
 <shortdesc lang="en">network interface name</shortdesc>
d42a06
-<content type="string" default="eth0" />
d42a06
+<content type="string" default="${OCF_RESKEY_interface_default}" />
d42a06
 </parameter>
d42a06
-<parameter name="profile" required="0">
d42a06
+
d42a06
+<parameter name="endpoint" required="0">
d42a06
 <longdesc lang="en">
d42a06
-Valid Aliyun CLI profile name (see 'aliyuncli-ra configure').
d42a06
+An endpoint is the service entry of an Alibaba Cloud service, i.e. vpc.cn-beijing.aliyuncs.com
d42a06
+</longdesc>
d42a06
+<shortdesc lang="en">service endpoint</shortdesc>
d42a06
+<content type="string" default="${OCF_RESKEY_endpoint_default}" />
d42a06
+</parameter>
d42a06
 
d42a06
-See https://www.alibabacloud.com/help/doc-detail/43039.htm?spm=a2c63.p38356.b99.16.38a914abRZtOU3 for more information about aliyuncli-ra.
d42a06
+<parameter name="profile" required="0">
d42a06
+<longdesc lang="en">
d42a06
+Valid Aliyun CLI profile name (see 'aliyun cli configure').
d42a06
+See https://www.alibabacloud.com/help/zh/product/29991.htm for more information about aliyun cli.
d42a06
 </longdesc>
d42a06
 <shortdesc lang="en">profile name</shortdesc>
d42a06
-<content type="string" default="default" />
d42a06
+<content type="string" default="${OCF_RESKEY_profile_default}" />
d42a06
 </parameter>
d42a06
 </parameters>
d42a06
+
d42a06
 <actions>
d42a06
 <action name="start" timeout="180s" />
d42a06
 <action name="stop" timeout="180s" />
d42a06
@@ -170,6 +248,11 @@
d42a06
 ecs_ip_validate() {
d42a06
 	ocf_log debug "function: validate"
d42a06
 
d42a06
+	if [ -z "${OCF_RESKEY_aliyuncli}" ]; then
d42a06
+		ocf_exit_reason "unable to detect aliyuncli binary"
d42a06
+		exit $OCF_ERR_INSTALLED
d42a06
+	fi
d42a06
+
d42a06
 	# IP address
d42a06
 	if [ -z "$OCF_RESKEY_address" ]; then
d42a06
 		ocf_log err "IP address parameter not set $OCF_RESKEY_ADDRESS!"
d42a06
@@ -250,10 +333,7 @@
d42a06
 
d42a06
 ecs_ip_monitor() {
d42a06
 	ocf_log debug "function: ecsip_monitor: check routing table"
d42a06
-	cmd="aliyuncli vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output text"
d42a06
-	ocf_log debug "executing command: $cmd"
d42a06
-
d42a06
-	ROUTE_TO_INSTANCE="$($cmd |grep $OCF_RESKEY_address | awk '{ print $3 }')"
d42a06
+	request_describe_route_tables
d42a06
 
d42a06
 	if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then
d42a06
 		ocf_log debug "not routed to this instance ($ECS_INSTANCE_ID) but to instance $ROUTE_TO_INSTANCE"