replace most of subprocess calls by new runner apis
[tools/mic.git] / plugins / imager / fs_plugin.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright 2011 Intel, Inc.
4 #
5 # This copyrighted material is made available to anyone wishing to use, modify,
6 # copy, or redistribute it subject to the terms and conditions of the GNU
7 # General Public License v.2.  This program is distributed in the hope that it
8 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
9 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License along with
13 # this program; if not, write to the Free Software Foundation, Inc., 51
14 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
15 # trademarks that are incorporated in the source code or documentation are not
16 # subject to the GNU General Public License and may only be used or replicated
17 # with the express permission of Red Hat, Inc.
18 #
19
20 import os
21
22 from mic import configmgr, pluginmgr, chroot, msger
23 from mic.utils import cmdln, errors
24 from mic.imager import fs
25
26 from mic.pluginbase import ImagerPlugin
27 class FsPlugin(ImagerPlugin):
28     name = 'fs'
29
30     @classmethod
31     @cmdln.option("--include-src", dest="include_src", help="include source pakcage")
32     def do_create(self, subcmd, opts, *args):
33         """${cmd_name}: create fs image
34
35         ${cmd_usage}
36         ${cmd_option_list}
37         """
38
39         if not args:
40             raise errors.Usage("More arguments needed")
41
42         if len(args) == 1:
43             ksconf = args[0]
44         else:
45             raise errors.Usage("Extra arguments given")
46
47         cfgmgr = configmgr.getConfigMgr()
48         createopts = cfgmgr.create
49         cfgmgr.setProperty("ksconf", ksconf)
50
51         # try to find the pkgmgr
52         pkgmgr = None
53         for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
54             if key == createopts['pkgmgr']:
55                 pkgmgr = pcls
56                 break
57
58         if not pkgmgr:
59             raise CreatorError("Can't find backend plugin: %s" % createopts['pkgmgr'])
60
61         creator = fs.FsImageCreator(createopts, pkgmgr)
62
63         destdir = os.path.abspath(os.path.expanduser(createopts["outdir"]))
64         fsdir = os.path.join(destdir, creator.name)
65
66         if not os.path.exists(destdir):
67             os.makedirs(destdir)
68         elif os.path.exists(fsdir):
69             if msger.ask('The target dir: %s already exists, need to delete it?' % fsdir):
70                 import shutil
71                 shutil.rmtree(fsdir)
72
73         try:
74             creator.check_depend_tools()
75             creator.mount(None, createopts["cachedir"])
76             creator.install()
77             #Download the source packages ###private options
78             if opts.include_src:
79                 installed_pkgs =  creator.get_installed_packages()
80                 msger.info('--------------------------------------------------')
81                 msger.info('Generating the image with source rpms included, The number of source packages is %d.' %(len(installed_pkgs)))
82                 if not misc.SrcpkgsDownload(installed_pkgs, createopts["repomd"], creator._instroot, createopts["cachedir"]):
83                     msger.warning("Source packages can't be downloaded")
84
85             creator.configure(createopts["repomd"])
86             creator.unmount()
87             creator.package(destdir)
88             outimage = creator.outimage
89             creator.print_outimage_info()
90         except errors.CreatorError:
91             raise
92         finally:
93             creator.cleanup()
94
95         msger.info("Finished.")
96         return 0
97
98     @classmethod
99     def do_chroot(self, target):#chroot.py parse opts&args
100             try:
101                 chroot.chroot(target, None, "/bin/env HOME=/root /bin/bash")
102             finally:
103                 chroot.cleanup_after_chroot("dir", None, None, None)
104                 return 1
105