From db9fa439f3dfca2f27649334d400210a147377ae Mon Sep 17 00:00:00 2001
From: Merlin Mathesius <merlinm@redhat.com>
Date: Jul 20 2017 21:42:24 +0000
Subject: [PATCH 1/2] inventory: Enable containers and VMs to be diagnosed by setting TEST_DIAGNOSE_INVENTORY environment variable


---

diff --git a/inventory/standard-inventory-docker b/inventory/standard-inventory-docker
index aca3aec..4f3d406 100755
--- a/inventory/standard-inventory-docker
+++ b/inventory/standard-inventory-docker
@@ -13,6 +13,7 @@ import sys
 import tempfile
 import time
 import traceback
+import distutils.util
 
 def main(argv):
     parser = argparse.ArgumentParser(description="Inventory for a container image in a registry")
@@ -58,6 +59,12 @@ def host(image):
     directory = tempfile.mkdtemp(prefix="inventory-docker")
     cidfile = os.path.join(directory, "cid")
 
+    # Determine if container should be kept available for diagnosis after completion
+    try:
+        diagnose = distutils.util.strtobool(os.getenv("TEST_DIAGNOSE_INVENTORY", "0"))
+    except ValueError:
+        diagnose = 0
+
     sys.stderr.write("Launching Docker container for {0}\n".format(image))
 
     # And launch the actual container
@@ -129,6 +136,17 @@ def host(image):
         except OSError:
             break # Either of the processes no longer exist
 
+    if diagnose:
+        sys.stderr.write("\n")
+        sys.stderr.write("DIAGNOSE: docker exec -it {0} /bin/bash\n".format(name))
+        sys.stderr.write("DIAGNOSE: kill {0} # when finished\n".format(os.getpid()))
+
+        def _signal_handler(*args):
+            sys.stderr.write("\nDIAGNOSE ending...\n")
+
+        signal.signal(signal.SIGTERM, _signal_handler)
+        signal.pause()
+
     # Dump the container logs
     try:
         os.makedirs(artifacts)
diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2
index 9ac7def..482f6b4 100755
--- a/inventory/standard-inventory-qcow2
+++ b/inventory/standard-inventory-qcow2
@@ -13,6 +13,7 @@ import sys
 import tempfile
 import time
 import traceback
+import distutils.util
 
 IDENTITY = """
 -----BEGIN RSA PRIVATE KEY-----
@@ -131,6 +132,12 @@ def host(image):
                            "-volid", "cidata", "-joliet", "-rock", "-quiet",
                            "-output", cloudinit, userdata, metadata], stdout=null)
 
+    # Determine if virtual machine should be kept available for diagnosis after completion
+    try:
+        diagnose = distutils.util.strtobool(os.getenv("TEST_DIAGNOSE_INVENTORY", "0"))
+    except ValueError:
+        diagnose = 0
+
     sys.stderr.write("Launching virtual machine for {0}\n".format(image))
 
     # And launch the actual VM
@@ -214,6 +221,17 @@ def host(image):
         except OSError:
             break # Either of the processes no longer exist
 
+    if diagnose:
+        sys.stderr.write("\n")
+        sys.stderr.write("DIAGNOSE: ssh -p {0} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@{1} # password: {2}\n".format(port, "127.0.0.3", "foobar"))
+        sys.stderr.write("DIAGNOSE: kill {0} # when finished\n".format(os.getpid()))
+
+        def _signal_handler(*args):
+            sys.stderr.write("\nDIAGNOSE ending...\n")
+
+        signal.signal(signal.SIGTERM, _signal_handler)
+        signal.pause()
+
     # Kill the qemu process
     try:
         os.kill(proc.pid, signal.SIGTERM)

From 14311f661be93fdb11df0df23b0c2d77f39bb493 Mon Sep 17 00:00:00 2001
From: Merlin Mathesius <merlinm@redhat.com>
Date: Jul 21 2017 13:11:10 +0000
Subject: [PATCH 2/2] inventory: rename environment variable to trigger container/VM diagnosis from TEST_DIAGNOSE_INVENTORY to TEST_DEBUG


---

diff --git a/inventory/standard-inventory-docker b/inventory/standard-inventory-docker
index 4f3d406..71e9cec 100755
--- a/inventory/standard-inventory-docker
+++ b/inventory/standard-inventory-docker
@@ -61,7 +61,7 @@ def host(image):
 
     # Determine if container should be kept available for diagnosis after completion
     try:
-        diagnose = distutils.util.strtobool(os.getenv("TEST_DIAGNOSE_INVENTORY", "0"))
+        diagnose = distutils.util.strtobool(os.getenv("TEST_DEBUG", "0"))
     except ValueError:
         diagnose = 0
 
diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2
index 482f6b4..52112cf 100755
--- a/inventory/standard-inventory-qcow2
+++ b/inventory/standard-inventory-qcow2
@@ -134,7 +134,7 @@ def host(image):
 
     # Determine if virtual machine should be kept available for diagnosis after completion
     try:
-        diagnose = distutils.util.strtobool(os.getenv("TEST_DIAGNOSE_INVENTORY", "0"))
+        diagnose = distutils.util.strtobool(os.getenv("TEST_DEBUG", "0"))
     except ValueError:
         diagnose = 0
 

