auto selecting backend with 'pkgmgr=auto'
[tools/mic.git] / plugins / imager / fs_plugin.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os
19 import sys
20
21 from mic import chroot, msger
22 from mic.utils import cmdln, misc, errors, fs_related
23 from mic.imager import fs
24 from mic.conf import configmgr
25 from mic.plugin import pluginmgr
26
27 from mic.pluginbase import ImagerPlugin
28 class FsPlugin(ImagerPlugin):
29     name = 'fs'
30
31     @classmethod
32     @cmdln.option("--include-src",
33                   dest="include_src",
34                   action="store_true",
35                   default=False,
36                   help="Generate a image with source rpms included")
37     def do_create(self, subcmd, opts, *args):
38         """${cmd_name}: create fs image
39
40         Usage:
41             ${name} ${cmd_name} <ksfile> [OPTS]
42
43         ${cmd_option_list}
44         """
45
46         creatoropts = configmgr.create
47         ksconf = args[0]
48
49         recording_pkgs = []
50         if len(creatoropts['record_pkgs']) > 0:
51             recording_pkgs = creatoropts['record_pkgs']
52
53         if creatoropts['release'] is not None:
54             if 'name' not in recording_pkgs:
55                 recording_pkgs.append('name')
56
57         ksconf = misc.normalize_ksfile(ksconf,
58                                        creatoropts['release'],
59                                        creatoropts['arch'])
60
61         configmgr._ksconf = ksconf
62
63         # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
64         if creatoropts['release'] is not None:
65             creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])
66
67         # try to find the pkgmgr
68         pkgmgr = None
69         for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
70             if 'auto' == creatoropts['pkgmgr']:
71                 pkgmgr = pcls
72                 break
73             elif key == creatoropts['pkgmgr']:
74                 pkgmgr = pcls
75                 break
76
77         if not pkgmgr:
78             pkgmgrs = pluginmgr.get_plugins('backend').keys()
79             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
80
81         creator = fs.FsImageCreator(creatoropts, pkgmgr)
82         creator._include_src = opts.include_src
83
84         if len(recording_pkgs) > 0:
85             creator._recording_pkgs = recording_pkgs
86
87         self.check_image_exists(creator.destdir,
88                                 creator.pack_to,
89                                 [creator.name],
90                                 creatoropts['release'])
91
92         try:
93             creator.check_depend_tools()
94             creator.mount(None, creatoropts["cachedir"])
95             creator.install()
96             #Download the source packages ###private options
97             if opts.include_src:
98                 installed_pkgs =  creator.get_installed_packages()
99                 msger.info('--------------------------------------------------')
100                 msger.info('Generating the image with source rpms included ...')
101                 if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]):
102                     msger.warning("Source packages can't be downloaded")
103
104             creator.configure(creatoropts["repomd"])
105             creator.copy_kernel()
106             creator.unmount()
107             creator.package(creatoropts["outdir"])
108             if creatoropts['release'] is not None:
109                 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
110             creator.print_outimage_info()
111         except errors.CreatorError:
112             raise
113         finally:
114             creator.cleanup()
115
116         msger.info("Finished.")
117         return 0
118
119     @classmethod
120     def do_chroot(self, target):#chroot.py parse opts&args
121             try:
122                 envcmd = fs_related.find_binary_inchroot("env", target)
123                 if envcmd:
124                     cmdline = "%s HOME=/root /bin/bash" % envcmd
125                 else:
126                     cmdline = "/bin/bash"
127                 chroot.chroot(target, None, cmdline)
128             finally:
129                 chroot.cleanup_after_chroot("dir", None, None, None)
130                 return 1
131