Blame SOURCES/bz1748768-docker-fix-stop-issues.patch

734564
From 5949405d0031a4aba91c81cb28c24821ad2d439a Mon Sep 17 00:00:00 2001
734564
From: Reid Wahl <nwahl@redhat.com>
734564
Date: Thu, 3 Jan 2019 15:05:20 -0800
734564
Subject: [PATCH] docker: Fix issues with stop operation
734564
734564
The docker RA's stop operation doesn't behave properly in some cases.
734564
  1. It returns a false success code in case of an error response from
734564
     the daemon.
734564
  2. It fails at `remove_container()` if the container does not exist
734564
     but another docker object of the same name does exist.
734564
734564
In case #1, the `container_exists()` function returns the same exit code
734564
(1) if the container is not found (an expected error) or if there is an
734564
error response from the docker daemon (an unexpected error). These types
734564
of errors should be handled differently.
734564
734564
In case #2, the `docker inspect` calls do not limit their search to
734564
containers. So if a non-container object is found with a matching name,
734564
the RA attempts to remove a container by that name. Such a container may
734564
not exist.
734564
734564
This patch fixes these issues as follows:
734564
  1. Match an error response in `container_exists()` against the string
734564
     "No such container".
734564
  2. Add `--type=container` to the `docker inspect` calls to restrict
734564
     the match.
734564
---
734564
 heartbeat/docker | 26 ++++++++++++++++++++++----
734564
 1 file changed, 22 insertions(+), 4 deletions(-)
734564
734564
diff --git a/heartbeat/docker b/heartbeat/docker
734564
index f5ba83ff2..c206344ad 100755
734564
--- a/heartbeat/docker
734564
+++ b/heartbeat/docker
734564
@@ -215,7 +215,7 @@ monitor_cmd_exec()
734564
 		out=$(docker exec ${CONTAINER} $OCF_RESKEY_monitor_cmd 2>&1)
734564
 		rc=$?
734564
 	else
734564
-		out=$(echo "$OCF_RESKEY_monitor_cmd" | nsenter --target $(docker inspect --format {{.State.Pid}} ${CONTAINER}) --mount --uts --ipc --net --pid 2>&1)
734564
+		out=$(echo "$OCF_RESKEY_monitor_cmd" | nsenter --target $(docker inspect --type=container --format {{.State.Pid}} ${CONTAINER}) --mount --uts --ipc --net --pid 2>&1)
734564
 		rc=$?
734564
 	fi
734564
 
734564
@@ -236,7 +236,25 @@ monitor_cmd_exec()
734564
 
734564
 container_exists()
734564
 {
734564
-	docker inspect --format {{.State.Running}} $CONTAINER | egrep '(true|false)' >/dev/null 2>&1
734564
+	local err
734564
+
734564
+	err=$(docker inspect --type=container $CONTAINER 2>&1 >/dev/null)
734564
+
734564
+	if [ $? -ne $OCF_SUCCESS ]; then
734564
+		case $err in
734564
+			*"No such container"*)
734564
+				# Return failure instead of exiting if container does not exist
734564
+				return 1
734564
+				;;
734564
+			*)
734564
+				# Exit if error running command
734564
+				ocf_exit_reason "$err"
734564
+				exit $OCF_ERR_GENERIC
734564
+				;;
734564
+		esac
734564
+	fi
734564
+
734564
+	return $OCF_SUCCESS
734564
 }
734564
 
734564
 remove_container()
734564
@@ -265,7 +283,7 @@ docker_simple_status()
734564
 	fi
734564
 
734564
 	# retrieve the 'Running' attribute for the container
734564
-	val=$(docker inspect --format {{.State.Running}} $CONTAINER 2>/dev/null)
734564
+	val=$(docker inspect --type=container --format {{.State.Running}} $CONTAINER 2>/dev/null)
734564
 	if [ $? -ne 0 ]; then
734564
 		#not running as a result of container not being found
734564
 		return $OCF_NOT_RUNNING
734564
@@ -295,7 +313,7 @@ docker_health_status()
734564
                 # if starting takes longer than monitor timeout then upstream will make this fail.
734564
                 while
734564
 
734564
-                        val=$(docker inspect --format {{.State.Health.Status}} $CONTAINER 2>/dev/null)
734564
+                        val=$(docker inspect --type=container --format {{.State.Health.Status}} $CONTAINER 2>/dev/null)
734564
                         if [ $? -ne 0 ]; then
734564
                                 #not healthy as a result of container not being found
734564
                                 return $OCF_NOT_RUNNING