Blame SOURCES/bz1135026-docker-monitor_cmd-arg.patch

c608c9
From 804b68824372f98e23b858f6284160c1f2b0e19f Mon Sep 17 00:00:00 2001
c608c9
From: David Vossel <dvossel@redhat.com>
c608c9
Date: Sat, 25 Oct 2014 20:54:14 -0400
c608c9
Subject: [PATCH 2/2] High: docker: monitor_cmd option for executing status
c608c9
 script within container
c608c9
c608c9
---
c608c9
 heartbeat/docker | 76 +++++++++++++++++++++++++++++++++++++++++++++++++-------
c608c9
 1 file changed, 67 insertions(+), 9 deletions(-)
c608c9
c608c9
diff --git a/heartbeat/docker b/heartbeat/docker
c608c9
index cdf4e82..929b26b 100755
c608c9
--- a/heartbeat/docker
c608c9
+++ b/heartbeat/docker
c608c9
@@ -106,6 +106,20 @@ it has initialized.
c608c9
 <content type="string"/>
c608c9
 </parameter>
c608c9
 
c608c9
+<parameter name="monitor_cmd" required="0" unique="0">
c608c9
+<longdesc lang="en">
c608c9
+Specifiy the full path of a command to launch within the container to check
c608c9
+the health of the container. This command must return 0 to indicate that
c608c9
+the container is healthy. A non-zero return code will indicate that the
c608c9
+container has failed and should be recovered.
c608c9
+
c608c9
+The command is executed using nsenter. In the future 'docker exec' will
c608c9
+be used once it is more widely supported.
c608c9
+</longdesc>
c608c9
+<shortdesc lang="en">monitor command</shortdesc>
c608c9
+<content type="string"/>
c608c9
+</parameter>
c608c9
+
c608c9
 <parameter name="force_kill" required="0" unique="0">
c608c9
 <longdesc lang="en">
c608c9
 Kill a container immediately rather than waiting for it to gracefully
c608c9
@@ -150,6 +164,22 @@ Expects to have a fully populated OCF RA-compliant environment set.
c608c9
 END
c608c9
 }
c608c9
 
c608c9
+
c608c9
+monitor_cmd_exec()
c608c9
+{
c608c9
+	local rc=$OCF_SUCCESS
c608c9
+	if [ -n "$OCF_RESKEY_monitor_cmd" ]; then
c608c9
+		out=$(echo "$OCF_RESKEY_monitor_cmd" | nsenter --target $(docker inspect --format {{.State.Pid}} ${CONTAINER}) --mount --uts --ipc --net --pid 2>&1)
c608c9
+		rc=$?
c608c9
+		if [ $rc -ne 0 ]; then
c608c9
+			ocf_log info "monitor cmd failed with exit code $rc"
c608c9
+			ocf_log info "stdout/stderr: $out"
c608c9
+			rc=$OCF_ERR_GENERIC
c608c9
+		fi
c608c9
+	fi
c608c9
+	return $rc
c608c9
+}
c608c9
+
c608c9
 container_exists()
c608c9
 {
c608c9
 	docker inspect $CONTAINER > /dev/null 2>&1
c608c9
@@ -171,7 +201,7 @@ remove_container()
c608c9
 	ocf_run docker rm $CONTAINER
c608c9
 }
c608c9
 
c608c9
-docker_monitor()
c608c9
+docker_simple_status()
c608c9
 {
c608c9
 	local val
c608c9
 
c608c9
@@ -195,11 +225,25 @@ docker_monitor()
c608c9
 	return $OCF_NOT_RUNNING
c608c9
 }
c608c9
 
c608c9
+docker_monitor()
c608c9
+{
c608c9
+	local rc=0
c608c9
+
c608c9
+	docker_simple_status
c608c9
+	rc=$?
c608c9
+
c608c9
+	if [ $rc -ne 0 ]; then
c608c9
+		return $rc
c608c9
+	fi
c608c9
+
c608c9
+	monitor_cmd_exec
c608c9
+}
c608c9
+
c608c9
 docker_start()
c608c9
 {
c608c9
 	local run_opts="-d --name=${CONTAINER}"
c608c9
 	# check to see if the container has already started
c608c9
-	docker_monitor
c608c9
+	docker_simple_status
c608c9
 	if [ $? -eq $OCF_SUCCESS ]; then
c608c9
 		return $OCF_SUCCESS
c608c9
 	fi
c608c9
@@ -233,19 +277,29 @@ docker_start()
c608c9
 		return $OCF_ERR_GENERIC
c608c9
 	fi
c608c9
 
c608c9
-	docker_monitor
c608c9
-	if [ $? -ne $OCF_SUCCESS ]; then
c608c9
-		ocf_exit_reason "Newly created docker container exited after start"
c608c9
-		return $OCF_ERR_GENERIC
c608c9
-	fi
c608c9
 
c608c9
-	return $OCF_SUCCESS
c608c9
+	# wait for monitor to pass before declaring that the container is started
c608c9
+	while true; do
c608c9
+		docker_simple_status
c608c9
+		if [ $? -ne $OCF_SUCCESS ]; then
c608c9
+			ocf_exit_reason "Newly created docker container exited after start"
c608c9
+			return $OCF_ERR_GENERIC
c608c9
+		fi
c608c9
+
c608c9
+		monitor_cmd_exec
c608c9
+		if [ $? -eq $OCF_SUCCESS ]; then
c608c9
+			return $OCF_SUCCESS
c608c9
+		fi
c608c9
+
c608c9
+		ocf_exit_reason "waiting on monitor_cmd to pass after start"
c608c9
+		sleep 1
c608c9
+	done
c608c9
 }
c608c9
 
c608c9
 docker_stop()
c608c9
 {
c608c9
 	local timeout=60
c608c9
-	docker_monitor
c608c9
+	docker_simple_status
c608c9
 	if [ $? -eq  $OCF_NOT_RUNNING ]; then
c608c9
 		remove_container
c608c9
 		return $OCF_SUCCESS
c608c9
@@ -310,6 +364,10 @@ docker_validate()
c608c9
 		exit $OCF_ERR_CONFIGURED
c608c9
 	fi 
c608c9
 
c608c9
+	if [ -n "$OCF_RESKEY_monitor_cmd" ]; then
c608c9
+		check_binary nsenter
c608c9
+	fi
c608c9
+
c608c9
 	image_exists
c608c9
 	if [ $? -ne 0 ]; then
c608c9
 		ocf_exit_reason "base image, ${OCF_RESKEY_image}, could not be found."
c608c9
-- 
c608c9
1.8.4.2
c608c9