Merge release-0.28.17 from 'tools/mic'
[platform/upstream/mic.git] / plugins / imager / fs_plugin.py
1 #! /usr/bin/python2
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 subprocess
19 from mic import chroot, msger, rt_util
20 from mic.utils import misc, errors, fs_related, runner
21 from mic.imager import fs
22 from mic.plugin import pluginmgr
23
24 from mic.pluginbase import ImagerPlugin
25 class FsPlugin(ImagerPlugin):
26     name = 'fs'
27
28     @classmethod
29     def do_create(self, args):
30         """${cmd_name}: create fs image
31
32         """
33         creatoropts, pkgmgr, recording_pkgs = rt_util.prepare_create(args)
34
35         creator = fs.FsImageCreator(creatoropts, pkgmgr)
36         creator._include_src = args.include_src
37
38         if len(recording_pkgs) > 0:
39             creator._recording_pkgs = recording_pkgs
40
41         self.check_image_exists(creator.destdir,
42                                 creator.pack_to,
43                                 [creator.name],
44                                 creatoropts['release'])
45
46         try:
47             creator.check_depend_tools()
48             creator.mount(None, creatoropts["cachedir"])
49             creator.install()
50             creator.tpkinstall()
51             #Download the source packages ###private options
52             if args.include_src:
53                 installed_pkgs =  creator.get_installed_packages()
54                 msger.info('Generating the image with source rpms included ...')
55                 if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"],
56                         creator._instroot, creatoropts["cachedir"]):
57                     msger.warning("Source packages can't be downloaded")
58
59             creator.configure(creatoropts["repomd"])
60             creator.copy_kernel()
61             creator.unmount()
62             creator.package(creatoropts["destdir"])
63             creator.create_manifest()
64             if creatoropts['release'] is not None:
65                 creator.release_output(args.ksfile, creatoropts['destdir'],
66                         creatoropts['release'])
67             creator.print_outimage_info()
68         except errors.CreatorError:
69             raise
70         finally:
71             creator.cleanup()
72
73         #Run script of --run_script after image created
74         if creatoropts['run_script']:
75             cmd = creatoropts['run_script']
76             try:
77                 runner.show(cmd)
78             except OSError as err:
79                 msger.warning(str(err))
80
81
82         msger.info("Finished.")
83         return 0
84
85     @classmethod
86     def do_chroot(self, target, cmd=[]):#chroot.py parse opts&args
87         try:
88             if len(cmd) != 0:
89                 cmdline = ' '.join(cmd)
90             else:
91                 cmdline = "/bin/bash"
92             envcmd = fs_related.find_binary_inchroot("env", target)
93             if envcmd:
94                 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
95             chroot.chroot(target, None, cmdline)
96         finally:
97             chroot.cleanup_after_chroot("dir", None, None, None)
98             return 1