03b3b1b30f3fe4c208ecd495a336ae7c00e81323
[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 key == creatoropts['pkgmgr']:
71                 pkgmgr = pcls
72                 break
73
74         if not pkgmgr:
75             pkgmgrs = pluginmgr.get_plugins('backend').keys()
76             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
77
78         creator = fs.FsImageCreator(creatoropts, pkgmgr)
79         creator._include_src = opts.include_src
80
81         if len(recording_pkgs) > 0:
82             creator._recording_pkgs = recording_pkgs
83
84         self.check_image_exists(creator.destdir,
85                                 creator.pack_to,
86                                 [creator.name],
87                                 creatoropts['release'])
88
89         try:
90             creator.check_depend_tools()
91             creator.mount(None, creatoropts["cachedir"])
92             creator.install()
93             #Download the source packages ###private options
94             if opts.include_src:
95                 installed_pkgs =  creator.get_installed_packages()
96                 msger.info('--------------------------------------------------')
97                 msger.info('Generating the image with source rpms included ...')
98                 if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]):
99                     msger.warning("Source packages can't be downloaded")
100
101             creator.configure(creatoropts["repomd"])
102             creator.copy_kernel()
103             creator.unmount()
104             creator.package(creatoropts["outdir"])
105             if creatoropts['release'] is not None:
106                 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
107             creator.print_outimage_info()
108         except errors.CreatorError:
109             raise
110         finally:
111             creator.cleanup()
112
113         msger.info("Finished.")
114         return 0
115
116     @classmethod
117     def do_chroot(self, target):#chroot.py parse opts&args
118             try:
119                 envcmd = fs_related.find_binary_inchroot("env", target)
120                 if envcmd:
121                     cmdline = "%s HOME=/root /bin/bash" % envcmd
122                 else:
123                     cmdline = "/bin/bash"
124                 chroot.chroot(target, None, cmdline)
125             finally:
126                 chroot.cleanup_after_chroot("dir", None, None, None)
127                 return 1
128