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

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