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

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