← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~abentley/launchpad/detect-xen into lp:launchpad

 

Aaron Bentley has proposed merging lp:~abentley/launchpad/detect-xen into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  #662664 recipe builds should ensure they run only under xen
  https://bugs.launchpad.net/bugs/662664


= Summary =
Fix bug #662664: recipe builds should ensure they run only under xen

== Proposed fix ==
Use xen-detect to determine whether the builder's running under Xen.  If
xen-detect is not present, assume not running under Xen.

== Pre-implementation notes ==
This change was at Elmo's request.

== Implementation details ==
xen-detect does not emit different status codes depending on whether Xen was
detected, so it is necessary to read stdout.

== Tests ==
No automated tests, as this depends on the build environment.

== Demo and Q/A ==
This code is currently deployed on clementine, and has been determined to work
there.


= Launchpad lint =

Checking for conflicts and issues in changed files.

Linting changed files:
  lib/canonical/buildd/buildrecipe
-- 
https://code.launchpad.net/~abentley/launchpad/detect-xen/+merge/38718
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~abentley/launchpad/detect-xen into lp:launchpad.
=== modified file 'lib/canonical/buildd/buildrecipe'
--- lib/canonical/buildd/buildrecipe	2010-09-09 14:12:02 +0000
+++ lib/canonical/buildd/buildrecipe	2010-10-18 13:26:20 +0000
@@ -7,10 +7,12 @@
 __metaclass__ = type
 
 
+import errno
 import os
 import pwd
+import re
 import socket
-from subprocess import call
+from subprocess import call, Popen, PIPE
 import sys
 
 
@@ -21,6 +23,26 @@
 RETCODE_FAILURE_BUILD_SOURCE_PACKAGE = 203
 
 
+class NotVirtualized(Exception):
+    """Exception raised when not running in a virtualized environment."""
+
+
+class NoXenDetect(NotVirtualized):
+    """xen-detect not installed."""
+
+    def __init__(self):
+        NotVirtualized.__init__(self, 'xen-detect is not installed.')
+
+
+class BadXenDetect(NotVirtualized):
+    """xen-detect's output not not indicate that we are virtualized."""
+
+    def __init__(self, xen_detect_output):
+        NotVirtualized.__init__(
+            self, 'Bad xen-detect output: %s' % xen_detect_output)
+        self.xen_detect_output = xen_detect_output
+
+
 class RecipeBuilder:
     """Builds a package from a recipe."""
 
@@ -64,6 +86,12 @@
         As a side-effect, sets self.source_dir_relative.
         :return: a retcode from `bzr dailydeb`.
         """
+        try:
+            ensure_virtualized()
+        except NotVirtualized, e:
+            sys.stderr.write('Aborting on failed virtualization check:\n')
+            sys.stderr.write(str(e))
+            return 1
         assert not os.path.exists(self.tree_path)
         recipe_path = os.path.join(self.work_dir, 'recipe')
         manifest_path = os.path.join(self.tree_path, 'manifest')
@@ -159,6 +187,24 @@
     return os.path.join(
         os.environ["HOME"], "build-" + build_id, *extra)
 
+
+def ensure_virtualized():
+    """Raise an exception if not running in a virtualized environment.
+
+    Raises if unsure, or if not running under Xen.
+    """
+    try:
+        proc = Popen(['xen-detect'], stdout=PIPE)
+    except OSError, e:
+        if e.errno != errno.ENOENT:
+            raise
+        raise NoXenDetect()
+    status_code = proc.wait()
+    output = proc.stdout.read()
+    if not re.match('Running in .* context on Xen', output):
+        raise BadXenDetect(output)
+
+
 if __name__ == '__main__':
     builder = RecipeBuilder(*sys.argv[1:])
     if builder.buildTree() != 0: