more compact plugin framwork
[tools/mic.git] / plugins / imager / fs_plugin.py
1 #!/usr/bin/python
2 import sys
3 import subprocess
4
5 import mic.utils.cmdln as cmdln
6 import mic.utils.errors as errors
7 import mic.configmgr as configmgr
8 import mic.pluginmgr as pluginmgr
9 import mic.imager.fs as fs
10 import mic.chroot as chroot
11
12 from mic.pluginbase import ImagerPlugin
13
14 class FsPlugin(ImagerPlugin):
15     name = 'fs'
16
17     @classmethod
18     @cmdln.option("--include-src", dest="include_src", help="include source pakcage")
19     def do_create(self, subcmd, opts, *args):
20         """${cmd_name}: create fs image
21
22         ${cmd_usage}
23         ${cmd_option_list}
24         """
25         if len(args) == 0:
26             return
27         if len(args) == 1:
28             ksconf = args[0]
29         else:
30             raise errors.Usage("Extra arguments given")
31
32         cfgmgr = configmgr.getConfigMgr()
33         createopts = cfgmgr.create
34         cfgmgr.setProperty("ksconf", ksconf)
35
36         plgmgr = pluginmgr.PluginMgr()
37         for (key, pcls) in plgmgr.get_plugins('backend').iteritems():
38             if key == createopts['pkgmgr']:
39                 pkgmgr = pcls
40
41         if not pkgmgr:
42             raise CreatorError("Can't find backend plugin: %s" % createopts['pkgmgr'])
43
44         creator = fs.FsImageCreator(createopts, pkgmgr)
45         try:
46             creator.check_depend_tools()
47             creator.mount(None, createopts["cachedir"])
48             creator.install()
49             #Download the source packages ###private options
50             if opts.include_src:
51                 installed_pkgs =  creator.get_installed_packages()
52                 print '--------------------------------------------------'
53                 print 'Generating the image with source rpms included, The number of source packages is %d.' %(len(installed_pkgs))
54                 if not misc.SrcpkgsDownload(installed_pkgs, createopts["repomd"], creator._instroot, createopts["cachedir"]):
55                     print "Source packages can't be downloaded"
56
57             creator.configure(createopts["repomd"])
58             creator.unmount()
59             creator.package(createopts["outdir"])
60             outimage = creator.outimage
61             creator.print_outimage_info()
62         except errors.CreatorError, e:
63             raise errors.CreatorError("failed to create image : %s" % e)
64         finally:
65             creator.cleanup()
66             print "Finished."
67
68         return 0
69
70     @classmethod
71     def do_chroot(self, target):#chroot.py parse opts&args
72             try:
73                 chroot.chroot(target, None, "/bin/env HOME=/root /bin/bash")
74             except:
75                 print >> sys.stderr, "Failed to chroot to %s." % target
76             finally:
77                 chroot.cleanup_after_chroot("dir", None, None, None)
78                 return 1
79