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

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