From a4705403aa6a4ba0f5ea8c67bff89a67d0be2070 Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Fri, 23 Sep 2016 15:08:48 +1000
Subject: [PATCH] yum_install: fix awk return code

TIL:

  Similarly, all the END rules are merged, and executed when all the
  input is exhausted (or when an exit statement is executed).

i.e. matching YUM_FAILED calls "exit", which falls through to the END
rules which calls "exit result" ... which is zero.  i.e. if the return
code is 1 then we actually hide that and return with zero.

This is rather annoying because errors that should halt to alert us of
a package install failure pass through, only for you to have to debug
much later on seemingly unrelated problems.

This always sets "result" and thus should be returning the right
thing.  I've updated the documentation to hopefully make it clearer
what's going on.

Change-Id: Ia15b7dc55efb8d3e3e945241b67a468b8a914672
---
 functions-common | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/functions-common b/functions-common
index 1a4a8f8eea..120aa970d9 100644
--- a/functions-common
+++ b/functions-common
@@ -1346,20 +1346,26 @@ function yum_install {
 
     time_start "yum_install"
 
-    # - We run with LC_ALL=C so string matching *should* be OK
-    # - Exit 1 if the failure might get better with a retry.
-    # - Exit 2 if it is fatal.
-    parse_yum_result='             \
-        BEGIN { result=0 }         \
-        /^YUM_FAILED/ { exit $2 }  \
-        /^No package/ { result=2 } \
-        /^Failed:/    { result=2 } \
-        //{ print }                \
+    # This is a bit tricky, because yum -y assumes missing or failed
+    # packages are OK (see [1]).  We want devstack to stop if we are
+    # installing missing packages.
+    #
+    # Thus we manually match on the output (stack.sh runs in a fixed
+    # locale, so lang shouldn't change).
+    #
+    # If yum returns !0, we echo the result as "YUM_FAILED" and return
+    # that from the awk (we're subverting -e with this trick).
+    # Otherwise we use awk to look for failure strings and return "2"
+    # to indicate a terminal failure.
+    #
+    # [1] https://bugzilla.redhat.com/show_bug.cgi?id=965567
+    parse_yum_result='              \
+        BEGIN { result=0 }          \
+        /^YUM_FAILED/ { result=$2 } \
+        /^No package/ { result=2 }  \
+        /^Failed:/    { result=2 }  \
+        //{ print }                 \
         END { exit result }'
-
-    # The manual check for missing packages is because yum -y assumes
-    # missing or failed packages are OK.
-    # See https://bugzilla.redhat.com/show_bug.cgi?id=965567
     (sudo_with_proxies "${YUM:-yum}" install -y "$@" 2>&1 || echo YUM_FAILED $?) \
         | awk "$parse_yum_result" && result=$? || result=$?