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

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