Blob Blame History Raw
From 127db5bea3e9409bee9bdbce30af88a42c4e1e60 Mon Sep 17 00:00:00 2001
From: "Bryn M. Reeves" <bmr@redhat.com>
Date: Mon, 26 Jan 2015 15:36:40 -0500
Subject: [PATCH 79/93] [policies] add container support to Red Hat policy

Check for the presence of container-specific environment variables
and set _host_sysroot if present:

  container_uuid=UUID
  HOST=/path

If both a container environment variable and HOST are present run
the PackageManager query command in a chroot.

Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
---
 sos/policies/redhat.py | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/sos/policies/redhat.py b/sos/policies/redhat.py
index 2219246..e85b166 100644
--- a/sos/policies/redhat.py
+++ b/sos/policies/redhat.py
@@ -37,13 +37,17 @@ class RedHatPolicy(LinuxPolicy):
     vendor = "Red Hat"
     vendor_url = "http://www.redhat.com/"
     _tmp_dir = "/var/tmp"
+    _rpmq_cmd = 'rpm -qa --queryformat "%{NAME}|%{VERSION}\\n"'
+    _in_container = False
+    _host_sysroot = '/'
 
     def __init__(self):
         super(RedHatPolicy, self).__init__()
         self.report_name = ""
         self.ticket_number = ""
-        self.package_manager = PackageManager(
-            'rpm -qa --queryformat "%{NAME}|%{VERSION}\\n"')
+        # need to set _host_sysroot before PackageManager()
+        sysroot = self._container_init()
+        self.package_manager = PackageManager(self._rpmq_cmd, chroot=sysroot)
         self.valid_subclasses = [RedHatPlugin]
 
         # handle PATH for UsrMove
@@ -62,6 +66,17 @@ class RedHatPolicy(LinuxPolicy):
         Fedora, RHEL or other Red Hat distribution or False otherwise."""
         return False
 
+    def _container_init(self):
+        """Check if sos is running in a container and if a host sysroot
+        has been passed in the environment.
+        """
+        if ENV_CONTAINER_UUID in os.environ:
+            self._in_container = True
+        if ENV_HOST_SYSROOT in os.environ:
+            self._host_sysroot = os.environ[ENV_HOST_SYSROOT]
+        use_sysroot = self._in_container and self._host_sysroot != '/'
+        return self._host_sysroot if use_sysroot else None
+
     def runlevel_by_service(self, name):
         from subprocess import Popen, PIPE
         ret = []
@@ -92,6 +107,10 @@ class RedHatPolicy(LinuxPolicy):
     def get_local_name(self):
         return self.host_name()
 
+# Container environment variables on Red Hat systems.
+ENV_CONTAINER_UUID = 'container_uuid'
+ENV_HOST_SYSROOT = 'HOST'
+
 
 class RHELPolicy(RedHatPolicy):
     distro = "Red Hat Enterprise Linux"
-- 
1.9.3