Merge release-0.28.17 from 'tools/mic'
[platform/upstream/mic.git] / plugins / imager / qcow_plugin.py
index d6758c5..2329958 100755 (executable)
@@ -19,47 +19,10 @@ import subprocess
 import shutil
 
 from mic import msger, rt_util
-from mic.conf import configmgr
 from mic.plugin import pluginmgr
 from mic.pluginbase import ImagerPlugin
-from mic.imager.loop import LoopImageCreator
-from mic.utils import errors, fs_related, runner
-
-class QcowImageCreator(LoopImageCreator):
-    img_format = 'qcow'
-
-    def __init__(self, creatoropts=None, pkgmgr=None):
-        LoopImageCreator.__init__(self, creatoropts, pkgmgr) 
-        self.cmd_qemuimg = 'qemu-img'
-
-    def _stage_final_image(self):
-        try:
-            self.cmd_qemuimg = fs_related.find_binary_path('qemu-img')
-        except errors.CreatorError:
-            return LoopImageCreator._stage_final_image(self)
-
-        self._resparse()
-
-        imgfile = None
-        for item in self._instloops:
-            if item['mountpoint'] == '/':
-                if item['fstype'] == "ext4":
-                    runner.show('/sbin/tune2fs -O ^huge_file,extents,uninit_bg %s'
-                                % imgfile)
-                self.image_files.setdefault('partitions', {}).update(
-                         {item['mountpoint']: item['label']})
-                imgfile = os.path.join(self._imgdir, item['name'])
-
-        if imgfile:
-            qemuimage = imgfile + ".x86"
-            runner.show("%s convert -O qcow2 %s %s"
-                        % (self.cmd_qemuimg, imgfile, qemuimage))
-            os.unlink(imgfile)
-            os.rename(qemuimage, imgfile)
-
-        for item in os.listdir(self._imgdir):
-            shutil.move(os.path.join(self._imgdir, item),
-                        os.path.join(self._outdir, item))
+from mic.imager.qcow import QcowImageCreator
+from mic.utils import errors, runner
 
 class QcowPlugin(ImagerPlugin):
     name = 'qcow'
@@ -67,53 +30,9 @@ class QcowPlugin(ImagerPlugin):
     @classmethod
     def do_create(cls, args):
         """${cmd_name}: create qcow image
-
-        Usage:
-            ${name} ${cmd_name} <ksfile> [OPTS]
-
-        ${cmd_option_list}
         """
-        if args is None:
-            raise errors.Usage("Invalid arguments")
-
-        creatoropts = configmgr.create
-        ksconf = args.ksfile
-
-        if creatoropts['runtime'] == "bootstrap":
-            configmgr._ksconf = ksconf
-            rt_util.bootstrap_mic()
-
-        recording_pkgs = []
-        if len(creatoropts['record_pkgs']) > 0:
-            recording_pkgs = creatoropts['record_pkgs']
-
-        if creatoropts['release'] is not None:
-            if 'name' not in recording_pkgs:
-                recording_pkgs.append('name')
-            if 'vcs' not in recording_pkgs:
-                recording_pkgs.append('vcs')
-
-        configmgr._ksconf = ksconf
-
-        # try to find the pkgmgr
-        pkgmgr = None
-        backends = pluginmgr.get_plugins('backend')
-        if 'auto' == creatoropts['pkgmgr']:
-            for key in configmgr.prefer_backends:
-                if key in backends:
-                    pkgmgr = backends[key]
-                    break
-        else:
-            for key in backends.keys():
-                if key == creatoropts['pkgmgr']:
-                    pkgmgr = backends[key]
-                    break
-
-        if not pkgmgr:
-            raise errors.CreatorError("Can't find backend: %s, "
-                                       "available choices: %s" %
-                                      (creatoropts['pkgmgr'],
-                                       ','.join(backends.keys())))
+
+        creatoropts, pkgmgr, recording_pkgs = rt_util.prepare_create(args)
 
         creator = QcowImageCreator(creatoropts,
                                    pkgmgr)
@@ -142,7 +61,7 @@ class QcowPlugin(ImagerPlugin):
             creator.create_manifest()
 
             if creatoropts['release'] is not None:
-                creator.release_output(ksconf,
+                creator.release_output(args.ksfile,
                                        creatoropts['destdir'],
                                        creatoropts['release'])
             creator.print_outimage_info()
@@ -156,8 +75,8 @@ class QcowPlugin(ImagerPlugin):
         if creatoropts['run_script']:
             cmd = creatoropts['run_script']
             try:
-                runner.show(cmd)   
-            except OSError,err:
+                runner.show(cmd)
+            except OSError as err:
                 msger.warning(str(err))