[autotest] Fix bug in lxc.install_packages

Ubuntu's version of pip is very old and causes segmentation faults
if installed.

Change the install_packages code to use the version of pip that is
recommended, this should come in the base container but if not it
should be installed by this code.

The only user I can find of this code is jetstream, I have created
an artificial test case in moblab to test that usage

TEST=local moblab testing
BUG=chromium:974384

Change-Id: Iceddb9f3153b1d098d6dab84d6420ce4c0cb183d
Reviewed-on: https://chromium-review.googlesource.com/1676410
Tested-by: Keith Haddow <haddowk@chromium.org>
Commit-Ready: Keith Haddow <haddowk@chromium.org>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Prathmesh Prabhu <pprabhu@chromium.org>
(cherry picked from commit f8d7079a47bf1a9bd49b41011517dbc25da63f81)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/1693330
Reviewed-by: Keith Haddow <haddowk@chromium.org>
Commit-Queue: Keith Haddow <haddowk@chromium.org>
Auto-Submit: Keith Haddow <haddowk@chromium.org>
diff --git a/moblab_config.ini b/moblab_config.ini
index 2e679dd..0ea07f5 100644
--- a/moblab_config.ini
+++ b/moblab_config.ini
@@ -11,7 +11,7 @@
 auto_start_servod: True
 
 # Name of the base container.
-container_base_name: moblab_base_08
+container_base_name: moblab_base_09
 
 # Exports tko job information to file.
 export_tko_job_to_file: True
diff --git a/site_utils/lxc/lxc.py b/site_utils/lxc/lxc.py
index 3ea7d9b..cf8752e 100644
--- a/site_utils/lxc/lxc.py
+++ b/site_utils/lxc/lxc.py
@@ -132,6 +132,32 @@
     return True
 
 
+
+def _remove_banned_packages(packages, banned_packages):
+    """Filter out packages.
+
+    @param packages: A set of packages names that have been requested.
+    @param items: A list of package names that are not to be installed.
+
+    @return: A sanatized set of packages names to install.
+    """
+    return {package for package in packages if package not in banned_packages}
+
+
+def _ensure_pip(target_setting):
+    """ Ensure pip is installed, if not install it.
+
+    @param target_setting: target command param specifying the path to where
+                           python packages should be installed.
+    """
+    try:
+        import pip
+    except ImportError:
+        common_utils.run(
+            'wget https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py')
+        common_utils.run('python /tmp/get-pip.py %s' % target_setting)
+
+
 @metrics.SecondsTimerDecorator(
     '%s/install_packages_duration' % constants.STATS_KEY)
 @retry.retry(error.CmdError, timeout_min=30)
@@ -174,10 +200,17 @@
     # Always run apt-get update before installing any container. The base
     # container may have outdated cache.
     common_utils.run('sudo apt-get update')
+
     # Make sure the lists are not None for iteration.
     packages = [] if not packages else packages
-    if python_packages:
-        packages.extend(['python-pip', 'python-dev'])
+    # Remove duplicates.
+    packages = set(packages)
+
+    # Ubuntu distribution of pip is very old, do not use it as it causes
+    # segmentation faults.  Some tests request these packages, ensure they
+    # are not installed.
+    packages = _remove_banned_packages(packages, ['python-pip', 'python-dev'])
+
     if packages:
         common_utils.run(
             'sudo DEBIAN_FRONTEND=noninteractive apt-get install %s -y '
@@ -191,8 +224,11 @@
     # Containers created in Moblab does not have autotest/site-packages folder.
     if not os.path.exists('/usr/local/autotest/site-packages'):
         target_setting = '--target="/usr/lib/python2.7/dist-packages/"'
+    # Pip should be installed in the base container, if not install it.
     if python_packages:
-        common_utils.run('sudo pip install %s %s' % (target_setting,
+        _ensure_pip(target_setting)
+        common_utils.run('python -m pip install pip --upgrade')
+        common_utils.run('python -m pip install %s %s' % (target_setting,
                                               ' '.join(python_packages)))
         logging.debug('Python packages are installed: %s.', python_packages)