Blame SOURCES/bz1759113-aws-vpc-route53-1-update.patch

ab36d8
--- ClusterLabs-resource-agents-e711383f/heartbeat/aws-vpc-route53.in	2018-06-29 14:05:02.000000000 +0200
ab36d8
+++ /home/oalbrigt/src/resource-agents/heartbeat/aws-vpc-route53.in	2019-11-07 12:24:18.822111495 +0100
ab36d8
@@ -152,9 +152,15 @@
ab36d8
 END
ab36d8
 }
ab36d8
 
ab36d8
-ec2ip_validate() {
ab36d8
+r53_validate() {
ab36d8
 	ocf_log debug "function: validate"
ab36d8
 
ab36d8
+	# Check for required binaries
ab36d8
+	ocf_log debug "Checking for required binaries"
ab36d8
+	for command in curl dig; do
ab36d8
+		check_binary "$command"
ab36d8
+	done
ab36d8
+
ab36d8
 	# Full name
ab36d8
 	[[ -z "$OCF_RESKEY_fullname" ]] && ocf_log error "Full name parameter not set $OCF_RESKEY_fullname!" && exit $OCF_ERR_CONFIGURED
ab36d8
 
ab36d8
@@ -175,32 +181,111 @@
ab36d8
 	ocf_log debug "ok"
ab36d8
 
ab36d8
 	if [ -n "$OCF_RESKEY_profile" ]; then
ab36d8
-		AWS_PROFILE_OPT="--profile $OCF_RESKEY_profile"
ab36d8
+		AWS_PROFILE_OPT="--profile $OCF_RESKEY_profile --cli-connect-timeout 10"
ab36d8
 	else
ab36d8
-		AWS_PROFILE_OPT="--profile default"
ab36d8
+		AWS_PROFILE_OPT="--profile default --cli-connect-timeout 10"
ab36d8
 	fi
ab36d8
 
ab36d8
 	return $OCF_SUCCESS
ab36d8
 }
ab36d8
 
ab36d8
-ec2ip_monitor() {
ab36d8
-	ec2ip_validate
ab36d8
+r53_monitor() {
ab36d8
+	#
ab36d8
+	# For every start action the agent  will call Route53 API to check for DNS record
ab36d8
+	# otherwise it will try to get results directly bu querying the DNS using "dig".
ab36d8
+	# Due to complexity in some DNS architectures "dig" can fail, and if this happens
ab36d8
+	# the monitor will fallback to the Route53 API call.
ab36d8
+	#
ab36d8
+	# There will be no failure, failover or restart of the agent if the monitor operation fails
ab36d8
+	# hence we only return $OCF_SUCESS in this function
ab36d8
+	#
ab36d8
+	# In case of the monitor operation detects a wrong or non-existent Route53 DNS entry
ab36d8
+	# it will try to fix the existing one, or create it again
ab36d8
+	#
ab36d8
+	#
ab36d8
+	ARECORD=""
ab36d8
+	IPREGEX="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
ab36d8
+	r53_validate
ab36d8
 	ocf_log debug "Checking Route53 record sets"
ab36d8
-	IPADDRESS="$(ec2metadata aws ip | grep local-ipv4 | /usr/bin/awk '{ print $2 }')"
ab36d8
-	ARECORD="$(aws $AWS_PROFILE_OPT route53 list-resource-record-sets --hosted-zone-id $OCF_RESKEY_hostedzoneid --query "ResourceRecordSets[?Name=='$OCF_RESKEY_fullname']" | grep RESOURCERECORDS | /usr/bin/awk '{ print $2 }' )"
ab36d8
-	ocf_log debug "Found IP address: $ARECORD ."
ab36d8
-	if [ "${ARECORD}" == "${IPADDRESS}" ]; then
ab36d8
-		ocf_log debug "ARECORD $ARECORD found"
ab36d8
+	#
ab36d8
+	IPADDRESS="$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)"
ab36d8
+	#
ab36d8
+	if [ "$__OCF_ACTION" = "start" ] || ocf_is_probe ; then
ab36d8
+		#
ab36d8
+		cmd="aws $AWS_PROFILE_OPT route53 list-resource-record-sets --hosted-zone-id $OCF_RESKEY_hostedzoneid --query ResourceRecordSets[?Name=='$OCF_RESKEY_fullname']"
ab36d8
+		ocf_log info "Route53 Agent Starting or probing - executing monitoring API call: $cmd"
ab36d8
+		CLIRES="$($cmd 2>&1)"
ab36d8
+		rc=$?
ab36d8
+		ocf_log debug "awscli returned code: $rc"
ab36d8
+		if [ $rc -ne 0 ]; then
ab36d8
+			CLIRES=$(echo $CLIRES | grep -v '^$')
ab36d8
+			ocf_log warn "Route53 API returned an error: $CLIRES"
ab36d8
+			ocf_log warn "Skipping cluster action due to API call error"
ab36d8
+			return $OCF_ERR_GENERIC
ab36d8
+		fi
ab36d8
+		ARECORD=$(echo $CLIRES | grep RESOURCERECORDS | awk '{ print $5 }')
ab36d8
+		#
ab36d8
+		if ocf_is_probe; then
ab36d8
+			#
ab36d8
+			# Prevent R53 record change during probe
ab36d8
+			#
ab36d8
+			if [[ $ARECORD =~ $IPREGEX ]] && [ "$ARECORD" != "$IPADDRESS" ]; then
ab36d8
+				ocf_log debug "Route53 DNS record $ARECORD found at probing, disregarding"
ab36d8
+				return $OCF_NOT_RUNNING
ab36d8
+			fi
ab36d8
+		fi
ab36d8
+	else
ab36d8
+		#
ab36d8
+		cmd="dig +retries=3 +time=5 +short $OCF_RESKEY_fullname 2>/dev/null"
ab36d8
+		ocf_log info "executing monitoring command : $cmd"
ab36d8
+		ARECORD="$($cmd)"
ab36d8
+		rc=$?
ab36d8
+		ocf_log debug "dig return code: $rc"
ab36d8
+		#
ab36d8
+		if  [[ ! $ARECORD =~ $IPREGEX ]] || [ $rc -ne 0 ]; then
ab36d8
+			ocf_log info "Fallback to Route53 API query due to DNS resolution failure"
ab36d8
+			cmd="aws $AWS_PROFILE_OPT route53 list-resource-record-sets --hosted-zone-id $OCF_RESKEY_hostedzoneid --query ResourceRecordSets[?Name=='$OCF_RESKEY_fullname']"
ab36d8
+			ocf_log debug "executing monitoring API call: $cmd"
ab36d8
+			CLIRES="$($cmd 2>&1)"
ab36d8
+			rc=$?
ab36d8
+			ocf_log debug "awscli return code: $rc"
ab36d8
+			if [ $rc -ne 0 ]; then
ab36d8
+				CLIRES=$(echo $CLIRES | grep -v '^$')
ab36d8
+				ocf_log warn "Route53 API returned an error: $CLIRES"
ab36d8
+				ocf_log warn "Monitor skipping cluster action due to API call error"
ab36d8
+				return $OCF_SUCCESS
ab36d8
+			fi
ab36d8
+			ARECORD=$(echo $CLIRES | grep RESOURCERECORDS | awk '{ print $5 }')
ab36d8
+		fi
ab36d8
+		#
ab36d8
+	fi
ab36d8
+	ocf_log info "Route53 DNS record pointing $OCF_RESKEY_fullname to IP address $ARECORD"
ab36d8
+	#
ab36d8
+	if [ "$ARECORD" == "$IPADDRESS" ]; then
ab36d8
+		ocf_log info "Route53 DNS record $ARECORD found"
ab36d8
+		return $OCF_SUCCESS
ab36d8
+	elif [[ $ARECORD =~ $IPREGEX ]] && [ "$ARECORD" != "$IPADDRESS" ]; then
ab36d8
+		ocf_log info "Route53 DNS record points to a different host, setting DNS record on Route53 to this host"
ab36d8
+		_update_record "UPSERT" "$IPADDRESS"
ab36d8
 		return $OCF_SUCCESS
ab36d8
 	else
ab36d8
-		ocf_log debug "No ARECORD found"
ab36d8
-		return $OCF_NOT_RUNNING
ab36d8
+		ocf_log info "No Route53 DNS record found, setting DNS record on Route53 to this host"
ab36d8
+		_update_record "UPSERT" "$IPADDRESS"
ab36d8
+		return $OCF_SUCCESS
ab36d8
 	fi
ab36d8
 
ab36d8
 	return $OCF_SUCCESS
ab36d8
 }
ab36d8
 
ab36d8
 _update_record() {
ab36d8
+	#
ab36d8
+	# This function is the one that will actually execute Route53's API call
ab36d8
+	# and configure the DNS record using the correct API calls and parameters
ab36d8
+	#
ab36d8
+	# It creates a temporary JSON file under /tmp with the required API payload
ab36d8
+	#
ab36d8
+	# Failures in this function are critical and will cause the agent to fail
ab36d8
+	#
ab36d8
 	update_action="$1"
ab36d8
 	IPADDRESS="$2"
ab36d8
 	ocf_log info "Updating Route53 $OCF_RESKEY_hostedzoneid with $IPADDRESS for $OCF_RESKEY_fullname"
ab36d8
@@ -209,19 +294,19 @@
ab36d8
 		ocf_exit_reason "Failed to create temporary file for record update"
ab36d8
 		exit $OCF_ERR_GENERIC
ab36d8
 	fi
ab36d8
-	cat >>"${ROUTE53RECORD}" <<-EOF
ab36d8
+	cat >>"$ROUTE53RECORD" <<-EOF
ab36d8
 	{
ab36d8
 		  "Comment": "Update record to reflect new IP address for a system ",
ab36d8
 		  "Changes": [
ab36d8
 			  {
ab36d8
-				  "Action": "${update_action}",
ab36d8
+				  "Action": "$update_action",
ab36d8
 				  "ResourceRecordSet": {
ab36d8
-					  "Name": "${OCF_RESKEY_fullname}",
ab36d8
+					  "Name": "$OCF_RESKEY_fullname",
ab36d8
 					  "Type": "A",
ab36d8
-					  "TTL": ${OCF_RESKEY_ttl},
ab36d8
+					  "TTL": $OCF_RESKEY_ttl,
ab36d8
 					  "ResourceRecords": [
ab36d8
 						  {
ab36d8
-							  "Value": "${IPADDRESS}"
ab36d8
+							  "Value": "$IPADDRESS"
ab36d8
 						  }
ab36d8
 					  ]
ab36d8
 				  }
ab36d8
@@ -229,46 +314,53 @@
ab36d8
 		  ]
ab36d8
 	}
ab36d8
 	EOF
ab36d8
-	cmd="aws --profile ${OCF_RESKEY_profile} route53 change-resource-record-sets --hosted-zone-id ${OCF_RESKEY_hostedzoneid} \
ab36d8
-	  --change-batch file://${ROUTE53RECORD} "
ab36d8
+	cmd="aws --profile $OCF_RESKEY_profile route53 change-resource-record-sets --hosted-zone-id $OCF_RESKEY_hostedzoneid --change-batch file://$ROUTE53RECORD "
ab36d8
 	ocf_log debug "Executing command: $cmd"
ab36d8
-	CHANGEID=$($cmd | grep CHANGEINFO |	 /usr/bin/awk -F'\t' '{ print $3 }' )
ab36d8
-	ocf_log debug "Change id: ${CHANGEID}"
ab36d8
-	rmtempfile ${ROUTE53RECORD}
ab36d8
-	CHANGEID=$(echo $CHANGEID |cut -d'/' -f 3 |cut -d'"' -f 1 )
ab36d8
-	ocf_log debug "Change id: ${CHANGEID}"
ab36d8
+	CLIRES="$($cmd 2>&1)"
ab36d8
+	rc=$?
ab36d8
+	ocf_log debug "awscli returned code: $rc"
ab36d8
+	if [ $rc -ne 0 ]; then
ab36d8
+		CLIRES=$(echo $CLIRES | grep -v '^$')
ab36d8
+		ocf_log warn "Route53 API returned an error: $CLIRES"
ab36d8
+		ocf_log warn "Skipping cluster action due to API call error"
ab36d8
+		return $OCF_ERR_GENERIC
ab36d8
+	fi
ab36d8
+	CHANGEID=$(echo $CLIRES | awk '{ print $12 }')
ab36d8
+	ocf_log debug "Change id: $CHANGEID"
ab36d8
+	rmtempfile $ROUTE53RECORD
ab36d8
+	CHANGEID=$(echo $CHANGEID | cut -d'/' -f 3 | cut -d'"' -f 1 )
ab36d8
+	ocf_log debug "Change id: $CHANGEID"
ab36d8
 	STATUS="PENDING"
ab36d8
-	MYSECONDS=2
ab36d8
+	MYSECONDS=20
ab36d8
 	while [ "$STATUS" = 'PENDING' ]; do
ab36d8
-		sleep	${MYSECONDS}
ab36d8
-		STATUS="$(aws --profile ${OCF_RESKEY_profile} route53 get-change --id $CHANGEID | grep CHANGEINFO |  /usr/bin/awk -F'\t' '{ print $4 }' |cut -d'"' -f 2 )"
ab36d8
-		ocf_log debug "Waited for ${MYSECONDS} seconds and checked execution of Route 53 update status: ${STATUS} "
ab36d8
+		sleep $MYSECONDS
ab36d8
+		STATUS="$(aws --profile $OCF_RESKEY_profile route53 get-change --id $CHANGEID | grep CHANGEINFO | awk -F'\t' '{ print $4 }' |cut -d'"' -f 2 )"
ab36d8
+		ocf_log debug "Waited for $MYSECONDS seconds and checked execution of Route 53 update status: $STATUS "
ab36d8
 	done
ab36d8
 }
ab36d8
 
ab36d8
-ec2ip_stop() {
ab36d8
-	ocf_log info "Bringing down Route53 agent. (Will remove ARECORD)"
ab36d8
-	IPADDRESS="$(ec2metadata aws ip | grep local-ipv4 | /usr/bin/awk '{ print $2 }')"
ab36d8
-	ARECORD="$(aws $AWS_PROFILE_OPT route53 list-resource-record-sets --hosted-zone-id $OCF_RESKEY_hostedzoneid --query "ResourceRecordSets[?Name=='$OCF_RESKEY_fullname']" | grep RESOURCERECORDS | /usr/bin/awk '{ print $2 }' )"
ab36d8
-	ocf_log debug "Found IP address: $ARECORD ."
ab36d8
-	if [ ${ARECORD} != ${IPADDRESS} ]; then
ab36d8
-		ocf_log debug "No ARECORD found"
ab36d8
-		return $OCF_SUCCESS
ab36d8
-	else
ab36d8
-		# determine IP address
ab36d8
-		IPADDRESS="$(ec2metadata aws ip | grep local-ipv4 | /usr/bin/awk '{ print $2 }')"
ab36d8
-		# Patch file
ab36d8
-		ocf_log debug "Deleting IP address to ${IPADDRESS}"
ab36d8
-		return $OCF_SUCCESS
ab36d8
-	fi
ab36d8
-
ab36d8
-	_update_record "DELETE" "$IPADDRESS"
ab36d8
+r53_stop() {
ab36d8
+	#
ab36d8
+	# Stop operation doesn't perform any API call or try to remove the DNS record
ab36d8
+	# this mostly because this is not necessarily mandatory or desired
ab36d8
+	# the start and monitor functions will take care of changing the DNS record
ab36d8
+	# if the agent starts in a different cluster node
ab36d8
+	#
ab36d8
+	ocf_log info "Bringing down Route53 agent. (Will NOT remove Route53 DNS record)"
ab36d8
 	return $OCF_SUCCESS
ab36d8
 }
ab36d8
 
ab36d8
-ec2ip_start() {
ab36d8
-	IPADDRESS="$(ec2metadata aws ip | grep local-ipv4 | /usr/bin/awk '{ print $2 }')"
ab36d8
-	_update_record "UPSERT" "$IPADDRESS"
ab36d8
+r53_start() {
ab36d8
+	#
ab36d8
+	# Start agent and config DNS in Route53
ab36d8
+	#
ab36d8
+	ocf_log info "Starting Route53 DNS update...."
ab36d8
+	IPADDRESS="$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)"
ab36d8
+	r53_monitor
ab36d8
+	if [ $? != $OCF_SUCCESS ]; then
ab36d8
+		ocf_log info "Could not start agent - check configurations"
ab36d8
+		return $OCF_ERR_GENERIC
ab36d8
+	fi
ab36d8
 	return $OCF_SUCCESS
ab36d8
 }
ab36d8
 
ab36d8
@@ -284,16 +376,16 @@
ab36d8
 		exit $OCF_SUCCESS
ab36d8
 		;;
ab36d8
 	monitor)
ab36d8
-		ec2ip_monitor
ab36d8
+		r53_monitor
ab36d8
 		;;
ab36d8
 	stop)
ab36d8
-		ec2ip_stop
ab36d8
+		r53_stop
ab36d8
 		;;
ab36d8
 	validate-all)
ab36d8
-		ec2ip_validate
ab36d8
+		r53_validate
ab36d8
 		;;
ab36d8
 	start)
ab36d8
-		ec2ip_start
ab36d8
+		r53_start
ab36d8
 		;;
ab36d8
 	*)
ab36d8
 		usage