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