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

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