Add diagnose() method in OSBase derived classes
[snf-image-creator] / image_creator / os_type / __init__.py
index 5298569..63b7f5a 100644 (file)
@@ -144,6 +144,22 @@ class OSBase(object):
         except RuntimeError:
             self._scrub_support = False
 
+    def diagnose(self):
+        """Run diagnostics to check if the media is supported"""
+
+        if self.image.is_unsupported():
+            return
+
+        self.out.output('Running OS diagnostics:')
+        try:
+            if not self.mount(readonly=True):
+                raise FatalError("Unable to mount the media read-only")
+            self._do_diagnose()
+        finally:
+            self.umount()
+
+        self.out.output()
+
     def collect_metadata(self):
         """Collect metadata about the OS"""
         try:
@@ -247,12 +263,17 @@ class OSBase(object):
     def do_sysprep(self):
         """Prepare system for image creation."""
 
+        self.out.output('Preparing system for image creation:')
+
+        if self.image.is_unsupported():
+            self.out.warn(
+                "System preparation is disabled for unsupported media")
+            return
+
         try:
             if not self.mount(readonly=False):
                 raise FatalError("Unable to mount the media read-write")
 
-            self.out.output('Preparing system for image creation:')
-
             enabled = [task for task in self.list_syspreps() if task.enabled]
 
             size = len(enabled)
@@ -351,10 +372,20 @@ class OSBase(object):
             if has_ftype(f, ftype):
                 action(full_path)
 
+    def _do_diagnose(self):
+        """helper method for diagnose"""
+        pass
+
     def _do_collect_metadata(self):
         """helper method for collect_metadata"""
-        self.meta['ROOT_PARTITION'] = \
-            "%d" % self.image.g.part_to_partnum(self.root)
+
+        try:
+            self.meta['ROOT_PARTITION'] = \
+                "%d" % self.image.g.part_to_partnum(self.root)
+        except RuntimeError:
+            self.out.warn("Unable to identify the partition number from root "
+                          "partition: %s" % self.root)
+
         self.meta['OSFAMILY'] = self.image.g.inspect_get_type(self.root)
         self.meta['OS'] = self.image.g.inspect_get_distro(self.root)
         if self.meta['OS'] == "unknown":