3 # Copyright (c) 2011 Intel, Inc.
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
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
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.
19 from optparse import SUPPRESS_HELP
22 from mic.utils import cmdln, errors, rpmmisc
23 from conf import configmgr
24 from plugin import pluginmgr
26 class Creator(cmdln.Cmdln):
27 """${name}: create an image
30 ${name} SUBCOMMAND <ksfile> [OPTS]
36 name = 'mic create(cr)'
38 def __init__(self, *args, **kwargs):
39 cmdln.Cmdln.__init__(self, *args, **kwargs)
41 # get cmds from pluginmgr
42 # mix-in do_subcmd interface
43 for subcmd, klass in pluginmgr.get_plugins('imager').iteritems():
44 if not hasattr(klass, 'do_create'):
45 msger.warning("Unsurpport subcmd: %s" % subcmd)
48 func = getattr(klass, 'do_create')
49 setattr(self.__class__, "do_"+subcmd, func)
51 def get_optparser(self):
52 optparser = cmdln.CmdlnOptionParser(self)
53 optparser.add_option('-d', '--debug', action='store_true', dest='debug',
55 optparser.add_option('-v', '--verbose', action='store_true',
58 optparser.add_option('', '--logfile', type='string', dest='logfile',
60 help='Path of logfile')
61 optparser.add_option('-c', '--config', type='string', dest='config',
63 help='Specify config file for mic')
64 optparser.add_option('-k', '--cachedir', type='string', action='store',
65 dest='cachedir', default=None,
66 help='Cache directory to store the downloaded')
67 optparser.add_option('-o', '--outdir', type='string', action='store',
68 dest='outdir', default=None,
69 help='Output directory')
70 optparser.add_option('-A', '--arch', type='string', dest='arch',
72 help='Specify repo architecture')
73 optparser.add_option('', '--release', type='string', dest='release',
74 default=None, metavar='RID',
75 help='Generate a release of RID with all necessary'
76 ' files, when @BUILD_ID@ is contained in '
77 'kickstart file, it will be replaced by RID')
78 optparser.add_option("", "--record-pkgs", type="string",
79 dest="record_pkgs", default=None,
80 help='Record the info of installed packages, '
81 'multiple values can be specified which '
82 'joined by ",", valid values: "name", '
83 '"content", "license"')
84 optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr',
86 help='Specify backend package manager')
87 optparser.add_option('', '--local-pkgs-path', type='string',
88 dest='local_pkgs_path', default=None,
89 help='Path for local pkgs(rpms) to be installed')
90 optparser.add_option('', '--runtime', type='string',
91 dest='runtime', default=None,
92 help='Specify runtime mode, avaiable: bootstrap')
93 optparser.add_option('', '--compress-disk-image', type='string',
94 dest='compress_disk_image', default=None,
95 help='Sets the disk image compression. NOTE: The '
96 'available values might depend on the used '
100 def preoptparse(self, argv):
101 optparser = self.get_optparser()
108 if arg in ('-h', '--help'):
111 elif optparser.has_option(arg):
114 if optparser.get_option(arg).takes_value():
116 largs.append(argv.pop(0))
118 raise errors.Usage("%s option requires an argument" % arg)
121 if arg.startswith("--"):
123 opt = arg.split("=")[0]
126 elif arg.startswith("-") and len(arg) > 2:
131 if opt and optparser.has_option(opt):
138 def postoptparse(self):
139 if self.options.verbose:
140 msger.set_loglevel('verbose')
141 if self.options.debug:
142 msger.set_loglevel('debug')
144 if self.options.logfile:
145 msger.set_interactive(False)
146 msger.set_logfile(self.options.logfile)
147 configmgr.create['logfile'] = self.options.logfile
149 if self.options.config:
151 configmgr._siteconf = self.options.config
153 if self.options.outdir is not None:
154 configmgr.create['outdir'] = self.options.outdir
155 if self.options.cachedir is not None:
156 configmgr.create['cachedir'] = self.options.cachedir
157 os.environ['ZYPP_LOCKFILE_ROOT'] = configmgr.create['cachedir']
158 if self.options.local_pkgs_path is not None:
159 configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
161 if self.options.release:
162 configmgr.create['release'] = self.options.release
164 if self.options.record_pkgs:
165 configmgr.create['record_pkgs'] = []
166 for infotype in self.options.record_pkgs.split(','):
167 if infotype not in ('name', 'content', 'license'):
168 raise errors.Usage('Invalid pkg recording: %s, valid ones:'
169 ' "name", "content", "license"' \
172 configmgr.create['record_pkgs'].append(infotype)
174 if self.options.arch is not None:
175 supported_arch = sorted(rpmmisc.archPolicies.keys(), reverse=True)
176 if self.options.arch in supported_arch:
177 configmgr.create['arch'] = self.options.arch
179 raise errors.Usage('Invalid architecture: "%s".\n'
180 ' Supported architectures are: \n'
181 ' %s\n' % (self.options.arch,
182 ', '.join(supported_arch)))
184 if self.options.pkgmgr is not None:
185 configmgr.create['pkgmgr'] = self.options.pkgmgr
187 if self.options.runtime:
188 configmgr.create['runtime'] = self.options.runtime
190 if self.options.compress_disk_image is not None:
191 configmgr.create['compress_disk_image'] = \
192 self.options.compress_disk_image
194 def main(self, argv=None):
198 argv = argv[:] # don't modify caller's list
200 self.optparser = self.get_optparser()
203 argv = self.preoptparse(argv)
204 self.options, args = self.optparser.parse_args(argv)
206 except cmdln.CmdlnUserError, ex:
207 msg = "%s: %s\nTry '%s help' for info.\n"\
208 % (self.name, ex, self.name)
211 except cmdln.StopOptionProcessing, ex:
214 # optparser=None means no process for opts
215 self.options, args = None, argv[1:]
218 return self.emptyline()
222 if os.geteuid() != 0 and args[0] != 'help':
223 msger.error('root permission is required to continue, abort')
225 return self.cmd(args)